caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David House <dmhouse@gmail.com>
To: Malcolm Matalka <mmatalka@gmail.com>
Cc: Edgar Friendly <thelema314@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Expressing module sig and impl in mli file
Date: Wed, 19 Sep 2012 08:51:28 +0100	[thread overview]
Message-ID: <CADy1MasDAEvGZ5UeUd18E27Ve4QaBrzHiG6nPvaMAefhtm+8Fg@mail.gmail.com> (raw)
In-Reply-To: <CADy1MauT6ry-sS-uETGvxNhKuSkT8056nGXtu5L3+9ngfSAgPQ@mail.gmail.com>

Oh, and one more thing to mention. If your identifier types are not
strings, but you have of_string and to_string functions, then you can
do the following:

module Bar = struct
  module T = struct
    type t = ...
    let of_string = ...
    let to_string = ...
  end
  include Identifiable.Of_stringable(T)
end

module Bar : Identifiable

On 19 September 2012 08:49, David House <dmhouse@gmail.com> wrote:
> So, this depends. If you have an identifier type that are internally
> just strings, then the simplest way is as I said before:
>
> module Bar = String_id (* or = String, both will work *)
>
> module Bar : sig
>   type t
>   include Identifiable with type t := t
>   (* other operations on your Bar go here *)
> end
>
> The Identifiable signature includes the Comparable signature, which
> includes a map module. So then you have a type ['a Bar.Map.t], which
> is a map from Bar.t's to 'a.
>
> If you want to use your own comparison function, then you can do the following:
>
> module Bar = struct
>   module T = struct
>     type t = string with sexp
>     let compare = ...
>   end
>   include T
>   include Comparable.Make(T)
>   let of_string = Fn.id
>   let to_string = Fn.id
> end
>
> module Bar : sig
>   type t
>   include Comparable with type t := t (* Map module, compare, etc. *)
>   include Strinagable with type t := t (* to_string, of_string *)
> end
>
> This will generate a map module that uses the comparison function you define.
>
> If additionally you can define a hash function, then you can do the following:
>
> module Bar = struct
>   module T = struct
>     type t = string with sexp
>     let compare t1 t2 = ...
>     let hash t = ...
>   end
>   include T
>   include Comparable.Make(T)
>   include Hashable.Make(T)
>   let of_string = Fn.id
>   let to_string = Fn.id
> end
>
> module Bar : sig
>   type t
>   include Comparable with type t := t (* Set module, Map module,
> compare, etc. *)
>   include Strinagable with type t := t (* to_string, of_string *)
>   include Hashable with type t := t (* hash, Table module, Hash_set
> module, etc. *)
> end
>
> And the final signature is actually equal (very nearly) to
> Identifiable, so you'd just write:
>
> module Bar : Identifiable
>
> On 19 September 2012 08:36, Malcolm Matalka <mmatalka@gmail.com> wrote:
>> Yes I think I'm confused.  In all parts of this module I want the
>> Identifiable behaviour, but at the same time I want a Map of these
>> identifiers to something, so this turned in to me trying to jerry-rig
>> that rather than thinking about what I actually want.
>>
>> Can something that is Identifiable be the key to a Map (in Core)?  Am
>> I doing something wrong if I want that?
>>
>> Thanks
>>
>> /M
>>
>> On Wed, Sep 19, 2012 at 9:11 AM, David House <dmhouse@gmail.com> wrote:
>>> The standard way of doing this is as follows (note that Identifier is
>>> changing to Identifiable in the next version, so I'll use that
>>> terminology):
>>>
>>> module Bar : sig
>>>   type t = string
>>>   include Identifiable with type t := t
>>> end
>>>
>>> But if this is literally what you're doing, I'm sort of confused. The
>>> point of identifiable is that you have explicit to_string/from_string
>>> functions and the type equality with string is not exposed. E.g. you
>>> might want to use a different comparison function than string
>>> equality. If you expose the type equality with string, then people are
>>> free to use String.compare on your type, so you don't get the
>>> abstraction you wanted.
>>>
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa-roc.inria.fr/wws/info/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>>

  reply	other threads:[~2012-09-19  7:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-18 21:02 Malcolm Matalka
2012-09-18 21:13 ` Gabriel Scherer
2012-09-18 21:31   ` Malcolm Matalka
2012-09-18 22:40     ` Malcolm Matalka
2012-09-19  2:11       ` Edgar Friendly
2012-09-19  7:11         ` David House
2012-09-19  7:36           ` Malcolm Matalka
2012-09-19  7:49             ` David House
2012-09-19  7:51               ` David House [this message]
2012-09-19  8:00                 ` Malcolm Matalka
2012-09-19  8:11                   ` Gabriel Scherer
2012-09-19  7:25       ` Jacques Garrigue

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=CADy1MasDAEvGZ5UeUd18E27Ve4QaBrzHiG6nPvaMAefhtm+8Fg@mail.gmail.com \
    --to=dmhouse@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=mmatalka@gmail.com \
    --cc=thelema314@gmail.com \
    /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).