haskell - 对受约束的 GADT 记录使用记录更新语法

标签 haskell record typeclass gadt

我偶然发现了以下小问题。我正在使用 Haskell 记录语法以及 GADT:

{-# LANGUAGE GADTs #-}

data Test a where 
  Test :: {someString :: String, someData :: a} -> Test a

现在我想创建一个新的Test someData 的不同类型的值, 但 someString 的值相同(以证明使用记录更新语法的合理性):
test :: Test a -> Test Bool
test t = t {someData = True}

假设我将另一个字段添加到 Test构造函数:
data Test a where 
  Test :: {someString :: String, someData :: a, someMoreData :: a} -> Test a

然后我必须更改这两个字段以保持我的代码类型正确:
test :: Test a -> Test Bool
test t = t {someData = True, someMoreData = False}

到目前为止,我不需要 GADT,但现在我想为数据类型添加类型类约束,例如 Eq :
data Test a where 
  Test :: Eq a => {someString :: String, someData :: a} -> Test a

当试图“更新” someData字段,就像在第一个示例中一样,我突然得到一个编译器错误:
Couldn't match type ‘a’ with ‘Bool’
  ‘a’ is a rigid type variable bound by
      the type signature for test :: Test a -> Test Bool at Test.hs:18:9
Expected type: Test Bool
  Actual type: Test a
Relevant bindings include
  t :: Test a (bound at Test.hs:19:6)
  test :: Test a -> Test Bool (bound at Test.hs:19:1)
In the expression: t
In the expression: t {someData = True}

我怀疑这与之前的案例中的“问题”相同,有两个类型为 a 的字段,但更隐晦一些。我猜 Eq 的字典类型类被视为构造函数的参数,就像我有一个字段 {eqDict :: Eq a} .如果我是对的,那么我还必须以某种方式“更新”“字典字段”,尽管我不知道该怎么做。问题是,当像这样涉及类型类时,有没有办法使用记录更新语法?

最佳答案

恐怕这还不可能;有一个seven year old outstanding feature request .

关于haskell - 对受约束的 GADT 记录使用记录更新语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439605/

相关文章:

haskell - 如何为其他人的库中定义的类型派生 Show?

haskell - 新数据的模式匹配

Haskell:如何告诉 hlint 不要: `Warning: Use string literal`

haskell - 我可以证明 (forall x.Coercible (a x) (b x)) 意味着 Coercible a b 吗?

r - R中的录音

objective-c - 对象-c/iOS :About use NSUserDefaults set serial numbers/password

haskell - Haskell 中有什么好的记录处理技巧吗?

haskell - 使用 Haskeline 的 getInputLine 的输入

haskell - 是否有任何语言扩展或 Haskell 的语言后代,有利于表现力,特别是在实例处理方面?

haskell - 为什么foldl1'没有推广到Foldable?