caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Strange syntax error
@ 2005-01-23 21:11 Richard Jones
  2005-01-23 21:44 ` [Caml-list] " Nicolas Cannasse
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Richard Jones @ 2005-01-23 21:11 UTC (permalink / raw)
  To: caml-list

The following compiles fine:

  type t = { keyword : string; cost : float option; }
  
  let f =
    function
      | { keyword = keyword } when keyword.[0] = '-' -> keyword
      | { keyword = keyword; cost = None } -> keyword
      | _ -> failwith "whatever"

But this, which surely is equivalent, fails with a syntax error at the
second '|' character:

  type t = { keyword : string; cost : float option; }
  
  let f =
    function
      | { keyword = keyword } when keyword.[0] = '-' (* -> keyword *)
      | { keyword = keyword; cost = None } -> keyword
      | _ -> failwith "whatever"

OCaml 3.08.1.

Is this a parsing bug?

Rich.

-- 


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

* Re: [Caml-list] Strange syntax error
  2005-01-23 21:11 Strange syntax error Richard Jones
@ 2005-01-23 21:44 ` Nicolas Cannasse
  2005-01-23 21:49 ` Jon Harrop
  2005-01-23 22:23 ` Kurt Welgehausen
  2 siblings, 0 replies; 5+ messages in thread
From: Nicolas Cannasse @ 2005-01-23 21:44 UTC (permalink / raw)
  To: caml-list, Richard Jones

> The following compiles fine:
>
>   type t = { keyword : string; cost : float option; }
>
>   let f =
>     function
>       | { keyword = keyword } when keyword.[0] = '-' -> keyword
>       | { keyword = keyword; cost = None } -> keyword
>       | _ -> failwith "whatever"
>
> But this, which surely is equivalent, fails with a syntax error at the
> second '|' character:
>
>   type t = { keyword : string; cost : float option; }
>
>   let f =
>     function
>       | { keyword = keyword } when keyword.[0] = '-' (* -> keyword *)
>       | { keyword = keyword; cost = None } -> keyword
>       | _ -> failwith "whatever"
>
> OCaml 3.08.1.
>
> Is this a parsing bug?
>
> Rich.

I guess you can't have a "when" clause between two patterns, that sounds
logical since it applies to all patterns in the same "group".

Nicolas Cannasse


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

* Re: [Caml-list] Strange syntax error
  2005-01-23 21:11 Strange syntax error Richard Jones
  2005-01-23 21:44 ` [Caml-list] " Nicolas Cannasse
@ 2005-01-23 21:49 ` Jon Harrop
  2005-01-23 22:23 ` Kurt Welgehausen
  2 siblings, 0 replies; 5+ messages in thread
From: Jon Harrop @ 2005-01-23 21:49 UTC (permalink / raw)
  To: caml-list

On Sunday 23 January 2005 21:11, Richard Jones wrote:
> ...
> Is this a parsing bug?

I assume a "when" clause is only allowed at the end of the list of alternative 
patterns. For example:

let f = function
  { keyword = keyword } -> keyword
| { keyword = keyword; cost = None } when keyword.[0] = '-' -> keyword
| _ -> failwith "whatever"

is allowed (but not what you wanted).

I guess that allowing "when" clauses on each alternative pattern would obviate 
the advantages of using a decision tree to represent pattern matching and, 
therefore, would make pattern matches significantly more inefficient. Hence 
they don't do it.

In this case, I'd go for:

let f = function
  { keyword = keyword } when keyword.[0] = '-' -> keyword
| { cost = None } as t -> t.keyword
| _ -> failwith "whatever"

Say, this wouldn't be related to that arg-passing problem would it? ;-)

Cheers,
Jon.


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

* Re: [Caml-list] Strange syntax error
  2005-01-23 21:11 Strange syntax error Richard Jones
  2005-01-23 21:44 ` [Caml-list] " Nicolas Cannasse
  2005-01-23 21:49 ` Jon Harrop
@ 2005-01-23 22:23 ` Kurt Welgehausen
  2005-01-23 23:12   ` Richard Jones
  2 siblings, 1 reply; 5+ messages in thread
From: Kurt Welgehausen @ 2005-01-23 22:23 UTC (permalink / raw)
  To: caml-list

> Is this a parsing bug?

See section 6.7 of the manual, 'pattern-matching ::=  ...'.

<when> is not mentioned in the definition of <pattern>
(section 6.6); it's mentioned only in the definition
of <expr> (section 6.7). Since an or-pattern is a
<pattern>, it appears that it can have only one <when>
clause.

Regards


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

* Re: [Caml-list] Strange syntax error
  2005-01-23 22:23 ` Kurt Welgehausen
@ 2005-01-23 23:12   ` Richard Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2005-01-23 23:12 UTC (permalink / raw)
  Cc: caml-list

On Sun, Jan 23, 2005 at 04:23:16PM -0600, Kurt Welgehausen wrote:
> > Is this a parsing bug?
> 
> See section 6.7 of the manual, 'pattern-matching ::=  ...'.
> 
> <when> is not mentioned in the definition of <pattern>
> (section 6.6); it's mentioned only in the definition
> of <expr> (section 6.7). Since an or-pattern is a
> <pattern>, it appears that it can have only one <when>
> clause.

Thanks to all who replied.  Yes, it was my misunderstanding of the
manual.

Rich.

-- 


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

end of thread, other threads:[~2005-01-23 23:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-23 21:11 Strange syntax error Richard Jones
2005-01-23 21:44 ` [Caml-list] " Nicolas Cannasse
2005-01-23 21:49 ` Jon Harrop
2005-01-23 22:23 ` Kurt Welgehausen
2005-01-23 23:12   ` Richard Jones

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