Skip to content

Instantly share code, notes, and snippets.

@runarorama
Created March 25, 2014 20:43
Show Gist options
  • Save runarorama/9770894 to your computer and use it in GitHub Desktop.
Save runarorama/9770894 to your computer and use it in GitHub Desktop.
Data kinds in Haskell to implement an n-way Either
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)))]
@tavisrudd
Copy link

Anyone wanting to try this, you'll need:

{-# LANGUAGE GADTs  #-}
{-# LANGUAGE KindSignatures  #-}
{-# LANGUAGE TypeOperators  #-}
{-# LANGUAGE DataKinds  #-}

@tavisrudd
Copy link

@runarorama It seems pattern matching on that would be quite verbose if one were to use it as an extended Either.

@runarorama
Copy link
Author

@travisrudd: Yeah, a hundred-way coproduct would be pretty terrible to deconstruct.

@jroesch
Copy link

jroesch commented Mar 25, 2014

@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