[英]Illegal polymorphic or qualified type using RankNTypes and TypeFamilies
I've been slowly working on porting the llvm package to use data kinds, type families and type-nats and ran into a minor issue when trying to remove the two newtypes used for classifying values ( ConstValue
and Value
) by introducing a new Value
type parameterized by its constness. 我一直在慢慢地将llvm包移植到使用数据种类,类型族和type-nats,并在尝试通过引入新的
Value
类型来删除用于分类值的两个新类型( ConstValue
和Value
)时遇到了一个小问题由constness参数化。
CallArgs
only accepts Value 'Variable a
arguments and provides a function for casting a Value 'Const a
to a Value 'Variable a
. CallArgs
只接受Value 'Variable a
参数,并提供一个函数,用于将Value 'Const a
转换为Value 'Variable a
。 I'd like to generalize CallArgs
to allow each argument to be either 'Const
or 'Variable
. 我想概括
CallArgs
以允许每个参数为'Const
或'Variable
。 Is this possible to encode this somehow using type families? 是否可以使用类型系列以某种方式对此进行编码? I think it's probably doable with fundeps.
我认为这可能与fundeps有关。
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
data Const = Const | Variable
data Value (c :: Const) (a :: *)
type family CallArgs a :: *
type instance CallArgs (a -> b) = forall (c :: Const) . Value c a -> CallArgs b
type instance CallArgs (IO a) = IO (Value 'Variable a)
... which fails to compile: ...无法编译:
/tmp/blah.hs:10:1: Illegal polymorphic or qualified type: forall (c :: Const). Value c a In the type instance declaration for `CallArgs'
Where the following solution works (equivalent to the legacy code), but requires the user to cast the each constant Value
: 以下解决方案的工作原理(相当于遗留代码),但要求用户强制转换每个常
Value
:
type family CallArgs' a :: *
type instance CallArgs' (a -> b) = Value 'Variable a -> CallArgs' b
type instance CallArgs' (IO a) = IO (Value 'Variable a)
The CallArgs
you're asking for is kind of like a non-deterministic function which takes a -> b
and returns either Value 'Const a -> blah
or Value 'Variable a -> blah
. 你要求的
CallArgs
有点像一个非确定性函数,它接受a -> b
并返回Value 'Const a -> blah
或Value 'Variable a -> blah
。 One thing you can sometimes to with nondeterministic functions is flip them around; 有时你可以用非确定性函数来翻转它们; indeed, this one has a deterministic inverse.
实际上,这个具有确定性的逆。
type family UnCallArgs a
type instance UnCallArgs (Value c a -> b) = a -> UnCallArgs b
type instance UnCallArgs (IO 'Variable a) = IO a
Now, anywhere you would have written a type like 现在,你可以写任何类型的类型
foo :: CallArgs t -> LLVM t
or something like that, you can write this instead: 或类似的东西,你可以写这个:
foo :: t -> LLVM (UnCallArgs t)
Of course, you might want to pick a better name than UnCallArgs
, maybe Native
or something like that, but doing that well requires a bit of domain knowledge that I don't have. 当然,您可能希望选择一个比
UnCallArgs
更好的名称,可能是Native
或类似的东西,但这样做很好需要一些我没有的领域知识。
Would wrapping the forall c. 将包裹forall c。 in a
newtype AV
work for you? 在一个
newtype AV
为你工作?
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
data CV = Const | Variable
data Value (c :: CV) (a :: *)
data AV a = AV (forall c. Value c a)
type family CallArgs a :: *
type instance CallArgs (a -> b) = AV a -> CallArgs b
type instance CallArgs (IO a) = IO (Value 'Variable a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.