[Haskell-cafe] Constructors on left and right hand sides of equation

Chris Warburton chriswarbo at googlemail.com
Wed Sep 17 16:49:56 UTC 2014


PATRICK BROWNE <patrick.browne at dit.ie> writes:

> drink (NewGlass i) j = (NewGlass i)   -- Eq 1
>
> In Eq 1 is  (NewGlass i) on the LHS distinct from (NewGlass i) on the
> RHS?

> drink (Fill m i) j
>   | full (Fill m i)  = Fill (NewGlass (size m))  ((size m) - j)  -- Eq2

> In Eq2  are the occurrences  of the data constructor Fill on the LHS and
> RHS distinct?

Yes they're distinct. This is important since their types can be
different. Here's an extreme example:

> data Proxy a = Proxy
>
> foo :: Proxy Int -> Proxy Bool
> foo Proxy = Proxy

It looks like "foo" is the identity function, returning its argument
value, similar to these "bar" functions:

> bar1 :: () -> ()
> bar1 () = ()
> 
> bar2 :: () -> ()
> bar2 = id

However, it's not:

> foo2 :: Proxy Int -> Proxy Bool
> foo2 = id

> [1 of 1] Compiling Main             ( test.hs, interpreted )
> 
> test.hs:7:8:
>     Couldn't match type `Int' with `Bool'
>     Expected type: Proxy Int -> Proxy Bool
>       Actual type: Proxy Int -> Proxy Int
>     In the expression: id
>     In an equation for `foo2': foo2 = id
> Failed, modules loaded: none.

Hence, in general, we must assume that they're different values. We
tend to use different terminology too, for example "patterns" on the
left, "expressions" on the right; "destructing" on the left,
"constructing" on the right.

Cheers,
Chris


More information about the Haskell-Cafe mailing list