3

First of all thanks for your help.

On to my problem: Let's say I have:

some_fact:- true.

and I want to asserta a rule on top of it that looks like this:

some_fact:- fail, !.

This is because I want to transform the "some_fact:- true." to force a false without removing the rule (I don't want to use abolish(some_fact,0). )

The problem is that I can't find the way to do it because I can't place the comma on the asserta/1. What I mean is that when I put:

asserta(some_fact:- fail, !).

the comma in between forces a call to asserta/2 instead of asserta/1 with the whole rule, and I cannot prevent that using quotes because it asserts a string.

Of course I can't just simply put asserta(some_fact:- fail). because prolog will search for the next some_fact which returns true.

Any ideas? Thanks again!

false
  • 10,264
  • 13
  • 101
  • 209
Gaspa79
  • 5,488
  • 4
  • 40
  • 63

2 Answers2

3

just add parenthesis:

?- asserta((some_fact:- fail, !)).
true.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
1

To assert the fact:

asserta(zoo(zebra)).

When the fact is no longer true:

retract(zoo(zebra)).

More general:

asserting(Fact) :-
    asserta(Fact).

retracting(Fact) :-
    retract(Fact).

Example:

?- asserting(zoo(zebra)).
true.

?- zoo(zebra).
true.

?- retracting(zoo(zebra)).
true.

?- zoo(zebra).
false.
Joost
  • 81
  • 3
  • I already said that I didn't want to do abolish (retract does not work btw because it's a rule with arity 0, not a fact). Thanks anyway. – Gaspa79 Oct 01 '13 at 14:34
  • 1
    It seemed you were asking about a fact because you consistently used the name 'some_fact' in your question. Regardless, it does work with rules with arity 0. Have you tried out my example? Also, like mbratch I don't understand why you are trying to avoid removing the rule. Can you explain? – Joost Oct 01 '13 at 15:22
  • Yeah... I should've used "some_rule:- true". Sorry haha. I'm not trying to avoid removing the rule actually, I just wanted to know how to assert that due to a more complicated scenario which I simplified here. Thanks! – Gaspa79 Oct 01 '13 at 16:34
  • Oh and for some reason my teacher said the same thing: that retract works for rules with arity 0. However, when we both tried it on the program it didn't work! So we used abolish instead. Thanks a ton mate! – Gaspa79 Oct 01 '13 at 18:07
  • Cheers for the reply! By the way: if the predicate is already in use and you want to assert/retract you have to declare it as 'dynamic' first. So that could be a reason for it not working. – Joost Oct 01 '13 at 21:06