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 instance variable in object ? #8074

Closed
vicuna opened this issue Mar 19, 2003 · 4 comments
Closed

polymorphic instance variable in object ? #8074

vicuna opened this issue Mar 19, 2003 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Mar 19, 2003

Original bug ID: 1607
Reporter: administrator
Status: acknowledged
Resolution: open
Priority: normal
Severity: feature
Category: typing

Bug description

first, a feature which : could it be possible to have polymorphic
instance variable ?

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
  method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}

(* type checker crash *)

class type ct = object ('s)
  method fold : ('b -> ct -> 'b) -> 'b -> 'b
end

type t = { mutable f : 'b. ('b -> ct -> 'b) -> 'b -> 'b }

class c = object (s: #ct)
  method fold = failwith "undefined" 
  (* OK, thanks to the relaxed value restriction *)
end

class c2 = object (self: #ct)
  val v = {f = failwith "undefined"}  (* bug ? *)
  method fold = v.f
  (* initializer v.f <- ...some code using self... *)
end

This field value has type ('a -> ct -> 'a) -> 'a -> 'a
which is less general than 'b. ('b -> ct -> 'b) -> 'b -> 'b
@vicuna
Copy link
Author

vicuna commented Mar 20, 2003

Comment author: administrator

From: lvibert@irisa.fr

first, a feature which : could it be possible to have polymorphic
instance variable ?

Not trivial: one can redefine instance variables with {< v = ... >},
so this can only be done explicitly, the same way as record fields.
But why not.

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}
(* type checker crash *)
Looks like an infinite loop. I shall investigate it.

class type ct = object ('s)
method fold : ('b -> ct -> 'b) -> 'b -> 'b
end

type t = { mutable f : 'b. ('b -> ct -> 'b) -> 'b -> 'b }

class c = object (s: #ct)
method fold = failwith "undefined"
(* OK, thanks to the relaxed value restriction *)
end

There is no relation with the relaxed value restriction: a method body
is a value, this is a function implicitely taking self as parameter.

class c2 = object (self: #ct)
val v = {f = failwith "undefined"} (* bug ? )
method fold = v.f
(
initializer v.f <- ...some code using self... *)
end

This field value has type ('a -> ct -> 'a) -> 'a -> 'a
which is less general than 'b. ('b -> ct -> 'b) -> 'b -> 'b

Interesting interaction of features: with the relaxed value
restriction, [failwith "undefined"] can indeed be given the type
\forall 'a. 'a; however, in order to catch errors earlier, the type
checker first unifies the type of [failwith "unified"] with a
monomorphic instance of the expected type, before attempting
generalization. And since this type contains contravariant variables,
it is not generalizable anymore.

This can indeed be seen a bug, but I'm afraid you cannot write any
meaningful program using such a feature: [failwith "undefined"] will
be called immediately, and you will not be able to construct any
object of this class anyway.

If typing were the only problem, there is an easy workaround:
class c2 =
let dummy = failwith "undefined" in
object (self : #ct)
val v = { f = dummy }
method fold = v.f
end;;
It is typable, but generates an exception at class definition time.

The correct way to do it would be
class c2 = object (self: #ct)
val v = {f = fun _ -> failwith "undefined"}
method fold = v.f
end;;
which already types correctly.

  Jacques

@vicuna
Copy link
Author

vicuna commented Mar 26, 2003

Comment author: administrator

From: lvibert@irisa.fr

first, a feature which : could it be possible to have polymorphic
instance variable ?

(Aux developpeurs)
Quelqu'un a une opinion sur le sujet?
Les variables d'instance ressemblant beaucoup a` des champs
d'enregistrement, on peut les traiter exactement de la meme facon pour
le polymorphisme. Est-ce que ca pose un probleme?

trying to pass this restriction, I fall in the 2 following bug
(CVS version) :

class type ct = object ('s)
method fold : ('b -> 's -> 'b) -> 'b -> 'b
end

type t = {f : 'a 'b. ('b -> (#ct as 'a) -> 'b) -> 'b}

(* type checker crash *)

C'est maintenant corrige' en CVS.
Ca m'a aide' a` corriger un autre bug, merci.

Jacques

@vicuna
Copy link
Author

vicuna commented Jul 4, 2004

Comment author: administrator

Crash fixed by JG (2003-03-26).
There is a wish too (polymorphic instance variables)

@github-actions
Copy link

github-actions bot commented May 6, 2020

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.

@github-actions github-actions bot added the Stale label May 6, 2020
@github-actions github-actions bot closed this as completed Jun 5, 2020
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

1 participant