🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fa.caml
Article #86

typing with first-class modules

#86
From: Radu Grigore
Date: Thu, 25 Oct 2012 18:02
44 lines
2634 bytes
Before I can ask my question I need to give some background. The following code fails to compile with 3.12.1:

  module type S = sig type t val x : t  end
  module M = struct type t = string let x = "Hi" end

  let f m p =
    let module X = (val m : S) in
    p X.x
  
  let () =
    f (module M : S) print_endline

The error claims that X.t would escape its scope.  However, it is possible to make it work by adding type annotations, as follows.

  module type S = sig type t val x : t  end
  module M = struct type t = string let x = "Hi" end

  let f (type a) m p =
    let module X = (val m : S with type t = a) in
    p X.x
  
  let () =
    f (module M : S with type t = string) print_endline

If type t is in a submodule, then the type annotations above become syntactically incorrect.  That is, the following doesn't parse.

  module type T = sig type t end
  module type S = sig module V : T val x : V.t  end
  module M = struct
    module V = struct type t = string end
    let x = "Hi"
  end

  let f (type a) m p =
    let module X = (val m : S with type V.t = a) in
    p X.x
  
  let () =
    f (module M : S with type V.t = string) print_endline

Without type annotations it again complains about escaping scope, because the type inference goes wrong.  Is it possible to give it the correct type for f? I would prefer not to change the modules, as those are in a library in the real code.

The type of f should be something like 
  (module S with type V.t = 'a) -> ('a -> 'b) -> 'b
Technical Details
Message-ID: <ed801ab9-cb63-4f32-95be-4a72bbb1dd60@googlegroups.com>
Path: arc1-ca.pugleaf.net!r17ni57592152qap.0!nntp.google.com!x14no8154920qar.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: c0fe4b917e,6b316d5a86de4e71,start
X-Google-Attributes: gidc0fe4b917e,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.224.180.141 with SMTP id bu13mr6459353qab.2.1351188141943;
        Thu, 25 Oct 2012 11:02:21 -0700 (PDT)
Received: by 10.52.93.229 with SMTP id cx5mr3874198vdb.19.1351188141915; Thu,
 25 Oct 2012 11:02:21 -0700 (PDT)
Path: arc1-ca.pugleaf.net!r17ni57592152qap.0!nntp.google.com!x14no8154920qar.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: fa.caml
Date: Thu, 25 Oct 2012 11:02:21 -0700 (PDT)
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=138.37.90.78; posting-account=e5mzsQoAAAB7g9y7Bkgam0zJDHRr7bCB
NNTP-Posting-Host: 138.37.90.78
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ed801ab9-cb63-4f32-95be-4a72bbb1dd60@googlegroups.com>
Subject: typing with first-class modules
From: Radu Grigore <radugrigore@gmail.com>
Injection-Date: Thu, 25 Oct 2012 18:02:21 +0000
Content-Type: text/plain; charset=ISO-8859-1