Subject: Re: problems with asserta.

From: "Richard A. O'Keefe" <[email-masked]>
Date: Wed, 15 Nov 2000 11:10:25 +1300 (NZDT)

fib(1,1).
	fib(2,1).
	fib(N,F) :-
	     N > 2,
	     N1 is N - 1, fib(N1, F1),
	     N2 is N - 2, fib(N2, F2),
	     F is F1 + F2,
	     asserta(fib(N,F)).

	ERROR: No permission to modify static_procedure 'fib/2'
	^ Exception: (14) asserta(fib(3,2)) ?
	
	What am I doing wrong?  Thanks for your help!
	
You are modifying a static procedure.
Good taste, efficiency, and a whole lot of other reasons dictate
a clean separation between static and dynamic code.

A good way to write this would be

	:- dynamic fib_table/2.

	fib(N, FN) :-
	    (   N < 3 -> FN = 1
	    ;   fib_table(N, F) -> FN = F
	    ;   N2 is N - 2, fib(N2, F2),
		N1 is N - 1, fib(N1, F1),
		F is F1 + F2,
		asserta(fib_table(N, F)),
		FN = F
	    ).