users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: question on assert and retract


From: Timo Karjalainen
Subject: Re: question on assert and retract
Date: Tue, 27 Aug 2002 09:45:34 +0300 (EEST)

On Mon, 26 Aug 2002, Vic Bancroft wrote:

> On Mon, 26 Aug 2002, bruno patin wrote:
>
> > retract(toto(2)).
> >
> > toto(X).
> > no
>
> It removes all the clauses that would unify with 2.

This is wrong. retract/1 either removes exactly one fact and succeeds; or
fails if it doesn't find any fact that unifies with its argument.

retractall/1 is the predicate that retracts any number of facts that unify
with its argument. It succeeds always, regardless of whether there were
zero, one, or more matches.

Bruno, what version of GProlog are you using? With 1.2.8, I get the
expected behaviour, i.e. the query succeeds exactly once after one
retract/1.

Or are you sure you didn't backtrack over the retract/1 call? It will be
executed again until there are no more matches.

>  The 1.2.12 version gives,
>
>   | ?- retract(toto(2)).
>
>   true ? ;
>
>   yes

So does my 1.2.8 (yes, I should probably update but I've been lazy...).
But did you look at the listing after that?

On 1.2.8, I get this:

| ?- assertz(fact(2)).

yes
| ?- assertz(fact(2)).

yes
| ?- listing.

fact(2).
fact(2).

yes
| ?- retract(fact(2)).

true ?

yes
| ?- listing.

fact(2).

yes
| ?-

So you see, the second fact(2) remains in the database.

Continuing from that:

| ?- assertz(fact(2)).

yes
| ?- listing.

fact(2).
fact(2).

yes
| ?- retract(fact(N)).

N = 2 ?

yes
| ?- listing.

fact(2).

yes

So it works the same with a free variable.


-- 
Timo Karjalainen             "I don't have a life.
                              I have a program."
                                   - Star Trek: Voyager






reply via email to

[Prev in Thread] Current Thread [Next in Thread]