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.8 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id ACE5ABC6E for ; Tue, 13 Nov 2007 19:41:33 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGd1OUfAXQInh2dsb2JhbACPBgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="4192845" Received: from concorde.inria.fr ([192.93.2.39]) by mail2-smtp-roc.national.inria.fr with ESMTP; 13 Nov 2007 19:02:08 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id lADI23dG002547 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 13 Nov 2007 19:02:08 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAACp1OUdIDtbkfGdsb2JhbACPBgEBCQQEERMF X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="4390581" Received: from hu-out-0506.google.com ([72.14.214.228]) by mail1-smtp-roc.national.inria.fr with ESMTP; 13 Nov 2007 19:02:07 +0100 Received: by hu-out-0506.google.com with SMTP id 16so2757249hue for ; Tue, 13 Nov 2007 10:02:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; bh=qCoiXYjix6usXnGK+9kM6IFcsvHq+ZyeUHaSHB+zBms=; b=rtQ9KZQboJYz6FgJKNW5M2Ksz7Uh0LBk/zyk43QAUfv5W0LRSN/rzgLfanrUgoYJHlZALOJkRHYxIR5iRpC+6QZk+96oKa7A+STnKqYkhEu+oR9Ffmu6KW0EK2zGlXFXmmYKB+BlSw+4LCiKypid+W9Rc/9o1JNoLo+lBlJLNCg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=GlrtDbMd1lBADRB1U5Ya+eKo39m+43KlqVMO1M1x9RqSQ+o25mMLg898a5Ec/OCMucph+S4QtGZFszmNnfxDH1C+Zz44qpG2+mmAfDFbFIsuoL8X0AJ5eah9B8oA9x51UzsKW6+HeMWe2x/7dsb8fnGmVeP4i3IKYkBL0UgusTs= Received: by 10.82.177.3 with SMTP id z3mr15627253bue.1194976927193; Tue, 13 Nov 2007 10:02:07 -0800 (PST) Received: from ?192.168.0.8? ( [87.88.165.197]) by mx.google.com with ESMTPS id j2sm13551269mue.2007.11.13.10.02.04 (version=SSLv3 cipher=RC4-MD5); Tue, 13 Nov 2007 10:02:05 -0800 (PST) Message-ID: <4739E6A6.2090704@lix.polytechnique.fr> Date: Tue, 13 Nov 2007 19:02:14 +0100 From: Arnaud Spiwack User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: Caml List Subject: Re: [Caml-list] polymorphic lists, existential types and asorted other hattery References: <200711131227.18910.peng.zang@gmail.com> In-Reply-To: <200711131227.18910.peng.zang@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Sender: Arnaud Spiwack X-Miltered: at concorde with ID 4739E69B.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; lix:01 existential:01 existential:01 ocaml:01 bool:01 bool:01 hash:01 struct:01 struct:01 variants:01 haskell:01 haskell:01 ocaml:01 camlp:01 functor:01 What you are describing is rather existential in essence. Existential type do not exist in OCaml but are encodable through interesting tricks (I know so because I kinda asked a similar question a few month ago :p ). See discussion thread here (which was pointed to me this very time I asked the question) : http://tinyurl.com/2p5oan . Here is your example refactored with this methodology : type 'e show = { item : 'e; show: 'e-> string } type 't show_scope = { bind_show : 'e. 'e show -> 't } type showable = { open_showable : 't. 't show_scope -> 't } let create_showable sh it = { open_showable = fun scope -> scope.bind_show { item = it ; show = sh } } let use_showable shw f = shw.open_showable f let show shw = use_showable shw { bind_show = fun sh -> sh.show sh.item } let showable_int = create_showable string_of_int let showable_bool = create_showable string_of_bool let showable_float = create_showable string_of_float let xs = [(showable_int 1); (showable_float 2.0); (showable_bool false)] List.map show xs Peng Zang a écrit : > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > Is there a way to create lists in which the elements may be of > differing types but which all have some set of operations defined > (eg. tostr) in common? One can then imagine mapping over such lists > with "generic" versions of those common operations. Here's a concrete > example of what I mean: > > module Int = struct > type t = int > let show x = string_of_int x > end > module Float = struct > type t = float > let show x = string_of_float x > end > module Bool = struct > type t = bool > let show x = string_of_bool x > end > > let xs = [`Int 1; `Float 2.0; `Bool false] > let showany x = match x with > | `Int x -> Int.show x > | `Float x -> Float.show x > | `Bool x -> Bool.show x > ;; > List.map showany xs;; > > Essentially we have ints, floats and bools. All these types can be > shown. It would be nice to be able to create a list of them [1; 2.0; > false] that you can then map a generalized show over. In the above > example, I used polymorphic variants in order to get them into the > same list and then had to define my own generalized show function, > "showany". This is fine as there is only one shared operation but if > there is a large set of these common operations, it becomes > impractical to define a generalized version for each of them. > > I've come across a way to do this in haskell using what they call > "existential types". > > http://www.haskell.org/haskellwiki/Existential_type > > I don't really understand existential types however and don't know if > OCaml has them nor how to use them. > > So. How can one do this in OCaml? Is there perhaps a camlp4 > extension that can do this? Is there a possible functor trick that > can take N modules as arguments and spit out a new module with a > generalized type that can take on any of the types in the arguments > and also make generalized versions of operations common to the N > modules? Are there existential types or equivalents in OCaml? If so > how does one go about using them? > > Thanks in advance to anyone who forays into this bundle of questions. > > Peng > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.7 (GNU/Linux) > > iD8DBQFHOd52fIRcEFL/JewRAkZNAJ9MUE4Ph4ybbtKjiV9h9ZxPsvDwGQCgwIJz > aOceerrixiPZosJq4a+r0qM= > =zOZF > -----END PGP SIGNATURE----- > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > >