From: Arnaud Spiwack <Arnaud.Spiwack@lix.polytechnique.fr>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] polymorphic lists, existential types and asorted other hattery
Date: Tue, 13 Nov 2007 19:02:14 +0100 [thread overview]
Message-ID: <4739E6A6.2090704@lix.polytechnique.fr> (raw)
In-Reply-To: <200711131227.18910.peng.zang@gmail.com>
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
>
>
next prev parent reply other threads:[~2007-11-13 18:41 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-13 17:27 Peng Zang
2007-11-13 18:02 ` Arnaud Spiwack [this message]
2007-11-13 18:29 ` [Caml-list] " Julien Moutinho
2007-11-13 18:35 ` Julien Moutinho
2007-11-13 21:14 ` Dmitri Boulytchev
2007-11-13 18:24 ` Peng Zang
2007-11-13 21:39 ` Dmitri Boulytchev
2007-11-13 19:13 ` Benjamin Canou
2007-11-14 4:48 ` Jacques Garrigue
2007-11-14 12:45 ` Peng Zang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4739E6A6.2090704@lix.polytechnique.fr \
--to=arnaud.spiwack@lix.polytechnique.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox