1 / 12

More on Prolog

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.

suchi
Download Presentation

More on Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Prolog

  2. 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.

  3. Want to know all spotted? That is, “For what X is X spotted?” • Query ?-spotted(X). • Answer X=fido. F8 X=leo. F8 No.

  4. 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

  5. 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

  6. 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.

  7. 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

  8. 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

  9. 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.

  10. 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

  11. actions – call, fail, exit, redo fail call ( success) exit redo fail call redo exit

  12. 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).

More Related