您好。我正在玩Ivory库,它很大程度上依赖于Haskell的现代功能。其中,它定义了类型IvoryType
接受所有类型和IvoryArea
接受特殊类型Area
的类型。定义如下:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ExistentialQuantification #-}
-- | Proxy datatype with a phantom arbitrary-kinded type
-- and a single constructor
data Proxy (a :: k) = Proxy
-- | The kind of memory-area types.
data Area k
= Struct Symbol
| Array Nat (Area k)
| CArray (Area k)
| Stored k
-- ^ This is lifting for a *-kinded type
class IvoryType t where
ivoryType :: Proxy t -> I.Type {- arguments are not important -}
-- | Guard the inhabitants of the Area type, as not all *s are Ivory *s.
class IvoryArea (a :: Area *) where
ivoryArea :: Proxy a -> I.Type {- arguments are not important -}
行。现在让我们尝试表达我们将存储定义了ivoryType
函数的值的事实。显然,他们 是IvoryType
类的成员,所以答案是
data TypeStorage = TypeStorage (forall t . IvoryType t => t)
到目前为止一切顺利。现在我们要存储定义了ivoryArea
函数的值。让我们使用IvoryArea
类作为过滤条件,就像在prevoius案例中一样:
data AreaStorage = AreaStorage (forall t . IvoryArea t => t)
令人惊讶的是,编译器(ghc版本7.8.4)输出错误
src/IvoryLL/Types.hs:59:45:
Expected a type, but ‘t’ has kind ‘Area *’
In the type ‘forall t. IvoryArea t => t’
In the definition of data constructor ‘AreaBase’
In the data declaration for ‘Area
请您解释一下如何正确表达Haskell中ivoryArea
函数的所有权?
修改 一些指向原始声明的链接:
答案 0 :(得分:2)
既然我们已经在评论中确定你不能直接做你想做的事情,那就是创造一个所有类型的特殊“subkind”,我们可以使用更多的腿部工作得到你想要的东西。
我们只是使用(已关闭)类型系列将您的Area *
类型解释为某种类型*
,然后将GADT(由Area *
编入索引)来保存此类值。然后我们可以将整个shebang包裹在一个存在主义中,以存储这种类型的任意值,如果需要的话。
考虑这个减少的例子:
data Area k
= Stored k
| List (Area k)
type family InterpIvoryArea a :: * where
InterpIvoryArea (Stored k) = k
InterpIvoryArea (List a) = [InterpIvoryArea a]
data AreaStorage a where
AreaStorage :: InterpIvoryArea a -> AreaStorage a
data AreaStorageExistential where
AreaStorageExistential :: AreaStorage a -> AreaStorageExistential
testValue :: AreaStorageExistential
testValue = AreaStorageExistential (AreaStorage [1,2,3] :: AreaStorage (List (Stored Int)))