Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0001607OCamlOCaml generalpublic2003-03-19 16:132004-07-04 17:11
Reporteradministrator 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0001607: polymorphic instance variable in object ?
Descriptionfirst, 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


TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0000150)
administrator (administrator)
2003-03-20 14:30

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

(0000151)
administrator (administrator)
2003-03-26 16:29

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

(0000152)
administrator (administrator)
2004-07-04 17:11

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

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker