120 likes | 237 Views
More on Prolog. Simple program. dog (fido). --fact spotted (fido). --fact dalmation (X) :- dog(X), spotted(X). --rule ?- dalmation (fido). -- query answers Yes. leopard (leo). --new fact spotted (leo). --new fact ?-dalmation (leo). --new query - answers Yes.
E N D
Simple program dog (fido). --fact spotted (fido). --fact dalmation (X) :- dog(X), spotted(X). --rule ?- dalmation (fido). -- query • answers Yes. leopard (leo). --new fact spotted (leo). --new fact ?-dalmation (leo). --new query -answers Yes.
Want to know all spotted? That is, “For what X is X spotted?” • Query ?-spotted(X). • Answer X=fido. F8 X=leo. F8 No.
Basic Elements of Prolog • Prolog statements constructed from terms • Term – constant, variable or structure • constant • either atom (begin with lowercase) or an integer • variable (begin with uppercase) • not bound by declarations to types • binding of value (thus type) to a var is an instantiation and occurs during the resolution process • instantiations last only as long as it takes to establish a goal • NOT like vars in imperative languages
structure – represent propositions of predicate calculus • functor (parm list) atom list of atoms, vars, or other structures • specify facts and rules (difference indicated by 2 modes) • specify a predicate when a query • responses: yes – proved goal was true no – proved false or unable to prove true
Inferencing Process (Resolution) • Queries are called goals • when goal is compound proposition, each of structures is a subgoal • To prove goal is true, process must use chain of inference rules that connect goal to 1 or more facts. Example: Q is a goal Q must be fact or there must be a seq of propositions p1, p2, … pn such that p1 => p2, p2 => p3, … pn => Q and p1 is a fact.
2 Kinds of resolutions • bottom-up (forward chaining) • When you have a large # possibly correct answers • begin with facts and rules and attempt to find a Q that matches that to the goal • top-down (backward chaining) • small set of candidate answers • begin with goal and work backwards to set of facts • implementations that use this use a depth-first search and backtracking
Simple Arithmetic • supported (orig. arith ops were functors) • all variables except LHS variable must be instantiated. • A is B/17 + C • illegal: sum is sum + number • because RHS sum is instantiated • is is not exactly like := in C++ • most Prolog programmers don’t need them
speed(ford, 100). speed(chvy, 105). speed(dodge, 95). time(ford, 20). time(chevy, 21). time(dodge, 24). distance(X,Y) :- speed(X, S), time(X, T), Y is S* T. ?-distance(chevy, Chevy_Distance), write(Chevy_Distance), nl. response 2205 yes.
How did it work? indicates call action subgoal to depth be matched • 1 Call: distance(chevy, _0)? • 2 Call: speed (chevy, _5)? (2) 2 Exit: speed(chevy, 105) • 2 Call: time(chevy, _6)? (3) 2 Exit: time(chevy, 21) (4) 2 Call: _0 is 105 * 21 ? • 2 Exit: 2205 is 105 * 21 • 1 Exit: distance (chevy, 2205) Chevy_Distance = 2205 _0 internal var
actions – call, fail, exit, redo fail call ( success) exit redo fail call redo exit
Study Question - Traces likes (jake, chocolate). likes(jake, apricots). likes(darcie, licorice). likes(darcie, apricots). trace likes(jake, X), likes(darcie, X). trace likes(X, apricots).