| Object oriented programming is useful to
encapsulate internal state in predicates and
manipulate the state.
MINERVA allows to define classes and their methods.
An object of a class has an internal state that can
be accessed like blackboard data, i.e. using bb_* predicates.
Example:
:- class(counter).
Counter :: init :-
bb_put(Counter, index, 0).
Counter :: init(InitialValue) :-
bb_put(Counter, index, InitialValue).
Counter :: get(Value) :-
bb_get(Counter, index, Value).
Counter :: set(Value) :-
bb_put(Counter, index, Value).
Counter :: next(Value) :-
Counter :: get(OldValue),
NewValue is OldValue + 1,
Counter :: set(NewValue),
Value = NewValue.
:- endclass.
The MINERVA compiler processes these predicates and in addition
generates predicates create_CLASS/1/2 to produce a new object.
Resulting predicates are
Object::Method(Arg1,..,ArgN)
where Object must be produced with create_object/2
and Method must be an atom or compound.
For the example above in classic Prolog:
create_counter(10,Counter),
Counter :: next(Index)
For the example above with function notation:
Counter <- create_counter(10),
Index <- Counter::next,
...
|