Are pattern guards obsolete?
apfelmus at quantentunnel.de
apfelmus at quantentunnel.de
Wed Dec 13 13:33:29 EST 2006
Iavor Diatchki wrote:
> I am not clear why you think the current notation is confusing...
> Could you give a concrete example? I am thinking of something along
> the lines: based on how "<-" works in list comprehensions and the do
> notation, I would expect that pattern guards do XXX but instead, they
> confusingly do YYY. I think that this will help us keep the
> discussion concrete.
Pattern guards basically are a special-case syntactic sugar for
(instance MonadPlus Maybe). The guard
foo m x
| empty m = bar
| Just r <- lookup x m, r == 'a' = foobar
directly translates to
foo m x = fromMaybe $
(do { guard (empty m); return bar;})
`mplus`
(do {Just r <- return (lookup m x); guard (r == 'a');
return foobar;})
The point is that the pattern guard notation
Just r <- lookup m x
does *not* translate to itself but to
Just r <- return (lookup m x)
in the monad. The <- in the pattern guard is a simple let binding. There
is no monadic action on the right hand side of <- in the pattern guard.
Here, things get even more confused because (lookup m x) is itself a
Maybe type, so the best translation into (MonadPlus Maybe) actually would be
r <- lookup m x
Regards,
apfelmus
More information about the Haskell-prime
mailing list