Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve type checking of applicative functors #5058

Closed
vicuna opened this issue May 20, 2010 · 4 comments
Closed

improve type checking of applicative functors #5058

vicuna opened this issue May 20, 2010 · 4 comments

Comments

@vicuna
Copy link

vicuna commented May 20, 2010

Original bug ID: 5058
Reporter: sweeks
Status: acknowledged (set by @mshinwell on 2010-05-24T08:16:19Z)
Resolution: open
Priority: normal
Severity: feature
Target version: later
Category: typing
Related to: #3476 #6467
Monitored by: @ygrek

Bug description

Issue #3476 identified a weakness in the type checking of applicative functors. It was marked as "won't fix" back in 2006. However, in March 2007, on the OCaml list, Derek Dreyer had a proposed improvement to the type system that would fix some such problems, and which Xavier thought was both sound and not too hard to implement.

Here's the simplest example I know of the problem.

module F (M : sig end) : sig type t end = struct type t = int end
module T = struct
module M = struct end
include F(M)
end
include T
let f (x : t) : T.t = x (* type error -- OCaml doesn't know that [t = T.t] *)

This problem does crop up from time to time, and it would save repeated rediscovery and workarounds if it got fixed. I am creating this issue to see if the solution could be reconsidered. Thanks.

Here's what Xavier said back in March 2007.

Very interesting. So now, looking back at Xavier's POPL'95 paper on
applicative functors, I see what he means by saying it's a fundamental
problem with how applicative functors work in OCaml. I.e. it's not
just a bug in the typechecker, but in the type system in the original
paper. In particular, the definition of signature strengthening on
page 7 of that paper includes the following case:

(module x_i : M; S)/p =
module x_i : (M/p.x); S/p

But I believe this is a mistake, and instead of S/p it should be
(S{x_i <- p.x})/p

Thanks for this very interesting suggestion.

As Derek knows, there are two deep limitations in the syntactic
type system for modules used in OCaml, namely

  1. at most one type equality can be recorded per type declaration, and
  2. structure equality is not tracked, therefore the types F(M).t and F(N).t
    (assuming t is abstract in F's result signature) are distinct even if
    N is an alias for M.

I managed to convince myself that the problems with applicative functors
discussed e.g. in #3476 cannot be solved without lifting one of
these limitations, which is something I don't know how to do (neither
theoretically nor implementation-wise).

Derek's suggestion seems to prove me wrong. The two definitions of
signature strengthening (the one in my papers, used in OCaml, and
Derek's proposal) appear to have the same expressive power in a system
with generative functors, but Derek's definition is definitely
stronger in conjunction with applicative functors.

In other words, first replace references to x_i (esp. in functor
applications in types) inside S with references to p.x, and then
proceed with selfification as usual. Is there some reason this would
not work or would be difficult to implement?

Difficult to implement: I don't think so. Would not work: some formal
soundness argument would be nice of course :-), but there is a
strong intuition that at the point of the ";" in the equation above,
the two paths "x_i" and "p.x" carry the same amount of typing information,
so replacing the latter by the former appears sound.

@vicuna
Copy link
Author

vicuna commented May 24, 2010

Comment author: @xavierleroy

For the record: I implemented Dreyer's alternate strengthening algorithm at that time, but it turned out to be incomparable with the current strengthening algorithm: some examples that fail today are accepted with the alternate algo, but some examples that typecheck today (IIRC, the ocamlgraph library) are rejected.

In the particular example given, the best thing to do is perhaps to turn applicative functors off entirely, using the -no-app-funct option that is being introduced in 3.12.

@vicuna
Copy link
Author

vicuna commented Mar 30, 2015

Comment author: @lpw25

This example now works in 4.02.1 because of module aliases.

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@Octachron
Copy link
Member

Since the example works (due to module aliases lifting the restriction 2), I am closing the issue. See #9566 for adding the corresponding examples to the testsuite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants