Next: retract
Up: Global variables and DataBase
Previous: Global variables and DataBase
assert(Clause)
- Will add Clause to the current program, and will always succeed
- The Clause is added at the end of the clauses associated with its
predicate name
- Clause may be a fact or a rule
- Has arity of 1
Facts should be given in the form :
windy
sunny
miles(new_album)
and rules
(Head :- C1, C2, _, Cn)
- Note : the rule must be enclosed in parenthesis
- Since Prolog has no global variables, assert must be used to save
the value of a variable computed during the execution of a program
assert(value(x,5))
Q: value(x, Value)
A: Value = 5
similarly
Q: crisis
A: no
Q: assert(crisis)
A: yes
Q: crisis
A: yes
- Assert can be used as part of the program - (program modifying itself!!)
- A way perhaps to deal with changing situations
- One program being able to modify another one - even the internal
structure of a program through the use of assert and retract
Q: assert((likes(keith,Person) :- likes(Person, jazz))).
A: yes
Q: assert(likes(james,coltrane)).
A: yes
The above will generate the following definition of likes
likes(keith,Person) :-
likes(Person, jazz).
likes(james,coltrane).
- Assert of two types asserta and assertz
- asserta : Adds a clause at the beginning of the clauses associated
with its predicate name
Q: asserta((likes(keith,Person) :- likes(Person, jazz))).
A: yes
Q: asserta(likes(james,coltrane)).
A: yes
The above will generate the following definition of likes
likes(james,coltrane).
likes(keith,Person) :-
likes(Person, jazz).
- Similarly assertz : add a clause at the end of the clauses associated
with its predicate name
- Something that LPA MacProlog offers in addition to the above two is
assertx/2 - which allows a clause to be inserted at a particular position
Q: assert(foo(a),0), assert(foo(b),1),
assert(foo(c),999),assert(foo(d),2)
foo(b)
foo(d)
foo(a)
foo(c)
- If position = 0 or greater than number of clauses acts as assertz
Omer F Rana
Thu Feb 20 20:05:17 GMT 1997