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

Inconsistent order when typing constraint patterns #7344

Closed
vicuna opened this issue Sep 5, 2016 · 3 comments
Closed

Inconsistent order when typing constraint patterns #7344

vicuna opened this issue Sep 5, 2016 · 3 comments
Assignees
Milestone

Comments

@vicuna
Copy link

vicuna commented Sep 5, 2016

Original bug ID: 7344
Reporter: @lpw25
Assigned to: @garrigue
Status: resolved (set by @garrigue on 2017-03-15T01:56:44Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.03.0
Target version: 4.05.0 +dev/beta1/beta2/beta3/rc1
Fixed in version: 4.06.0 +dev/beta1/beta2/rc1
Category: typing
Related to: #7389
Monitored by: @gasche @hcarty

Bug description

The following code does not type-check:

let rec f : unit -> < m: 'a. 'a -> 'a> =

  fun () ->
    let x = f () in
    ignore (x#m 1);
    ignore (x#m "hello");
    assert false;;

Characters 117-124:
ignore (x#m "hello");
^^^^^^^
Error: This expression has type string but an expression was expected of type
int

but it does if you add parentheses:

let rec (f : unit -> < m: 'a. 'a -> 'a>) =

  fun () ->
    let x = f () in
    ignore (x#m 1);
    ignore (x#m "hello");
    assert false;;

val f : unit -> < m : 'a. 'a -> 'a > =

or if you add an unused polymorphic variable:

let rec f : 'b. unit -> < m: 'a. 'a -> 'a> =

  fun () ->
    let x = f () in
    ignore (x#m 1);
    ignore (x#m "hello");
    assert false;;

val f : unit -> < m : 'a. 'a -> 'a > =

or if you turn on -principal:

#principal true;;

let rec f : unit -> < m: 'a. 'a -> 'a> =

  fun () ->
    let x = f () in
    ignore (x#m 1);
    ignore (x#m "hello");
    assert false;;

val f : unit -> < m : 'a. 'a -> 'a > =

@vicuna
Copy link
Author

vicuna commented Nov 15, 2016

Comment author: tadeuzagallo

I understand the underlying problem is the one mentioned in the related issue, but would it make sense to fix this issue at the parsing level? I was playing with it, and unrolling fun_binding into let_binding_body and moving the constraint to the left-hand side of the binding would fix it. I don't really have any context here, so I'm sorry if that's too hacky, but I was browsing through and thought I'd mention as it'd be a tiny change.

@vicuna
Copy link
Author

vicuna commented Nov 15, 2016

Comment author: @gasche

The problem is that for some other let-bindings of the form "let x : t = e", the current interpretation as "let x = (e : t)" is the right one, it allows you to type-check more programs (this also depends on whether the binding is recursive; see #7389 for more discussion). In -principal mode, type-checking is made more robust by inferring less from the annotation, but then it actually requires more annotations.

@vicuna
Copy link
Author

vicuna commented Mar 15, 2017

Comment author: @garrigue

Fixed by commit fd0dc6a.
The type annotation is duplicated on the pattern in the first case (it is also left on the expression for propagation on both sides).
The only cases where propagation does not occur on the expression side are the second one (annotation on a parenthesized pattern), and the 3rd case (polymorphic type with variables limited to the annotations). The "let rec f : type a. ... = ..." already propagates on both sides.

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