Pravin Mittal CS 538, Lecture 38 Date: 4/26/2000 TERMINATION ISSUES AND EXTRALOGICAL ISSUES CONTINUED: fact(N, F) :- N =< 1, F = 1. fact(N, F) :- N > 1, M is N - 1, fact(M, G), F is N*G. This program can be applied in four different ways: 1. ?- (3, 6). N and F are fixed/grounded. The program works. 2. ?- (3, Y). N is fixed, F is free. The program works. 3. ?- (X, 6). F is fixed, N is free. The program DOES NOT WORK. 4. ?- (X, Y). Both N and F are free. The program DOES NOT WORK. If N is free, fact can't work because fact can't guess whether N =< 1 or N > 1. You can use nonvar and ! to solve this problem in a better version of the factorial function: fact(1, 1). fact(N, 1) :- nonvar(N), N <= 1, !. fact(N, F) :- nonvar(N), N > 1, !, M is N-1, fact(M, G), F is N*G, !. fact(N, F) :- nonvar(F), !, fact(M, G), N is M+1, F2 is N*G, F is =< F2, !, F = F2. fact(N, F) :- fact(M, G), N is M+1, F is N*G. !, are very important as it locks the program to the specific rule, Consider rules of the form B:- !,C, in which a cut appears as the first condition. If the goal C fails, then control backtracks past B without considering any remaining rules for B. Thus, the cut has the effect of making B fail if C fails. There are two modes: 1) Query mode 2) Input mode Handy pieces of information and syntax in prolog: ^D = end of file It is also possible to read the file with the help of command consult ('filename.pro') , = AND ; = OR \+ = NOT read (X) just takes one argument from input board N is M+1 is mathematical comparison whereas N= M+1 is a setting. var (X), X is a variable and do not have binding norvar (X), X is bounded integer (X) , X is integer atomic (X), X is not a complex predicate Debugging trace to turn on the debugger notrace to turn off the debugger