隐式参数和函数 [英] Implicit parameter and function

查看:116
本文介绍了隐式参数和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem considering implicit parameters in Haskell (GHC). I have a function f, that assumes the implicit parameter x, and would like to encapsulate it in a context by applying f to 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 没有绑定,例如:

I get an error that x is not bound, e.g.:

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'

有人可以告诉我,我在做什么错吗?

Can anybody tell me, what I am doing wrong?

(我正在尝试让Haskell使用WordNet界面- http://www.umiacs.umd.edu/~hal/HWordNet/-它以上述方式在隐式参数上使用,并且在尝试编译时会不断出现上述错误)

(I am trying to get the WordNet Interface for Haskell to work - http://www.umiacs.umd.edu/~hal/HWordNet/ - and it uses on implicit parameters in the above manner, and I keep getting errors as the one above when I try to compile it)

推荐答案

g的第一个参数必须为((?x::Int) => Int -> Int)类型,以阐明应将?x传递给f.这可能不是启用Rank2Types(或RankNTypes)的原因.不幸的是,GHC无法推断出这种类型.

The first parameter of g must be of type ((?x::Int) => Int -> Int) to clarify that ?x should be passed to f. This can be dony be enabling Rank2Types (or RankNTypes). Unfortunately, GHC cannot infer this type.

{-# 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有效.

这篇关于隐式参数和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆