Created
April 24, 2017 14:40
-
-
Save imccoy/66e8c406c16e8ea4d3f512bf8431f3d7 to your computer and use it in GitHub Desktop.
playing with church numerals, inspired by a certain dependently typed programming textbook.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE ScopedTypeVariables, InstanceSigs, RankNTypes #-} | |
| import Prelude hiding (succ) | |
| type CN a = (a -> a) -> a -> a | |
| inCN :: CN a -> CN (CN a) | |
| inCN n f x = undefined | |
| zero :: CN a | |
| zero _ x = x | |
| succ :: CN a -> CN a | |
| succ n f x = f (n f x) | |
| one :: CN a | |
| one = succ zero | |
| plus :: CN a -> CN a -> CN a | |
| plus a b f x = a f (b f x) | |
| times :: CN a -> CN a -> CN a | |
| times a b f x = a (b f) x | |
| exp :: forall a. CN a -> CN (CN a) -> CN a | |
| exp a b = b (times a) (succ zero) | |
| exp' a b = b (times a) (succ zero) | |
| newtype CN1 = CN1 (forall a. (a -> a) -> a -> a) | |
| zero1 :: CN1 | |
| zero1 = CN1 $ \_ x -> x | |
| succ1 :: CN1 -> CN1 | |
| succ1 (CN1 n) = CN1 $ \f x -> f (n f x) | |
| one1 :: CN1 | |
| one1 = succ1 zero1 | |
| plus1 :: CN1 -> CN1 -> CN1 | |
| plus1 (CN1 a) (CN1 b) = CN1 $ \f x -> a f (b f x) | |
| times1 :: CN1 -> CN1 -> CN1 | |
| times1 (CN1 a) (CN1 b) = CN1 $ \f x -> a (b f) x | |
| exp1 :: CN1 -> CN1 -> CN1 | |
| --exp1 a (CN1 b) = b (times1 a) (succ1 zero1) | |
| exp1 m (CN1 n) = n (times1 m) (succ1 zero1) | |
| exp1' :: CN1 -> CN1 -> CN1 | |
| exp1' (CN1 n) (CN1 m) = m (\(CN1 b) -> CN1 $ \f x -> n (b f) x) (succ1 zero1) | |
| print1 (CN1 f) = show $ f (+1) 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment