Syntax...

Vladimir Kirichenko vladimir.kirichenko at gmail.com
Sat Nov 21 10:19:25 PST 2009


Neal Gafter wrote:
> I can't quite infer the structure of the language from your examples. 
> Can you please show the syntax grammar (BNF) for this suggestion?

Ok.

But I'd like to explain some grounds of this syntax first. This proposal
is actually based on experience with other languages with lambda.

ocaml:
	fun x -> x + 1

haskell:
	\x -> x + 1

scala:
	(x:Int) => x + 1

C#:
	(int x) => x + 1

....

So we can see some (as for me it's very successful) pattern here. As I
can see there are 2 possible difficulties with possible lambda syntax:
inconvenient c-like prefixed type manifestation and necessity of checked
exceptions declaration.

Most of these languages have "pascal-like" type manifestation - using
colon. In Java we have similar situation as in C#, it could be good to
consider experience of good enough implementation there.

There are two issues: actual lambda definition and definition of
"function variable" with it's type. There are delegates in C# those were
used to declare type of function:

	delegate int increment(int i);

We have no delegates in java so we have to invent something new for it.
Let's take it from OCaml: "fun".

So possible proposed syntax by example:

a) Anonymous lambda:

EBNF:

	/AnonymousLambda/:
		fun /TypeSpec/ => /Body/
	/TypeSpec/:
		/Type/? /ParamsSpec/? /ThrowsSpec/?
	/ParamsSpec/:
		( /Param/+ )
	/Param/:
		/Type/ ID
	/ThrowsSpec/:
		throws /ExceptionType/+
	/Body/:
		/Expression/ | { /StatementList/ }

The return type, params and throws declared optional to reduce
unnecessary "empty" constructs. See examples:
		
Examples:

1. fully typed:

	fun int (int x) throws Exception => x + 1

2. no exceptions:

	fun int (int x) => x + 1

3. void lambda:

	fun (int x) => System.out.println(x)

4. no params:

	fun => System.out.println("hello")

5. nonvoid with statements

	fun int => { return 10 }

6. exceptional only
	
	fun throws IOException => {
		if (!(new File("/a")).exists())
			 throw new IOException("oops")
	}

b) Named function declaration:

It's very similar to the anonymous lambda except it has identifier, uses
assignment operator instead of implication ( => ), and has no param
identifiers:

EBNF:

	/Function/:
		fun /TypeSpec/ /Assignment/?
	/Assignment/:
		= /AnonymousLambda/
	/TypeSpec/:
		/Type/? ID /ParamsSpec/? /ThrowsSpec/?
	/ParamsSpec/:
		( /Type/+ )
	/ThrowsSpec/:
		throws /ExceptionType/+

Examples:

1. fully typed w/o assignment:

	fun int increment(int) throws Exception;
	....
	increment = fun int (int x) => x + 1

2. void w/o params and w/o assignment:
	
	fun hello;
	....
	hello = System.out.println("hello");

3. different usages as argument:

	int method1( fun int f(int), fun int g(int) ) {
		return f(g(10));
	}

	void method2( fun code ) {
		code();
	}

4. passing as argument (to previous declarations) and  in-place assignment:
	
	fun int inc(int) = fun int (int x) => x + 1;

	method1( inc, fun int (int x) => x * 2 );
	
	method2( fun => System.out.println("hello") );
	

As we can see in example a.4. there is some type duplication in in-place
assignment. So:

c) Named function declaration aka short form. Difference between c) and
b) are IDs in param spec and implication (=>) instead of assignment. So
basically it's Anonymous lambda with name:

EBNF:

	/Function/:
		fun /TypeSpec/ => /Body/
	/TypeSpec/:
		/Type/? ID /ParamsSpec/? /ThrowsSpec/?
	/ParamsSpec/:
		( /Param/+ )
	/Param/:
		/Type/ ID
	/ThrowsSpec/:
		throws /ExceptionType/+
	/Body/:
		 /Expression/ | { /StatementList/ }

Examples:

1. fully typed w/o assignment:

	fun int increment(int) throws Exception => x + 1;

2. void w/o params and w/o assignment:
	
	fun hello => System.out.println("hello");


An the end:

Optionality of empty argument list or/and return type could be
sacrificed for the purposes of parsing.

Benefits of this syntax:

1. It has usual syntax (follows the pattern of different languages with
lambda).

2. Contains no Perl'ish hieroglyphs. Let's not start please!

3. It proposes short form syntax of named function definition w/o
necessity of type duplications.

-- 
Best Regards,
Vladimir Kirichenko

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
Url : http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20091121/db7c12f6/attachment.bin 


More information about the closures-dev mailing list