caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Leo White <lpw25@cam.ac.uk>
To: Thomas Braibant <thomas.braibant@gmail.com>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Quizz
Date: Tue, 13 Jan 2015 20:51:03 +0000	[thread overview]
Message-ID: <877fwqqua0.fsf@study.localdomain> (raw)
In-Reply-To: <CAHR=Vkx1Lb43qh5QtcnKM2tw=64m6NuU8xsd0CoFR5P7tP2X8g@mail.gmail.com> (Thomas Braibant's message of "Tue, 13 Jan 2015 18:09:54 +0100")

Hi,

In OCaml 4.01 and earlier, first-class modules were typed
nominatively. In other words, their type was based on the name of the
module type used, not its structure. For example:

            OCaml version 4.01.0

    # module type T = sig type t end;;
    module type T = sig type t end

    # module type S = sig type t end;;
    module type S = sig type t end

    # let f (x : (module T)) : (module S) = x;;
    Characters 38-39:
      let f (x : (module T)) : (module S) = x;;
                                            ^
    Error: This expression has type (module T)
           but an expression was expected of type (module S)

This obviously requires that the module type has a name:

    # type t = (module sig type t end);;
    Characters 17-20:
      type t = (module sig type t end);;
                       ^^^
    Error: Syntax error

In version 4.02, the type-checking of first-class modules was changed to
be structural instead of nominative:

            OCaml version 4.02.1

    # module type T = sig type t end;;
    module type T = sig type t end

    # module type S = sig type t end;;
    module type S = sig type t end

    # let f (x : (module T)) : (module S) = x;;
    val f : (module T) -> (module S) = <fun>

This means that it would now be possible to support first-class modules
for types which have no name, but such support has not yet been
added. (There are some awkward issues around the syntax in relation to
with constraints, but they are probably solvable.)

So, in summary, you can't do that, but it is now at least theoretically
something you could do.

Regards,

Leo

Thomas Braibant <thomas.braibant@gmail.com> writes:

> Hi list,
>
> Here is a quizz that can easily be solved using OCaml (version
> 4.02.1), but it would be less fun than trying to infer which functions
> are accepted.
>
> ```
> module type S = sig val test : 'a list end
>
> type t = (module S)
>
> let a (x : t) =
>   let module M = (val x) in
>   List.length M.test
>
> let b (module M : S) =
>   List.length M.test
>
> let c ((module M) : t) =
>   List.length M.test
>
> let d (module M : t) =
>   List.length M.test
>
> let e (x : (module S)) =
>   let module M = (val x) in
>   List.length M.test
>
> let f ((module M) : (module S)) =
>   List.length M.test
>
> let g : (module S) -> int = fun (module M) ->
>   List.length M.test
>
> let h ((module M) : (module sig val test : 'a list end)) =
>   List.length M.test
>
> ```
> (I am sure I have missed interesting variations, though.)
>
> I have read through
>
> http://caml.inria.fr/pub/docs/manual-ocaml/types.html,
> http://caml.inria.fr/pub/docs/manual-ocaml/modtypes.html
> http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec230
>
> but what I do not get is how to make a variation of `h` work. That is,
> I would like to inline the module signature. Is that possible?
>
> Best,
> Thomas

  reply	other threads:[~2015-01-13 20:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 17:09 Thomas Braibant
2015-01-13 20:51 ` Leo White [this message]
2015-01-13 21:10   ` Milan Stanojević
2015-01-14  0:09     ` Leo White
2015-01-14  7:02       ` Jacques Garrigue
2015-01-14 16:32         ` Milan Stanojević
2015-01-14 17:24           ` Leo White
2015-01-15  9:41             ` Jacques Garrigue

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=877fwqqua0.fsf@study.localdomain \
    --to=lpw25@cam.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=thomas.braibant@gmail.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).