caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] "subclassing" a char map
Date: Mon, 16 Sep 2013 09:42:19 +0200	[thread overview]
Message-ID: <20130916074218.GB9309@frosties> (raw)
In-Reply-To: <CAFrFfuGg3n4m5AU1QtE6GahMgdfADfP6ohbrxd5zpM8ju-g97w@mail.gmail.com>

On Sun, Sep 15, 2013 at 09:31:34PM -0700, Martin DeMello wrote:
> Ah, thanks, that will do very nicely. I just wanted a type of char map
> I couldn't insert anything else other than A-Z into, that the
> typechecker would prevent me from mixing with the regular char map.
> 
> martin
> 
> On Sun, Sep 15, 2013 at 9:20 PM, Ivan Gotovchits <ivg@ieee.org> wrote:
> > Martin DeMello <martindemello@gmail.com> writes:
> >
> >> I have a char multiset defined via
> >>
> >> module MultiSet = Map.Make(struct type t = char let compare = compare end)
> >>
> >> Now I would like to split off a distinct type that can only contain
> >> A-Z as keys. What's the best way to do this?
> >>
> >> martin
> >
> > Not sure, that I correctly understood your needs... but you can
> > implement «A-Z keys» as an abstract type, contained in a module with the
> > following signature:
> >
> > module type Caps =  sig
> >   type t
> >   val create: char -> t option
> >   val compare: t -> t -> int
> >   val project: t -> char
> > end
> >
> > Next, you can instatiate a Map from abstract type Cap.t to 'a:
> >
> > module CapMap = Map.Make(Caps)
> >
> > where Caps is an implementation, conforming to the signature Caps. For
> > example like this:
> >
> > module Caps : Caps = struct
> >     type t = char
> >     let create = function
> >       | 'A'..'Z' as ch -> Some ch
> >       | otherwise      -> None
> >     let compare = compare
> >     let project ch = ch
> > end

I recommend using a private type (in the module type) over an abstract
one:
    type t = private char

That way a type t instance can only be constructed in the module,
where you check the upper case, but can be used in place of a char
otherwise. No need to manually project it everywhere.

MfG
	Goswin

  reply	other threads:[~2013-09-16  7:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-15 22:29 Martin DeMello
2013-09-16  4:20 ` Ivan Gotovchits
2013-09-16  4:31   ` Martin DeMello
2013-09-16  7:42     ` Goswin von Brederlow [this message]
2013-09-16 21:09       ` Martin DeMello
2013-09-17  8:13     ` oleg

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=20130916074218.GB9309@frosties \
    --to=goswin-v-b@web.de \
    --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).