compiler directive dynamic/1

[email protected]
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
Hi!

I am working my way through a tutorial in (and introduction to)
prolog. I've been using gnu-prolog along the way compiled on
my NetBSD 1.6.2.

The top-level-loop reckognizes "dynamic" but when
working with consulted code (byte-compiled), it seems
to think that dynamic/1 is a user predicate instead of
a compiler directive that should have worked at consult 
(byte-code-compiling) time!

So I get permission_error(modify, static_procedure) on
the predicates I try to retract/asserta to the database!

The compiler doesn't reckognize it either and refuses
to compile the code.

What am I doing wrong here?

Following is the file I'm working with. It is not complete 
(naturally), but should give an idea of the running tutorial 
example I'm working through.

Best Regards,

Dennis Decker Jensen

% My adventure game: Nani search

% this directive doesn't work as described in the manual
% and gets interpreted like a user predicate!!!

dynamic( [have/1, here/1, turned_off/1, turned_on/1, location/2] ).

room(kitchen).
room(office).
room(hall).
room('dining room').
room(cellar).

location(desk, office).
location(apple, kitchen).
location(flashlight, desk).
location('washing machine', cellar).
location(nani, 'washing machine').
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).
location(envelope, desk).
location(stamp, envelope).
location(key, envelope).

door(office, hall).
door(kitchen, office).
door(hall, 'dining room').
door(kitchen, cellar).
door('dining room', kitchen).

is_closed(office, hall).
is_closed(kitchen, office).
is_closed(kitchen, cellar).

is_opened(hall, 'dining room').
is_opened('dining room', kitchen).

edible(apple).
edible(crackers).

tastes_yucky(broccoli).

turned_off(flashlight).

here(kitchen).

% where are the good things to eat?
where_food(Food, Where) :-
	location(Food, Where),
	edible(Food).
where_food(Food, Where) :-
	location(Food, Where),
	tastes_yucky(Food).

% two-way connections between doors, because
% order of arguments count in prolog
connect(X,Y) :- door(X,Y).
connect(X,Y) :- door(Y,X).

% list all things in a place
list_things(Place) :-
	location(Thing, Place),
	tab(2), write(Thing), nl,
	fail.
list_things(_). % when no more locations, succeed here instead to continue.
		% that way we can continue...

list_connections(Place) :-
	connect(Place, Another_place),
	tab(2), write(Another_place), nl,
	fail.
list_connections(_). % ditto

is_contained_in(Place1, Place2) :-
	location(Place1, Place2).
is_contained_in(Place1, Place2) :-
	location(X, Place2),
	is_contained_in(Place1, X).

look :-
	here(Place),
	write('You are in the '), write(Place), nl,
	write('You can see:'), nl,
	list_things(Place),
	write('You can go to:'), nl,
	list_connections(Place).

look_in(Place) :- list_things(Place).

goto(Place) :-
	can_go(Place),
	move(Place),
	look.

open_door(Here, There) :-
	retract(is_closed(Here, There)),
	asserta(is_opened(Here, There)).

close_door(Here, There) :-
	retract(is_opened(Here, There)),
	asserta(is_closed(Here, There)).

can_go(Place) :-
	here(Here),
	connect(Here, Place).
can_go(_) :-
	write('You can''t get there from here'), nl,
	fail.
	% (here(X), close(X, Place) -> write('Door is closed'), nl ; fail),

move(Place) :-
	retract(here(_)),
	asserta(here(Place)).


take(X) :-
	can_take(X),
	take_object(X).

can_take(X) :-
	here(Place),
	is_contained_in(X, Place).
can_take(Thing) :-
	write('There is no '), write(Thing), write(' here.'), nl,
	fail.

take_object(X) :-
	retract(location(X,_)),
	asserta(have(X)),
	write('taken'), nl.

put_down(Thing) :-
	can_put(Thing),
	put_object(Thing).

can_put(Thing) :- have(Thing).
can_put(Thing) :-
	write('You don''t have '), write(Thing), nl,
	fail.

put_object(X) :-
	retract(have(X)),
	here(Place),
	asserta(location(X, Place)),
	write('put'), nl.

inventory :- have(X), tab(2), write(X), nl, fail.

turn_on(Light) :-
	turned_off(Light),
	retract(turned_off(Light)),
	asserta(turned_on(Light)).
turn_on(Light) :-
	write(Light), write(' is already turned on.'), nl,
	fail.

turn_off(Light) :-
	turned_on(Light),
	retract(turned_on(Light)),
	asserta(turned_off(Light)).
turn_off(Light) :-
	write(Light), write(' is already turned off.'), nl,
	fail.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.