Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit parameter and function

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)

like image 622
niklascp Avatar asked Jun 21 '12 15:06

niklascp


People also ask

What is implicit by the argument of a function?

An implicit argument of a function is an argument which can be inferred from contextual knowledge. There are different kinds of implicit arguments that can be considered implicit in different ways. There are also various commands to control the setting or the inference of implicit arguments.

What is implicit parameter in C++?

Implicit and dynamic parameters are proposed as a general means to reduce the length of argument lists of function calls without resorting to dangerous global variables. In C++, these new kinds of parameters constitute a generalization of parameters with default arguments, whose values can be omitted in function calls.

How do you pass an implicit parameter?

The implicit parameter in Java is the object that the method belongs to. It's passed by specifying the reference or variable of the object before the name of the method. An implicit parameter is opposite to an explicit parameter, which is passed when specifying the parameter in the parenthesis of a method call.

How many implicit parameters can a method have?

A method or constructor can have only one implicit parameter list, and it must be the last parameter list given. A method with implicit parameters can be applied to arguments just like a normal method.


2 Answers

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.

like image 92
real-or-random Avatar answered Oct 19 '22 23:10

real-or-random


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
like image 38
jekor Avatar answered Oct 20 '22 00:10

jekor