koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.3k stars 168 forks source link

Syntax for anonymous functions overly restrictive #86

Closed freddie-freeloader closed 4 years ago

freddie-freeloader commented 4 years ago

Currently, the parser works the following way:

> val x = (fun(x) { x })
x : forall<a> (x : a) -> a

> val x = (fun (x) { x })
           ^
((2),10): error: invalid syntax
 unexpected keyword fun
 expecting expression or ")"

This seems to me overly restrictive and the error message doesn't help here either.

dhil commented 4 years ago

I think this is a duplicate of #44.

freddie-freeloader commented 4 years ago

Sorry, for not making a clearer suggestion.

The only difference between the two versions is the white space after fun.

I'd suggest that zero or more white spaces would be allowed at this location.

EDIT: Or this breaking the disambiguation between named and anonymous functions regarding lookahead?

dhil commented 4 years ago

Thank you for the clarification.

According to the specification this behaviour is not a bug. The specification says the following about production rule matching:

[..] the order of the productions is not important and at each point the longest matching lexeme is preferred [..] a prefix or postfix pattern is included when considering a longest match.

The rule for anonymous functions reads (here I have quoted lexemes for clarity):

funanon ::= ('fun' | 'function')< '<' | '(' >       (anonymous functions must be followed by a '(' or '<')) 

Thus no space is allowed between fun/function and </( as that would break the longest matching lexeme rule.

As I mentioned in #44, an argument can be made for relaxing such syntactic constraints, however, as per the current language specification the program

val x = (fun (x) { x })

is not a valid Koka program.