Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add type annotation in let binding

Tags:

haskell

I'm a beginner(ish) in Haskell and I find error message really hard to understand (I guess it comes with time). Anyway, to help me understanding my mistakes, I tried to add intermediate variable with type annotation in a let binding and found that it generate even more errors, even though my type are (I think) correct.

Example;

f :: a -> a f x =      let x' = x :: a     in x' 

Generate the following error

test.hs:3:12: Couldn't match expected type `a2' with actual type `a'   `a2' is a rigid type variable bound by        an expression type signature: a2 at test.hs:3:12   `a' is a rigid type variable bound by       the type signature for f :: a -> a at test.hs:1:6 In the expression: x :: a In an equation for x': x' = x :: a In the expression: let x' = x :: a in x 

Am I doing something wrong or is it not possible to do so ?

like image 205
mb14 Avatar asked Mar 16 '14 11:03

mb14


2 Answers

Solution

You need the ScopedTypeVariables extension for this to work, like this:

{-# LANGUAGE ScopedTypeVariables #-}  f :: forall a. a -> a f x =      let x' = x :: a     in x' 

Explanation

If you have a type signature like this

f :: a -> a 

then it indicates that f is polymorphic and works for any choice of a. So f could be used at type Int -> Int, or at type Bool -> Bool, or at type [Int -> Bool] -> [Int -> Bool] – whatever you like.

If you have a type annotation like this

x :: a 

it means a similar thing, namely that x should be usable at any type of your choice. But that's not true in your case. The outer function f is polymorphic, but within the function, x has to be of the same type a that the user has chosen for the outer x. By default, Haskell makes no connection between type variables occurring in different type signatures and annotations. However, you can tell it to do so by enabling the ScopedTypeVariables extension. Now, by prefixing a -> a with forall a., you can explicitly instruct Haskell to make a visible as a specific type variable within the definition of f. If you then annotate x :: a, it refers to the outer a rather than a new polymorphic a.

like image 59
kosmikus Avatar answered Sep 18 '22 12:09

kosmikus


For anyone who is looking to type annotate the binding instead of the expression - ScopedTypeVariables allows you to do that too!

f1 = do   let x :: Int = 5   y :: Int <- someMonadicOperation   return $ x + y 
like image 35
Jacob Wang Avatar answered Sep 21 '22 12:09

Jacob Wang