Created
January 21, 2014 05:15
-
-
Save mperry/8534788 to your computer and use it in GitHub Desktop.
This is an attempt to explore the duality of fold and unfold
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
Haskell definitions: | |
unfoldr :: (b -> Maybe (a, b)) -> b -> [a] | |
foldr :: (a -> b -> b) -> b -> [a] -> b | |
If I reverse the arrows on unfoldr | |
dual(unfoldr) :: [a] -> (Maybe (a, b) -> b) -> b | |
I think I need to show the type isomorphism (a -> b -> b) -> b is equivalent to Maybe (a, b) -> b, which is equivalent to | |
(a -> b -> b) == Maybe(a, b) | |
Now I am stuck | |
These slides on slide 15 seems to show this transformation (http://conal.net/talks/folds-and-unfolds.pdf), but I don't understand the steps. | |
Any help on understanding (a -> b -> b) == Maybe(a, b)? |
What is the process for "reversing the arrows"? E.g. we derived
unfoldr:: (Maybe (a, b) -> b) -> [a] -> b
foldr:: (b -> Maybe (a, b)) -> b -> [a]
If we simplify the types to remove some noise we get:
unfoldr :: (m -> b) -> la -> b
foldr :: (b -> m) -> b -> la
If we work on unfoldr, what is the process from here? Do we put in explicit parenthesis for unfoldr?
(m -> b) -> (la -> b)
(m <- b) <- (la <- b) -- reverse arrows
(b -> la) -> (b -> m) -- rearrange terms
Which is not what we want from the simplified types:
foldr :: (b -> m) -> b -> la
Does the process of reversing the arrows depend on whether a type is in the domain or co-domain?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I have how function types are exponential types now after reading http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/. Thanks Matt!