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.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 56A2BBC6B for ; Fri, 29 Jun 2007 16:19:42 +0200 (CEST) Received: from pascoe.ucs.ed.ac.uk (pascoe.ucs.ed.ac.uk [129.215.128.240]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l5TEJfgu029956 for ; Fri, 29 Jun 2007 16:19:42 +0200 Received: from rydell.inf.ed.ac.uk (rydell.inf.ed.ac.uk [129.215.212.186]) by pascoe.ucs.ed.ac.uk (8.13.8/8.13.4) with ESMTP id l5TEJfeS028260 for ; Fri, 29 Jun 2007 15:19:41 +0100 (BST) Date: Fri, 29 Jun 2007 15:19:41 +0100 (BST) From: Jeremy Yallop X-X-Sender: s0456219@rydell.inf.ed.ac.uk Reply-To: Jeremy Yallop To: Caml List Subject: ANN: pattern guards Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Edinburgh-Scanned: at pascoe.ucs.ed.ac.uk with MIMEDefang 2.52, Sophie, Sophos Anti-Virus, Clam AntiVirus X-Scanned-By: MIMEDefang 2.52 on 129.215.128.240 X-Miltered: at concorde with ID 468514FD.002 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 val:01 haskell:01 haskell:01 matched:01 arbitrary:01 patt:01 patt:01 simonpj:02 match:02 match:02 variables:02 binding:02 binding:02 env:03 I'm pleased to announce the initial release of `patterns', an OCaml extension providing general-purposes additions to pattern matching. This release includes a single feature: "pattern guards"; others will be made available in the near future. You can download patterns from http://code.google.com/p/ocaml-patterns/ Pattern guards generalize "when"-guards to allow arbitrary pattern matching with binding. After each pattern in a match you can write one or more binding phrases as follows: match e with | patt1 with p1 = e1 ... with pn = en -> e | patt2 ... The expressions e1 ... en are evaluated in turn and matched with the corresponding patterns p1 ... pn until either a match fails (in which case matching proceeds with patt2 etc.) or all matches succeed. Any variables bound in p1 ... pn are in scope within subsequent guards and within e. For example, given a function val lookup : 'a -> ('a * 'b) list -> 'b option you might write the following let f env = function | Var x with Some v = lookup x env -> ... v ... instead of the less elegant and less efficient let f env = function | Var x when mem_assoc x env -> ... assoc x env ... Pattern guards and regular guards can be freely intermixed; for example, you can write match e with | patt when c1 with patt1 = e1 when c2 with patt2 = e2 -> e | ... Pattern guards were proposed (for Haskell) in Martin Erwig and Simon Peyton Jones Pattern Guards and Transformational Patterns Haskell Workshop, 2000 See also: http://research.microsoft.com/~simonpj/Haskell/guards.html Jeremy.