Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named patterns and type inference?

Tags:

haskell

I was working through some examples and came across an error with my implementation of bind (>>=) for the Error monad:

data E a = Success a
         | Error String

instance Monad E where
    return a = Success a
    (Success a) >>= f = f a
    e@(Error s) >>= _ = e

 

Error.hs:15:25:
Couldn't match type `a' with `b'
  `a' is a rigid type variable bound by
      the type signature for >>= :: E a -> (a -> E b) -> E b
      at Error.hs:14:5
  `b' is a rigid type variable bound by
      the type signature for >>= :: E a -> (a -> E b) -> E b
      at Error.hs:14:5
Expected type: E b
  Actual type: E a
In the expression: e
In an equation for `>>=': e@(Error s) >>= _ = e
In the instance declaration for `Monad E'

If don't use a named pattern (the @ syntax), everything works:

(Error s) >>= _ = Error s

Why aren't these two forms equivalent? What is going on?

like image 230
John Freeman Avatar asked Dec 14 '22 23:12

John Freeman


1 Answers

Let's start by looking at the type of Error:

Error :: String -> E a

That means that for any a type, you can get an E a by using something like Error "foo". However, each specific Error "foo" value has to choose a specific a, and you can't change it afterwards. So Error "foo" :: E Int is not the same as Error "foo" :: E String.

So in your specific example, e is referring to the actual "original" Error s value, of type E a, whereas in the alternative formulation you are constructing a new Error s value, which type inference forces to have type E b.

like image 164
GS - Apologise to Monica Avatar answered Dec 28 '22 00:12

GS - Apologise to Monica