call
execute(Functor,ArgumentList):- Goal =.. [Functor|ArgumentList], call(Goal).
call
.
execute(Functor,ArgumentList):- Goal =.. [Functor|ArgumentList], Goal.
apply(_,[],[]). apply(Functor,[Head|Tail],[HeadResult|TailResult]):- Function =.. [Functor,Head,HeadResult], Function, apply(Functor,Tail,TailResult).
my_not(Goal):- Goal, !, fail. my_not(Goal).
asserta
, assertz
, retract
asserta(X)
adds the clause X
to the program
before any other clauses with the same head predicate symbol.
X
must be instantiated so that the head predicate symbol
is known.
assertz(X)
adds X
to the program after any
clauses with the same head predicate symbol.
This is simply assert in some (including Eclipse) Prologs.
retract
must be used to remove something from the program.
retract(X)
removes, from the program, a clause that unifies
with X
.
X
must be instantiated so that the head predicate symbol
and arity are known.
retract
is not undone on backtracking.
borrow(Book,Date):- retract(shelf(Book)), assertz(loan(Book,Date)). return(Book,Date,Fine):- retract(loan(Book,BorrowDate)), asserta(shelf(Book)), calculate_fine(BorrowDate,Date,Fine). calculate_fine(BorrowDate,ReturnDate,25):- Days is ReturnDate - BorrowDate, Days > 14, !. calculate_fine(_,_,0). :- dynamic shelf/1. shelf(bible). shelf(koran). shelf(ekat).
make_table(RowNumbers,ColumnNumbers):- member(Row,RowNumbers), member(Column,ColumnNumbers), Product is Row*Column, assertz(product(Row,Column,Product)), fail. ----------------------------------------------------------- ?- make_table([1,2,3],[4,5,6]). no. ?- product(2,4,X). yes X = 8
:-dynamic seed/1. %----The intial seed seed(1111). %----The random number generator random(Lowest,Highest,Random):- retract(seed(Seed)), Random is Lowest + (Seed mod (Highest-Lowest+1)), New_seed is (125 * Seed + 1) mod 4096, asserta(seed(New_seed)).
%----This root encountered before get_root_number(Root,Number):- retract(current_number(Root,N1)), !, Number is N1 + 1, asserta(current_number(Root,Number)). %----First time for this root get_root_number(Root,1):- asserta(current_number(Root,1)). generate_symbol(Root,Atom):- get_root_number(Root,Number), name(Root,Name1), name(Number,Name2), append(Name1,Name2,Name), name(Atom,Name).
copy(Original,Copy):- assertz(save(Original)), retract(save(Copy)).
:-dynamic fibonacci/2. fibonacci(1,1). fibonacci(2,1). fibonacci(Number,Fibonnaci):- Number > 2, N1 is Number - 1, fibonacci(N1,F1), N2 is Number - 2, fibonacci(N2,F2), Fibonacci is F1 + F2, asserta((fibonacci(Number,Fibonacci):-!)).
clause(Head,Tail)
clause(Head,Tail)
unifies
Head
and Tail
with the head and tail of a
dynamic clause in the program.
Head
must be instantiated enough so that the head
predicate symbol and arity are known.
Tail
is true
.
listing(Predicate,Arity):- functor(Head,Predicate,Arity), clause(Head,Tail), write(Head), write(':-'), write(Tail), fail.
run(true):- !. run((FirstQuery,RestOfQueries)):- !, run(FirstQuery), run(RestOfQueries). run(Query):- clause(Query,Tail), run(Tail).
a(
<object>).
is in the program)
also has property b.
The intelligent program may then assert b(X):-a(X)
,
and remove all the specific facts.
findall(Variable,Query,ListOfSolutionsForTheVariable)
Query
, the values of
Variable
are collected together into the List
,
in the order in which they are found.
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- findall(Subject,lecturer(Person,Subject),SubjectList). yes SubjectList = [ai,pascal,ai,ai,programming,networks,graphics,projects,ppl,graphics]
bagof(Variable,Query,ListOfSolutionsForTheVariable)
Variable
in the Query
, all possible
solutions for the Variable
are collected together into
the List
, in the order in which they are found.
bagof
has itself multiple solutions.
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- bagof(Subject,lecturer(Person,Subject),SubjectList). yes Person = olivier SubjectList = [ai,ai,ppl] ? ; yes Person = john SubjectList = [pascal,programming,graphics] ? ; yes Person = geoff SubjectList = [ai,projects] ? etc, etc
setof(Variable,Query,ListOfSolutionsForTheVariable)
bagof
, but the list has duplicates removed and
is ordered lexicographically.
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- setof(Subject,lecturer(Person,Subject),SubjectList). yes Person = olivier SubjectList = [ai,ppl] ? ; yes Person = john SubjectList = [graphics,pascal,programming] ? ; yes Person = geoff SubjectList = [ai,projects] ? etc, etc
findall
that does not
work for nested findalls.
findall(Variable,Goal,_):- Goal, assertz(found(Variable)), fail. findall(_,_,List):- collect_found(List). collect_found([FirstFound|RestFound]):- retract(found(FirstFound)), !, collect_found(RestFound). collect_found([]).Write a version of
findall
that works for nested invocations.