caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* MicMatch
@ 2007-12-01 16:00 Jon Harrop
  2007-12-02 22:12 ` [Caml-list] MicMatch Martin Jambon
  0 siblings, 1 reply; 2+ messages in thread
From: Jon Harrop @ 2007-12-01 16:00 UTC (permalink / raw)
  To: caml-list


I just stumbled upon this Wiki page discussing MicMatch:

  http://ocaml.pbwiki.com/Micmatch

and noted that the implementation of views disables exhaustiveness checking. I 
think it is worth keeping the static checking of view patterns.

So MicMatch uses definitions like:

  type 'a lazy_list = Empty | Cons of 'a * 'a lazy_list lazy_t

  let view Empty = fun l -> Lazy.force l = Empty
  let view Cons = fun l -> match Lazy.force l with Cons x -> Some x

  match ll with
      %Empty -> ...
    | %Cons (x, %Empty) -> ...
    | %Cons (x1, %Cons (x2, %Empty)) -> ...
    | _ -> ...

where F# would use:

  let (|PEmpty|PCons|) l =
    match Lazy.force l with
    | Empty -> PEmpty
    | Cons(h, t) -> PCons(h, t)

This basically defines a new sum type and every time a view pattern is 
encountered, it is converted using this function into the new sum type and 
then matched over. This means you cannot mix view and non-view patterns in 
the same match but you do get to keep exhaustiveness checking.

Having said all of that, the only application of F#-style views in OCaml that 
I can think of is simply matching lazy values, which could be implemented 
more easily and with no syntactic overhead.

There are other applications that MicMatch might not cater for. Specifically, 
factoring patterns and parameterizing patterns over values. For example, you 
might want an active pattern than handles commutativity:

  Commute(p1, p2)  =>  p1, p2 | p2, p1

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


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

* Re: [Caml-list] MicMatch
  2007-12-01 16:00 MicMatch Jon Harrop
@ 2007-12-02 22:12 ` Martin Jambon
  0 siblings, 0 replies; 2+ messages in thread
From: Martin Jambon @ 2007-12-02 22:12 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Sat, 1 Dec 2007, Jon Harrop wrote:

>
> I just stumbled upon this Wiki page discussing MicMatch:
>
>  http://ocaml.pbwiki.com/Micmatch
>
> and noted that the implementation of views disables exhaustiveness checking. I
> think it is worth keeping the static checking of view patterns.
>
> So MicMatch uses definitions like:
>
>  type 'a lazy_list = Empty | Cons of 'a * 'a lazy_list lazy_t
>
>  let view Empty = fun l -> Lazy.force l = Empty
>  let view Cons = fun l -> match Lazy.force l with Cons x -> Some x

The 2 lines above can be written in standard OCaml as follows:

let view_Empty = fun l -> Lazy.force l = Empty
let view_Cons = fun l -> match Lazy.force l with Cons x -> Some x

Now you can see better that the view "constructors" are simply independent 
functions. No magic here.

>  match ll with
>      %Empty -> ...
>    | %Cons (x, %Empty) -> ...
>    | %Cons (x1, %Cons (x2, %Empty)) -> ...
>    | _ -> ...
> 
> where F# would use:
>
>  let (|PEmpty|PCons|) l =
>    match Lazy.force l with
>    | Empty -> PEmpty
>    | Cons(h, t) -> PCons(h, t)
>
> This basically defines a new sum type and every time a view pattern is
> encountered, it is converted using this function into the new sum type and
> then matched over. This means you cannot mix view and non-view patterns in
> the same match but you do get to keep exhaustiveness checking.

Right.

In micmatch, you can do this:

match (x : int) with
   %Odd -> ...
| %Positive -> ...
| %Large -> ...
| %Even -> ...
| _ -> ...

The same int can be Odd, Positive and Large at the same time.


> Having said all of that, the only application of F#-style views in OCaml that
> I can think of is simply matching lazy values, which could be implemented
> more easily and with no syntactic overhead.
>
> There are other applications that MicMatch might not cater for. Specifically,
> factoring patterns and parameterizing patterns over values. For example, you
> might want an active pattern than handles commutativity:
>
>  Commute(p1, p2)  =>  p1, p2 | p2, p1

That could be done with camlp4, but right now there are other priorities, 
like translating micmatch to camlp4 3.10.


What was your question by the way?


Note that there's a mailing-list for micmatch if you're interested:
   http://groups.google.com/group/micmatch


--
http://wink.com/profile/mjambon
http://martin.jambon.free.fr


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

end of thread, other threads:[~2007-12-02 22:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-01 16:00 MicMatch Jon Harrop
2007-12-02 22:12 ` [Caml-list] MicMatch Martin Jambon

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