Prolog - ASSERT and RETRACT
Asked Answered
B

1

27

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

Basel answered 12/3, 2010 at 19:27 Comment(0)
S
40

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.
Stemware answered 13/3, 2010 at 13:38 Comment(1)
thanks, seems to work , i was just wondering if its possible to point swi to a particular directory instead of the default directory? when using the tell command of course,Basel

© 2022 - 2024 — McMap. All rights reserved.