From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Constraining abstract type to be of a given subtype
Date: Mon, 21 Feb 2011 22:49:47 +0100 [thread overview]
Message-ID: <AANLkTim=oKFE-p75Yv_6oDf+0tO9+CAqc1EcufGr0yuO@mail.gmail.com> (raw)
In-Reply-To: <AANLkTi=8Jw8Zvndm2duQy-=dREwLXZA_Y45E-yeDOye0@mail.gmail.com>
Ok last message I promise.
I don't know exactly what you want but you may prefer the following
FOO (see the signature of kind_to_kind) :
module type FOO =
sig
type custom_t
type 'a kind_t constraint 'a = [< Kind.t]
type 'a t = int * custom_t * 'a kind_t
val make: int -> custom_t -> 'a kind_t -> 'a t
val string_of_custom: custom_t -> string
val string_of_kind: 'a kind_t -> string
val kind_to_kind : 'a kind_t -> 'a
end
In that case the idea is that each implementer declares kind as :
type 'a kind = 'a constraint 'a = [< Kind.t]
but controls which 'a it will actually allow via the kind_of_kind
function (better names to be found) :
module FooAB : sig
type custom_t
type 'a kind_t constraint 'a = [< Kind.t]
type 'a t = int * custom_t * 'a kind_t
val make_custom : float -> custom_t
val make: int -> custom_t -> 'a kind_t -> 'a t
val string_of_custom: custom_t -> string
val string_of_kind: 'a kind_t -> string
val kind_of_kind : ([ `A | `B] as 'a) -> 'a kind_t
val kind_to_kind : 'a kind_t -> 'a
end = struct
type custom_t = float
type 'a kind_t = 'a constraint 'a = [< Kind.t]
type 'a t = int * custom_t * 'a kind_t
let make_custom f = f
let make id custom kind = (id, custom, kind)
let string_of_custom = string_of_float
let string_of_kind k = Kind.to_string (k :> Kind.t)
let kind_of_kind k = k
let kind_to_kind k = k
end
let (v : [`A | `B] FooAB.t) =
FooAB.make 2 (FooAB.make_custom 2.) (FooAB.kind_of_kind `A)
let (v : [`A | `B] FooAB.t) =
FooAB.make 2 (FooAB.make_custom 2.) (FooAB.kind_of_kind `B)
let (k : [`A |`B]) = FooAB.kind_to_kind (FooAB.kind_of_kind `A)
(* Should not typecheck
let v = FooAB.make 2 (FooAB.make_custom 2.) (FooAB.kind_of_kind `C) *)
module FOOCheck = (FooAB : FOO)
next prev parent reply other threads:[~2011-02-21 21:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-21 15:56 Dario Teixeira
2011-02-21 17:07 ` Guillaume Yziquel
2011-02-21 18:42 ` Dario Teixeira
2011-02-21 19:26 ` Guillaume Yziquel
2011-02-21 19:39 ` Daniel Bünzli
2011-02-21 20:22 ` Dario Teixeira
2011-02-21 20:59 ` Daniel Bünzli
2011-02-21 21:31 ` Daniel Bünzli
2011-02-21 21:49 ` Daniel Bünzli [this message]
2011-02-22 16:15 ` Dario Teixeira
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='AANLkTim=oKFE-p75Yv_6oDf+0tO9+CAqc1EcufGr0yuO@mail.gmail.com' \
--to=daniel.buenzli@erratique.ch \
--cc=caml-list@inria.fr \
--cc=darioteixeira@yahoo.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