caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Object typing
@ 2005-07-09 23:55 Matthew O'Connor
  2005-07-10  0:20 ` [Caml-list] " Pietro Abate
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Matthew O'Connor @ 2005-07-09 23:55 UTC (permalink / raw)
  To: caml-list

Hello all, I'm in the process of writing a cellular agent-based immune
system simulator.  It uses objects as the agents/entities.  I have a
hierarchy of different entity types (ie, entity, cell, bacteria, virus,
antigen, etc.).  I want to be able to determine, at run time, if a given
entity is of a given type.

My current solution is to use functors to instantiate a weak set for all
the types I care about and then have that type's initializer add itself
to the set.  Then given an object it is simple to determine if it is a
member of that set.  This solution works, but it seems to be rather
slow.  I want to speed this process up.

Here is a version of what I would like to be able to do, but can't or
don't know how.  I would like to use polymorphic variants to enable
easier extensibility of the hierarchy.

(* test.ml *)
class type ientity =
object
  method is_a : [> ] -> bool
end

class entity : ientity =
object
  method is_a v =
    if v = `Entity then true
    else false
end

class predator =
object
  inherit entity as super
  method is_a v =
    if v = `Predator then true
    else super#is_a v
end

let ent = new predator;;
ent#is_a `Predator;; (* returns true *)
ent#is_a `Entity;; (* returns true *)
ent#is_a `Chicken;; (* returns false *)

(* ocaml test.ml *)
File "test.ml", line 7, characters 0-72:
The class type object method is_a : [> `Entity ] -> bool end
is not matched by the class type ientity
The class type object method is_a : [> `Entity ] -> bool end
is not matched by the class type object method is_a : [>  ] -> bool end
The method is_a has type [> `Entity ] -> bool but is expected to have type
  'a. ([>  ] as 'a) -> bool

I'm at a loss as how to proceed.  Any other suggestions or ideas or
better solutions would be greatly appreciated.  Thank you.


Matt


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

* Re: [Caml-list] Object typing
  2005-07-09 23:55 Object typing Matthew O'Connor
@ 2005-07-10  0:20 ` Pietro Abate
  2005-07-10  6:19   ` Matthew O'Connor
  2005-07-10 18:25 ` Stephane Glondu
  2005-07-10 22:06 ` Christophe TROESTLER
  2 siblings, 1 reply; 9+ messages in thread
From: Pietro Abate @ 2005-07-10  0:20 UTC (permalink / raw)
  To: caml-list, caml-list

On Sat, Jul 09, 2005 at 05:55:24PM -0600, Matthew O'Connor wrote:
> I'm at a loss as how to proceed.  Any other suggestions or ideas or
> better solutions would be greatly appreciated.  Thank you.

you can try to restrict the polimorphy variant type (and maybe 
having more than one type to make the hierarchy explicit).

hope this helps.
:)
p

type t = [ `Predator | `Entity | `Chicken ];;

class type ientity =
object
method is_a : t -> bool
end

class entity : ientity =
object
  method is_a = function
  |`Entity -> true
  |#t -> false
end

class predator =
object
  inherit entity as super
  method is_a = function
  |`Predator -> true
  |#t as v -> super#is_a v
end

let ent = new predator;;
print_endline (string_of_bool (ent#is_a `Predator));; (* returns true *)
print_endline (string_of_bool (ent#is_a `Entity));; (* returns true *)
print_endline (string_of_bool (ent#is_a `Chicken));; (* returns false *)


-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


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

* Re: [Caml-list] Object typing
  2005-07-10  0:20 ` [Caml-list] " Pietro Abate
@ 2005-07-10  6:19   ` Matthew O'Connor
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew O'Connor @ 2005-07-10  6:19 UTC (permalink / raw)
  To: caml-list

Pietro Abate wrote:
> you can try to restrict the polimorphy variant type (and maybe 
> having more than one type to make the hierarchy explicit).

Restricting the type to a fixed variant is an option that I would like
to stay away from as it would require the base entity class to know of
all possible subtypes.  (Yes, this could be solved using the module
system, but I'd rather not.)  Thanks, though.


Matt



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

* Re: [Caml-list] Object typing
  2005-07-09 23:55 Object typing Matthew O'Connor
  2005-07-10  0:20 ` [Caml-list] " Pietro Abate
@ 2005-07-10 18:25 ` Stephane Glondu
  2005-07-10 22:06 ` Christophe TROESTLER
  2 siblings, 0 replies; 9+ messages in thread
From: Stephane Glondu @ 2005-07-10 18:25 UTC (permalink / raw)
  To: caml-list

On Saturday 09 July 2005 16:55, Matthew O'Connor wrote:
> Here is a version of what I would like to be able to do, but can't or
> don't know how.  I would like to use polymorphic variants to enable
> easier extensibility of the hierarchy.
>
> [...]
>
> I'm at a loss as how to proceed.  Any other suggestions or ideas or
> better solutions would be greatly appreciated.  Thank you.

How about:

(* test.ml *)
class ['a] entity =
object
  method is_a (v : 'a) =
    if v = `Entity then true
    else false
end

class ['a] predator =
object
  inherit ['a] entity as super
  method is_a v =
    if v = `Predator then true
    else super#is_a v
end

let ent = new predator;;
ent#is_a `Predator;; (* returns true *)
ent#is_a `Entity;; (* returns true *)
ent#is_a `Chicken;; (* returns false *)


Stephane Glondu.


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

* Re: [Caml-list] Object typing
  2005-07-09 23:55 Object typing Matthew O'Connor
  2005-07-10  0:20 ` [Caml-list] " Pietro Abate
  2005-07-10 18:25 ` Stephane Glondu
@ 2005-07-10 22:06 ` Christophe TROESTLER
  2005-07-11  6:23   ` Matthew O'Connor
  2005-07-11  7:18   ` Remi Vanicat
  2 siblings, 2 replies; 9+ messages in thread
From: Christophe TROESTLER @ 2005-07-10 22:06 UTC (permalink / raw)
  To: angagon; +Cc: caml-list

On Sat, 09 Jul 2005, "Matthew O'Connor" <angagon@earthlink.net> wrote:
> 
> Here is a version of what I would like to be able to do, but can't or
> don't know how.  I would like to use polymorphic variants to enable
> easier extensibility of the hierarchy.

To understand what does not work, try

class entity  =
object
  method is_a v = (v = `Entity)
end

You get an error message of which the important part is "The method
is_a has type ([> `Entity ] as 'a) -> bool where 'a is unbound".  It
says that the method [is_a] is of type 'a -> bool with a constraint on
'a.  Since you do not want a method of type (forall 'a) 'a -> bool
(written "'a. ([> ] as 'a) -> bool" in your error message), you
parametrize the class by that type 'a (the rest is in interactive
mode):

# class ['a] entity =
object
  method is_a (v:'a) = (v = `Entity)
end;;
  class ['a] entity :
  object constraint 'a = [> `Entity ] method is_a : 'a -> bool end

# class ['a] predator =
object
  inherit ['a] entity as super
  method is_a (v:'a) = (v = `Predator) || super#is_a v
end;;
  class ['a] predator :
  object
    constraint 'a = [> `Entity | `Predator ]
    method is_a : 'a -> bool
  end

# let ent = new predator;;
val ent : _[> `Entity | `Predator ] predator
# ent#is_a `Predator;;
- : bool = true
# ent#is_a `Entity;;
- : bool = true
# ent#is_a `Chicken;;
- : bool = false


P.S.  Notice the "_" in "_[> `Entity | `Predator ]".  It means that
the full type is yet to be determined.  Close it before the end of
your program.


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

* Re: [Caml-list] Object typing
  2005-07-10 22:06 ` Christophe TROESTLER
@ 2005-07-11  6:23   ` Matthew O'Connor
  2005-07-11  7:18   ` Remi Vanicat
  1 sibling, 0 replies; 9+ messages in thread
From: Matthew O'Connor @ 2005-07-11  6:23 UTC (permalink / raw)
  To: caml-list

Christophe TROESTLER wrote:
> On Sat, 09 Jul 2005, "Matthew O'Connor" <angagon@earthlink.net> wrote:
> 
>>Here is a version of what I would like to be able to do, but can't or
>>don't know how.  I would like to use polymorphic variants to enable
>>easier extensibility of the hierarchy.
> 
> 
> To understand what does not work, try
> 
> class entity  =
> object
>   method is_a v = (v = `Entity)
> end
> 
> You get an error message of which the important part is "The method
> is_a has type ([> `Entity ] as 'a) -> bool where 'a is unbound".  It
> says that the method [is_a] is of type 'a -> bool with a constraint on
> 'a.  Since you do not want a method of type (forall 'a) 'a -> bool
> (written "'a. ([> ] as 'a) -> bool" in your error message), you
> parametrize the class by that type 'a (the rest is in interactive
> mode):

Ahh, thank you for the explanation.

> # class ['a] entity =
> object
>   method is_a (v:'a) = (v = `Entity)
> end;;
>   class ['a] entity :
>   object constraint 'a = [> `Entity ] method is_a : 'a -> bool end

Yes, this is exactly what I was looking for.  Again, thanks to all who
answered.


Matt


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

* Re: [Caml-list] Object typing
  2005-07-10 22:06 ` Christophe TROESTLER
  2005-07-11  6:23   ` Matthew O'Connor
@ 2005-07-11  7:18   ` Remi Vanicat
  2005-07-11  8:22     ` Jacques Garrigue
  1 sibling, 1 reply; 9+ messages in thread
From: Remi Vanicat @ 2005-07-11  7:18 UTC (permalink / raw)
  To: caml-list

2005/7/11, Christophe TROESTLER <debian00@tiscali.be>:
[...]

> # class ['a] entity =
> object
>   method is_a (v:'a) = (v = `Entity)
> end;;
>   class ['a] entity :
>   object constraint 'a = [> `Entity ] method is_a : 'a -> bool end
>
[...]

> P.S.  Notice the "_" in "_[> `Entity | `Predator ]".  It means that
> the full type is yet to be determined.  Close it before the end of
> your program.

Notice that one can do better than this :
# class [+'a] entity =
object
 method is_a (v:'a) = (v = `Entity)
end;;
     class ['a] entity :
  object constraint 'a = [> `Entity ] method is_a : 'a -> bool end
# let e = new entity;;
val e : [> `Entity ] entity = <obj>

now we don't have an _ type, and there is no need to close it before
the end of the program.

(Ps: sorry Christophe for the private mail, I used the wrong button)


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

* Re: [Caml-list] Object typing
  2005-07-11  7:18   ` Remi Vanicat
@ 2005-07-11  8:22     ` Jacques Garrigue
  2005-07-11  9:16       ` Marcin 'Qrczak' Kowalczyk
  0 siblings, 1 reply; 9+ messages in thread
From: Jacques Garrigue @ 2005-07-11  8:22 UTC (permalink / raw)
  To: remi.vanicat; +Cc: caml-list

From: Remi Vanicat <remi.vanicat@gmail.com>

> Notice that one can do better than this :
> # class [+'a] entity =
> object
>  method is_a (v:'a) = (v = `Entity)
> end;;
>      class ['a] entity :
>   object constraint 'a = [> `Entity ] method is_a : 'a -> bool end
> # let e = new entity;;
> val e : [> `Entity ] entity = <obj>
> 
> now we don't have an _ type, and there is no need to close it before
> the end of the program.

Thank you for finding a serious bug in the variance check!
Didn't it strike you as strange that a method parameter could be
declared with a covariant type? This goes clearly against the
definition.
The problem was that the variance check omitted the row-variable in
variants, so that if a variant constraint is used on a type parameter,
any variance is accepted...

After fixing this in CVS for 3.08.4, you get rightfully:
  In this definition, a type variable has a variance that is not reflected
  by its occurence in type parameters.
or in 3.09
  In this definition, expected parameter variances are not satisfied

If you really want to make it covariant, you should define is_a out of
the class:

# class [+'a] entity =
  object
    method classes : 'a = [`Entity]
  end;;
class ['a] entity :
  object constraint 'a = [> `Entity ] list method classes : 'a end
# let e = new entity;;
val e : [> `Entity ] list entity = <obj>
# let is_a ob cl = List.mem cl ob#classes;;
val is_a : < classes : 'a list; .. > -> 'a -> bool = <fun>

(And be careful about the above bug before 3.08.4.)

Jacques Garrigue

P.S. I really think that the variance check is now OK, but if you find
strange behaviours in typing, please report them. Particularly when
something that shouldn't type is accepted.


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

* Re: [Caml-list] Object typing
  2005-07-11  8:22     ` Jacques Garrigue
@ 2005-07-11  9:16       ` Marcin 'Qrczak' Kowalczyk
  0 siblings, 0 replies; 9+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2005-07-11  9:16 UTC (permalink / raw)
  To: caml-list

Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> writes:

> After fixing this in CVS for 3.08.4, you get rightfully:
>   In this definition, a type variable has a variance that is not reflected
>   by its occurence in type parameters.

It's spelled "occurrence".

There are other "occurences" in OCaml sources.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


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

end of thread, other threads:[~2005-07-11  9:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-09 23:55 Object typing Matthew O'Connor
2005-07-10  0:20 ` [Caml-list] " Pietro Abate
2005-07-10  6:19   ` Matthew O'Connor
2005-07-10 18:25 ` Stephane Glondu
2005-07-10 22:06 ` Christophe TROESTLER
2005-07-11  6:23   ` Matthew O'Connor
2005-07-11  7:18   ` Remi Vanicat
2005-07-11  8:22     ` Jacques Garrigue
2005-07-11  9:16       ` Marcin 'Qrczak' Kowalczyk

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