Created
September 5, 2012 02:16
-
-
Save hyone/3629254 to your computer and use it in GitHub Desktop.
Define function that accepts only a specific data constructor on a type
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
{-# 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