Skip to content

Instantly share code, notes, and snippets.

@Shimuuar
Created July 18, 2011 11:19
Show Gist options
  • Save Shimuuar/1089228 to your computer and use it in GitHub Desktop.
Save Shimuuar/1089228 to your computer and use it in GitHub Desktop.
GADT with set of allowed parameters
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
-- Type constructor for list of types which is used in tests for
-- memberhip
data a ::: b
infixr :::
-- a and b are instances if a is element of list
class Elem a b
instance Elem a a -- Singleton list
instance Elem a (a ::: l) -- Head matches
instance Elem a l => Elem a (x ::: l) -- Check rest of list
-- Contrived GADT
data AST a where
Var :: (a `Elem` (Int ::: Double ::: String)) => String -> AST a
Str :: String -> AST String
instance Show (AST a) where
show (Var s) = "Var " ++ s
show (Str s) = "Str " ++ s
{-
Int is OK
>>> Var "test" :: AST Int
Var test
But () is not
>>> Var "test" :: AST ()
<interactive>:1:1:
No instance for (Elem () [Char])
arising from a use of `Var'
Possible fix: add an instance declaration for (Elem () [Char])
In the expression: Var "test" :: AST ()
In an equation for `it': it = Var "test" :: AST ()
Also Var should have concrete type. Otherwise GHC will not know which
type to choose and will reply rather unelpfl massage about incoherent
instances
>>> Var "Test"
<interactive>:1:1:
Overlapping instances for Elem a0 (Int ::: (Double ::: String))
arising from a use of `Var'
Matching instances:
instance [overlap ok] Elem a l => Elem a (x ::: l)
-- Defined at qqq/teq.hs:18:10-37
instance [overlap ok] Elem a (a ::: l)
-- Defined at qqq/teq.hs:17:10-25
instance [overlap ok] Elem a a -- Defined at qqq/teq.hs:16:10-17
(The choice depends on the instantiation of `a0'
To pick the first instance above, use -XIncoherentInstances
when compiling the other instance declarations)
In the expression: Var "test"
In an equation for `it': it = Var "test"
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment