Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it so uncommon to use type signatures in where clauses?

Tags:

Does it help the compiler to optimise, or is it just surplus work to add additional type signatures? For example, one often sees:

foo :: a -> b foo x = bar x       where bar x = undefined 

Rather than:

foo :: a -> b foo x = bar x       where bar :: a -> b             bar x = undefined 

If I omit the top level type signature, GHC gives me a warning, so if I don't get warnings I am quite confident my program is correct. But no warnings are issued if I omit the signature in a where clause.

like image 381
epsilonhalbe Avatar asked May 15 '12 22:05

epsilonhalbe


1 Answers

There exists a class of local functions whose types cannot be written in Haskell (without using fancy GHC extensions, that is). For example:

f :: a -> (a, Int) f h = g 1   where g n = (h, n) 

This is because while the a in the f type signature is polymorphic viewed from outside f, this is not so from within f. In g, it is just some unknown type, but not any type, and (standard) Haskell cannot express "the same type as the first argument of the function this one is defined in" in its type language.

like image 87
Ingo Avatar answered Oct 14 '22 19:10

Ingo