Skip to content

Instantly share code, notes, and snippets.

@hyone
Created September 5, 2012 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyone/3629254 to your computer and use it in GitHub Desktop.
Save hyone/3629254 to your computer and use it in GitHub Desktop.
Define function that accepts only a specific data constructor on a type
{-# LANGUAGE GADTs #-}
data Nil
data Cons
data StrongList x tag where
Nil :: StrongList a Nil
Cons :: a -> StrongList a b -> StrongList a Cons
safeHead :: StrongList x Cons -> x
safeHead (Cons a b) = a
{-
ghci> safeHead (Cons 1 (Cons 2 Nil))
1
ghci> safeHead Nil
<interactive>:51:10:
Couldn't match expected type `Cons' with actual type `Nil'
Expected type: StrongList x0 Cons
Actual type: StrongList x0 Nil
In the first argument of `safeHead', namely `Nil'
In the expression: safeHead Nil
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment