[Haskell-cafe] function decl syntax oddities

Anthony Clayden anthony.d.clayden at gmail.com
Tue Apr 18 01:27:23 UTC 2023


Consider

> let f1 x     = x + 1 in f1 1             -- ==> 2 ok
> let f2 x y   = x + y in (f2 2) 3     -- ==> 5 ok
> let (f2 x) y = x + y in f2 3 4      -- ==> 7 ok
> let (f1 x)   = x + 1 in f1 4

The last gives a syntax error `Parse error in pattern: f1`. This is in line
with the Language Report, but why? Something to do with patterns?

> let ( x )    = 1      in x             -- ==> 1 ok
> let (Just x) = Just 6 in x           -- ==> 6, pattern binding for x ok
> let  Just x  = Just 7 in x           -- ==> 7 ok
> let (CP x y) = (CP 1) 2 in x    -- ==> 1.0 ok
> let (CP x) y = (CP 3) 4 in x      -- `Parse error in pattern: (CP x)`

So the parens are optional for a pattern binding. But if you use them
they must enclose the whole pattern -- unlike partially applying a data
constr on rhs.

OTOH for a function binding, on lhs there must be at least one var (or
pattern) not appearing inside outermost parens.   Why not optional parens
around a whole function binding?

It might be your lhs is declaring an operator. Then that can't be inside
parens, fair enough:

> let (Just x) +? (Just y) = (x +) y in ...   -- ok
> let ((Just x) +?) (Just y) = x + y in ...    -- rejected

Lexically we can tell a function (starts lower case) from a data constr.
Why the extra restriction with the parens?

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230418/8202bb4b/attachment.html>


More information about the Haskell-Cafe mailing list