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, SUBJECT_EXCESS_QP autolearn=disabled version=3.1.3 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 42C08BC69 for ; Tue, 13 Nov 2007 19:24:58 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAGN5OUdIDszkjWdsb2JhbACPBgEBAQEHBAQ X-IronPort-AV: E=Sophos;i="4.21,411,1188770400"; d="scan'208";a="19242681" Received: from qb-out-0506.google.com ([72.14.204.228]) by mail4-smtp-sop.national.inria.fr with ESMTP; 13 Nov 2007 19:24:57 +0100 Received: by qb-out-0506.google.com with SMTP id e11so2820066qbe for ; Tue, 13 Nov 2007 10:24:56 -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:cc:references:in-reply-to:content-type:content-transfer-encoding:content-disposition:message-id; bh=Ug5gcgI4Bh2fqmOxDQDv13HmkUsGKyYAiWyUMm5cHW4=; b=DopURx0nzXecpSSozrZWrE4VvEPLkFMCJDVg5kiDaZKAmzAsWvKP7cSqaMZGdieJndHW6XFz/bo8sOQltDvzAOcZuTXTjB8DcA7lhxoTd2KFDh0FCHnQGZ/rU9ETv+3rig74GT9iyTthb03PTYrAjvIGrEK9XGI46mmRnVkHfKQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:reply-to:to:subject:date:user-agent:cc:references:in-reply-to:content-type:content-transfer-encoding:content-disposition:message-id; b=EWrliKNWVmw4aYuKN46yBwgU+vCHr98xwhEmOUrEyUscN+JwGwxWN6OEGDVUe1yUGBSh5pIy+fh0uerIPvjGMRrHmOSCS7f/50q/b/EyI66bBVLR49x2+xNjq+fT9bcOY+CmyRxEaYjhfvdMLrRosZljCCVFutNDntJdDhczS40= Received: by 10.142.239.11 with SMTP id m11mr113203wfh.1194978295458; Tue, 13 Nov 2007 10:24:55 -0800 (PST) Received: from tama-chan ( [24.98.134.200]) by mx.google.com with ESMTPS id 62sm8015122wri.2007.11.13.10.24.54 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 13 Nov 2007 10:24:54 -0800 (PST) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] polymorphic lists,=?iso-8859-1?q?=09existential_types_and_asorted_other?= hattery Date: Tue, 13 Nov 2007 13:24:51 -0500 User-Agent: KMail/1.9.7 Cc: Dmitri Boulytchev References: <200711131227.18910.peng.zang@gmail.com> <473A139E.80408@tepkom.ru> In-Reply-To: <473A139E.80408@tepkom.ru> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200711131324.53588.peng.zang@gmail.com> X-Spam: no; 0.00; existential:01 hash:01 functor:01 existential:01 bool:01 iter:01 printf:01 printf:01 struct:01 struct:01 bool:01 variants:01 haskell:01 haskell:01 ocaml:01 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ahh, right! Sorry, I forgot to mention I'm looking for a possible solution without classes. I ask because most of my code base is modules and functor based and it would be a pain to convert over. Also because performance is typically better with just functions and data types. I feel like a solution without the OO side is possible through perhaps an analog of existential types? Peng On Tuesday 13 November 2007 04:14:06 pm Dmitri Boulytchev wrote: > Try using classes for this purpose: > > let show l = List.map (fun x -> x#show) l > > class integer x = > object > method show = string_of_int x > end > > class floating x = > object > method show = string_of_float x > end > > class boolean x = > object > method show = string_of_bool x > end > > > let _ = > List.iter > (Printf.printf "%s\n") > (show > [ > new integer 10; > new floating 3.14; > new boolean true; > ] > ) > > Best regards, > Dmitri Boulytchev, > St.Petersburg State University. > > > 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 > > _______________________________________________ > 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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFHOev1fIRcEFL/JewRAsbpAKCn2Kn/1eKHNVsukHU8IJvcJFcYdACgsgpr Ln3sZuR+I1aA+yl++iZxsLc= =2TsY -----END PGP SIGNATURE-----