Haskell:存在类型的记录更新

时间:2011-01-02 20:00:29

标签: haskell record existential-type

当我遇到错误时,我试图将记录更新用于存在记录。一个快速谷歌引导我feature request #2595,这表明它是在版本6.8.3中为GHC实现的。我正在使用6.10.4,所以我认为它应该可行,但功能请求中的示例代码:

{-# LANGUAGE ExistentialQuantification,Rank2Types #-}
module Foo where

data Foo = forall a . Foo { foo :: a -> a, bar :: Int }

x :: Foo 
x = Foo { foo = id, bar = 3 } 

f :: Foo -> Foo 
f rec = rec { foo = id }

g :: Foo -> Foo 
g rec = rec { bar = 3 } 

产生与功能请求中抱怨相同的错误:

test.hs:10:8:
    Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
    Use pattern-matching instead
    In the expression: rec {foo = id}
    In the definition of `f': f rec = rec {foo = id}

test.hs:13:8:
    Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
    Use pattern-matching instead
    In the expression: rec {bar = 3}
    In the definition of `g': g rec = rec {bar = 3}

在某些时候,这是一个有意识地删除的功能,还是应该提交错误报告?

2 个答案:

答案 0 :(得分:5)

实际上,Trac单据称它是在6.12版本中实现的 - 在6.8.3版本中发现发现的错误。因此,您使用的版本比修复程序早。

此外,修复程序的更改日志条目似乎表明它没有完全修复;你仍然会得到第一个错误,而不是第二个错误。如果还没有针对问题的其余部分的错误报告,我会说继续并提交。

答案 1 :(得分:3)

还有另一种方法!


如果您从

更改数据类型定义
data Foo = forall a . Foo { foo :: a -> a, bar :: Int }

data Foo = Foo { foo :: forall a . a -> a, bar :: Int }

,然后编译没有错误。 - 使用ghc-6.12.2.20100531