caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
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 20:39:53 +0100	[thread overview]
Message-ID: <AANLkTimgHHdw1Yxo396COphLEYh3otEc_rKUrTdB7qZ0@mail.gmail.com> (raw)
In-Reply-To: <982043.53335.qm@web111502.mail.gq1.yahoo.com>

I'm just wondering.

Abstraction is about hiding type representation. Variant subtyping is
a kind of structural subtyping and is thus about type representation.
So constraining an abstract type to be of a given structural subtype
seems contradictory.

The only way I see to type what you want is to make FOO.kind_t
abstract. This doesn't ensure that the implementation of 'a kind_t is
a subtype of Kind.t itself. However if your goal is some kind of
phantom type constraints, it will do the job. And you can still
require a function to convert 'a kind to Kind.t. See the code below.

Best,

Daniel


module Kind:
sig
       type t = [ `A | `B | `C ]

       val to_string: t -> string
end =
struct
       type t = [ `A | `B | `C ]

       let to_string = function
               | `A -> "A"
               | `B -> "B"
               | `C -> "C"
end

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 -> Kind.t
end


module Foo1: FOO =
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 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_to_kind k = (k :> Kind.t)
end

  parent reply	other threads:[~2011-02-21 19:39 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 [this message]
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
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=AANLkTimgHHdw1Yxo396COphLEYh3otEc_rKUrTdB7qZ0@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;
as well as URLs for NNTP newsgroup(s).