Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Is a Cow an Animal?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pixel <pixel@m...>
Subject: Re: [Caml-list] Is a Cow an Animal?
james woodyatt <jhw@wetware.com> writes:

[...]

> I borrowed a technique I learned here from Brian Rogoff that uses an abstract
> type with a contravariant type parameter for passing the type of energy a food
> object contains to an animal object that can eat it.  (I wish I were smart
> enough to know the name for this technique.)

well, i don't really understand the """type 'diet energy = int""" stuff.

but neither is my ocaml's:

# let carrot = new carrot 2 ;;
# let cow = new cow 10 ;;
# let _ = cow#feed carrot#energy ;;

# cow#feed;;
- : [ `E_cow] energy -> cow = <fun>
# carrot#energy;;
- : [ `E_rabbit | `E_human] energy = 2
# (carrot#energy : [ `E_cow ] energy);;
- : [ `E_cow] energy = 2

what am i missing?


anyway i do understand the trick:

(* instead of having the animal tied with its accepted_food, *)
(* have the food tied with its eaters *)

which allow both "a_human#eat a_beef" and "a_human#eat carrot" with no
upcasting.


but... trying... thinking... trying... I really can't make this to work :-(



> I also used the functional style because the problem statement had some
> language about ensuring that animals are never slaughtered twice.  You really
> can't do that at compile time in Caml, but you could pretty easily modify the
> code I present below so that it raises Failure if the 'consume' method is
> called on the same meat object more than once, or if an energy value is fed to
> more than one animal.

i've added a functional version (ocaml5) which checks 10/11 should_fail.

the last check (a_beef is already eaten) i can't manage to achieve because i
can't overload "eat" on vegetable vs meat. I could have eat_vegetable and
eat_meat, but that would break the (somewhat implicit) rules of the game.


thanks!
-------------------
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