-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Extensible variant types and scope escaping #6752
Comments
Comment author: @zoggy Adding a type annotation in the Common module: let (q : msg Queue.t) = Queue.create () solves the problem. The error message is quite confusing, though. |
Comment author: @gasche I believe there is a bug in the type-checker: it seems to believe that Your workaround is correct (it removes the weak variable: pass The reason why I believe the type-checker is wrong about the ordering is that the following variant, which is careful to define the extensible type outside the module A = struct type msg = .. end
module Common =
struct
type msg = A.msg = ..
let handle_msg = ref (fun _ -> failwith "Unable to handle message")
let extend_handle f =
let old = !handle_msg in
handle_msg := f old
let q = Queue.create ()
let add msg = Queue.add msg q
let handle_queue_messages () = Queue.iter !handle_msg q
end
module M1 =
struct
type Common.msg += Reload of string | Alert of string
let handle fallback = function
Reload s -> print_endline ("Reload "^s)
| Alert s -> print_endline ("Alert "^s)
| x -> fallback x
let () = Common.extend_handle handle
let () = Common.add (Reload "config.file")
let () = Common.add (Alert "Initialisation done")
end |
Comment author: @lpw25 This is not related to extensible variants, the same thing happens for regular variants:
gives the error:
Nor do I think it is a bug in the type-checker. It is simply a more involved example of:
You have a monomorphic type variable and you are unifying it with a type which is defined after the monomorphic type variable was created. It looks odd in this case because |
Comment author: @garrigue Note first that there is a simple workaround: Whether this is a bug or not is not completely clear to me: we would like it to work for principality, but non-generalized type variables already break principality at times. |
Comment author: @garrigue Fixed in trunk at revision 15783. The fix is to create the identifier for the module before typing it, so that references inside the module are recognized as being created after it. |
Comment author: @yallop I'm not sure about creating the module identifier in advance. It seems that we end up with invalid signatures, at least with the current implementation: $ cat m.ml let () = M.x := Some M.T |
Comment author: @garrigue Good point. However, this is only one case of such signatures, and this is not unsound. Do you see any case where this use of references could cause unsoundness? |
Comment author: @yallop I'm not sure I understand the original purpose of the restriction. Would it be unsound in general to allow unification with types that are defined later in a file? I thought that the restriction was to prevent ill-formedness rather than unsoundness, but I could well be mistaken. I don't see that there's anything about the case of types defined within a named module that warrants special treatment, though. |
Comment author: @garrigue After a private discussion with Alain Frisch and Nicolas Ojeda Bar, it appears that Jeremy's concerns are not purely theoretical: there are practical use cases where well-formedness is required (i.e., to generate type witnesses). So I reverted to strict scoping in commit e85ba9c. Also note that this does not mean reverting the original commits: I have a feeling that the original behavior only worked by chance (cf #7152). |
Original bug ID: 6752
Reporter: @zoggy
Assigned to: @garrigue
Status: resolved (set by @garrigue on 2015-01-17T04:01:34Z)
Resolution: fixed
Priority: normal
Severity: tweak
Version: 4.02.1
Target version: 4.02.3+dev
Fixed in version: 4.03.0+dev / +beta1
Category: typing
Related to: #7152 #7313 #7401 #7414
Monitored by: @gasche @hcarty
Bug description
Here is some code for an exercise:
Compiling it gives an unexpectec (at least for me) error:
The indicated line is
I've encountered (and understood) such an error before, but not with extensible types.
The text was updated successfully, but these errors were encountered: