From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Two camlp4 questions
Date: Fri, 25 Apr 2008 15:58:17 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.64.0804251521520.9402@martin.ec.wink.com> (raw)
In-Reply-To: <20080425131812.GB4100@annexia.org>
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
next prev parent reply other threads:[~2008-04-25 14:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-04-25 13:18 Richard Jones
2008-04-25 13:58 ` Martin Jambon [this message]
2008-04-25 14:54 ` [Caml-list] " Richard Jones
2008-04-26 12:54 ` Nicolas Pouillard
2008-04-26 16:47 ` Richard Jones
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=Pine.LNX.4.64.0804251521520.9402@martin.ec.wink.com \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@inria.fr \
--cc=rich@annexia.org \
/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