简体   繁体   English

使用RankNTypes和TypeFamilies的非法多态或限定类型

[英]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类型来删除用于分类值的两个新类型( ConstValueValue )时遇到了一个小问题由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 -> blahValue '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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM