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 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 41F7ABC0B for ; Mon, 5 Feb 2007 06:57:38 +0100 (CET) Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l155vbfr007621 for ; Mon, 5 Feb 2007 06:57:37 +0100 Received: from out1.internal (unknown [10.202.2.149]) by out1.messagingengine.com (Postfix) with ESMTP id 04E81B0E3A for ; Mon, 5 Feb 2007 00:57:27 -0500 (EST) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by out1.internal (MEProxy); Mon, 05 Feb 2007 00:57:27 -0500 X-Sasl-enc: +H7tanW0N7PieSTIUkQRw+bUhxp4cqhzZNfqk/b0wNa/ 1170655046 Received: from adsl-75-18-118-249.dsl.sndg02.sbcglobal.net (adsl-75-18-118-249.dsl.sndg02.sbcglobal.net [75.18.118.249]) by mail.messagingengine.com (Postfix) with ESMTP id 8E95F1995A for ; Mon, 5 Feb 2007 00:57:25 -0500 (EST) Date: Sun, 4 Feb 2007 21:57:18 -0800 (PST) From: Martin Jambon X-X-Sender: martin@droopy To: caml-list@inria.fr Subject: Views in OCaml Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at concorde with ID 45C6C751.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ens-lyon:01 ocaml:01 haskell:01 ocaml:01 runtime:01 bool:01 haskell:01 wiki:01 wiki:01 abstract:01 avoids:01 experimental:01 functions:01 ghc:01 short:01 Dear all, Intro ===== Inspired by Simon Peyton Jones' proposal of simple views for Haskell [1,2], I incorporated something very close to his proposal into Micmatch [3]. It's an experimental feature of Micmatch 0.697 [4,5] which is available in GODI. What are views? =============== Views allow pattern matching on data that don't have a type that allows pattern matching. Useful uses of views in OCaml include the following: * pattern matching over object fields (methods w/o arguments) * pattern matching over lazy values, such as lazy lists * filtering sets of elements (e.g. Even/Odd, Positive, Short_list, ...) * generally: matching abstract values when you only have functions to obtain information about their structure. * parametrization of pattern matching (patterns with gaps filled at runtime) In general the use of views avoids breaking match-with blocks into several pieces when the patterns are complex. There are several kinds of views, each with their own advantages and difficulties. The main disadvantage of the solution that we chose here is that it prevents the detection of redundant or missing cases whenever a view pattern is used. The advantages are the flexibility and simplicity of use: several overlapping views can be used in the same match-with block (e.g. match x with (%Even | %Positive) -> ... | ... -> ...). Example ======= (* Define a view function view_XY *) let view XY = fun obj -> try Some (obj#x, obj#y) with _ -> None (* Test if a list of objects starts with coordinates x=0 and y=0 *) let starts_from_origin = function %XY (0, 0) :: _ -> true | _ -> false You can see that a view is simply defined as a function that takes the subject value and returns an option. This is the case of views with an argument. Views without arguments must be defined as predicates (return a bool). Efficiency ========== Don't use simple views such as %Even or %Positive if you are concerned about speed at this particular point in your program. References ========== [1] SPJ's email: http://www.haskell.org/pipermail/haskell/2007-January/019014.html [2] Proposal for Haskell: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns [3] Micmatch home: http://martin.jambon.free.fr/micmatch.html [4] Views in Micmatch: http://martin.jambon.free.fr/micmatch-manual.html#htoc10 [5] Wiki for your comments: http://ocaml.pbwiki.com/Micmatch Martin -- Martin Jambon http://martin.jambon.free.fr