caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andreas Rossberg <rossberg@mpi-sws.org>
To: David Sheets <sheets@alum.mit.edu>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] First-class Functor Forgetting for Free
Date: Sun, 11 Aug 2013 09:53:48 +0200	[thread overview]
Message-ID: <1407E74D-EDC8-4638-8917-4CAC80B4C682@mpi-sws.org> (raw)
In-Reply-To: <CAAWM5TyT4kryhLbDzsNHyLw76QpWFzBXHR4+Vf9bSwKGy16-=Q@mail.gmail.com>


On Aug 11, 2013, at 03:55 , David Sheets <sheets@alum.mit.edu> wrote:

> What issue prevents functions over first-class modules from forgetting
> the first-class modules' type constraints?
> 
> That is, given:
> 
> ###############
> 
> module type D = sig
>  type t
> 
>  val x : t
>  val f : t -> int
> end
> 
> module type C = sig
>  val q : int
> end
> 
> module F(X : D) : C = struct
>  let q = X.(f x)
> end
> 
> let f x =
>  let module X = (val x : D) in
>  let module Q = struct
>    let q = X.(f x)
>  end in
>  (module Q : C)
> 
> let x =
>  let module X = struct
>    type t = int
> 
>    let x = 2
>    let f x = x * 3
>  end in
>  (module X : D with type t = int)
> 
> ;;
> let module X = (val x : D with type t = int) in
> let module M = F(X) in
> (* let x = (module (val x : D with type t = int) : D) in*)
> let module M' = (val f x : C) in
> 
> Printf.printf "M.q  is %d\n%!" M.q;
> Printf.printf "M'.q is %d\n%!" M'.q;
> ()
> 
> ###############
> 
> Why must I uncomment (* let x = (module (val x : D with type t = int)
> : D) in*) to compile?
> 
> I understand why structural subtyping requires a module cast but I
> don't see why type relaxation would.
> I looked at the generated assembly and this line seems to disappear.
> 
> Why is it needed?

There is no implicit subtyping in OCaml's core language. The reason for that is not code generation, but type inference. For example, what type should the following function be given if the subtyping you ask for could apply?

  let g x = let module X = (val x : D) in X.(f x)

That said, as usual in OCaml, you can force subtyping explicitly. That is, you are able to just say

  let module M' = (val f (x :> (module D)) : C) in …

in your example. (Explicit subtyping on package types is still very limited, though, i.e. only allows forgetting type equations on the same named signature.)

/Andreas


  reply	other threads:[~2013-08-11  7:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-11  1:55 David Sheets
2013-08-11  7:53 ` Andreas Rossberg [this message]
2013-08-11 13:29   ` David Sheets
2013-08-11 14:32     ` Andreas Rossberg
2013-08-11 14:58       ` Leo White
2013-08-12 11:01         ` Andreas Rossberg
2013-08-12 11:37           ` Leo White
2013-08-12 12:15             ` Andreas Rossberg
2013-08-12 13:06               ` Leo White
2013-08-12 14:15                 ` Andreas Rossberg
2013-08-12 15:17                   ` Leo White
2013-08-12 16:08                     ` Andreas Rossberg
2013-08-12 16:46                       ` Leo White
2013-08-13 11:22                         ` Andreas Rossberg

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=1407E74D-EDC8-4638-8917-4CAC80B4C682@mpi-sws.org \
    --to=rossberg@mpi-sws.org \
    --cc=caml-list@inria.fr \
    --cc=sheets@alum.mit.edu \
    /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).