Created
March 25, 2014 20:43
-
-
Save runarorama/9770894 to your computer and use it in GitHub Desktop.
Data kinds in Haskell to implement an n-way Either
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
ghci> data OneOf :: [*] -> * where Zero :: a -> OneOf (a ': xs); Succ :: OneOf xs -> OneOf (a ': xs) | |
ghci> let foo = Zero 'a' | |
ghci> let bar = Succ (Zero 42) | |
ghci> let baz = Succ (Succ (Zero True)) | |
ghci> :t [foo, bar, baz] | |
[foo, bar, baz] | |
:: [OneOf ((':) * Char ((':) * Integer ((':) * Bool xs)))] | |
@runarorama It seems pattern matching on that would be quite verbose if one were to use it as an extended Either.
@travisrudd: Yeah, a hundred-way coproduct would be pretty terrible to deconstruct.
@travisrudd: You could probably use the new pattern synonyms to make it nicer if you actually were going to use something like this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anyone wanting to try this, you'll need: