fork download
  1. import Prelude hiding (succ, pred, and, or, not)
  2. import Control.Arrow
  3.  
  4. -- Arrow style natural numbers by itchyny
  5.  
  6.  
  7. toint = ($ ((1+), 0))
  8. tobool = ($ (True, False))
  9.  
  10. -- Do not use except for
  11. -- fst
  12. -- snd
  13. -- (,)
  14. -- app
  15. -- id
  16. -- (>>>)
  17. -- (&&&)
  18. -- (***)
  19. --
  20. -- and following is allowed
  21. -- (<<<) == flip (>>>)
  22. -- curry == ((<<<) >>> ((,) >>>))
  23. -- flip = uncurry >>> ((snd &&& fst) >>>) >>> curry
  24. --
  25. -- don't use
  26. -- uncurry == (*** id) >>> (>>> app)
  27. -- == \f -> (f *** id) >>> app
  28. -- (.) f g == g >>> f
  29. -- first f == f *** id
  30. -- second g == id *** g
  31. -- ($) == curry app
  32. -- const == ((,) >>> (>>> fst))
  33. -- etc.
  34.  
  35.  
  36. -- zero = \(f, x) -> x
  37. zero = snd
  38.  
  39. -- succ = \n (f, x) -> f (n (f, x))
  40. succ = curry ((snd >>> fst) &&& app >>> app)
  41.  
  42. -- plus = \m n (f, x) -> m (f, (n (f, x)))
  43. plus = (((snd >>> fst) &&& app) >>>) >>> curry
  44.  
  45. -- mult = \m n -> m ((plus n), zero)
  46. mult = ((plus >>> (flip (,) zero)) >>>)
  47.  
  48. -- pred = \n f x -> n (\g h -> h (g f)) (\u -> x) (\u -> u)
  49.  
  50. -- true = \(x, y) -> x
  51. true = fst
  52.  
  53. -- false = \(x, y) -> y
  54. false = snd
  55.  
  56. -- and = \p q -> p (q, false)
  57. and = (flip (,) false >>>)
  58.  
  59. -- or = \p q -> p (true, q)
  60. or = ((,) true >>>)
  61.  
  62. -- not = \p -> p (false, true)
  63. not = flip (curry app) ((,) false true)
  64.  
  65. main = do
  66. let one = succ zero
  67. two = succ one
  68. three = succ two
  69. four = succ three
  70. five = succ four
  71. six = succ five
  72. print $ toint zero
  73. print $ toint one
  74. print $ toint two
  75. print $ toint three
  76. print $ toint $ plus five three
  77. print $ toint $ mult four six
  78. -- print $ toint $ pred six
  79. -- print $ toint $ pred (plus (two six))
  80. print $ "Bool"
  81. print $ tobool true
  82. print $ tobool false
  83. print $ "and"
  84. print $ tobool $ and true true
  85. print $ tobool $ and true false
  86. print $ tobool $ and false true
  87. print $ tobool $ and false false
  88. print $ "or"
  89. print $ tobool $ or true true
  90. print $ tobool $ or true false
  91. print $ tobool $ or false true
  92. print $ tobool $ or false false
  93. print $ "not"
  94. print $ tobool $ not true
  95. print $ tobool $ not false
  96. return ()
  97.  
Success #stdin #stdout 0.01s 3604KB
stdin
Standard input is empty
stdout
0
1
2
3
8
24
"Bool"
True
False
"and"
True
False
False
False
"or"
True
True
True
False
"not"
False
True