提升的数据类型具有固定数量的类型,它们是提升的数据种类的成员。在这个封闭的世界中,支持在没有明确范围内的字典的类型类上调用函数是否有意义?
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
data DataType = Constructor
data DataTypeProxy (e :: DataType) = DataTypeProxy
class Class (e :: DataType) where
classFunction :: DataTypeProxy e -> IO ()
-- this is the only instance that can be created
instance Class 'Constructor where
classFunction _ = return ()
-- adding the Class constraint fixes the build break
-- disp :: Class e => DataTypeProxy e -> IO ()
disp :: DataTypeProxy e -> IO ()
disp = classFunction
main :: IO ()
main = disp (DataTypeProxy :: DataTypeProxy 'Constructor)
这个人为的例子在 GHC 头中不起作用。这一点也不奇怪,但似乎DataKind
扩展可能使这成为可能。
test.hs:18:8:
No instance for (Class e) arising from a use of `classFunction'
Possible fix:
add (Class e) to the context of
the type signature for disp :: DataTypeProxy e -> IO ()
In the expression: classFunction
In an equation for `disp': disp = classFunction