Search code examples
haskelltypecheckingnested-function

How can I declare the type of a function defined within a let


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?


Solution

  • 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