Unification & Backtracking Lab #8 Lab # 08 UNIFICATION AND BACKTRACKING IN PROLOG OBJECTIVE Write programs defining unification and backtracking. THEORY 8.1 UNIFICATION Unification is the process of matching two predicates and assigning free variables to make the predicates identical. This mechanism is necessary so that Prolog can identify which clauses to call and bind values to variables. These are the major points about matching (unification) presented in this chapter: When Prolog begins an attempt to satisfy a goal, it starts at the top of the program in search of a match. When a new call is made, a search for a match to that call also begins at the top of the program. When a call has found a successful match, the call is said to return, and the next subgoal in turn can be tried. Once a variable has been bound in a clause, the only way to free that binding is through backtracking. 8.1.1 EXAMPLE PROGRAMS EXAMPLE 8.1 PREDICATES nondeterm country(symbol,real) % (name ,population) print_countries CLAUSES country(england,3e7). country(france,2.3e7). country(germany,1.6e7). 34 CE-415:Artificial Intelligence Unification & Backtracking Lab #8 country(denmark,2.4e6). country(canada,7.3e6). country(chile,2.5e6). print_countries :country(X,P), /* bind country name to X and population to P */ P > 1e7, /* is population greater than 10 million? */ write(X), /* write the value of X */ nl, /* start a new line */ fail. /* Force backtracking to find all solutions */ print_countries. /* make sure print_countries succeeds */ GOAL print_countries. 8.2 BACKTRACKING Backtracking is the mechanism that instructs Prolog where to go to look for solutions to the program. This process gives Prolog the ability to search through all known facts and rules for a solution. These are the four basic principles of backtracking given in this chapter: Subgoals must be satisfied in order, from top to bottom. Predicate clauses are tested in the order they appear in the program, from top to bottom. When a subgoal matches the head of a rule, the body of that rule must be satisfied next. The body of the rule then constitutes a new set of subgoals to be satisfied. A goal has been satisfied when a matching fact is found for each of the extremities (leaves) of the goal tree. A call that can produce multiple solutions is non-deterministic, while a call that can produce one and only one solution is deterministic. 35 CE-415:Artificial Intelligence Unification & Backtracking Lab #8 EXAMPLE 8.2 DOMAINS title,author = symbol pages = unsigned PREDICATES book(title, pages) nondeterm written_by(author, title) nondeterm long_novel(title) CLAUSES written_by(fleming, "DR NO"). written_by(melville, "MOBY DICK"). book("MOBY DICK", 250). book("DR NO", 310). long_novel(Title):written_by(_, Title), book(Title, Length), Length > 300. GOAL long_novel(X). 8.3 CONTROLLING PROGRAM EXECUTION Prolog provides three tools for controlling the course of your program's logical search for solutions: these are the two predicates fail and not and the cut. The fail predicate always fails; it forces backtracking in order to find alternate solutions. The not predicate succeeds when its associated subgoal can't be proven true. The cut prevents backtracking. It's easy to think of a Prolog rule as a procedure definition. From a procedural perspective, rules can function as case statements, perform Boolean tests, act like goto statements (using the cut), and return computed values. 36 CE-415:Artificial Intelligence Unification & Backtracking Lab #8 EXAMPLE PROGRAMS Program 8.3. 1 PREDICATES nondeterm repeat nondeterm typewriter CLAUSES repeat. repeat:-repeat. typewriter:repeat, readchar(C), /* Read a char, bind C to it */ write(C), C = '\r'. /* Is it a carriage return? fail if isn't */ GOAL typewriter,nl. Program8.3. 2 PREDICATES cutcount2(long) cutcount3(long) nondeterm check(long) CLAUSES /* cutcount2: There is a clause that has not been tried at the time the recursive call is made. */ cutcount2(X):37 CE-415:Artificial Intelligence Unification & Backtracking Lab #8 X>=0,!, write('\r',X), NewX = X + 1, cutcount2(NewX). cutcount2(_):write("X is negative."). /* cutcount3: There is an untried alternative in a clause called before the recursive call. */ cutcount3(X):write('\r',X), NewX = X+1, check(NewX), !, cutcount3(NewX). check(Z):-Z >= 0. check(Z):-Z < 0. GOAL cutcount3(123). Program 8.3.3 PREDICATES nondeterm country(symbol) print_countries CLAUSES country("England"). country("France"). country("Germany"). country("Denmark"). print_countries:38 CE-415:Artificial Intelligence Unification & Backtracking Lab #8 country(X), write(X), /* write the value of X */ nl, /* start a new line */ fail. print_countries. GOAL print_countries. EXERCISES 1. Type the above examples in Visual Prolog environment and execute them individually. 2. Explain the working of the above examples. 3. What things Prolog is unifying in the above examples? 39 CE-415:Artificial Intelligence