From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 37FC1BC69 for ; Sun, 2 Dec 2007 23:12:58 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEO8UkdCbwQZmGdsb2JhbACPSgEBAQEHBAQr X-IronPort-AV: E=Sophos;i="4.23,241,1194217200"; d="scan'208";a="19904547" Received: from out1.smtp.messagingengine.com ([66.111.4.25]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Dec 2007 23:12:57 +0100 Received: from compute1.internal (compute1.internal [10.202.2.41]) by out1.messagingengine.com (Postfix) with ESMTP id 19F835B377; Sun, 2 Dec 2007 17:12:56 -0500 (EST) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by compute1.internal (MEProxy); Sun, 02 Dec 2007 17:12:56 -0500 X-Sasl-enc: 21owhTgcXhJegGskHy0X1zAq9t7BRuccidyamupwmcqF 1196633575 Received: from [192.168.1.11] (AMontsouris-756-1-7-81.w90-46.abo.wanadoo.fr [90.46.114.81]) by mail.messagingengine.com (Postfix) with ESMTP id 4FE9F2727F; Sun, 2 Dec 2007 17:12:55 -0500 (EST) Date: Sun, 2 Dec 2007 23:12:48 +0100 (CET) From: Martin Jambon X-X-Sender: martin@martin.ec.wink.com To: Jon Harrop Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] MicMatch In-Reply-To: <200712011600.00627.jon@ffconsultancy.com> Message-ID: References: <200712011600.00627.jon@ffconsultancy.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Spam: no; 0.00; ens-lyon:01 wiki:01 ocaml:01 ocaml:01 constructors:01 camlp:01 translating:01 camlp:01 2007,:98 cater:98 wrote:01 syntactic:01 defines:01 matched:01 caml-list:01 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