🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #2116

Object Prolog

#2116
From: Harry Virtanen INF
Date: Mon, 16 Nov 1992 10:32
88 lines
3348 bytes
Hi!

There has been some discussion on Object Prolog in this newsgroup. I have
programmed (in prolog), just for the fun of it, an Object Prolog interpreter
that at the moment supports:

1) an object with its associated set of methods and attributes.
2) "dynamic objects"
3) a demon 'demon(Object,Attribute)' which, if it exists, will react to a 
   change in the value of the Attribute in the Object.
4) a special object 'myself' which enables to send a message from a method to
   the object in which the method was declared.
5) a demon called 'init' which, if present in the set of methods of an object,
   will be activated when a new instance of the object is created.
6) an inheritance network
7) multiple inheritance. An object may inherit the methods and attributes from
   several parent objects.
8) message passing between objects. Search for an appropriate method is
   depth-first.
9) Objects as values to attributes. Does not however support recursive 
   attributes, nor two objects that refer to each other through their 
   attributes.

and here is a stupid example:

animal
     with_attributes
         [(age:0)]
     with_methods
         [(setage(Age):-
                 age:Age),
          (show:-
                 age:Age, write(Age),nl)].

bird
     with_attributes
         [(skin:feathers),
          (name:unknown)]
     with_methods
         [(init:-
                 write('A bird has been born'),nl),
          (fly(Me,Tree):-
                 write('Im flying to a tree'),nl,
                 ontree(Me) send_to Tree)].
bird is_a animal.

tree
     with_attributes
         [(inhabitant:bird)]
     with_methods
         [(ontree(What):-(inhabitant:bird:name):What)].

demon(tree,(inhabitant:bird:name)):-
     (inhabitant:bird:name):Value,
     write('The name of the inhabitant of the tree has changed to '),
     write(Value),nl. 

test:-
      use_objects,
      new_instance(theNightHawk,bird),		% new bird
      new_instance(oak,tree),			% new tree
      ontree(Whats) send_to oak,                % whats on the tree
      write(Whats),write(' is on the tree'),nl,
      fly(theNightHawk,oak) send_to theNightHawk, % make the bird fly, hmm?
                                                  % this I have to work on
      show send_to theNightHawk,		% show age of nighthawk
      setage(30) send_to theNightHawk,		% set his age to 30
      show send_to theNightHawk,		% check age
      ontree(What) send_to oak,			% what is now on the oak
      write(What),write(' is on the tree'),
      clear_all.
                        
The output of the test program will (suprise, suprise) be:

A bird has been born
unknown is on the tree
Im flying to a tree
The name of the inhabitant of the tree has changed to theNightHawk
0
30
theNightHawk is on the tree


Comments and questions are wellcome!

Harry "the bird" Virtanen
Abo Akademi University
Abo, Finland
Technical Details
Message-ID: <1992Nov16.103244.28734@abo.fi>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!aton.abo.fi!usenet

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.prolog
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!aton.abo.fi!usenet
From: HVIRTANEN@finabo.abo.fi (Harry Virtanen INF)
Subject: Object Prolog
Message-ID: <1992Nov16.103244.28734@abo.fi>
Sender: usenet@abo.fi (Usenet NEWS)
Organization: Abo Akademi University, Finland
Date: Mon, 16 Nov 1992 10:32:44 GMT
X-News-Reader: VMS NEWS 1.23
Lines: 89