问题
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
But when i try to evaluate
g f 10
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?
(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)
回答1:
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`
Now g f 10
works.
回答2:
The problem here is that ?x
isn't bound at the point it's referenced. You and I can see that ?x
will be bound within g
, but the compiler can't. One (confusing) solution is to change
g f 10
to
g (let ?x = 5 in f) 10
来源:https://stackoverflow.com/questions/11141600/implicit-parameter-and-function