Prolog day2 4-14-00 Programs are comprised of 1. facts : specification of a relation Relation is a name predicate, variable or constant Predicate naming relationship -Constants start with lower case eg. inOrder([]). ***NOTE*** odd(1). ends with . -Variables start with Upper Case eg. InOrder([x]). (example1) define append relationship append ([], L, L). (appending [] with L gives L) 2. rules (of inference) form if a is true then b is true Using Prolog - Two phases 1. program entry 2. program testing and execution - can type interactively. to do, enter '[user].' This begins the user mode and begins program entry. 1. Enter Facts and Rules 2. Enter ^D to end entry mode. ['filename'] 3. Query Mode begins. (Marked by ? prompt.) Query mode responds with either yes, true or no, not true, not as far as you've told me. eg. ?- odd(2). no ?- odd(1). yes. (checks relevant predicates 1=1.) ?- odd(x). x=1 (x must equal 1 for odd(x) to be true.) Prolog has a different approach to programming. -order is not important -add new rules Unification: all occurances of same varible get same value. can use , to use this. (, means and) eg. fatherOf(tom, x) , fatherOf(x, harry). |_____________| | \|/ V x must be father of Tom and x must be son of harry. ***Note: variables are local to rule. Rules of inference (the symbol :- means if) Form: predicate :- cond1, cond2, cond3... head body of rule of rule This is backward to how we ususally think. Instead of if true then do this it is do this if these conditions hold. grandMotherOf (x, GM) :- motherOf (x, M), motherOf(M, GM). grandMotherOf (x, GM) :- fatherOf (x, F), motherOf(F, GM). (You can put multiple rules on a line, but for readability sake, it is not advised.) (From now on motherOf will be abbreivated as mO, fatherOf as fO, and grandMotherOf as gMO) fO(tom, dick). mO(tom, judy). fO(dick, harry). mO(dick, mary). fO(jane, harry). mO(jane, mary). example: ?- mO(tom, x). x = judy ?- ; (; means get another repsonse) no. ?- mO(tome, x), mO(Y,Z). x = judy Y = \ Can be any combination of above. Z = / Y = tom, Z = judy Y = dick, Z = mary Y = dick, Z = mary and so, any response is valid. To exhaust all possible responses, keep typing ;s until a no response is received. sibs(x,y) :- mO(x,m), fO(x, f), mO(y,m), fO(y,f), not (x = y). ?- gMO(tom, Q). tries: x = tom, GM = Q, m = judy. gives: mO(tom, m), mO(m, Q). yes no FAILS. backtracks, and tries: x = tom, GM = Q, f = dick gives: fO(tom, f), mO(f, Q). yes yes SOLUTION.