caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Two camlp4 questions
@ 2008-04-25 13:18 Richard Jones
  2008-04-25 13:58 ` [Caml-list] " Martin Jambon
  2008-04-26 12:54 ` Nicolas Pouillard
  0 siblings, 2 replies; 5+ messages in thread
From: Richard Jones @ 2008-04-25 13:18 UTC (permalink / raw)
  To: caml-list

(1) How do I match on the pattern which is literally '_' in the
original code?

    match mypatt with
    | <:patt< _ >> -> ...

seems like it matches any pattern.

(2) Is there a function hiding anywhere which tests whether a pattern
is exhaustive?  Here's the problem I have: I want to generate code
like this:

    <:expr< match $someexpr$ with $mypatt$ -> $code$ | _ -> () >>

However this gives a compile-time warning if mypatt is already
exhaustive because the second case could never be matched.  If mypatt
is already exhaustive then I'd want to generate this code instead to
avoid the warning:

    <:expr< match $someexpr$ with $mypatt$ -> $code$ >>

I hacked around it a little with this function:

  let pattern_is_exhaustive = function
  | <:patt< $lid:_$ >> -> true
  | _ -> false

but I guess you can see that this function is not a complete solution.

Rich.

-- 
Richard Jones
Red Hat


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

* Re: [Caml-list] Two camlp4 questions
  2008-04-25 13:18 Two camlp4 questions Richard Jones
@ 2008-04-25 13:58 ` Martin Jambon
  2008-04-25 14:54   ` Richard Jones
  2008-04-26 12:54 ` Nicolas Pouillard
  1 sibling, 1 reply; 5+ messages in thread
From: Martin Jambon @ 2008-04-25 13:58 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

On Fri, 25 Apr 2008, Richard Jones wrote:

> (1) How do I match on the pattern which is literally '_' in the
> original code?
>
>    match mypatt with
>    | <:patt< _ >> -> ...
>
> seems like it matches any pattern.

Strange.


> (2) Is there a function hiding anywhere which tests whether a pattern
> is exhaustive?

It's something that can't be done with camlp4.
If in some module M, some type t is defined by "type t = A | B | C"
then knowing that "M.A -> 1 | M.B -> 2" is incomplete requires access to 
more information than camlp4 has.


>  Here's the problem I have: I want to generate code
> like this:
>
>    <:expr< match $someexpr$ with $mypatt$ -> $code$ | _ -> () >>
>
> However this gives a compile-time warning if mypatt is already
> exhaustive because the second case could never be matched.  If mypatt
> is already exhaustive then I'd want to generate this code instead to
> avoid the warning:
>
>    <:expr< match $someexpr$ with $mypatt$ -> $code$ >>

One possible hack is to add "when true" guards everywhere and one final 
"| _ -> assert false" case.
This disables completeness checking and turns off warnings, at your own 
risk.

Note: "assert false" is not ideal, since you want to raise Match_failure 
with the proper location. <:expr< match () with [] >> should do it 
(assuming quotations in the revised syntax)


> I hacked around it a little with this function:
>
>  let pattern_is_exhaustive = function
>  | <:patt< $lid:_$ >> -> true
>  | _ -> false
>
> but I guess you can see that this function is not a complete solution.
>
> Rich.
>
> -- 
> Richard Jones
> Red Hat
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

--
http://wink.com/profile/mjambon
http://mjambon.com


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

* Re: [Caml-list] Two camlp4 questions
  2008-04-25 13:58 ` [Caml-list] " Martin Jambon
@ 2008-04-25 14:54   ` Richard Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2008-04-25 14:54 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

On Fri, Apr 25, 2008 at 03:58:17PM +0200, Martin Jambon wrote:
> One possible hack is to add "when true" guards everywhere and one final 
> "| _ -> assert false" case.
> This disables completeness checking and turns off warnings, at your own 
> risk.

Nice, thanks.  In this case I don't want the exhaustiveness checks at
all so this works out fine for me.

Rich.

-- 
Richard Jones
Red Hat


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

* Re: [Caml-list] Two camlp4 questions
  2008-04-25 13:18 Two camlp4 questions Richard Jones
  2008-04-25 13:58 ` [Caml-list] " Martin Jambon
@ 2008-04-26 12:54 ` Nicolas Pouillard
  2008-04-26 16:47   ` Richard Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Pouillard @ 2008-04-26 12:54 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

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

Excerpts from Richard Jones's message of Fri Apr 25 15:18:12 +0200 2008:
> (1) How do I match on the pattern which is literally '_' in the
> original code?
> 
>     match mypatt with
>     | <:patt< _ >> -> ...
> 
> seems like it matches any pattern.

I don't think so, it's supposed to match only the wildcard pattern.

Proof:
  $ camlp4oof -str 'match x with <:patt< _ >> -> y'
  match x with | Ast.PaAny _ -> y

-- 
Nicolas Pouillard aka Ertai

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 194 bytes --]

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

* Re: [Caml-list] Two camlp4 questions
  2008-04-26 12:54 ` Nicolas Pouillard
@ 2008-04-26 16:47   ` Richard Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2008-04-26 16:47 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: caml-list

On Sat, Apr 26, 2008 at 02:54:12PM +0200, Nicolas Pouillard wrote:
> Excerpts from Richard Jones's message of Fri Apr 25 15:18:12 +0200 2008:
> > (1) How do I match on the pattern which is literally '_' in the
> > original code?
> > 
> >     match mypatt with
> >     | <:patt< _ >> -> ...
> > 
> > seems like it matches any pattern.
> 
> I don't think so, it's supposed to match only the wildcard pattern.
> 
> Proof:
>   $ camlp4oof -str 'match x with <:patt< _ >> -> y'
>   match x with | Ast.PaAny _ -> y

OK thanks - I was obviously confused.

Rich.

-- 
Richard Jones
Red Hat


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

end of thread, other threads:[~2008-04-26 16:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-25 13:18 Two camlp4 questions Richard Jones
2008-04-25 13:58 ` [Caml-list] " Martin Jambon
2008-04-25 14:54   ` Richard Jones
2008-04-26 12:54 ` Nicolas Pouillard
2008-04-26 16:47   ` 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).