Skip to content

Instantly share code, notes, and snippets.

@EarlGray
Last active August 29, 2015 14:20
Show Gist options
  • Save EarlGray/2c68e28595232ee500e8 to your computer and use it in GitHub Desktop.
Save EarlGray/2c68e28595232ee500e8 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GADTs #-}
module SafeList where
data Empty
data NonEmpty
class Emptiness e
instance Emptiness Empty
instance Emptiness NonEmpty
data List t s where
Nil :: List t Empty
Cons :: t -> List t s -> List t NonEmpty
safeHead :: List t NonEmpty -> t
safeHead (Cons h _) = h
safeTail :: Emptiness s1 => List t NonEmpty -> List t s1
safeTail (Cons _ tl@(Cons y ys)) = safeTail tl -- OMG
{-# LANGUAGE GADTs, EmptyDataDecls, KindSignatures #-}
{-
- http://stackoverflow.com/questions/19698904/tail-function-for-safe-list-using-gadts
-}
data NonEmpty a
data Empty
data SafeList :: * -> * -> * where
Nil :: SafeList t Empty
Cons :: a -> SafeList a b -> SafeList a (NonEmpty b)
safeHead :: SafeList a (NonEmpty b) -> a
safeHead (Cons h _) = h
safeTail :: SafeList a (NonEmpty b) -> SafeList a b
safeTail (Cons _ t) = t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment