Last active
June 7, 2017 20:44
-
-
Save WillNess/5266509 to your computer and use it in GitHub Desktop.
Church-encoded Lists are efficient?
This file contains 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
http://stackoverflow.com/questions/15589556/why-are-difference-lists-not-an-instance-of-foldable | |
/15593349?noredirect=1#comment22277557_15593349 | |
a = singleton 1 singleton x = ChurchList $ \k z -> k x z | |
b = snoc a 2 snoc xs x = ChurchList $ \k z -> runList xs k (k x z) | |
c = snoc b 3 | |
d = append c c append u v = ChurchList $ \k z -> runList u k (runList v k z) | |
runList a k z = k 1 z a := ChurchList $ \k z -> k 1 z | |
runList b k z = runList a k (k 2 z) = k 1 (k 2 z) b := ChurchList $ \k z -> runList a k (k 2 z) | |
runList c k z = runList b k (k 3 z) = k 1 (k 2 (k 3 z)) c := ChurchList $ \k z -> runList b k (k 3 z) | |
runList d k z = runList c k (runList c k z) = d := ChurchList $ \k z -> runList c k (runList c k z) | |
= k 1 (k 2 (k 3 (runList c k z))) FORCED to WHNF | |
-- OR: | |
runList d k z = d := ChurchList $ \k z -> runList c k (runList c k z) | |
runList c k (runList c k z) = c := ChurchList $ \k z -> runList b k (k 3 z) | |
runList b k (k 3 (runList c k z)) = b := ChurchList $ \k z -> runList a k (k 2 z) | |
runList a k (k 2 (k 3 (runList c k z))) = a := ChurchList $ \k z -> k 1 z | |
= k 1 (k 2 (k 3 (runList c k z))) | |
---------------------------------------------------------------------------------------- | |
g = (((a `append` b) `append` c) `append` d) | |
= append (append (append a b) c) d | |
= ChurchList $ \k z -> runList (append (append a b) c) k (runList d k z) -- ****** | |
------------ g2 | |
----------------------- g1 | |
runList g k z -- g gets forced to WHNF: -- ****** | |
= runList g1 k (runList d k z) -- g1 gets forced to WHNF | |
= runList g2 k (runList c k (runList d k z)) -- g2 gets forced | |
= runList a k (runList b k (runList c k (runList d k z))) -- a gets forced | |
= k 1 (runList b k (runList c k (runList d k z))) | |
---------------------------------------------------------------------------------------- | |
e = append d c -- left-associative -- NB! | |
runList e k z = runList d k (runList c k z) | |
= k 1 (k 2 (k 3 (runList c k (runList c k z)))) -- right-associative -- NB! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment