Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog - ASSERT and RETRACT

I was wondering, I am aware you can use assert to add facts or rules or whatever if you have declared the predicate to be -:dynamic, but this only allows the changes that are made to be kept in that session only, e.g. if you close the Prolog window then the database changes are lost.

So I was wondering, is there any way of making it so that the assert and retract predicates can make permanent changes to the Prolog .pl file?

Thanks

like image 315
KP65 Avatar asked Mar 12 '10 19:03

KP65


People also ask

What is retract in Prolog?

retract. retract , retractall. retract is a built-in meta-predicate used to remove facts or rules from the Prolog database while a program is executing. It is most often used in partnership with assert (or one of its relatives).

What is dynamic in Prolog?

dynamic. dynamic. In Prolog, a procedure is either static or dynamic. A static procedure is one whose facts/rules are predefined at the start of execution, and do not change during execution. Normally, the facts/rules will be in a file of Prolog code which will be loaded during the Prolog session.

How do you query in Prolog?

In making a query you are asking Prolog whether it can prove that your query is true. If so, it answers "yes" and displays any variable bindings that it made in coming up with the answer. If it fails to prove the query true, it answers "No". Whenever you run the Prolog interpreter, it will prompt you with ?-.

How do you write and in Prolog?

To write a Prolog program, firstly, the user has to write a program which is written in the Prolog language, load that program, and then specify a sequence of one or more goals at the prompt.


1 Answers

I can suggest you a very simple way of doing this.

1 ?- assert(a(1)). true.  2 ?- assert(a(2)). true.  3 ?- assert(a(3)). true.  4 ?- a(A). A = 1 ; A = 2 ; A = 3.  5 ?- tell('a_db.txt'), listing(a), told. true. 

Then close session, reopen.

1 ?- a(A). ERROR: toplevel: Undefined procedure: a/1 (DWIM could not correct goal) 2 ?- ['a_db.txt']. % a_db.txt compiled 0.00 sec, 516 bytes true.  3 ?- a(A). A = 1 ; A = 2 ; A = 3.  4 ?- listing(a). :- dynamic a/1.  a(1). a(2). a(3).  true. 
like image 138
Volodymyr Gubarkov Avatar answered Sep 19 '22 22:09

Volodymyr Gubarkov