From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 9D0E6BB81 for ; Thu, 1 Dec 2005 09:45:59 +0100 (CET) Received: from mail11.bluewin.ch (mail11.bluewin.ch [195.186.18.61]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id jB18jxRa001643 for ; Thu, 1 Dec 2005 09:45:59 +0100 Received: from [10.0.1.2] (83.76.65.14) by mail11.bluewin.ch (Bluewin 7.2.069.1) id 438529A0001647E0 for caml-list@yquem.inria.fr; Thu, 1 Dec 2005 08:45:58 +0000 Mime-Version: 1.0 (Apple Message framework v746.2) In-Reply-To: <1133400888.20197.40.camel@rosella> References: <1133400888.20197.40.camel@rosella> Content-Type: text/plain; charset=ISO-8859-1; delsp=yes; format=flowed Message-Id: Content-Transfer-Encoding: quoted-printable From: =?ISO-8859-1?Q?Daniel_B=FCnzli?= Subject: Re: [Caml-list] composing functions... Date: Thu, 1 Dec 2005 09:46:20 +0100 To: caml-list@yquem.inria.fr X-Mailer: Apple Mail (2.746.2) X-Miltered: at concorde with ID 438EB847.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; bunzli:01 buenzli:01 epfl:01 caml-list:01 existential:01 ocaml:01 sardes:01 inrialpes:01 aschmitt:01 sig:01 datatype:01 constructors:01 val:01 val:01 struct:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Le 1 d=E9c. 05 =E0 02:34, skaller a =E9crit : > let h =3D compose f g > > You can of course fold 'compose' over a list if all the > functions have the same type, if not, then you can't > make the list in the first place :) Unless you define your own type for lists of composable functions =20 (see below). This was an example I wrote in this discussion [1] about =20= encoding existential types in ocaml. Best, Daniel [1] module Funlist : sig (* The funlist datatype *) type ('a, 'b) t (* Constructors *) val nil : ('a, 'a) t val cons : ('a -> 'b) -> ('b, 'c) t -> ('a, 'c) t (* Applying a value to a composition *) val apply : ('a, 'b) t -> 'a -> 'b end =3D struct (* List of composable functions. The intended type expressed by the four types below is : type ('a, 'b) t =3D Nil of ('a -> 'b) | Cons of exists 'c. ('a -> 'c) * ('c, 'b) t *) type ('a, 'b) t =3D Nil of ('a -> 'b) | Cons of ('a, 'b) packed_list and ('a, 'b, 'z) list_scope =3D { bind_list : 'c. ('a -> 'c) * ('c, 'b) = t -> 'z} and ('a, 'b) packed_list =3D { open_list : 'z. ('a, 'b, 'z) list_scope = -> 'z } (* Packing and unpacking lists *) let pack_list h t =3D { open_list =3D fun scope -> scope.bind_list (h,t) = } let with_packed_list p e =3D p.open_list e (* Constructors *) let nil =3D Nil (fun x -> x) let cons h t =3D Cons (pack_list h t) (* Type to handle the polymorphic recursion of the apply function *) type poly_rec =3D { apply : 'a 'b. poly_rec -> ('a, 'b) t -> 'a -> 'b } let apply' r l x =3D match l with | Nil id -> id x | Cons l -> with_packed_list l { bind_list =3D function h,t -> r.apply r t (h =20= x) } let poly_rec =3D { apply =3D apply' } let apply l x =3D apply' poly_rec l x end (* Example of use *) let twice x =3D 2*x let double x =3D (x, x) let list =3D Funlist.cons twice (Funlist.cons (( =3D ) 4) (Funlist.cons double Funlist.nil)) let a, b =3D Funlist.apply list 2=