gpt4 book ai didi

haskell - 定义幻像类型 - 无法编译示例

转载 作者:行者123 更新时间:2023-12-02 04:08:10 25 4
gpt4 key购买 nike

我正在尝试了解有关幻影类型的更多信息。我正在尝试通读 Fun with Phantom Types作者:拉尔夫·欣策。他使用了我以前从未见过的关键字 with,而且我无法编译它。当我尝试此操作时,我在 = 上遇到解析错误。

data Type t = RInt                    with t = Int
| RChar with t = Char
| RList (Type a) with t = [a ]
| RPair (Type a) (Type b) with t = (a, b)

在论文的前面,他说“with”语句并不是绝对必要的,您可以设置a = t,但我不知道如何定义这些数据没有它们的情况下输入。以下错误:不在范围内:类型变量'a'

data Type t = RInt
| RChar
| RList (Type a)
| RPair (Type a) (Type b)

我错过了什么?

最佳答案

虽然丑陋,但以下内容将起作用,并且可能被认为在精神上稍微接近该表示法。这本质上是将 GADT 脱糖为类型相等(它们似乎没有自己的扩展,因此您需要启用 GADTsTypeFamilies 才能使用它们)和存在主义。

{-# LANGUAGE ExistentialQuantification, TypeFamilies #-}

data Type t = t ~ Int => RInt
| t ~ Char => RChar
| forall a. t ~ [a] => RList (Type a)
| forall a b. t ~ (a, b) => RPair (Type a) (Type b)

关于haskell - 定义幻像类型 - 无法编译示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32871086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com