2

I need to create a rule that will search for the facts that matches my_rule. These facts will be use to change the knowledge base. (my_rule (Conclusion, Premise)).

I have this knowledge base to start with :

:- dynamic( is/2 ).

is( m1, house ).
is( m1, thing ).
is( m2, house ).
is( m2, thing ).
is( m3, niche ).
is( m3, house ).
is( m3, thing ).
is( m4, car ).
is( m4, mobile ).
is( m4, house ).
is( m4, thing ).

my_rule( is( X, thing ), is( X, house ) ).
my_rule( is( X, house ), is( X, niche ) ).

When a rule is found, the code will search if a Conclusion and its Premise exist in the database.

I have no idea how to achieve this, and yes this is for a homework. I just want someone to point out where to start.

Thanks.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Vini
  • 116
  • 1
  • 7
  • 3
    What are you actually trying to do? Are you wanting to reduce the number of `is/2` facts (using `retract/1`) based upon whether they can be deduced with `my_rule/2`? Enter your facts into Prolog and play with the query, `my_rule(Fact1, Fact2), call(Fact1), call(Fact2)`. This succeeds if both facts established by `my_rule` are in your database. So if it does succeed, you can retract either `Fact1` or `Fact2` since one is deducible from the other. – lurker Apr 16 '18 at 21:18
  • @lurker yes that is the purpose if the exercice. Thanks for your answer. – Vini Apr 17 '18 at 17:35

1 Answers1

3

Firstly, you need to pick a different predicate name, since is/2 is a built-in to evaluate arithmetic expressions, e.g.

?- X is 3+2.
X = 5.

Your code leads to the following error if you try to consult the sourcefile:

?- [myfile].
ERROR: /home/someuser/code/myfile.pl:1:
        dynamic/1: No permission to modify static procedure `(is)/2'

Let's rename it to is_a/2. Then your code looks like:

:- dynamic( is_a/2 ).

is_a(m1, house).
is_a(m1, thing).
is_a(m2, house).
is_a(m2, thing).
is_a(m3, niche).
is_a(m3, house).
is_a(m3, thing).
is_a(m4, car).
is_a(m4, mobile).
is_a(m4, house).
is_a(m4, thing).

Then you could define a predicate to describe pairs of conclusions and premises like so:

conclusion_premise(is_a(X, thing), is_a(X, house)).
conclusion_premise(is_a(X, house), is_a(X, niche)).

Building on this you could define my_rule/2 to describe that C and P must be a corresponding pair of conclusion and premise and to subsequently call both as goals:

my_rule(C,P) :-
   conclusion_premise(C,P),
   call(C),
   call(P).

Now you can query my_rule/2 to search for corresponding conclusion-premise pairs:

?- my_rule(Conclusion,Premise).
Conclusion = is_a(m1, thing),
Premise = is_a(m1, house) ;
Conclusion = is_a(m2, thing),
Premise = is_a(m2, house) ;
Conclusion = is_a(m3, thing),
Premise = is_a(m3, house) ;
Conclusion = is_a(m4, thing),
Premise = is_a(m4, house) ;
Conclusion = is_a(m3, house),
Premise = is_a(m3, niche) ;
false.
tas
  • 8,100
  • 3
  • 14
  • 22
  • Thanks for your answer. My code was originally in French and I translated it and didn't run it after. Sorry about it. – Vini Apr 17 '18 at 17:33
  • Your piece of code is exactly what I was looking for. Thanks a million ! – Vini Apr 17 '18 at 17:34