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 discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 341D4BC0A for ; Wed, 14 Feb 2007 22:33:36 +0100 (CET) Received: from coriana6.cis.mcmaster.ca (coriana6.CIS.McMaster.CA [130.113.128.17]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l1ELXZrZ018994 for ; Wed, 14 Feb 2007 22:33:35 +0100 Received: from Gorash7.UTS.McMaster.CA (Gorash7.UTS.mcmaster.ca [130.113.196.61]) by coriana6.cis.mcmaster.ca (8.13.7/8.13.7) with ESMTP id l1ELXPLU017849; Wed, 14 Feb 2007 16:33:33 -0500 (EST) Received: from cgpsrv2.cis.mcmaster.ca (univmail.CIS.mcmaster.ca [130.113.64.46]) by Gorash7.UTS.McMaster.CA (8.13.7/8.13.7) with ESMTP id l1ELX00w021071; Wed, 14 Feb 2007 16:33:00 -0500 Received: from [74.109.166.109] (account carette@univmail.cis.mcmaster.ca HELO [192.168.1.101]) by cgpsrv2.cis.mcmaster.ca (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 162250994; Wed, 14 Feb 2007 16:33:00 -0500 Message-ID: <45D38011.2040907@mcmaster.ca> Date: Wed, 14 Feb 2007 16:33:05 -0500 From: Jacques Carette Organization: McMaster University User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: Jon Harrop Cc: caml-list@inria.fr Subject: Re: [Caml-list] Patterns that evaluate References: <45D23608.4030104@mcmaster.ca> <45D352F2.3080003@gmail.com> <1171479313.24335.33.camel@localhost.localdomain> <200702142105.48663.jon@ffconsultancy.com> In-Reply-To: <200702142105.48663.jon@ffconsultancy.com> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version-Mac: 4.7.1.128075, Antispam-Engine: 2.4.0.264935, Antispam-Data: 2007.2.14.131434 X-PerlMx-Spam: Gauge=IIIIIII, Probability=7%, Report='__CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __USER_AGENT 0' X-Miltered: at discorde with ID 45D3802F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; elegantly:01 constructors:01 higher-order:01 polymorphism:01 wrote:01 rec:01 rec:01 rewrite:01 rewrite:01 caml-list:01 binary:01 int:01 mul:02 mul:02 mcmaster:02 [Many excellent ideas on Active Patterns removed] Jon Harrop wrote: > That could be written much more elegantly, something like this: > > let rec rewrite rule = function > | Int _ | Var _ as f -> rule f > | Add(f, g) -> Add(rewrite rule f, rewrite rule g) > | Mul(f, g) -> Mul(rewrite rule f, rewrite rule g) > Following ideas of Barry Jay, this could be even more simply written as let rec rewrite rule = function | u _ as f -> rule f | b(f, g) -> b(rewrite rule f, rewrite rule g) where u and b are supposed to be bound (to respectively unary and binary constructors). Yes, the 'type' of the function rewrite needs higher-order polymorphism (or polytypism or ...) to work. See Tim Sheard's Omega for one rather nice way to do that. Jacques