Version française
Home     About     Download     Resources     Contact us    
Browse thread
Two camlp4 questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] Two camlp4 questions
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