I want to write something like:
f :: (a -> b) -> a -> c -> b
f g =
let inner :: a -> c -> b
inner x y = g x
in inner
but this gives me an error.because it doesn't recognize that I'm trying to refer to the same "a" and "b" types as in the declaration as f
How can I explicitly give the proper type for inner?
The types of functions are defined on the basis of the domain, range, and function expression. The expression used to write the function is the prime defining factor for a function. Along with expression, the relationship between the elements of the domain set and the range set also accounts for the type of function.
A function can be defined as a relation between a set of inputs where each input has exactly one output.
Definition. Function declaration is a prototype that specifies the function name, return types and parameters without the function body. Function Definition, on the other hand, refers to the actual function that specifies the function name, return types and parameters with the function body.
let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
You'll need the extension ScopedTypeVariables
. You also need to add an explicit forall a b c .
to your signature, which signals to bind the variables for the whole scope of the definition.
{-# LANGUAGE ScopedTypeVariables #-}
f :: forall a b c . (a -> b) -> a -> c -> b
f g =
let inner :: a -> c -> b
inner x y = g x
in inner
One way to do this would be to bind the outer and inner types by making g a parameter in inner, like this....
f g =
let inner :: (a->b)->a -> c -> b
inner g x y = g x
in inner g
This does change your structure slightly though.... And possibly negates the reason to have an inner let in the first place, but in many cases (depending on the larger program) this might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With