【问题标题】:Implicit parameter and function隐式参数和函数
【发布时间】:2012-06-21 15:31:44
【问题描述】:

我在考虑 Haskell (GHC) 中的隐式参数时遇到问题。我有一个函数 f,它假定隐式参数 x,并希望通过将 f 应用于 将其封装在上下文中g

f :: (?x :: Int) => Int -> Int
f n = n + ?x

g :: (Int -> Int) -> (Int -> Int)
g t = let ?x = 5 in t

但是当我尝试评估时

g f 10

我收到 x 未绑定的错误,例如:

Unbound implicit parameter (?x::Int)
  arising from a use of `f'
In the first argument of `g', namely `f'
In the second argument of `($)', namely `g f 10'

谁能告诉我,我做错了什么?

(我正在尝试让 Haskell 的 WordNet 接口工作 - http://www.umiacs.umd.edu/~hal/HWordNet/ - 它以上述方式用于隐式参数,当我尝试编译它时,我不断收到上述错误)

【问题讨论】:

    标签: haskell parameters ghc implicit wordnet


    【解决方案1】:

    g 的第一个参数必须是 ((?x::Int) => Int -> Int) 类型,以明确 ?x 应传递给 f。这可以通过启用 Rank2Types(或 RankNTypes)来实现。不幸的是,GHC 无法推断出这种类型。

    {-# LANGUAGE ImplicitParams #-}
    {-# LANGUAGE Rank2Types #-}
    
    f :: (?x::Int) => Int -> Int
    f n = n + ?x
    
    g :: ((?x::Int) => Int -> Int) -> (Int -> Int)
    g f = let ?x = 5 in f`
    

    现在g f 10 有效。

    【讨论】:

      【解决方案2】:

      这里的问题是?x 没有在它被引用的地方绑定。你和我可以看到?x 将绑定在g 内,但编译器不能。一个(令人困惑的)解决方案是改变

      g f 10
      

      g (let ?x = 5 in f) 10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-13
        • 2023-03-17
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多