Skip to content

Instantly share code, notes, and snippets.

@plaidfinch
Last active August 29, 2015 14:07
Show Gist options
  • Save plaidfinch/23894b1ffbecf0552fcd to your computer and use it in GitHub Desktop.
Save plaidfinch/23894b1ffbecf0552fcd to your computer and use it in GitHub Desktop.
-- a Wrapper is a pair of *something*, and if that something is an Int, an additional boolean
data Wrapper a =
Wrapper { get :: a
, test :: (a ~ Int) => Bool }
-- We can construct a Wrapper from something of any type
makeWrapper :: a -> Wrapper a
makeWrapper x = Wrapper x (x == 0) -- Whoa! We're comparing it to zero even if it might not be an Int!
-- We can construct a Wrapper, as noted above, at various type indices…
foo :: Wrapper String
foo = makeWrapper "foo"
bar :: Wrapper Int
bar = makeWrapper 5
-- And we can destruct it with both destructors if it has an Int type index...
get bar == 5
test bar == False
-- But we can only use one of the destructors if it was not...
get foo == "foo"
--test foo -- type error: can’t unify String with Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment