caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic methods and ellipsis
@ 2002-09-21  0:26 Alessandro Baretta
  2002-09-21  0:31 ` Pixel
  0 siblings, 1 reply; 7+ messages in thread
From: Alessandro Baretta @ 2002-09-21  0:26 UTC (permalink / raw)
  To: Ocaml; +Cc: Jacques Garrigue

I'm trying to write a polymorphic method that will accept as 
a parameter any instance of any subclass of given class. For 
example

class parent = object
   method foo = "Hello "
   method bar = "World!"
end

class son = object
   inherit parent
   method buz = "Cheers"
end

class salutation : object
   method greet : 'a. (#parent as 'a) -> string
end = object
   method greet obj = obj # foo ^ obj # bar
end


But of course, this does not work. What is problem? How can 
I get a method with a polymorphic ellipsis in its type?

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-21  0:26 [Caml-list] Polymorphic methods and ellipsis Alessandro Baretta
@ 2002-09-21  0:31 ` Pixel
  2002-09-21  8:35   ` Alessandro Baretta
  0 siblings, 1 reply; 7+ messages in thread
From: Pixel @ 2002-09-21  0:31 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml, Jacques Garrigue

Alessandro Baretta <alex@baretta.com> writes:

> I'm trying to write a polymorphic method that will accept as a parameter any
> instance of any subclass of given class. For example

what about the simpler:

class salutation = object
   method greet : 'a. (#parent as 'a) -> string = fun obj -> obj # foo ^ obj # bar
end

which works :)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-21  0:31 ` Pixel
@ 2002-09-21  8:35   ` Alessandro Baretta
  2002-09-22  9:07     ` Jacques Garrigue
  0 siblings, 1 reply; 7+ messages in thread
From: Alessandro Baretta @ 2002-09-21  8:35 UTC (permalink / raw)
  To: Ocaml


Pixel wrote:
> Alessandro Baretta <alex@baretta.com> writes:
> 
> 
>>I'm trying to write a polymorphic method that will accept as a parameter any
>>instance of any subclass of given class. For example
> 
> 
> what about the simpler:
> 
> class salutation = object
>    method greet : 'a. (#parent as 'a) -> string = fun obj -> obj # foo ^ obj # bar
> end
> 
> which works :)

True. I remember trying something of this kind last night, 
but I probably did not use the fun construct to define the 
obj parameter, so it did not work.

At any rate, why does my technique not work?

class salutation : object
   method greet : 'a. (#parent as 'a) -> string
end = object
   method greet obj -> obj # foo ^ obj # bar
end


Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-21  8:35   ` Alessandro Baretta
@ 2002-09-22  9:07     ` Jacques Garrigue
  2002-09-22  9:39       ` Alessandro Baretta
  0 siblings, 1 reply; 7+ messages in thread
From: Jacques Garrigue @ 2002-09-22  9:07 UTC (permalink / raw)
  To: alex; +Cc: caml-list

From: Alessandro Baretta <alex@baretta.com>
> At any rate, why does my technique not work?
> 
> class salutation : object
>    method greet : 'a. (#parent as 'a) -> string
> end = object
>    method greet obj -> obj # foo ^ obj # bar
> end

You can see the above (and it is more or less type the same way) as

class salutation0 = object
   method greet obj -> obj # foo ^ obj # bar
end

class salutation : object
   method greet : 'a. (#parent as 'a) -> string
end = salutation0

So you see that your constraint is applied too late: salutation0
itself is not typable.
I though at some time of attempting to make this work, since something
like that is already done for recursive functions for instance, but
that's not regular. In fact, you would probably end up wanting to
write the implementation in the .ml and the interface in the .mli, and
this would not work anyway.
So, there is a deep problem about which constraints apply
where. The examples in the tutorial give you the way to do that
without ambiguity.

        Jacques
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-22  9:07     ` Jacques Garrigue
@ 2002-09-22  9:39       ` Alessandro Baretta
  2002-09-22 17:09         ` brogoff
  0 siblings, 1 reply; 7+ messages in thread
From: Alessandro Baretta @ 2002-09-22  9:39 UTC (permalink / raw)
  To: Jacques Garrigue, Ocaml



Jacques Garrigue wrote:
> From: Alessandro Baretta <alex@baretta.com>
> 
>>At any rate, why does my technique not work?
>>
>>class salutation : object
>>   method greet : 'a. (#parent as 'a) -> string
>>end = object
>>   method greet obj -> obj # foo ^ obj # bar
>>end
> 
> 
> You can see the above (and it is more or less type the same way) as
> 
> class salutation0 = object
>    method greet obj -> obj # foo ^ obj # bar
> end
> 
> class salutation : object
>    method greet : 'a. (#parent as 'a) -> string
> end = salutation0
> 
> So you see that your constraint is applied too late: salutation0
> itself is not typable.

Oh! This is curious. You are telling me that the type 
constraints defined in the class type are applied once and 
for all *after* the class implementation has been typed? 
This would make sense after all, given that the class type 
and the class implementation are two distinct syntactic and 
semantic entities in O'Caml. This is not something I would 
have thought of on my own.

> I though at some time of attempting to make this work, since something
> like that is already done for recursive functions for instance, but
> that's not regular. In fact, you would probably end up wanting to
> write the implementation in the .ml and the interface in the .mli, and
> this would not work anyway.

This is not really a problem. I can live with locally 
applied constraints. I just did not realize why the my 
attempt did not work.

Let me suggest another idea where you might better spend 
your time. I can write
 > let f x : 'a -> 'a = x
but I cannot write
method f x : 'a . 'a -> 'a = x
I have to use the more cumbersome
method f : 'a .'a -> 'a = fun x -> x

The ability to type check correctly the first form of method 
declaration is an improvement I would appreciate. While not 
terriblly important, this is something that might be worth 
your time for some future release.

> So, there is a deep problem about which constraints apply
> where. The examples in the tutorial give you the way to do that
> without ambiguity.
> 
>         Jacques

Thank you very much for your explanations, Jacques. It is a 
pleasure to work with a language where I continually get to 
learn new, conceptually interesting ideas. I'd get bored to 
death otherwise.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-22  9:39       ` Alessandro Baretta
@ 2002-09-22 17:09         ` brogoff
  2002-09-24  9:04           ` Alessandro Baretta
  0 siblings, 1 reply; 7+ messages in thread
From: brogoff @ 2002-09-22 17:09 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml

On Sun, 22 Sep 2002, Alessandro Baretta wrote:
> Let me suggest another idea where you might better spend 
> your time. I can write
>  > let f x : 'a -> 'a = x

Does this do what you think it does? What is the type of f?

Incidentally, I'd prefer some form of separate, forward type declaration.

-- Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Caml-list] Polymorphic methods and ellipsis
  2002-09-22 17:09         ` brogoff
@ 2002-09-24  9:04           ` Alessandro Baretta
  0 siblings, 0 replies; 7+ messages in thread
From: Alessandro Baretta @ 2002-09-24  9:04 UTC (permalink / raw)
  To: brogoff; +Cc: Ocaml



brogoff@speakeasy.net wrote:
> On Sun, 22 Sep 2002, Alessandro Baretta wrote:
> 
>>Let me suggest another idea where you might better spend 
>>your time. I can write
>> > let f x : 'a -> 'a = x
> 
> 
> Does this do what you think it does? What is the type of f?
> 
> Incidentally, I'd prefer some form of separate, forward type declaration.
> 
> -- Brian
> 
> 

Nope. Right, the colon-type-equals construct is for the 
definition of the return type, as opposed the actual type of 
f. Let me then rework my suggestion as

method f (x:'a) : 'a.'a = x

which is now actually equivalent to

let f (x:'a) : 'a = x

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-09-24  8:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-21  0:26 [Caml-list] Polymorphic methods and ellipsis Alessandro Baretta
2002-09-21  0:31 ` Pixel
2002-09-21  8:35   ` Alessandro Baretta
2002-09-22  9:07     ` Jacques Garrigue
2002-09-22  9:39       ` Alessandro Baretta
2002-09-22 17:09         ` brogoff
2002-09-24  9:04           ` Alessandro Baretta

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).