* Some questions about type inference
@ 2000-10-26 13:30 Benedikt Rosenau
2000-10-26 16:46 ` John Max Skaller
2000-10-26 16:56 ` Didier Remy
0 siblings, 2 replies; 3+ messages in thread
From: Benedikt Rosenau @ 2000-10-26 13:30 UTC (permalink / raw)
To: caml-list
I have played with covariance. Namely, I tried to define a class
of objects with a method "eat" who would only eat objects defining
a method "eat" themselves.
The direct approach fails:
class virtual broken_carnivore =
object
method virtual eat: <eat: _; ..> -> unit
end
since there is an unbound type variable in the ellipsis.
However, the following works (more or less):
class virtual ['a] carnivore =
object
constraint 'a = ('a) #carnivore
method virtual eat: <eat: _; ..> -> unit
end
and I can only speculate how this binds the type variable.
Another piece de resistence for my understanding is the following:
match []::[] with a::b -> a = [] & b = []
works, while
match []::[] with a::b -> a = b
gives a type error.
Benedikt
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Some questions about type inference
2000-10-26 13:30 Some questions about type inference Benedikt Rosenau
@ 2000-10-26 16:46 ` John Max Skaller
2000-10-26 16:56 ` Didier Remy
1 sibling, 0 replies; 3+ messages in thread
From: John Max Skaller @ 2000-10-26 16:46 UTC (permalink / raw)
To: Benedikt.Rosenau; +Cc: caml-list
Benedikt Rosenau wrote:
> Another piece de resistence for my understanding is the following:
> match []::[] with a::b -> a = [] & b = []
What does the '&' mean? Do you mean '@' ??
Assuming [] @ b, then this is a 'x list for some 'x,
and thus can be compared with [] which has type 'y list
for some 'y, and we can set 'x = 'y.
> works, while
> match []::[] with a::b -> a = b
> gives a type error.
That one is easy: the type of a is 'x list, and that of b is 'x list
list.
You cannot compare a 'x list with a 'x list list.
--
John (Max) Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Some questions about type inference
2000-10-26 13:30 Some questions about type inference Benedikt Rosenau
2000-10-26 16:46 ` John Max Skaller
@ 2000-10-26 16:56 ` Didier Remy
1 sibling, 0 replies; 3+ messages in thread
From: Didier Remy @ 2000-10-26 16:56 UTC (permalink / raw)
To: Benedikt.Rosenau; +Cc: caml-list
> Namely, I tried to define a class
> of objects with a method "eat" who would only eat objects defining
> a method "eat" themselves.
...
> However, the following works (more or less):
> class virtual ['a] carnivore =
> object
> constraint 'a = ('a) #carnivore
> method virtual eat: <eat: _; ..> -> unit
> end
This is correct but maybe cumbersome. You may also write:
class virtual ['a] carnivore =
object
constraint 'a = < eat: _; .. >
method virtual eat: 'a -> unit
end
;;
which may be clearer.
BTW, if you are trying variations on the well-known cow/animal example
introduced by David Shang ["Is a cow an animal?", Unpublished note January
1996] you may find a discussion of this example together with a version in
Ocaml at:
http://cristal.inria.fr/~remy/work/virtual/
>> [Section] Expressiveness and abstraction
-Didier
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-10-26 18:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-26 13:30 Some questions about type inference Benedikt Rosenau
2000-10-26 16:46 ` John Max Skaller
2000-10-26 16:56 ` Didier Remy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox