caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] a question about syntax
@ 2018-02-14  3:31 Tim Leonard
  2018-02-14  3:41 ` Kenneth Adam Miller
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Tim Leonard @ 2018-02-14  3:31 UTC (permalink / raw)
  To: caml-list

A simple question of syntax: why does the first definition of function f cause a syntax error?
Shouldn’t the semicolon syntactically terminate the match expression?

type my_record = { field1 : bool; field2 : int };;

let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (* this fails *)

let f x = { field1 = ( match x with _ -> true ); field2 = 2 };; (* this is ok *)


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

* Re: [Caml-list] a question about syntax
  2018-02-14  3:31 [Caml-list] a question about syntax Tim Leonard
@ 2018-02-14  3:41 ` Kenneth Adam Miller
  2018-02-14  4:12 ` Yawar Amin
  2018-02-14 18:50 ` Oliver Bandel
  2 siblings, 0 replies; 9+ messages in thread
From: Kenneth Adam Miller @ 2018-02-14  3:41 UTC (permalink / raw)
  To: Tim Leonard; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 1646 bytes --]

No, it's keywords "in" that assigns the terminus to the current phrase so
to speak.

The first would look like this, if I understand you correctly:

let f x = { field1 = match x with _ -> true | field2 = 2} in

But there are several problems with the above statement. First, you used a
semicolon in place of a |, and a | expresses to the match statement what
the various cases are. As in,

match x with | case1 ... | case 2 | ...

Second, alternatively to that interpretation, you might want it to be that
you match within the assignment like this:

let f x = { field1 = (match x with ... ); field2 = ... } in

A problem is that you used match x with _ -> true | field2, but the _ is
the catchall keyword, so field2 is never going to get hit. What you've
expressed in the second let statement is equivalent to this:

let f1 = match x with _ -> true in
let f x = { field1 = f1; field2.... }

On Tue, Feb 13, 2018 at 10:31 PM, Tim Leonard <Tim@timleonard.us> wrote:

> A simple question of syntax: why does the first definition of function f
> cause a syntax error?
> Shouldn’t the semicolon syntactically terminate the match expression?
>
> type my_record = { field1 : bool; field2 : int };;
>
> let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (* this
> fails *)
>
> let f x = { field1 = ( match x with _ -> true ); field2 = 2 };; (* this is
> ok *)
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 2412 bytes --]

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

* Re: [Caml-list] a question about syntax
  2018-02-14  3:31 [Caml-list] a question about syntax Tim Leonard
  2018-02-14  3:41 ` Kenneth Adam Miller
@ 2018-02-14  4:12 ` Yawar Amin
  2018-02-14  4:32   ` Tim Leonard
  2018-02-14 18:50 ` Oliver Bandel
  2 siblings, 1 reply; 9+ messages in thread
From: Yawar Amin @ 2018-02-14  4:12 UTC (permalink / raw)
  To: Tim Leonard; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 809 bytes --]

Hi,

On Tue, Feb 13, 2018 at 10:31 PM, Tim Leonard <Tim@timleonard.us> wrote:

> [...]
> type my_record = { field1 : bool; field2 : int };;
>
> let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (* this
> fails *)
>

This fails because OCaml overloads ';' in a few different ways,
significantly: (1) as a field separator in a record, and (2) as a
sequencing operator between two expressions. In case of ambiguity OCaml
parses as the latter, but you really meant the former.

(2) 'sequencing operator' means that something like:

let x = print_endline "Hi!"; 1

... will evaluate whatever is on the left of the ';', throw away its value
(in this case 'unit'), then evaluate whatever is on the right of the ';'
and return *its* value as the final value of the compound expression.

Regards,

Yawar

[-- Attachment #2: Type: text/html, Size: 1346 bytes --]

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

* Re: [Caml-list] a question about syntax
  2018-02-14  4:12 ` Yawar Amin
@ 2018-02-14  4:32   ` Tim Leonard
  0 siblings, 0 replies; 9+ messages in thread
From: Tim Leonard @ 2018-02-14  4:32 UTC (permalink / raw)
  To: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1155 bytes --]

Thank you, Yawar. I’m familiar with using semicolon for sequencing, but it hadn’t occurred to me that it might be interpreted that way here, and that explains the failure perfectly.

Tim

> On Feb 13, 2018, at 11:12 PM, Yawar Amin <yawar.amin@gmail.com> wrote:
> 
> Hi,
> 
> On Tue, Feb 13, 2018 at 10:31 PM, Tim Leonard <Tim@timleonard.us <mailto:Tim@timleonard.us>> wrote:
> [...]
> type my_record = { field1 : bool; field2 : int };;
> 
> let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (* this fails *)
> 
> This fails because OCaml overloads ';' in a few different ways, significantly: (1) as a field separator in a record, and (2) as a sequencing operator between two expressions. In case of ambiguity OCaml parses as the latter, but you really meant the former.
> 
> (2) 'sequencing operator' means that something like:
> 
> let x = print_endline "Hi!"; 1
> 
> ... will evaluate whatever is on the left of the ';', throw away its value (in this case 'unit'), then evaluate whatever is on the right of the ';' and return its value as the final value of the compound expression.
> 
> Regards,
> 
> Yawar


[-- Attachment #2: Type: text/html, Size: 2341 bytes --]

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

* Re: [Caml-list] a question about syntax
  2018-02-14  3:31 [Caml-list] a question about syntax Tim Leonard
  2018-02-14  3:41 ` Kenneth Adam Miller
  2018-02-14  4:12 ` Yawar Amin
@ 2018-02-14 18:50 ` Oliver Bandel
  2018-02-14 23:02   ` Chet Murthy
  2 siblings, 1 reply; 9+ messages in thread
From: Oliver Bandel @ 2018-02-14 18:50 UTC (permalink / raw)
  To: caml-list


Zitat von Tim Leonard <Tim@timleonard.us> (Tue, 13 Feb 2018 22:31:14 -0500)

> A simple question of syntax: why does the first definition of  
> function f cause a syntax error?
> Shouldn’t the semicolon syntactically terminate the match expression?

No.
You can use semicolon to put more then one expression together.
So the "field2 = 2" is seen as part of the match.

If you have a pattern matching, this way you can put multiple  
commands/expressions in a row,
without the need to use begin/end or ( ) in any match-case.
It's the other way around: you need to put begin/end or ( ) around a  
match-statement.

This way you have to add one such enclosing around a match-statement,
instead of one such enclosing in any match-case of such a statement.


>
> type my_record = { field1 : bool; field2 : int };;
>
> let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (*  
> this fails *)

Here I get "Error: Unbound value field2",
which is, because the match-case reaches until the }.



>
> let f x = { field1 = ( match x with _ -> true ); field2 = 2 };; (*  
> this is ok *)

Here it works, because the match-statement is sorrounded / enclosed by  
( and ).

Ciao,
   Oliver


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

* Re: [Caml-list] a question about syntax
  2018-02-14 18:50 ` Oliver Bandel
@ 2018-02-14 23:02   ` Chet Murthy
  2018-02-14 23:40     ` Ian Zimmerman
  0 siblings, 1 reply; 9+ messages in thread
From: Chet Murthy @ 2018-02-14 23:02 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1841 bytes --]

I remember back in the day Pierre Weis explaining to me that this syntactic
trade-off was made in order to allow that
"let" and "match" didn't have ending key-words (e.g. "end").  Unlike in
SML/NJ.

--chet--


On Wed, Feb 14, 2018 at 10:50 AM, Oliver Bandel <oliver@first.in-berlin.de>
wrote:

>
> Zitat von Tim Leonard <Tim@timleonard.us> (Tue, 13 Feb 2018 22:31:14
> -0500)
>
> A simple question of syntax: why does the first definition of function f
>> cause a syntax error?
>> Shouldn’t the semicolon syntactically terminate the match expression?
>>
>
> No.
> You can use semicolon to put more then one expression together.
> So the "field2 = 2" is seen as part of the match.
>
> If you have a pattern matching, this way you can put multiple
> commands/expressions in a row,
> without the need to use begin/end or ( ) in any match-case.
> It's the other way around: you need to put begin/end or ( ) around a
> match-statement.
>
> This way you have to add one such enclosing around a match-statement,
> instead of one such enclosing in any match-case of such a statement.
>
>
>
>> type my_record = { field1 : bool; field2 : int };;
>>
>> let f x = { field1 =   match x with _ -> true  ; field2 = 2 };; (* this
>> fails *)
>>
>
> Here I get "Error: Unbound value field2",
> which is, because the match-case reaches until the }.
>
>
>
>
>> let f x = { field1 = ( match x with _ -> true ); field2 = 2 };; (* this
>> is ok *)
>>
>
> Here it works, because the match-statement is sorrounded / enclosed by (
> and ).
>
> Ciao,
>   Oliver
>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 3120 bytes --]

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

* Re: [Caml-list] a question about syntax
  2018-02-14 23:02   ` Chet Murthy
@ 2018-02-14 23:40     ` Ian Zimmerman
  2018-02-15  0:17       ` Evgeny Roubinchtein
  2018-02-15  1:17       ` Chet Murthy
  0 siblings, 2 replies; 9+ messages in thread
From: Ian Zimmerman @ 2018-02-14 23:40 UTC (permalink / raw)
  To: caml-list

On 2018-02-14 15:02, Chet Murthy wrote:

> I remember back in the day Pierre Weis explaining to me that this
> syntactic trade-off was made in order to allow that "let" and "match"
> didn't have ending key-words (e.g. "end"). 

I would understand and accept that choice.  But the larger question is,
why was the semicolon overloaded like this?  In SML the semicolon serves
just one purpose: separating consecutive imperative statements (well it
can also separate declarations but that is optional).  Why has CAML
chosen to use the semicolon in list and record patterns and values,
instead of the comma like SML?

> Unlike in SML/NJ.

SML has multiple implementations (as implied in the S).

-- 
Please don't Cc: me privately on mailing lists and Usenet,
if you also post the followup to the list or newsgroup.
To reply privately _only_ on Usenet and on broken lists
which rewrite From, fetch the TXT record for no-use.mooo.com.

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

* Re: [Caml-list] a question about syntax
  2018-02-14 23:40     ` Ian Zimmerman
@ 2018-02-15  0:17       ` Evgeny Roubinchtein
  2018-02-15  1:17       ` Chet Murthy
  1 sibling, 0 replies; 9+ messages in thread
From: Evgeny Roubinchtein @ 2018-02-15  0:17 UTC (permalink / raw)
  To: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1530 bytes --]

> Why has CAML chosen to use the semicolon in list and record patterns and
values, instead of the comma like SML?

Because it was designed in France, where, unlike in the US, the standard
list separator is a semicolon? ;-)

-- 
Best,
Evgeny ("Zhenya"2

On Wed, Feb 14, 2018 at 3:40 PM, Ian Zimmerman <itz@very.loosely.org> wrote:

> On 2018-02-14 15:02, Chet Murthy wrote:
>
> > I remember back in the day Pierre Weis explaining to me that this
> > syntactic trade-off was made in order to allow that "let" and "match"
> > didn't have ending key-words (e.g. "end").
>
> I would understand and accept that choice.  But the larger question is,
> why was the semicolon overloaded like this?  In SML the semicolon serves
> just one purpose: separating consecutive imperative statements (well it
> can also separate declarations but that is optional).  Why has CAML
> chosen to use the semicolon in list and record patterns and values,
> instead of the comma like SML?
>
> > Unlike in SML/NJ.
>
> SML has multiple implementations (as implied in the S).
>
> --
> Please don't Cc: me privately on mailing lists and Usenet,
> if you also post the followup to the list or newsgroup.
> To reply privately _only_ on Usenet and on broken lists
> which rewrite From, fetch the TXT record for no-use.mooo.com.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2560 bytes --]

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

* Re: [Caml-list] a question about syntax
  2018-02-14 23:40     ` Ian Zimmerman
  2018-02-15  0:17       ` Evgeny Roubinchtein
@ 2018-02-15  1:17       ` Chet Murthy
  1 sibling, 0 replies; 9+ messages in thread
From: Chet Murthy @ 2018-02-15  1:17 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1203 bytes --]

i don't remember the answer to your question, but I suspect it is this:

# 1,2 ;;
- : int * int = (1, 2)

That is, again, syntactic succinctness.  tuples need not always be
parenthesized.  But I'm really just making this up -- I certainly don't
remember well enough to be definitive on this.

It does seem to follow the general pattern, though: with generativity of
record-field-names as another example.

On Wed, Feb 14, 2018 at 3:40 PM, Ian Zimmerman <itz@very.loosely.org> wrote:

> On 2018-02-14 15:02, Chet Murthy wrote:
>
> > I remember back in the day Pierre Weis explaining to me that this
> > syntactic trade-off was made in order to allow that "let" and "match"
> > didn't have ending key-words (e.g. "end").
>
> I would understand and accept that choice.  But the larger question is,
> why was the semicolon overloaded like this?  In SML the semicolon serves
> just one purpose: separating consecutive imperative statements (well it
> can also separate declarations but that is optional).  Why has CAML
> chosen to use the semicolon in list and record patterns and values,
> instead of the comma like SML?
>
> > Unlike in SML/NJ.
>
> SML has multiple implementations (as implied in the S).
>

[-- Attachment #2: Type: text/html, Size: 1780 bytes --]

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

end of thread, other threads:[~2018-02-15  1:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-14  3:31 [Caml-list] a question about syntax Tim Leonard
2018-02-14  3:41 ` Kenneth Adam Miller
2018-02-14  4:12 ` Yawar Amin
2018-02-14  4:32   ` Tim Leonard
2018-02-14 18:50 ` Oliver Bandel
2018-02-14 23:02   ` Chet Murthy
2018-02-14 23:40     ` Ian Zimmerman
2018-02-15  0:17       ` Evgeny Roubinchtein
2018-02-15  1:17       ` Chet Murthy

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).