caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] semi-colons and begin
@ 2001-04-04 21:33 Chris Hecker
  2001-04-04 22:40 ` Gerd Stolpmann
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Chris Hecker @ 2001-04-04 21:33 UTC (permalink / raw)
  To: caml-list


I must admit I'm still slightly confused about the semicolon thing, even after writing a fair amount of caml code.  The current sticking point is that 

# if false then print_int 2; print_int 3;;
3- : unit = ()

# if false then print_int 2; print_int 3 else ();;
Characters 39-43:
Syntax error
# 

# if false then begin print_int 2; print_int 3 end;;
- : unit = ()
# if false then begin print_int 2; print_int 3 end else ();;
- : unit = ()

The "BNF" grammar implies the first and second tests should compile and print nothing, since expr = expr ; expr, although Xavier et al. have said the BNF in the docs isn't really accurate.

I also thought begin...end was needed in a pattern-match, but apparently expr ; expr works there?

Is there a chart or something that says exactly where they're allowed?  I'm afraid of introducing a subtle bug in my program.  In C I always use brackets with my if statements to avoid the similar problem, so I suppose I could use begin..end everywhere, but yuck!

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 21:33 [Caml-list] semi-colons and begin Chris Hecker
@ 2001-04-04 22:40 ` Gerd Stolpmann
  2001-04-04 23:32   ` Chris Hecker
  2001-04-05  3:35 ` Michael Hicks
  2001-04-05  3:53 ` Daniel de Rauglaudre
  2 siblings, 1 reply; 11+ messages in thread
From: Gerd Stolpmann @ 2001-04-04 22:40 UTC (permalink / raw)
  To: Chris Hecker, caml-list

On Wed, 04 Apr 2001, Chris Hecker wrote:
>I must admit I'm still slightly confused about the semicolon thing, even after writing a fair amount of caml code.  The current sticking point is that 
>
># if false then print_int 2; print_int 3;;
>3- : unit = ()
>
># if false then print_int 2; print_int 3 else ();;
>Characters 39-43:
>Syntax error
># 
>
># if false then begin print_int 2; print_int 3 end;;
>- : unit = ()
># if false then begin print_int 2; print_int 3 end else ();;
>- : unit = ()
>
>The "BNF" grammar implies the first and second tests should compile and print nothing,
>since expr = expr ; expr, although Xavier et al. have said the BNF in the docs
>isn't really accurate. 

The BNF is ambigous (as most grammars). So you can't say which is the right way
to set the parentheses:

(if false then print_int 2); print_int 3   -- OR
if false then (print_int 2; print_int 3)

Both ways are correct if you only look at the grammar. However, in the O'Caml
manual there is a precedence table (section 6.7) determining that "if" has
higher precedence than ";". So the first way is what the compiler interprets.

>
>also thought begin...end was needed in a
>pattern-match, but apparently expr ; expr works there? 

In the precedence table "match" has lower precedence than ";".

>Is there a chart or something that says exactly where they're allowed?  I'm
>afraid of introducing a subtle bug in my program.  In C I always use brackets
>with my if statements to avoid the similar problem, so I suppose I could use
>begin..end everywhere, but yuck! 

You can use begin ... end (or (...) which is the same) everywhere in
expressions. But I don't recommend it because it makes code unreadable which is
another cause for errors.

I personally have no problems with "if" because it is in the same style as in
the first structured programming language I've learned (Pascal).

If you have problems I would recommend to use Emacs as editor with one of the
two O'Caml modes. It automatically indents the code, so you see:

if false then print_int 2;
print_int 3

and _not_

if false then print_int 2;
  print_int 3

which would mean that you did it wrong.

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 22:40 ` Gerd Stolpmann
@ 2001-04-04 23:32   ` Chris Hecker
  2001-04-05  1:03     ` mahamud
  2001-04-06  9:33     ` Gerd Stolpmann
  0 siblings, 2 replies; 11+ messages in thread
From: Chris Hecker @ 2001-04-04 23:32 UTC (permalink / raw)
  To: gerd, caml-list


>However, in the O'Caml
>manual there is a precedence table (section 6.7) determining that "if" has
>higher precedence than ";". So the first way is what the compiler interprets.

Ah, duh!  Thank you.  Coming from C, it's hard to remember that everything is an expression, so of course the precedence table is the place to look.

> If you have problems I would recommend to use Emacs as editor with one of the
>two O'Caml modes. It automatically indents the code, so you see:

I do use Emacs and caml-mode (what is the difference between it an tuareg, anyway?), but this was all on one line.

Thanks!

Chris

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 23:32   ` Chris Hecker
@ 2001-04-05  1:03     ` mahamud
  2001-04-05  3:48       ` Brian Rogoff
  2001-04-05  6:05       ` David Brown
  2001-04-06  9:33     ` Gerd Stolpmann
  1 sibling, 2 replies; 11+ messages in thread
From: mahamud @ 2001-04-05  1:03 UTC (permalink / raw)
  To: caml-list

>>>>> ">" == Chris Hecker <checker@d6.com> writes:

    >> However, in the O'Caml manual there is a precedence table
    >> (section 6.7) determining that "if" has higher precedence than
    >> ";". So the first way is what the compiler interprets.

on an unrelated point, speaking of precedence rules,
is there a good reason why unary minus for floats (-.) has higher 
precedence than power of (**). i think the usual precedence is the
other way. atleast this is what i'm used to in languages that are
primarily numerically oriented like matlab. i've been caught by this 
quite a few times. 

another minor point regarding syntax : 
the use of "(* ... *)" prevents one from passing the multiplication
"*" operator as an argument (say to List.map2) without 
padding some space as in : ( * ) as opposed to (*).
it's a small point, but i don't think i've come across such unintended
interactions between comments and other constructs in other languages.

while i still have your attention, one last comment :
as you may have guessed i primarily use OCAML for writing code that
mostly do number crunching. i know you've all seen others writing similar
code beg for something like overloading. and i want to chime in too
for what its worth : why can't OCAML do what SML does ? 
overload the standard arithmetic operators to do both int and double
and assume a default type if the type can't be inferred.
this would i think avoid the majority of annoyances for people who use OCAML
for number crunching. I don't know if there is a deep issue involved 
that prevents OCAML from doing what SML does, but it seems inconsistent 
from a user's viewpoint that for eg. the comparison operators are allowed to
be overloaded but not the arithmetic operators. perhaps the comparison 
operators are easier to overload since the result returned always has the same
type ?

- shyjan


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 21:33 [Caml-list] semi-colons and begin Chris Hecker
  2001-04-04 22:40 ` Gerd Stolpmann
@ 2001-04-05  3:35 ` Michael Hicks
  2001-04-05  3:53 ` Daniel de Rauglaudre
  2 siblings, 0 replies; 11+ messages in thread
From: Michael Hicks @ 2001-04-05  3:35 UTC (permalink / raw)
  To: Chris Hecker; +Cc: caml-list

>In C I always use brackets with my if statements to avoid the similar
>problem, so I suppose I could use begin..end everywhere, but yuck! 

You can use parens instead of begin...end (I haven't checked the grammar,
but that's what I use all the time, anyway).

Mike

-- 
Michael Hicks
Ph.D. Candidate, the University of Pennsylvania
http://www.cis.upenn.edu/~mwh            mailto://mwh@dsl.cis.upenn.edu
"In essential things, unity; in doubtful things, liberty; in all things,
 charity." --Pope John XXIII, Ad Petri Cathedram, and popularly 
 attributed to St. Augustine

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-05  1:03     ` mahamud
@ 2001-04-05  3:48       ` Brian Rogoff
  2001-04-05  6:05       ` David Brown
  1 sibling, 0 replies; 11+ messages in thread
From: Brian Rogoff @ 2001-04-05  3:48 UTC (permalink / raw)
  To: mahamud; +Cc: caml-list

On Wed, 4 Apr 2001 mahamud@cs.cmu.edu wrote:
> another minor point regarding syntax : 
> the use of "(* ... *)" prevents one from passing the multiplication
> "*" operator as an argument (say to List.map2) without 
> padding some space as in : ( * ) as opposed to (*).
> it's a small point, but i don't think i've come across such unintended
> interactions between comments and other constructs in other languages.

Not comments, but C++ has a similar issue with template syntax last I
looked. That's no excuse, just an observation. 

> while i still have your attention, one last comment :
> as you may have guessed i primarily use OCAML for writing code that
> mostly do number crunching. i know you've all seen others writing similar
> code beg for something like overloading. and i want to chime in too
> for what its worth : why can't OCAML do what SML does ? 
> overload the standard arithmetic operators to do both int and double

No no no no no no! I'd run screaming if the good folks at INRIA botched
overloading as feebly as SML. I *hate* languages that provide some
overloading and then place bizarre restrictions on it, or where the
designers tell you that overloading is bad, except where they've decided
it's good, like Java. 

Phew, that said, it looks like the extensional polymorphism approach is
what will make it (maybe) into OCaml. No response yet on the question of
having to define all overloadings in the same place. 

As long as we're making random comments, it would be nice to see a bit
more discussion of syntax here (apart from the annual label war :). I
really liked ddr's approach to separating the cases of tupling in
Revised. I think that I'm leaning more to Gerard Huet's syntax preference
on imperative constructs though, which is to use parens around the
sequence, rather than the do .. return exp syntax, though I suppose in
the interests of looking like Haskell (and making imperative constructs
stand out a bit more) I could handle a do (e1; ...; en) or some brackets
that stand out more, like (< >) for example

-- Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 21:33 [Caml-list] semi-colons and begin Chris Hecker
  2001-04-04 22:40 ` Gerd Stolpmann
  2001-04-05  3:35 ` Michael Hicks
@ 2001-04-05  3:53 ` Daniel de Rauglaudre
  2001-04-05  6:23   ` Mattias Waldau
  2 siblings, 1 reply; 11+ messages in thread
From: Daniel de Rauglaudre @ 2001-04-05  3:53 UTC (permalink / raw)
  To: caml-list

Hi,

On Wed, Apr 04, 2001 at 02:33:03PM -0700, Chris Hecker wrote:

> I must admit I'm still slightly confused about the semicolon thing,
> even after writing a fair amount of caml code.  The current sticking
> point is that
> 
> # if false then print_int 2; print_int 3;;
> 3- : unit = ()

Do you want me to confuse you more? Try this:

# if false then let _ = () in print_int 2; print_int 3;;

> # if false then print_int 2; print_int 3 else ();;
> Characters 39-43:
> Syntax error

And this one:
# if false then let _ = () in print_int 2; print_int 3 else print_int 4;;

And this situation:
   if blahblah then
      thing1;
   thing2;
   thing3;
I decide to add a let..in for the thing1.

What you see:
   if blahblah then
      let x = e in
      thing1;
   thing2;
   thing3;
What you get:
   if blahblah then
      let x = e in
      thing1;
      thing2;
      thing3;

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-05  1:03     ` mahamud
  2001-04-05  3:48       ` Brian Rogoff
@ 2001-04-05  6:05       ` David Brown
  1 sibling, 0 replies; 11+ messages in thread
From: David Brown @ 2001-04-05  6:05 UTC (permalink / raw)
  Cc: caml-list

On Wed, Apr 04, 2001 at 09:03:42PM -0400, mahamud@cs.cmu.edu wrote:

> another minor point regarding syntax : 
> the use of "(* ... *)" prevents one from passing the multiplication
> "*" operator as an argument (say to List.map2) without 
> padding some space as in : ( * ) as opposed to (*).
> it's a small point, but i don't think i've come across such unintended
> interactions between comments and other constructs in other languages.

C does it to with division and pointers.

        foo = bar/*ptr;

Dave Brown
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [Caml-list] semi-colons and begin
  2001-04-05  3:53 ` Daniel de Rauglaudre
@ 2001-04-05  6:23   ` Mattias Waldau
  2001-04-05 17:34     ` Pierre Weis
  0 siblings, 1 reply; 11+ messages in thread
From: Mattias Waldau @ 2001-04-05  6:23 UTC (permalink / raw)
  To: caml-list

There are a lot of problems with the Caml-syntax and I can live with the
most
ones. I am really glad that Emacs indents correctly, and that makes the
following

What you see:
   if blahblah then
      let x = e in
      thing1;
   thing2;
   thing3;
What you get:
   if blahblah then
      let x = e in
      thing1;
      thing2;
      thing3;

code correctly, since Emacs will indent it the second way. In Python
indentation
means something. It would be nice if the ocaml-compiler would complain if
indentation is inconsistent. In the example above thing2 and thing3 are
inconsistent intended since it should be on the same level as thing1.

Also, regarding if-then, I have learned to use begin/end.

My biggest problem is when the syntax error occurs in a completely different
location in the file, because it makes finding the bugs very difficult.
(I understand why the typechecker sometimes complains at the source,
sometimes
and the destination). Whould it be possible to pinpoint the error more
exactly
or is there a reason that in

let _ = 1+2;

let _ = 2

let _ = 4

we get a syntax error at the last 'let'?

/mattias

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-05  6:23   ` Mattias Waldau
@ 2001-04-05 17:34     ` Pierre Weis
  0 siblings, 0 replies; 11+ messages in thread
From: Pierre Weis @ 2001-04-05 17:34 UTC (permalink / raw)
  To: Mattias Waldau; +Cc: caml-list

> There are a lot of problems with the Caml-syntax and I can live with the
> most
> ones. I am really glad that Emacs indents correctly, and that makes the
> following
[...]
> My biggest problem is when the syntax error occurs in a completely different
> location in the file, because it makes finding the bugs very difficult.
> (I understand why the typechecker sometimes complains at the source,
> sometimes
> and the destination). Whould it be possible to pinpoint the error more
> exactly
> or is there a reason that in
> 
> let _ = 1+2;
> 
> let _ = 2
> 
> let _ = 4
> 
> we get a syntax error at the last 'let'?
> 
> /mattias
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr

The reason is that your set of phrases

let _ = 1+2;

let _ = 2

let _ = 4

is understood as a single definition of a sequence expression as:

let _ =
  1 + 2;
  let _ = 2
  let _ = 4

hence the second ``let'' is is a local definition that needs an in
part that is lacking. Hence, the syntax error on the third let (the
parser is waiting for an ``in'' keyword, and finding ``let'' triggers
the syntax error).

The problem here is that there is no end-of-phrase marks that would
prevent the parser from shifting for ever. Hence, using explicit ends
of phrase here would be less error prone, although arguably a bit more
heavy since you would have to write:

1+2;;

2;;

4;;

On the other hand, you get rid of the ``let _ =''

Objective Caml supports those two styles, so you can choose the style
you prefer.

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] semi-colons and begin
  2001-04-04 23:32   ` Chris Hecker
  2001-04-05  1:03     ` mahamud
@ 2001-04-06  9:33     ` Gerd Stolpmann
  1 sibling, 0 replies; 11+ messages in thread
From: Gerd Stolpmann @ 2001-04-06  9:33 UTC (permalink / raw)
  To: Chris Hecker, gerd, caml-list

On Thu, 05 Apr 2001, Chris Hecker wrote:
>>However, in the O'Caml
>>manual there is a precedence table (section 6.7) determining that "if" has
>>higher precedence than ";". So the first way is what the compiler interprets.
>
>Ah, duh!  Thank you.  Coming from C, it's hard to remember that everything is an expression, so of course the precedence table is the place to look.
>
>> If you have problems I would recommend to use Emacs as editor with one of the
>>two O'Caml modes. It automatically indents the code, so you see:
>
>I do use Emacs and caml-mode (what is the difference between it an tuareg,
>anyway?), but this was all on one line.

I'm a tuareg user, and this mode indents as I said. (Tuareg seems to be more
correct, but is also slower than caml-mode.)

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2001-04-08 20:12 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-04 21:33 [Caml-list] semi-colons and begin Chris Hecker
2001-04-04 22:40 ` Gerd Stolpmann
2001-04-04 23:32   ` Chris Hecker
2001-04-05  1:03     ` mahamud
2001-04-05  3:48       ` Brian Rogoff
2001-04-05  6:05       ` David Brown
2001-04-06  9:33     ` Gerd Stolpmann
2001-04-05  3:35 ` Michael Hicks
2001-04-05  3:53 ` Daniel de Rauglaudre
2001-04-05  6:23   ` Mattias Waldau
2001-04-05 17:34     ` Pierre Weis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).