caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien <Damien.Pous@ens-lyon.fr>
Cc: caml-list@pauillac.inria.fr
Subject: Re: [Caml-list] polymorphic methods
Date: Thu, 13 Mar 2003 10:04:02 +0100	[thread overview]
Message-ID: <20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr> (raw)
In-Reply-To: <20030313105656R.garrigue@kurims.kyoto-u.ac.jp>

[-- Attachment #1: Type: text/plain, Size: 2980 bytes --]

On Thu, 13 Mar 2003 10:56:56 +0900
Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> wrote:

> From: Damien <Damien.Pous@ens-lyon.fr>
> 
> > Does someone know why the following class type
> > is not accepted ?
> > 
> > # class type a = object method m: 'a. (#a as 'a) -> unit end;;
> > The abbreviation #a expands to type #a but is used with type < .. >
> [...]
> Now, the serious part: can such a type be defined?
> After working a bit on this riddle, I'm afraid the answer is no.
> The reason is that there is no way to recurse on a quantified
> polymorphic method rather than on the whole object.
> The closest I found is:
>   class type a =
>     object method m : < d : 'b. (< m : 'a; .. > as 'b) -> unit > as 'a
>     end
> The dummy wrapper <d : 'b. ... > is just there to allow one to recurse
> on the polymoprhic method itself. You can call the method m by doing
> [a#m#d a'] in place of [a#m a'].
I worked with this class type, but it is a bit tedious :-(

My goal is to define a tree, whose node's type is a class
type a_t, containing a method for adding children.
but the child type can be any subtype of a_t

* this method can safely be typed [a_t->unit], but this will require a
lot of coercions in the rest of the code...

* that's why I wanted to type it ['a. (#a_t as 'a)->unit] (i.e. the
coercion is written only once, in the method)

 using your trick, I obtained the attached code. It works fine,
but that's not really a beautiful piece of code !



I don't know what do you mean when saying 
"such a type cannot be defined", in fact, I could define this 
equivalent (I think it is...) :

>#class type a_t = object 
>   method m: 'a.(<m: 'a -> unit; ..> as 'a) -> unit 
>end 

this class type definition being accepted, why is the former 
rejected ? 
both should be either rejected if this make no sense, or accepted else


then I tried to implement it, 
but I couldn't  do it "meaningfully" :

># class a: a_t = object
>   val mutable l = []
>   method m: 'a.(<m: 'a -> unit; ..> as 'a) -> unit = 
>     fun o -> l <- (o:>a_t) :: l
> end;;
>        Characters 116-117:
>      fun o -> l <- (o:>a_t) :: l
>                     ^
>This expression cannot be coerced to type
>  a_t = < m : 'a. (< m : 'a -> unit; .. > as 'a) -> unit >;
>it has type < m : 'b -> unit; .. > as 'b but is here used with type
>  'b = < m : 'c. < m : 'c -> unit; .. > -> unit; .. >.
>This simple coercion was not fully general. Consider using a double
>coercion.
>
>
># class a: a_t = object
>   val mutable l = []
>   method m: 'a.(<m: 'a -> unit; ..> as 'a) -> unit = 
>     fun o -> l <- (o:(<m: 'a -> unit; ..> as 'a):>a_t) :: l
> end;;
>
>        Characters 115-151:
>      fun o -> l <- (o:(<m: 'a -> unit; ..> as 'a):>a_t) :: l
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Type < m : 'a -> unit; .. > as 'a is not a subtype of type
>  a_t = < m : 'b. (< m : 'b -> unit; .. > as 'b) -> unit > 
>The universal variable 'b would escape its scope


any idea ?


damien

[-- Attachment #2: a_trees.ml --]
[-- Type: application/octet-stream, Size: 2031 bytes --]

(* using the trick *)

class type a_t = object
  method add: <it: 'b. (< add : 'a; children: a_t list; ga: int; .. > as 'b) -> unit > as 'a 
  method children: a_t list
  method ga: int
end

class ['a] wrap l = object
  method it: 'b. (< add : 'a; children: a_t list; ga: int; .. > as 'b) -> unit = 
    fun o -> l := (o :> <add: 'a; children: a_t list; ga: int>):: !l
end

class a: a_t = 
let children = ref [] in
let it = new wrap children in
object 
  method children = !children	      
  method add = it
  method ga = 33
end


class a1 = object 
  inherit a
  method bu = true
end

class a2 = object 
  inherit a
  method zo = ""
end

let a  = new a
let a1 = new a1
let a2 = new a2
let _ = a#add#it a
let _ = a#add#it a1
let _ = a#add#it a2
let _ = a1#add#it a
let c = a#children






(* equivalent un-implemented... *)
class type a_t = object 
  method m: 'a.(<m: 'a -> unit; ..> as 'a) -> unit 
end 

class a: a_t = object (* don't work :-( *)
  val mutable l = []
  method m: 'a.(<m: 'a -> unit; ..> as 'a) -> unit = 
    fun o -> l <- (o:>a_t) :: l
end




(* an other trick ... *)


(* container for objects of type 'b *)
class type ['b] at = object
  method b: 'b
end
(* objects contained by objects of type 'b #at *)
class type ['b] bt = object
  method al: 'b at list
  method add: 'b #at -> unit
end
(* container and contained *)
class type abt = object
  inherit [abt] at
  inherit [abt] bt
  method ga: int
end

(* implementation *)
class ['b] a b : ['b] at = object
  method b = b
end
class ['b] b : ['b] bt = object
  val mutable al = []
  method al = al
  method add : 'a. ('b #at as 'a) -> unit = fun a -> al <- (a :> 'b at)::al
end
class ab b: abt = object
  inherit [abt] a (b:>abt)
  inherit [abt] b
  method ga = 50
end

class ab1 b = object
  inherit ab b
  method bu = ""
end
class ab2 b = object
  inherit ab b
  method zo = true
end

(* tests *)
let c = new ab (Obj.magic()) (*sorry*)
let a = new ab c
let a1 = new ab1 a
let a2 = new ab2 c
let _ = c#add a
let _ = c#add a2
let _ = a#add a1
let l = c#al

  reply	other threads:[~2003-03-13  9:06 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-12 23:07 Damien
2003-03-13  0:56 ` brogoff
2003-03-13  1:56 ` Jacques Garrigue
2003-03-13  9:04   ` Damien [this message]
2003-03-13  9:27     ` Jacques Garrigue
2003-03-13 14:49       ` Damien
2003-03-13  9:41     ` Olivier Andrieu
  -- strict thread matches above, loose matches on Subject: below --
2015-12-18  9:18 Christoph Höger
2015-12-18 10:07 ` Leo White
2002-08-23 15:46 [Caml-list] Polymorphic methods Frederic Tronel
2002-08-23 17:32 ` Fred Smith
2002-08-23 18:21 ` Remi VANICAT
2002-07-14 23:16 [Caml-list] polymorphic methods nadji
2002-07-15  1:05 ` Jacques Garrigue
2002-07-15  2:08   ` Brian Smith
2002-07-15 16:24   ` nadji
2001-11-19 15:29 [Caml-list] Polymorphic methods Alain Frisch
2001-11-20  0:29 ` Jacques Garrigue
2001-11-20  9:33   ` Alain Frisch
2001-11-20 20:55   ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030313100402.02e1ce82.Damien.Pous@ens-lyon.fr \
    --to=damien.pous@ens-lyon.fr \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).