4 Rules4.1 RulesConsider the following sentence : 'All men are mortal' We can express this thing in Prolog by :mortal(X) :- human(X).The clause can be read as 'X is mortal if X is human'. To continue with this example, let us define the fact that Socrate is a human. Our program will be : mortal(X) :- human(X). human(socrate).Now if we ask to prolog : ?- mortal(socrate).Prolog will respond : yesIn order to solve the query -? mortal(socrates). Prolog will use the rule we have given. It says that in order to prove that someone is mortal we can prove that he is human. So from the goal mortal(socrate) Prolog generate the subgoal human(socrate). We can still use variables. For example we might want to know who is mortal : ?- mortal(X).Then Prolog should respond : P=socrateThis means that Prolog was able to succed the goal by unifying the variable X to socrates. Again this was done by using the subgoal human(X). Sometimes we may wish to specify alternatives ways to provre something. We can do this by using differents rules ands facts with the same name. For exeample, we can represent the sentence 'Something is fun if it is a PC running UNIX or am old amiga or an ice cream' with the following program : fun(X) :- /* something is fun if */ pc(X), /* it is a pc and */ unix(X). /* it is running unix */ fun(X) :- /* or it is fun if */ old(X), /* it is old and */ amiga(X). /* it is an amiga */ fun(ice_cream). /* the ice_crean is also fun */This program says that there are three ways to know if an object is fun or not. Like for pure facts, Prolog will start from the first clause (a clause can be a rule or a fact) of fun and try it. If that does not succed Prolog will try the next clause. If there is no more clauses then it fails and Prolog responds ' no '. We can also see in this example that the 'and' is represented by a ',' and the 'or' by differents clause. If needed the 'or' can also be represebted by ';'. In the previous examples when we was asking eats(fred,What) and pressing the key ';' to see the following results we was in fact asking 'or'. All identically-named variables in a rule (for example X in the last rule we've seem) are of course considered as one unic variable and must have the same instantiation for each solution in a particular query. Identical variables names in differents rules are considerated as differents variables and are totally independent, this is the same as if we had used differents names. The following program : fun(X) :- pc(X), unix(X). fun(X) :- old(X), amiga(X).Will be seen by Prolog as : fun(X_1) :- pc(X_1), unix(X_1). fun(X_2) :- old(X_2), amiga(X_2). 4.2 How to add a rule with a programIt is possible to add new rules or facts with the instruction Assert(fact1) which will add the fact called fact1. The predicates added with this command are considereted like any other in the source of the program.4.2.1 The instructionsThe usefull instructions are :
4.2.2 Example?- sunny. no. ?- assert(sunny). yes. ?- sunny. yes ?- retract(sunny). yes. ?- sunny. no.
|
|