Browse thread
If OCaml were a car
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | skaller <skaller@u...> |
| Subject: | Re: [Caml-list] If OCaml were a car |
On Mon, 2007-08-20 at 04:37 +0100, Jon Harrop wrote:
> >
> > The thing is that 'else' has a different 'precedence' than 'then'.
> > So you can write:
> >
> > if e then e1 else a; b; c
> >
> > and a,b,c are all executed if the condition is false,
>
> I do not believe this is true. The "b" and "c" are executed in both cases
> because the above is parsed as:
>
> (if e then e1 else a); b; c
>
> which is syntactically uniform:
>
> # (<:expr< (if true then a else a);b;c >>) =
> (<:expr< if true then a else a;b;c >>);;
> - : bool = true
>
> Perhaps you were thinking of this:
>
> if p then t else
> let () = () in
> f1;
> f2;
> f3
>
> because "let" binds more tightly.
Well there you go! I've been writing Ocaml almost 10 years and
didn't know that. Here's my real code:
if ret = `BTYP_tuple [] then "// elided (returns unit)\n","" else
let funtype = fold syms.dfns (`BTYP_function (argtype, ret)) in
I guess I'm just lucky. From what you say, the grammar is exceptionally
bad: the scope of the 'else' is *extended* by the let/in non-terminal
so it goes past ';' -- that should be a syntax error.
Roughly a 'loosely' binding operator is extending the scope
of a 'tightly' binding operator: doing that is fragile. For example
if I drop the 'let .. in' part the semantics may** change silently.
The 'right' way to extend a scope is to use an scope extension
operator with no semantics, such as ( .. ) or begin .. end,
or $ in Felix in Haskell.
But please note: just because I present this analysis doesn't mean
I believe it 100%. Contrarily .. it is just a data point. It has
to be weighted against other factors: every syntactic form has
both advantages and disadvantages, and it is very dependent
on the client's prior experience. ***
German, for example, has the ridiculously clumsy grammar of
putting the verb at the end and concatenating adjectives onto
nouns: "Ein Schwartz-Wald-Spatzieren-Gehen" = "A Walk in
the Black Forest" .. literally "A Black Forest Walk Going"
(FYI: a famous jazz number from the '60s).
I'd probably fall over laughing at how stupid French is :)
Except .. I happen to know English is the most absurd language
around. It just happens to be my native language so I'm used
to it.
** it's likely I'll get a type error and become very confused,
but that is much better than not getting a type error, which is
quite possible if everything is returning unit.
*** when I designed Felix I made a decision that the grammar
had to be LALR1 and entirely unambiguous, a choice easily
enforced by using Ocamlyacc (without any %prec rubbish,
and by grepping the .output to make sure there were
no shift/reduce conflicts).
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net