Advertisement
daniliambo

Untitled

Nov 21st, 2023
1,501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- necessary funcs
  2. chAdd :: ChurchNumber -> ChurchNumber -> ChurchNumber
  3. chAdd a (Succ b) = chAdd (Succ a) b
  4. chAdd a churchZero = a
  5.  
  6. chSucc :: ChurchNumber -> ChurchNumber
  7. chSucc = Succ
  8.  
  9. -- for testing purposes
  10. churchZero :: ChurchNumber
  11. churchZero = Zero
  12.  
  13. churchOne :: ChurchNumber
  14. churchOne = chSucc churchZero
  15.  
  16. churchTwo :: ChurchNumber
  17. churchTwo = chSucc churchOne
  18.  
  19. churchThree :: ChurchNumber
  20. churchThree = chSucc churchTwo
  21.  
  22. churchIntZero :: (ChurchNumber, ChurchNumber)
  23. churchIntZero = (churchZero, churchZero)
  24.  
  25. churchIntOne :: (ChurchNumber, ChurchNumber)
  26. churchIntOne = (churchOne, Zero)
  27.  
  28. churchIntOneNeg :: (ChurchNumber, ChurchNumber)
  29. churchIntOneNeg = churchIntNegation churchIntOne
  30.  
  31. churchIntTwo :: (ChurchNumber, ChurchNumber)
  32. churchIntTwo = churchIntCalcSum churchIntOne churchIntOne
  33.  
  34. churchIntTwoNeg :: (ChurchNumber, ChurchNumber)
  35. churchIntTwoNeg = churchIntCalcSum churchIntOneNeg churchIntOneNeg
  36.  
  37. churchIntThree :: (ChurchNumber, ChurchNumber)
  38. churchIntThree = churchIntCalcSum churchIntTwo churchIntOne
  39.  
  40. churchIntThreeNeg :: (ChurchNumber, ChurchNumber)
  41. churchIntThreeNeg = churchIntCalcSum churchIntTwoNeg churchIntOneNeg
  42.  
  43. (+&) :: ChurchNumber -> ChurchNumber -> ChurchNumber
  44. (+&) a (Succ b) = (+&) (Succ a) b
  45. (+&) a Zero = a
  46.  
  47. (*&) :: ChurchNumber -> ChurchNumber -> ChurchNumber
  48. (*&) a (Succ b) = (+&) a (a *& b)
  49. (*&) a Zero = Zero
  50.  
  51. chMult :: ChurchNumber -> ChurchNumber -> ChurchNumber
  52. chMult a (Succ b) = chAdd a (a `chMult` b)
  53. chMult a Zero = Zero
  54.  
  55. churchNullify :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  56. churchNullify (Zero, Zero) = (Zero, Zero)
  57. churchNullify (pos, Zero) = (pos, Zero)
  58. churchNullify (Zero, neg) = (Zero, neg)
  59. churchNullify (Succ pos, Succ neg) = churchNullify (pos, neg)
  60.  
  61. -- (pos1 + neg2), (pos2 + neg1)
  62. churchIntCalcDifference :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  63. churchIntCalcDifference (pos1, neg1) (pos2, neg2) = churchNullify (pos1 +& neg2, neg1 +& pos2)
  64.  
  65. -- (pos1 + neg2), (pos2 + neg1)
  66. churchIntCalcSum :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  67. churchIntCalcSum (pos1, neg1) (pos2, neg2) = churchNullify (pos1 +& pos2, neg1 +& neg2)
  68.  
  69. churchIntCalcProd :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  70. churchIntCalcProd chN1 chN2 = helper (churchNullify chN1) (churchNullify chN2)
  71.   where
  72.     -- helper chN1 chN2 = chN2
  73.     helper (_, _) (Zero, Zero) = (Zero, Zero)
  74.     helper (Zero, Zero) (_, _)  = (Zero, Zero)
  75.     helper (Zero, b) (c, Zero) = (Zero, b *& c)
  76.     helper (a, Zero) (c, Zero) = (a *& c, Zero)
  77.     helper (a, Zero) (Zero, d) = (Zero, a *& d)
  78.     helper (Zero, b) (Zero, d) = (b *& d, Zero)
  79.  
  80. churchIntAbs :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  81. churchIntAbs chN = helper (churchNullify chN)
  82.   where
  83.     helper (Zero, Zero) = (Zero, Zero)
  84.     helper (a, Zero) = (a, Zero)
  85.     helper (Zero, a) = (a, Zero)
  86.  
  87. churchIntSignum :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  88. churchIntSignum chN = helper (churchNullify chN)
  89.   where
  90.     helper (Zero, Zero) = (Zero, Zero)
  91.     helper (a, Zero) = (Succ Zero, Zero)
  92.     helper (Zero, a) = (Zero, Succ Zero)
  93.  
  94. fromInteger' :: Integer -> (ChurchNumber, ChurchNumber)
  95. fromInteger' int = helper int (Zero, Zero)
  96.   where
  97.     helper 0 (a, b) = (a, b)
  98.     helper int (a, b) = helper (int - 1) (Succ a, Zero)
  99.  
  100. churchIntNegation :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
  101. churchIntNegation (pos1, neg1) = churchNullify (neg1, pos1)
  102.  
  103. churchIntIsPositive :: (ChurchNumber, ChurchNumber) -> Bool
  104. churchIntIsPositive chN = helper (churchNullify chN)
  105.   where
  106.     helper (Zero, Zero) = False
  107.     helper (_, Zero) = True
  108.     helper (Zero, _) = False
  109.  
  110. churchIntIsZero :: (ChurchNumber, ChurchNumber) -> Bool
  111. churchIntIsZero (Zero, Zero) = True
  112. churchIntIsZero (_, _) = False
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement