Version française
Home     About     Download     Resources     Contact us    
Browse thread
F# lightweight syntax for Ocaml with camlp4 - possible?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Dmitry Bely <dmitry.bely@g...>
Subject: F# lightweight syntax for Ocaml with camlp4 - possible?
Some times ago F# team has introduced a new Caml syntax option called
"lightweight".   From the F# manual:

http://research.microsoft.com/fsharp/manual/lexyacc.aspx
[...]
F# supports the optional use of lightweight syntax through the use of
whitespace to make indentation significant. At the time of this
release this is an experimental feature, though it is assumed that its
use will become widespread. The indentation-aware syntax option is a
conservative extension of the explicit language syntax, in the sense
that it simply lets you leave out certain tokens such as in and ;; by
having the parser take indentation into account. This can make a
surprising difference to the readability of code.
[...]
Basic rules by example

// Without the light syntax option
// 'done' is required
let FunctionSample() =
    let tick x = printf "tick %d\n" x in
    let tock x = printf "tock %d\n" x in
    let choose f g h x =
        if f x then g x else h x in
    for i = 0 to 10 do
        choose (fun n -> n%2 = 0) tick tock i
    done;
    printf "done!\n"

// When the light syntax option is
// enabled 'done' is optional and the scope of
// structured constructs such as match, for, while
// and if/then/else is determined by indentation.
#light
let FunctionSample() =
    let tick x = printf "tick %d\n" x
    let tock x = printf "tock %d\n" x
    let choose f g h x =
        if f x then g x else h x
    for i = 0 to 10 do
         choose (fun n -> n%2 = 0) tick tock i
    printf "done!\n"

[end of quote]

Could the same idea be implemented for Ocaml with Camlp4? Can it use
an indentation level while parsing?

I realize that the third syntax will be 'too much' but it really seems
to be quite useful...

- Dmitry Bely