

Haskell: Let the Type Inferencer Work for You - msangi
http://blog.mariosangiorgio.com/2012/12/17/let-the-type-inferencer-work-for-you/

======
crntaylor
I don't get this line -- _"the Haskell compiler will complain because we still
miss the definition of the type of the function."_

That's not true. The compiler will give the function the most general type it
can infer, which in this case is

    
    
        holds :: Eq a => UnionFindElement a -> a -> Bool
    

without you needing to specify it in the file.

The _point_ of the article is sound - using the interpreter to ask for types
is a great way to work (and often leads to surprising realizations which can
lead you to generalize and abstract your code), but the _reasoning_ is
spurious. Most of the time, you don't need to supply types in Haskell, as the
compiler is perfectly capable of figuring them out for itself.

~~~
msangi
Thanks, I'm going to fix the post right now.

I have been confused by the fact that I got errors while I was not
constraining the type in the right way.

~~~
jerf
More specifically, you remove your type annotations for the target
functions/values entirely, and then ask GHCi what it thinks by loading the
module into it.

You should then examine the result and make sure it is correct. In this case,
you wrote the type signature incorrectly on an otherwise-correct function, but
you will also frequently find that what you wrote is syntactically valid but
has a type you did not intend at all, because it is actually wrong. For
instance, it's easy to end up missing a parameter passing somewhere and ending
up with an unexpected function showing up in the inferred signature.

You can also find that the GHCi-inferred type signature is correct, but not
what you particularly wanted. Sometimes you may want to deliberately constrain
it to something tighter than it will infer. And the other major case I've seen
is that you may want to write the type signature with synonyms you've created;
if you fix the type signature with those, GHCi will use them in the future if
you ask for the type.

------
gtani
More on how much GHC (and other compilers) can reason about resources
(alternatively:infer/thorem prove from type signatures and knowledge about
data structures used:

[http://stackoverflow.com/questions/11725899/is-there-a-
theor...](http://stackoverflow.com/questions/11725899/is-there-a-theory-that-
combines-category-theory-abstract-algebra-and-computation)

[http://homepages.inf.ed.ac.uk/bcampbe2/thesis/thesis-
final.p...](http://homepages.inf.ed.ac.uk/bcampbe2/thesis/thesis-final.pdf)

[http://research-repository.st-
andrews.ac.uk/bitstream/10023/...](http://research-repository.st-
andrews.ac.uk/bitstream/10023/564/6/Pedro%20B%20Vasconcelos%20PhD%20thesis.pdf)

<http://www.cs.cmu.edu/~rwh/papers/iolambda/short.pdf>

Also, there's djinn

~~~
dbaupp
Djinn: "I've written a small program that takes a (Haskell) type and gives you
back a function of that type if one exists."

("cabal install djinn" works to install it.)

<http://lambda-the-ultimate.org/node/1178>

~~~
gtani
Thx, I was looking for a ref:

[http://www.polyomino.f2s.com/david/haskell/programsfromproof...](http://www.polyomino.f2s.com/david/haskell/programsfromproofs.html)

------
loqi
You can also get type signatures non-interactively with ghc's -ddump-types
flag.

