From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA04117; Fri, 19 Apr 2002 00:44:42 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA04118 for ; Fri, 19 Apr 2002 00:44:41 +0200 (MET DST) Received: from leia.mandrakesoft.com (office.mandrakesoft.com [195.68.114.34]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g3IMifb17242 for ; Fri, 19 Apr 2002 00:44:41 +0200 (MET DST) Received: by leia.mandrakesoft.com (Postfix, from userid 505) id B59E85B15; Fri, 19 Apr 2002 00:44:33 +0200 (CEST) To: james woodyatt Cc: caml-list@inria.fr Subject: Re: [Caml-list] Is a Cow an Animal? References: <538FE5B6-52FE-11D6-9686-000502DB38F5@wetware.com> From: Pixel Date: 19 Apr 2002 00:44:33 +0200 In-Reply-To: <538FE5B6-52FE-11D6-9686-000502DB38F5@wetware.com> Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk james woodyatt 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 = # 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