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=1.0 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 33CC9BC69 for ; Tue, 13 Nov 2007 18:27:24 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAADJtOUfAXQInh2dsb2JhbACPBgEBAQgKKQ X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="5751715" Received: from concorde.inria.fr ([192.93.2.39]) by mail3-smtp-sop.national.inria.fr with ESMTP; 13 Nov 2007 18:27:23 +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 lADHRNhc000447 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 13 Nov 2007 18:27:23 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAPdsOUfRVYT3mGdsb2JhbACPBgEBAQEHAgYr X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="4389167" Received: from an-out-0708.google.com ([209.85.132.247]) by mail1-smtp-roc.national.inria.fr with ESMTP; 13 Nov 2007 18:27:23 +0100 Received: by an-out-0708.google.com with SMTP id c36so291208ana for ; Tue, 13 Nov 2007 09:27:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:reply-to:to:subject:date:user-agent:content-type:content-transfer-encoding:content-disposition:message-id; bh=1n2YfFAY2nxfW3woZ/oreRKYstL4boOr3IIfsfHQpZY=; b=CkSg5tcvV+wKtAt/gxl9QiQOn3J0FUW+Yx/D273nf9YZaorj8JBCKVn+fnGxBWubLLG235d1ppEjjneb565uQfsODRf+cKWL8zGEdQiloBL9hYlKAbuiq9RwA6A/npjtq8NPsSJwhjibiWK/wFZd69hSkA5AbihoahxXt3Ldp64= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:reply-to:to:subject:date:user-agent:content-type:content-transfer-encoding:content-disposition:message-id; b=drTSkyB5kmjGLZd8xHalDwvKCUBOf4hxE4kRP1F++rlkNhhttuTEceLuAG2VpPwGCevTIpoDPtRzPLvJvwGSAZ4Q75Jvg07Q/pSHqu2ePfWmy6eDcU2215Y87/FJVs6Zl5ePBwKflIhPlPLl6OjxVRBkq546Us1YEBADxyHnIP0= Received: by 10.142.79.15 with SMTP id c15mr252497wfb.1194974840892; Tue, 13 Nov 2007 09:27:20 -0800 (PST) Received: from tama-chan ( [24.98.134.200]) by mx.google.com with ESMTPS id h35sm15634672wxd.2007.11.13.09.27.19 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 13 Nov 2007 09:27:20 -0800 (PST) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@inria.fr Subject: polymorphic lists, existential types and asorted other hattery Date: Tue, 13 Nov 2007 12:27:07 -0500 User-Agent: KMail/1.9.7 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200711131227.18910.peng.zang@gmail.com> X-Miltered: at concorde with ID 4739DE7B.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; existential:01 hash:01 struct:01 struct:01 bool:01 bool:01 variants:01 haskell:01 existential:01 haskell:01 ocaml:01 ocaml:01 camlp:01 functor:01 peng:98 -----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-----