From: Gerard Huet <Gerard.Huet@inria.fr>
To: caml-list@inria.fr
Subject: [Caml-list] Syntax
Date: Tue, 05 Feb 2002 15:18:57 +0100 [thread overview]
Message-ID: <200202051419.g15EJSH21401@concorde.inria.fr> (raw)
At 13:05 05/02/02 +0100, Daniel de Rauglaudre wrote:
>Hi,
>
>On Tue, Feb 05, 2002 at 12:53:39PM +0100, Remi VANICAT wrote:
>
>> So the fact that a library or a tool is written in revised or
>> standard library doesn't change anything for those who use it. The
>> only problem is for those who want to change the source, but it's
>> not so hard to learn this syntax if you want to use it.
>
>Exactly.
Yessir.
It is trivial to switch to revised syntax. It will take you one week to
get used to True/False vs true/false, to use square brackets in [x :: l],
and to write (list int) instead of (int list). I never missed the double
semicolon, and many problems will ill-parenthesised matches just went into
oblivion. What I missed most is that you can't anymore step through the
toplevel with mouse cut-and-paste, because the inner let's are not accepted
at toplevel, you have to write the godamn "value" keyword instead.
Until recently there was no point in pushing the revised syntax, because it
was very hard to teach the language when the system's printer used another
syntax than what you typed in. Now that there is smooth integration of
camlp4 with ocaml with the 3.04, there is no excuse not to use the much
superior revised syntax, in my opinion. Which does not mean that there
should be a big concerted effort to switch all our code from one syntax to
the next.
I am currently developing a computational linguistics platform in ocaml, now
around 12000 loc in about 50 modules, mostly in revised syntax, some others
borrowed from other sources or mechanically produced and still in vanila
syntax, so what. The makefile does what is needed, and I never have any
problem reading other people's programs from libraries or the hump, etc.
The crucial point is that we need good tutorials, reference manuals, and
books
in the revised syntax before being serious about "standardizing" in something
else than the usual syntax. Once this material exists, then we can talk.
Let me tell you about an experience I did recently, in using Ocaml as a
publication language. In the first version of my paper, I said "we shall
use as algorithmic meta-language OCaml under so-called revised syntax".
Now one referee got very interested in the code, tried it on his machine
with ocaml 3.02, missed my remark about revised syntax, and said "it is too
bad these are not real programs which people can directly use". Another
referee got completely turned off by the code and said "remove all
proselytism about this weird Ocaml stuff, and use some pidgin algorithmic
language". Typical dilemna.
What I ended up was writing a short presentation of an algorithmic
meta-language which I called "Pidgin ML", without any proselytism about
existing programming languages; it is only in the evaluation part of the
paper that I reveal that Pidgin ML can be compiled and executed as such by
OCaml+Camlp4. So everybody is happy !
Just as a short plug for revised syntax, here is my introduction:
____________________________________
We shall use as {\sl meta language} for the description of our algorithms
a pidgin version of the functional language ML. Readers familiar with ML
may skip this section, which gives a crash overview of its syntax and
semantics.
The core language has types, values, and exceptions.
Thus, \verb:1: is a value of predefined type \verb:int:, whereas
\verb:"CL": is a \verb:string:.
Pairs of values inhabit the corresponding product type. Thus:
\verb|(1,"CL") : (nat * string)|.
Recursive type declarations create new types,
whose values are inductively built from the associated constructors.
Thus the Boolean type could be declared as a sum by:
\verb:type bool = [True | False];:\\
Parametric types give rise to polymorphism.
Thus if \verb:x: is of type \verb:t: and \verb:l: is of type
\verb:(list t):, we construct the list adding \verb:x: to \verb:l:
as \verb|[x :: l]|. The empty list is \verb:[]:, of (polymorphic) type
\verb:(list 'a):. Although the language is strongly typed, explicit type
specification is rarely needed from the designer, since principal types
may be inferred mechanically.
The language is functional in the sense that functions are first class
objects. Thus the doubling integer function may be written as
\verb:fun x -> x+x:, and it has type \verb:int -> int:. It may be associated
to the name \verb:double: by declaring: \verb:value double = fun x -> x+x;:\\
Equivalently we could write: \verb:value double x = x+x;:\\
Its application to value \verb:n: is written as \verb:(double n): or even
\verb:double n: when there is no ambiguity. Application associates to the
left, and thus \verb:f x y: stands for \verb:((f x) y):.
Recursive functional values are declared with the keyword \verb:rec:.
Thus we may define the factorial function as:\\
\verb:value rec fact n = n*(fact (n-1));:\\
Functions may be defined by pattern matching. Thus the first projection of
pairs could be defined by:\\
\verb:value fst = fun [ (x,y) -> x ];:\\
or equivalently (since there is only one pattern in this case) by:\\
\verb:value fst (x,y) = x;:\\
Pattern-matching is also usable in \verb:match: expressions which generalise
case analysis,
such as: \verb:match l with [ [] -> True | _ -> False ]:, which
tests whether list \verb:l: is empty, using underscore as catch-all
pattern.
Evaluation is strict, which means that \verb:x: is evaluated before
\verb:f: in the evaluation of \verb:(f x):. The \verb:let: expressions
permit to sequentialise computation, and to share sub-computations. Thus
\verb:let x = fact 10 in x+x: will compute \verb:fact 10: first,
and only once.
An equivalent postfix \verb:where: notation may be used as well. Thus
the conditional expression \verb:if b then e1 else e2: is equivalent to:
\verb:choose b where choose = fun [ True -> e1 | False -> e2]:.
Exceptions are declared with the type of their parameters, like in:
\verb:exception Failure of string;:
An exceptional value may be raised, like in:
\verb:raise (Failure "div 0"): and handled by a \verb:try: switching on
exception patterns, such as:
\verb:try expression with [ Failure s -> ... ]:.
Other imperative constructs may be used, such as
references, mutable arrays, while loops and I/O commands,
but we shall seldom need them. Sequences of instructions are
evaluated in left to right regime in \verb:do: expressions, such as:
\verb:do {e1; ... en}:.
ML is a {\sl modular} language, in the sense that sequences of type, value
and exception declarations may be packed in a structural unit called a
\verb:module:, amenable to separate treatment.
Modules have types themselves, called {\sl signatures}. Parametric
modules are called {\sl functors}. The algorithms presented in this paper
will use in essential ways
this modularity structure, but the syntax ought to be self-evident.
Readers uninterested in computational details may think of ML
definitions as recursive equations over inductively defined algebras. Most
of them are simple primitive recursive functionals.
___________________________________________________
At this point, note that Pidgin ML has no objects (not even records!) nor
labels.
And later on I spill the beans:
Pidgin ML definitions may actually be directly executed as Objective Caml
programs \cite{ocaml}, under the so-called revised syntax \cite{camlp4}.
>> By the way, is there any caml-mode for Emacs and the revised syntax ?
>
>I don't think so but there is a request for that (Gérard Huet asked
>me yesterday). I use a very old caml-light-or-what emacs mode and I
>am too lazy to look at emacs-lisp to create my mode.
This is an important point. I myself use a slighly hacked version of Tuareg
as an interim solution. I shall have a look at otags, which, being built
with camlp4 support, ought to be parametrizable (?).
I suggest this syntax problem should be seriously considered, but as a
long-term effort, encompassing development tools and documentation and
training material. This is not a battle that can be won by one round of
email flame.
Gerard
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
next reply other threads:[~2002-02-05 14:19 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-02-05 14:18 Gerard Huet [this message]
2002-02-05 14:49 ` Markus Mottl
2002-02-05 15:16 ` Jean-Francois Monin
2002-02-05 17:51 ` Diego olivier FERNANDEZ PONS
2002-02-05 21:47 [Caml-list] syntax Michael Vanier
2002-02-06 12:24 ` Daniel de Rauglaudre
2002-02-06 12:53 ` Achim Blumensath
[not found] <3C60E263.D2E35B3A@tsc.uc3m.es>
2002-02-06 10:08 ` [Caml-list] Syntax Diego olivier FERNANDEZ PONS
[not found] <200202061059.g16Ax2n25555@concorde.inria.fr>
2002-02-06 12:09 ` Diego olivier FERNANDEZ PONS
2002-10-11 11:34 [Caml-list] Threats on future of Camlp4 Kontra, Gergely
2002-10-11 16:36 ` [Caml-list] Syntax brogoff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200202051419.g15EJSH21401@concorde.inria.fr \
--to=gerard.huet@inria.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox