caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Type Constraints and .mli
@ 2014-08-06 12:14 Trevor Smith
  2014-08-06 15:36 ` Török Edwin
  2014-08-06 22:06 ` Nick Lucaroni
  0 siblings, 2 replies; 5+ messages in thread
From: Trevor Smith @ 2014-08-06 12:14 UTC (permalink / raw)
  To: caml-list

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

Hello,

I have a question about using .mli files for increased readability. I think
my question boils down to: "Can one tersely add type constraints to a
signature defined in a .mli in that same .mli file?"

Detailed problem: You want to have a read interface and a write interface
for the same implementation.

We'll use a trivial example with a character and a name.

module type CharacterSig = sig
  val t
  val create : string -> t
  val name : t -> string
end

module type MutableCharacterSig = sig
  val t
  val create : string -> t
  val name : t -> string
  val set_name : t -> string -> unit
end

module CharacterImpl = struct
  type t = {name : string ref}
  let create name  =
    {name = ref name }
  let name c = !(c.name)
  let set_name c name =
    c.name := name
end

module Character = (CharacterImpl : CharacterSig with type t =
CharacterImpl.t)
module MutableCharacter = (CharacterImpl : MutableCharacterSig with type t
= CharacterImpl.t)

But what I would like is to specify the read and write signatures in .mli
files for a more readable codebase.

So:

character.mli:
  val t
  val create : string -> t
  val name : t -> string

mCharacter.mli:
  val t
  val create : string -> t
  val name : t -> string
  val set_name : t -> string -> unit

characterImpl.ml (* ... implementation as above ... *)

However, it is not clear to me that there is a way to attach the type
constraint to character.mli and mCharacter.mli, while keeping the terse
readability of the .mli file. One idea for a solution, would be to
reference a "this" so that the interface could show that it was being
implemented by CharacterImpl, and include the type constraint.

The solution I've come to use, that is still pretty readable, is to define
the signature in the .ml file (so no .mli file) and then defining an
internal module which I include (so that I can still reference file name as
the module). So:

character.ml

module type CharacterSig = sig
    type t
    val create : string -> t
    val name : t -> string
end

module T = (CharacterImpl : CharacterSig with type t = CharacterImpl.t)
include T

However, it seems like there could be a slightly more readable way of doing
this.

Thoughts? Thank you.

Trevor

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-08  8:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-06 12:14 [Caml-list] Type Constraints and .mli Trevor Smith
2014-08-06 15:36 ` Török Edwin
2014-08-07 22:06   ` Trevor Smith
2014-08-08  8:19     ` Frédéric Bour
2014-08-06 22:06 ` Nick Lucaroni

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).