Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- necessary funcs
- chAdd :: ChurchNumber -> ChurchNumber -> ChurchNumber
- chAdd a (Succ b) = chAdd (Succ a) b
- chAdd a churchZero = a
- chSucc :: ChurchNumber -> ChurchNumber
- chSucc = Succ
- -- for testing purposes
- churchZero :: ChurchNumber
- churchZero = Zero
- churchOne :: ChurchNumber
- churchOne = chSucc churchZero
- churchTwo :: ChurchNumber
- churchTwo = chSucc churchOne
- churchThree :: ChurchNumber
- churchThree = chSucc churchTwo
- churchIntZero :: (ChurchNumber, ChurchNumber)
- churchIntZero = (churchZero, churchZero)
- churchIntOne :: (ChurchNumber, ChurchNumber)
- churchIntOne = (churchOne, Zero)
- churchIntOneNeg :: (ChurchNumber, ChurchNumber)
- churchIntOneNeg = churchIntNegation churchIntOne
- churchIntTwo :: (ChurchNumber, ChurchNumber)
- churchIntTwo = churchIntCalcSum churchIntOne churchIntOne
- churchIntTwoNeg :: (ChurchNumber, ChurchNumber)
- churchIntTwoNeg = churchIntCalcSum churchIntOneNeg churchIntOneNeg
- churchIntThree :: (ChurchNumber, ChurchNumber)
- churchIntThree = churchIntCalcSum churchIntTwo churchIntOne
- churchIntThreeNeg :: (ChurchNumber, ChurchNumber)
- churchIntThreeNeg = churchIntCalcSum churchIntTwoNeg churchIntOneNeg
- (+&) :: ChurchNumber -> ChurchNumber -> ChurchNumber
- (+&) a (Succ b) = (+&) (Succ a) b
- (+&) a Zero = a
- (*&) :: ChurchNumber -> ChurchNumber -> ChurchNumber
- (*&) a (Succ b) = (+&) a (a *& b)
- (*&) a Zero = Zero
- chMult :: ChurchNumber -> ChurchNumber -> ChurchNumber
- chMult a (Succ b) = chAdd a (a `chMult` b)
- chMult a Zero = Zero
- churchNullify :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchNullify (Zero, Zero) = (Zero, Zero)
- churchNullify (pos, Zero) = (pos, Zero)
- churchNullify (Zero, neg) = (Zero, neg)
- churchNullify (Succ pos, Succ neg) = churchNullify (pos, neg)
- -- (pos1 + neg2), (pos2 + neg1)
- churchIntCalcDifference :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntCalcDifference (pos1, neg1) (pos2, neg2) = churchNullify (pos1 +& neg2, neg1 +& pos2)
- -- (pos1 + neg2), (pos2 + neg1)
- churchIntCalcSum :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntCalcSum (pos1, neg1) (pos2, neg2) = churchNullify (pos1 +& pos2, neg1 +& neg2)
- churchIntCalcProd :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntCalcProd chN1 chN2 = helper (churchNullify chN1) (churchNullify chN2)
- where
- -- helper chN1 chN2 = chN2
- helper (_, _) (Zero, Zero) = (Zero, Zero)
- helper (Zero, Zero) (_, _) = (Zero, Zero)
- helper (Zero, b) (c, Zero) = (Zero, b *& c)
- helper (a, Zero) (c, Zero) = (a *& c, Zero)
- helper (a, Zero) (Zero, d) = (Zero, a *& d)
- helper (Zero, b) (Zero, d) = (b *& d, Zero)
- churchIntAbs :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntAbs chN = helper (churchNullify chN)
- where
- helper (Zero, Zero) = (Zero, Zero)
- helper (a, Zero) = (a, Zero)
- helper (Zero, a) = (a, Zero)
- churchIntSignum :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntSignum chN = helper (churchNullify chN)
- where
- helper (Zero, Zero) = (Zero, Zero)
- helper (a, Zero) = (Succ Zero, Zero)
- helper (Zero, a) = (Zero, Succ Zero)
- fromInteger' :: Integer -> (ChurchNumber, ChurchNumber)
- fromInteger' int = helper int (Zero, Zero)
- where
- helper 0 (a, b) = (a, b)
- helper int (a, b) = helper (int - 1) (Succ a, Zero)
- churchIntNegation :: (ChurchNumber, ChurchNumber) -> (ChurchNumber, ChurchNumber)
- churchIntNegation (pos1, neg1) = churchNullify (neg1, pos1)
- churchIntIsPositive :: (ChurchNumber, ChurchNumber) -> Bool
- churchIntIsPositive chN = helper (churchNullify chN)
- where
- helper (Zero, Zero) = False
- helper (_, Zero) = True
- helper (Zero, _) = False
- churchIntIsZero :: (ChurchNumber, ChurchNumber) -> Bool
- churchIntIsZero (Zero, Zero) = True
- churchIntIsZero (_, _) = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement