caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Kerneis <gabriel@kerneis.info>
To: caml-list@yquem.inria.fr
Subject: [Caml-list] Signature substitution and type parameters
Date: Mon, 12 Aug 2013 08:45:45 +0100	[thread overview]
Message-ID: <20130812074545.GA4646@kerneis.info> (raw)

Dear all,

Is there a concise way to instanciate a module containing a parametric
type for a particular value of this type parameter?

  module type Poly = sig type 'a t val f : 'a t -> 'a t end ;;
  module type Mono = sig type t val f : t -> t end ;;

  module PolyM : Poly = struct type 'a t let f x = x end ;;

The following works but is tedious when Poly contains many functions
instead of only one:

  module MonoM : Mono = struct
    open PolyM
    type t = int PolyM.t
    let f = PolyM.f
  end ;;

My other attempts so far:

  # module MonoM : (Mono with type t := int PolyM.t) = PolyM ;;
  Error: Only type constructors with identical parameters can be
  substituted.

  # module MonoM : (Mono with type t = int PolyM.t) = PolyM ;;
  Error: Signature mismatch:
    Modules do not match:
      sig type 'a t = 'a PolyM.t val f : 'a t -> 'a t end
    is not included in
      sig type t = int PolyM.t val f : t -> t end
    Type declarations do not match:
      type 'a t = 'a PolyM.t
    is not included in
      type t = int PolyM.t
    They have different arities. 

Is this a fundamental limitation of the typechecker, which would
otherwise lead to unsafe behaviour?

  Note: The initial motivation for doing this was to use Jean-Christophe
  Filliâtre's Hset as a drop-in replacement for OCaml's Set in one of my
  programs, without having to add type parameters all over the place.
  https://www.lri.fr/~filliatr/ftp/ocaml/ds/hset.mli.html

Kind regards,
-- 
Gabriel

             reply	other threads:[~2013-08-12  7:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-12  7:45 Gabriel Kerneis [this message]
2013-08-12  9:57 ` Raphaël Proust
2013-08-12 10:20 ` Jeremy Yallop
2013-08-12 12:25   ` Gabriel Kerneis

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=20130812074545.GA4646@kerneis.info \
    --to=gabriel@kerneis.info \
    --cc=caml-list@yquem.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).