users-prolog
[Top][All Lists]

## Re: question on assert and retract

 From: Vic Bancroft Subject: Re: question on assert and retract Date: Tue, 27 Aug 2002 23:59:43 -0400 (EDT)

```On Tue, 27 Aug 2002, bruno patin wrote:

> how not to add the same facts two times ?

Consider the following use of (=..)/2 - univ for unique literals,

%%% -*- Mode: Prolog; -*-
%%% unique literals

dynamic( ul/2 ).

u_assertz( X ) :-
X =.. [ Key | Values ],
Z =.. [ 'ul', Key, Values ],
Q =.. [ 'ul', Key, Priors ],
catch( call( Q ), _, fail ),
!,
retract( Q ),
assertz( Z ).

u_assertz( X ) :-
X =.. [ Head | Tail ],
Z =.. [ 'ul', Head, Tail ],
assertz( Z ).

This treats the principle functor as the key and the term list as the data for
the key.  The length of the term list can vary.  An example usage might be,

| ?- u_assertz( toto(1) ).

yes
| ?- listing( ul/2 ).

ul(toto, [1]).

yes
| ?- u_assertz( toto(2) ).

yes
| ?- listing( ul/2 ).

ul(toto, [2]).

yes
| ?- u_assertz( toto(3,'go',P) ).

yes
| ?- listing( ul/2 ).

ul(toto, [3, go, _]).

yes

It doesn't seem to matter if some element of the term list is unbound.
It seems fairly natural to add another predicate, u_call/1, as

u_call( X ) :-
X =.. [ Key | Values ],
Z =.. [ 'ul', Key, Values ],
catch( call( Z ), _, fail ).

In the prior example, one can then do

| ?- u_call( toto( 3, X, 9 ) ).

X = go

more,
l8r,

-------------------------------------------------------------------
Victor Bancroft, Principal Engineer, Zvolve Systems [v]770.551.4505
1050 Crown Pointe Pkwy, Suite 300, Atlanta GA 30338 [f]770.551.4509
Fellow, Artificial Intelligence Center              [v]706.542-0358
Athens, Georgia  30602, U.S.A           http://ai.uga.edu/~bancroft

```