caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeff Meister <nanaki@gmail.com>
To: Caml List <caml-list@inria.fr>
Subject: [Caml-list] Writing the function Set.map using first-class modules and 4.00 inference
Date: Thu, 1 Nov 2012 14:00:00 -0700	[thread overview]
Message-ID: <CAHaHOqSD=dYrBxjNTnCT0+uT2j06w6pC-_QEGVDUUFZx9-QkQw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2389 bytes --]

I found an interesting (to me, anyway) use of OCaml's first-class modules,
and particularly the new 4.00 type inference features, which I thought was
worth sharing with the list. This has probably been observed by someone
else already, but I haven't seen it discussed.

In the OCaml standard library, the polymorphic set data structure is
implemented as a functor, which takes a module containing a type t and
total ordering function over t, and returns a module representing sets
whose elements have type t. Like so:

module StringSet = Set.Make(String)
module CharSet = Set.Make(Char)

One disadvantage of this method is that once the functor has been called,
the type of the set elements is fixed. As a consequence, OCaml's set
interface has no map function. If we had a polymorphic type like 'a set,
this function would have type 'a set -> ('a -> 'b) -> 'b set. But
StringSet.t and CharSet.t are not polymorphic; the corresponding type elt
in each module cannot be changed.

However, using first-class modules, we can write a function map for sets,
which takes as an extra argument the packaged module representing the set
we're mapping from. Maybe this function is better called map_from. Check it
out:

# module Set = struct
    module type OrderedType = Set.OrderedType
    module type S = Set.S
    module Make(Ord : OrderedType) = struct
      include Set.Make(Ord)
      let map (type e') (type t') (module OtherSet : S with type elt = e'
and type t = t') os f =
       OtherSet.fold (fun x accu -> add (f x) accu) os empty
   end
 end;;
[... bunch of output ...]
val map :
  (module S with type elt = 'a and type t = 'b) ->
  'b -> ('a -> elt) -> t

Now, back in OCaml 3.12, this function could be written (without the nice
package-expanding pattern I've made use of), but calling it was quite a
pain, enough to invalidate the whole enterprise. One would have to type
this:

# let strs = StringSet.(add "foo" (add "bar" empty));;
val strs : StringSet.t = <abstr>
# let chrs = CharSet.map (module StringSet : Set.S with type elt =
StringSet.elt and type t = StringSet.t) strs (fun s -> s.[0]);;
val chrs : CharSet.t = <abstr>

It's much easier with the type inference changes in OCaml 4.00:

# let strs = StringSet.(add "foo" (add "bar" empty));;
val strs : StringSet.t = <abstr>
# let chrs = CharSet.map (module StringSet) strs (fun s -> s.[0]);;
val chrs : CharSet.t = <abstr>

[-- Attachment #2: Type: text/html, Size: 2693 bytes --]

             reply	other threads:[~2012-11-01 21:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-01 21:00 Jeff Meister [this message]
     [not found] <fa.FUGAe9RTYsSPA4RwbpKO14B0oVo@ifi.uio.no>
2012-11-02 14:59 ` Radu Grigore
2012-11-02 15:11   ` Radu Grigore
2012-11-02 16:00     ` Gabriel Scherer
2012-11-02 16:21       ` Radu Grigore
     [not found] <fa.yP8czrxqEGCABee05wzAlISIlN4@ifi.uio.no>
     [not found] ` <fa.rve4x46ugIvZs4BizovvjOCeTG0@ifi.uio.no>
     [not found]   ` <fa.i9E+7rAvghoTZ1MXH4g+uL4dpCY@ifi.uio.no>
     [not found]     ` <fa.AX0JYJa8kXsH/YSZ08tuyBc8m+0@ifi.uio.no>
     [not found]       ` <fa.YAoOn2iUcpHbr53eeBfGhPTDvtM@ifi.uio.no>
2012-11-05  8:05         ` Radu Grigore
2012-11-05 10:12           ` Gabriel Scherer
2012-11-05 12:02             ` Radu Grigore

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='CAHaHOqSD=dYrBxjNTnCT0+uT2j06w6pC-_QEGVDUUFZx9-QkQw@mail.gmail.com' \
    --to=nanaki@gmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).