From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA15752; Fri, 9 Jan 2004 22:23:43 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA15426 for ; Fri, 9 Jan 2004 22:23:41 +0100 (MET) Received: from mail6.bluewin.ch (mail6.bluewin.ch [195.186.4.229]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id i09LNf502084 for ; Fri, 9 Jan 2004 22:23:41 +0100 (MET) Received: from [10.0.1.2] (62.202.50.142) by mail6.bluewin.ch (Bluewin AG 7.0.024) id 3FBC8461006538AE; Fri, 9 Jan 2004 21:23:41 +0000 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v609) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: <286C1B71-42EA-11D8-A280-000393DBC266@epfl.ch> Content-Transfer-Encoding: 7bit Cc: caml-list@inria.fr From: =?ISO-8859-1?Q?Daniel_B=FCnzli?= Subject: Re: [Caml-list] Encoding existential types without modules Date: Fri, 9 Jan 2004 22:24:07 +0100 To: Jean-Christophe Filliatre X-Mailer: Apple Mail (2.609) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 existential:01 recursion:01 recursion:01 200208:01 00334.:01 datatype:01 val:01 val:01 struct:01 unpacking:01 apply':01 apply':01 caml:01 sig:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Thanks for your feedback. Besides, I would just like to point out that the way I solved the problem of polymorphic recursion for the apply function in the example of the list of composable function cannot be applied in general. A general way to solve the problem of polymorphic recursion was given by Brian Rogoff in a previous post [1]. Applying this solution gives the following (much simpler and maybe more efficient) code. Daniel [1] http://caml.inria.fr/archives/200208/msg00334.html 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 = struct (* List of composable functions. The intended type expressed by the four types below is : type ('a, 'b) t = Nil of ('a -> 'b) | Cons of exists 'c. ('a -> 'c) * ('c, 'b) t *) type ('a, 'b) t = Nil of ('a -> 'b) | Cons of ('a, 'b) packed_list and ('a, 'b, 'z) list_scope = { bind_list : 'c. ('a -> 'c) * ('c, 'b) t -> 'z} and ('a, 'b) packed_list = { open_list : 'z. ('a, 'b, 'z) list_scope -> 'z } (* Packing and unpacking lists *) let pack_list h t = { open_list = fun scope -> scope.bind_list (h,t) } let with_packed_list p e = p.open_list e (* Constructors *) let nil = Nil(fun x -> x) let cons h t = Cons(pack_list h t) (* Type to handle the polymorphic recursion of the apply function *) type poly_rec = { apply : 'a 'b. poly_rec -> ('a, 'b) t -> 'a -> 'b } let apply' r l x = match l with | Nil id -> id x | Cons l -> with_packed_list l { bind_list = function h,t -> r.apply r t (h x) } let poly_rec = { apply = apply' } let apply l x = apply' poly_rec l x end (* Example of use *) let twice x = 2*x let double x = (x, x) let list = Funlist.cons twice (Funlist.cons (( = ) 4) (Funlist.cons double Funlist.nil)) let a, b = Funlist.apply list 2 ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners