[Haskell-beginners] Why are the types of the result different?

Brent Yorgey byorgey at seas.upenn.edu
Wed Sep 29 22:57:26 EDT 2010

On Wed, Sep 29, 2010 at 06:19:33PM -0700, Russ Abbott wrote:
> testWhere1 = a
>              where b = 1
>                       a = b+1
> > testWhere1
>  2
> testWhere2 = a
>              where b = 1
>                       c = 1/(b - b)
>                       a = b+1
> > testWhere2
>  2.0

Haskell infers the types of a, b, and c *statically*, at compile time.
In the first example, a and b are simply constrained to be any numeric
type.  By the type defaulting mechanism, their type is defaulted to
Integer.  In the second example, the division operator (/) only
operates on fractional numeric types, so (b - b) must also have a
fractional type.  Since the arguments and result of subtraction all
must have the same type, b must also have a fractional type, and hence
so must a since it is defined by addition on b.  Types which are
constrained to be fractional but not otherwise constrained are
defaulted to Double.

Note that this all happens statically at compile-time, so the fact
that c is never evaluated is not relevant.


More information about the Beginners mailing list