haskell - promoted datatypes and class instances -
promoted datatypes have fixed number of types members of promoted data kind . in closed world make sense support calling function on typeclass without dictionary explicitly in scope?
{-# language datakinds #-} {-# language polykinds #-} data datatype = constructor data datatypeproxy (e :: datatype) = datatypeproxy class class (e :: datatype) classfunction :: datatypeproxy e -> io () -- instance can created instance class 'constructor classfunction _ = return () -- adding class constraint fixes build break -- disp :: class e => datatypeproxy e -> io () disp :: datatypeproxy e -> io () disp = classfunction main :: io () main = disp (datatypeproxy :: datatypeproxy 'constructor)
this contrived example doesn't work in ghc head. it's not @ surprising seems datakind
extension might make possible.
test.hs:18:8: no instance (class e) arising use of `classfunction' possible fix: add (class e) context of type signature disp :: datatypeproxy e -> io () in expression: classfunction in equation `disp': disp = classfunction
no. doing allowing mean phantom data types need "tagged" type information @ runtime, , produce ambiguity.
data datatype = constructor | somethingelse data datatypeproxy (e :: datatype) = datatypeproxy ... instance class 'somethingelse classfunction _ = putstrln "hello world" instance class 'constructor classfunction _ = return () disp :: datatypeproxy e -> io () disp = classfunction main = disp datatypeproxy
what should program do? should not compile? if not, adding constructor type, took program compile, , produced 1 not. if should not compile has 2 valid behaviors.
main = disp (datatypeproxy :: datatypeproxy 'constructor)
has 1 possible interpretation...but requires dispatch on phantom type. is,
main = disp (datatypeproxy :: datatypeproxy 'somethingelse)
is identical program @ term level, has different behavior. breaks nice properties parametrically. typeclass based dispatching solution this, since instances in scope effect program behavior in predictable (and semantically specified) way.
Comments
Post a Comment