haskell - Pattern Matching on GADTs -


i've created gadt expressions. when pattern match on constructors have constraints, typechecker unable deduce constraints on type variables used in constructor's constraints. think code , error message more elucidating.

{-# language gadts, multiparamtypeclasses #-} import data.word  data expr   value :: -> expr   cast :: (castable b) => expr -> expr b  class castable b   cast :: -> b  instance castable word64 word32   cast = fromintegral  instance (show a) => show (expr a)   show (cast e) = "cast " ++ show e -- error 

the error get:

gadt.hs:16:30:     not deduce (show a1) arising use of `show'     context (show a)       bound instance declaration @ gadt.hs:15:10-34     or (castable a1 a)       bound pattern constructor                  cast :: forall b a. castable b => expr -> expr b,                in equation `show'       @ gadt.hs:16:9-14     possible fix:       add (show a1) context of         data constructor `cast'         or instance declaration     in second argument of `(++)', namely `show e'     in expression: "cast " ++ show e     in equation `show': show (cast e) = "cast " ++ show e 

edit: if comment out show (expr a) instance , add following code, works fine:

eval :: expr -> eval (value a) = eval (cast e) = cast $ eval e  main =   let bigvalue = maxbound `div` 2 + 5 :: word64       e = cast (value bigvalue) :: expr word32       v = eval e   putstrln "typechecks."   print (bigvalue, v) 

i want show instance print cast (value bigvalue).

cast :: (castable b) => expr -> expr b 

so here:

instance (show a) => show (expr a)   show (cast e) = "cast " ++ show e -- error 

cast e of type expr a. have show a constraint, , instance implies show (expr a), can call show on things of type expr a.

but e not of type expr a. cast takes argument of any type expr a1 , gives expr a (renaming type variables stay consistent we're talking in instance), e of type expr a1. don't have show constraint type a1, , require show a1 imply show (expr a1), there's no way show e.

and there's no way add such constraint in show instance, because type a1 doesn't appear in type of cast e. seems whole point of using gadt here; you've deliberately thrown away information type of thing cast applied (other fact castable a1 a holds), , declared result expr a.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -