将特定类型归纳为GADT中的某个类 [英] Generalizing from a specific type to a class in a GADT
本文介绍了将特定类型归纳为GADT中的某个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下定义
{-# LANGUAGE GADTs, TypeInType, RankNTypes #-}
import Data.Kind
class Character (a :: * -> *) where
showVal :: a b -> b -> String
data ExampleCharacter a where
Variable :: ExampleCharacter String
EqualSign :: ExampleCharacter ()
Deref :: ExampleCharacter ()
instance Character ExampleCharacter where
showVal Variable = id
showVal EqualSign = const "="
showVal Deref = const "*"
data Symbol :: forall a. ExampleCharacter a -> * where
Terminal :: a -> Symbol (b :: ExampleCharacter a)
如您所见,
我定义了一个在类型签名中使用ExampleCharacters的Symbol类.用法示例为let sym = Terminal "xyy" :: Symbol Variable
创建名称为"xyy"
的变量符号.
as you can see I have defined a Symbol class that uses ExampleCharacters in the type signature. Example usage would be let sym = Terminal "xyy" :: Symbol Variable
creating a variable symbol with the name "xyy"
.
现在,显而易见的下一步是将ExampleCharacter
概括为Character c
.我尝试了以下方法:
Now, the obvious next step would be to generalize ExampleCharacter
to Character c
. I tried the following:
data Symbol :: (forall c (a :: *). Character c) => c a -> * where
Terminal :: a -> Symbol (b :: c a)
但是出现以下错误:
main.hs:22:20: error:
• Illegal constraint in a type: forall (c :: * -> *) a. Character c
• In the type ‘Symbol (b :: c a)’
In the definition of data constructor ‘Terminal’
In the data declaration for ‘Symbol’
我不太确定错误要告诉我什么.为什么forall (c :: * -> *) a. Character c
是非法约束,如何成为非法约束?有什么办法解决吗?
I'm not quite sure what the error wants to tell me. Why is forall (c :: * -> *) a. Character c
an illegal constraint and how so? Is there any way around it?
推荐答案
查看全文