haskell - Typeably casting GADTs -
let's i'm writing dsl , want have support both phantom type support , badly typed expressions. value types might be
{-# language gadts, datakinds #-} data ty = num | bool deriving (typeable) data val vnum :: int -> val num vbool :: bool -> val bool and can work phantom erased version
{-# language existentialquantification #-} data valunk = forall . valunk (v' a) now, can operate on values of valunk caseing out both vnum , vbool , reestablish phantom types in way
getnum :: valunk -> maybe (val num) getnum (valunk n@(vnum _)) = n getnum _ = nothing but feels i'm reimplementing typeable machinery. unfortunately, ghc won't let me derive typeable val
src/types/core.hs:97:13: can't make derived instance of `typeable (val a)': val must have arguments of kind `*' in data declaration val is there way around restriction? i'd love write
getit :: typeable => valunk -> maybe (val a) getit (valunk v) = cast v but right have resort machinery this
class typeably b x kast :: x -> maybe (x b) instance typeably num val kast n@(vnum _) = n kast _ = nothing for of types.
first of all, need store witness quantified type in valunk in typeable:
data valunk = forall . typeable => valunk (val a) once have this, can use gcast achieve you're asking for:
getit :: typeable => valunk -> maybe (val a) getit (valunk v) = gcast v this tested with:
data val vnum :: int -> val int vbool :: bool -> val bool
Comments
Post a Comment