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

Polymorphic field values no longer generalizable with (type t) #5790

Closed
vicuna opened this Issue Oct 15, 2012 · 3 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link
Collaborator

commented Oct 15, 2012

Original bug ID: 5790
Reporter: lealanko
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2015-07-24T08:39:03Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.00.1
Fixed in version: 4.00.0
Category: typing
Monitored by: @hcarty

Bug description

Previously, in 3.12, one could use "fun (type t) ->" in front of an expression to keep it general enough for assignment in a polymorphic field:

    Objective Caml version 3.12.1

type t = { x : 'r . 'r -> 'r };;

type t = { x : 'a. 'a -> 'a; }

let id x = x;;

val id : 'a -> 'a =

{ x = id id };;

Error: This field value has type 'a -> 'a which is less general than
'b. 'b -> 'b

{ x = fun (type r) -> id id };;

  • : t = {x = }

However, in 4.00 this no longer works:

    OCaml version 4.00.1

type t = { x : 'a . 'a -> 'a };;

type t = { x : 'a. 'a -> 'a; }

let id x = x;;

val id : 'a -> 'a =

{ x = fun (type a) -> id id };;

Error: This field value has type 'b -> 'b which is less general than
'a. 'a -> 'a

In my actual code, the field does not have a function type, so eta-expansion is not an option.

Steps to reproduce

type t = { x : 'r . 'r -> 'r }
let id x = x
let _ = { x = fun (type r) -> id id }

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 16, 2012

Comment author: @garrigue

Actually, the wrong one here is 3.12.1.
This code is clearly unsound.
Here is an example accepted by 3.12.1 but refused by 4.00.0

type t = { x : 'r . 'r -> 'r };;
let id x = x;;
let id' f = let r = ref [] in
fun x -> let y = if !r = [] then x else List.hd !r in r := [x]; f y;;
let t = { x = fun (type r) -> id' id };;
t.x 3;;
t.x true;;

(Corrected the example)

Is it really necessary to fix this in 3.12 too ?

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 16, 2012

Comment author: @ygrek

ftr, example obviously meant
let t = { x = fun (type r) -> id' id };;

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 16, 2012

Comment author: lealanko

Ah, right, the value restriction. I've been doing too much Haskell, already forgot about it. :) This poses a bit of a problem for me, but that's the price of imperative features. Sorry about the false alert.

@vicuna vicuna closed this Jul 24, 2015

@vicuna vicuna added the typing label Mar 14, 2019

@vicuna vicuna added the bug label Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.