300 likes | 473 Views
Unification. CS 321. CLAUSES person(kelly). person(judy). person(ellen). person(mark). car(lemon). car(hot_rod). likes(kelly, hot_rod). likes(judy, pizza). likes(ellen, tennis). likes(mark, tennis). for_sale(pizza). for_sale(lemon). for_sale(hot_rod). can_buy(X,Y):- person(X),
E N D
Unification CS 321
CLAUSES person(kelly). person(judy). person(ellen). person(mark). car(lemon). car(hot_rod). likes(kelly, hot_rod). likes(judy, pizza). likes(ellen, tennis). likes(mark, tennis). for_sale(pizza). for_sale(lemon). for_sale(hot_rod). can_buy(X,Y):- person(X), car(Y), likes(X,Y), for_sale(Y). Recall: Prog1-2.pro Variables X and Y
How Variables Get Their Values • Prolog has no assignment statement • Variables in Prolog get their values by being matched to constants in facts or rules. • Until it gets a value, a variable is said to befree • When it gets a value, it becomesbound.
Matching sub-goals • In our example, a goal could be can_buy(kelly, What). • When the program is run, Prolog binds kelly to X and What becomes associated with the variable Y can_buy(kelly, Y):- person(kelly), car(Y), likes(kelly,Y), for_sale(Y). • The idea is to find known facts that match the same value to Y/What, for all clauses in the body of the rule – this is called Unification.
Resolution • When attempting to find a satisfactory resolution to a goal, • Prolog starts at the top of the clauses section, looking at each fact and rule as it searches for a match. • As Prolog proceeds down through the clauses section, it places internal pointers next to each clause that matches the current sub-goal. • If the clause is not found to be part of a logical path that leads to a solution, then Prolog returns to the set pointer and looks for another match.
Unbinding and Backtracking • A variable only stays bound to a value for the time needed to obtain one solution to a query • Then Prolog unbinds it, backs up, and looks for alternative solutions. (This is called backtracking.)
Consequence • Variables are used as part of the pattern-matching process, not as a kind of information storage.
Comments • C style: /* …. */ • Or in-line style: % • Comment out to end of line
Anonymous Variables • If you only need certain information from a query, you can use anonymous variables to ignore the values you don't need. • In Prolog, the anonymous variable is represented by a lone underscore "_" .
Example Prog2-1.pro male(bill). male(joe). female(sue). female(tammy). parent(bill,joe). parent(sue,joe). parent(joe,tammy).
Using _ parent(Parent, _). Prolog realizes that each time you use the underscore symbol in the query, you don't care about what value is substituted in that variable's place… so Prolog will not report any values that are used in that position
Anonymous variables in facts • The following Prolog facts owns(_, shoes). eats(_). • could be used to express the natural language statements Everyone owns shoes. Everyone eats.
Anonymous variables in facts • If you enter the goal in gprolog: owns(bill, shoes). The result is: (4 ms) yes | ?- Even though the fact owns(bill,shoes) is not in the database.
Structures • Prolog allows nesting relationships • Example: class(proglang, mwf, 12, 1, john, barr, williams, 303). • This is a class relationship (or fact) with 8 separate items.
Structures class(proglang, mwf, 12, 1, john, barr, williams, 303). Alternative: class(proglang, time(mwf, 12, 1), instructor(john, barr), location(williams, 303)). This is called a structure These facts only exist in the context of the class fact!
Structures • Structures allow rules like: teaches(Instructor, Day) :- class (Classname, time(Day, Start, Finish), Instructor, Location ). • Note that the head does not have to contain the same number of variables as the body! instructor(Instructor, Class) :- class(Class, Time, Instructor, Location ).
Structures • What classes does Ali Erkan teach? • ?- instructor(instructor(ali, erkan), Class). • What instructors teach on wednesdays? • ?-teaches(Instructor, mwf). • What days does John Barr teach? • ?-teaches(instructor(john,barr), When). If you entered the goal: time(mwf, 12, 1). in gprolog you’d get: uncaught exception: error(existence_error(procedure,time/3),top_level/0)
Structures • Can add rules to query about specific information: room(Class,Building,Room) :- class(Class, Time, Teacher, location(Building, Room) ). • Queries (or goals): room(Class, williams, 309). room(progLang, Building, Room).
Operators and Functions • Normal Prolog operators are prefix: • @>(Item1, Item2). • @=<(Item1, Item2). • ==(Item1, Item2). • \==(Item1, Item2). • Some symbols can be used infix: arithmetic and comparison
Arithmetic • Example. bonus(Number) :- Number is 2 + 3. ?- bonus(3). No ?- bonus(5). Yes ?- bonus(X). X = 5
Arithmetic • Example. | ?- X is 5 + 2. X = 7 yes | ?- X = 5 + 2. X = 5+2 yes | ?- X is 5.3 + 7. X = 12.300000000000001 yes | ?- = and is have different meanings = means term assignment is means arithmetic assignment Integers are coerced to float
Arithmetic • Example: temperature.pro. ave_temp(addis_ababa, 62). ave_temp(berlin, 49). ave_temp(calgary, 38). ave_temp(belgrade, 52). ave_temp(chicago, 50). ave_temp(boston, 48). ave_temp(washington_dc, 55). ave_temp(jersualem, 61). ave_temp(khartoum, 84). ave_temp(san_diego, 61). ave_temp(ithaca, 40). ave_temp_celsius(Location, C_temp) :- ave_temp(Location, F_temp), C_temp is (F_temp - 32) * 5 // 9. Try: ave_temp_celsius(berlin,C). ave_temp_celsius(X, 16).
Arithmetic • Example: geography.pro /* north latitudes and west longitudes are positive. sound latitudes and east longitudes are negative. */ location(tokyo, 35, -139). location(rome, 41, -12). location(london, 51, 0). location(canberra, -31, -149). location(madrid, 48, 3). north_of(X, Y) :- location(X, Lat1, _), location(Y, Lat2, _), Lat1 > Lat2. west_of(X, Y) :- location(X, _, Long1), location(Y, _, Long2), Long1 > Long2. Try: north_of(madrid, tokyo). west_of(X, tokyo). west_of(london, X).
Tracing • The gprolog command is trace |?-trace. yes {trace} |?-north_of(madrid, tokyo). 1 1 Call: north_of(madrid,tokyo) ? 2 2 Call: location(madrid,_81,_41) ? 2 2 Exit: location(madrid,48,3) ? 3 3 Call: location(tokyo,_108,_68) ? 3 2 Exit: location(tokyo,35,-139) ? 4 2 Call: 48>35 ? 4 2 Exit: 48>35 ? 1 1 Exit: north_of(madrid,tokyo) ? Yes {trace} | ?- Must have a period! 1 1 means top level call 3 2 means 3rd level call in response to the current 2nd level call.
Tracing The command to turn off is notrace |?-notrace. The debugger is switched off Yes |?-
Making Decisions • There are no if or case statements in prolog. • How do we make decisions? • Example: determine how hot it is: // determine how hot it is void howHot(string &HowHot, int Temp){ if (Temp >= 100) HowHot = “very”; else if (Temp >= 90) HowHot = “pretty”; else if (Temp >= 70) HowHot = “perfect”; else if (Temp < 70) HowHot = “cold”; }
Making Decisions • Making decisions (howHot.pro) % determine how hot it is Hot(HowHot, Temp) :- Temp >= 100, HowHot = ‘very’; Temp < 100, Temp >= 90, HowHot = ‘pretty’; Temp < 90, Temp >= 70, HowHot = ‘perfect’; Temp < 70, HowHot = ‘cold’. Can ask howHot(X, 80). But not howHot(very, X). Cannot use is must use = is only works on arithmetic expressions Must have this test because prolog will view each or clause independently. If we left out Temp < 100 then a temp of 110 would return both “very” and “pretty”
In-Class exercise 2 • See handout.