1 / 22

Formal Models of Computation Part II The Logic Model

Formal Models of Computation Part II The Logic Model. Lecture 4 – Execution/backtracking. Executing Prolog Programs. Prolog implements the resolution algorithm: A fact or a clause is chosen like this: Scan program from top to bottom (left to right) Pick out the first one

dinh
Download Presentation

Formal Models of Computation Part II The Logic Model

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. Formal Models of ComputationPart IIThe Logic Model Lecture 4 – Execution/backtracking

  2. Executing Prolog Programs • Prolog implements the resolution algorithm: • A fact or a clause is chosen like this: • Scan program from top to bottom (left to right) • Pick out the first one • The test A = Ci (unification) is term matching. • resolution(KB,Query):boolean • let Query be :- C1,…,Cn • fori = 1 to ndo • if there is a fact A in KB such that A = Ci • then true • else if there is a rule A :- B1,…,Bnin KB • such that A = Ci • then resolution(KB,:- B1,…,Bn) • elsefalse • if all Ci are true then return true • else return false • KB is a Prolog program • How do we choose a fact or a clause? • How do we test if A = Ci? formal models of computation

  3. Commitment in Haskell vs Prolog • Haskell commits to the first case of a definition whose pattern matches and whose guard is true. • Prolog tries the first matching clause first, but returns and tries others if this does not work out. • Consider: a :- b, c. b. d. e. a :- d, e. :- a. • Less commitment in Prolog – searches through all possibilities formal models of computation

  4. Executing Prolog Programs • Let’s suppose we have edited the program: • Next we load it up in Prolog and type the query: The query means: “who are my friends?” • Prolog uses the resolution algorithm to find a solution to the query. rich(bill). rich(arnie). famous(bill). friend(X):- rich(X),famous(X). ?- friend(F). formal models of computation

  5. Executing Prolog Programs • Prolog finds rule whose head matches the query: • A fresh copy of the chosen rule is made friend(X1):- rich(X1),famous(X1). (this prevents accidental name clashes) • Because of the match, the variable F of the query is aliased (or bound) with the fresh variable X1. • Prolog now has to prove the body of the clause it chose, that is “rich(X1),famous(X1)” rich(bill). rich(arnie). famous(bill). friend(X):- rich(X),famous(X). formal models of computation

  6. Executing Prolog Programs • Prolog tries to find a fact to match rich(X1): • The matching assigns bill to X1 • Prolog tries to prove famous(X1) – however, it is now famous(bill): • Prolog finishes the execution and gives the value of the variable F, that is, rich(bill). rich(arnie). famous(bill). friend(X):- rich(X),famous(X). rich(bill). rich(arnie). famous(bill). friend(X):- rich(X),famous(X). ?- friend(F). F = bill ? At this point, Prolog offers (via the “?”) the possibility of another execution, but choosing different rules/facts. formal models of computation

  7. Backtracking “;” means: give me another answer (if there is one) • What happens if we ask Prolog, in the previous example, for another answer? • Prolog tries to find another fact/clause for the last choice made (last time in the execution it had to chose a fact/clause). • The last choice was when Prolog tried to find a fact that matched famous(bill). • Prolog tries to find another fact, but it doesn’t find any. • Prolog then backtracks to the previous goal rich(X1), undoing any assignments that had taken place; ?- friend(F). F = bill ? ; formal models of computation

  8. Backtracking Prolog is ready to take on another query • Prolog finds another fact that matches rich(X1): • The matching assigns arnie to X1 • Prolog tries to prove famous(X1) – however, it is now famous(arnie); • Prolog cannot find a fact/rule that matches it; • Prolog fails, issuing a “no” at the prompt: rich(bill). rich(arnie). famous(bill). friend(X):- rich(X),famous(X). ?- friend(F). F = bill ? ; no ?- formal models of computation

  9. Backtracking • When Prolog is trying to prove a query, it makes decisions on which fact/rule to use; • These choices, however, may lead to failures. • When Prolog fails a proof, it tries, if possible, other facts/rules • If a fact/rule is not good, then Prolog chooses the one immediately after (top-down) the one it had chosen before. • In order to say “no”, Prolog has to try all possible ways to prove a goal. • When we type “;” at the prompt, we are forcing the backtracking. formal models of computation

  10. Backtracking • Another example: same program, different order • A similar execution takes place, however, when Prolog tries to prove rich(X1), the first fact it finds is rich(arnie) • The matching assigns arnie to X1; • Prolog tries to prove famous(X1) – however, it is now famous(arnie); • Prolog cannot prove famous(arnie)!! rich(arnie). rich(bill). famous(bill). friend(X):- rich(X),famous(X). ?- friend(F). formal models of computation

  11. Backtracking • Prolog backtracks to the last choice point and tries another fact/clause; • The last choice was rich(arnie) for rich(X1), and it now chooses rich(bill); • The rest of the proof is as before… formal models of computation

  12. Programming in Prolog • What kind of programming language is this? • NO built-in loops (do-while, do-until, for) • NO goto’s or if-then-else’s. • There are, however, a number of built-ins: • Operators and arithmetic (>, <, *, /, div, mod) • I/O: read, write, nl; • Graphics: windows, buttons, panels, etc; • Internet programming: fetch URLs, parse HTML, etc. • Some of the built-ins create side-effects (e.g. I/O) and so go beyond pure logic • If-then-else’s are offered as “syntactic sugar”: p :- (q -> r ; s). % if q then r else s formal models of computation

  13. Programming in Prolog (Cont’d) • The execution flow is given by the resolution proof procedure. • As programmers, we take advantage of this, e.g. in the placing of calls to built-ins that create side effects. • Iteration is achieved via recursion, i.e. a predicate defined in terms of itself. • Variables are local to a rule/fact; • There is no need to declare variables or their types. formal models of computation

  14. Programming in Prolog • An example: loop:- % this loop predicate consists of read(L), % reading a term from the standard Input (keyboard) L \= end, % if input different from “end” then loop, % carry on looping (until “end” is typed) write(L), % then print the term typed (standard output – screen) nl. % skip a line loop. % if input is “end”, then this rule is used. ?- loop. |: a. |: b. |: c. |: end. c b a yes ?- Can you follow the execution of this example? formal models of computation

  15. Programming in Prolog • Its execution: loop:- read(L), L \= end, loop, write(L), nl. loop. 1. Prolog looks for clause (fact or rule) that matches loop – first one top-down is chosen; 2. Prolog creates fresh instance of the clause and tries to prove its body: read(L1),L1 \= end,loop,write(L1),nl. 2.1 Prolog executes built-in, assigning a value to variable L1: read(L1),L1 \= end,loop,write(L1),nl. {L1/a} ?- loop. ?- loop. |: a. 2.2 Prolog compares value of variable L1: read(L1),L1 \= end,loop,write(L1),nl. {L1/a} 2.3 Prolog reaches recursive call – the same sequence of previous steps will be followed: read(L1),L1 \= end,loop,write(L1),nl. {L1/a} formal models of computation

  16. Programming in Prolog • 1st recursive call: loop:- read(L), L \= end, loop, write(L), nl. loop. 1. Prolog looks for clause (fact or rule) that matches loop – first one top-down is chosen; 2. Prolog creates fresh instance of the clause and tries to prove its body: read(L2),L2 \= end,loop,write(L2),nl. 2.1 Prolog executes built-in, assigning a value to variable L2: read(L2),L2 \= end,loop,write(L2),nl. {L2/b} ?- loop. |: a. ?- loop. |: a. |: b. 2.2 Prolog compares value of variable L2: read(L2),L2 \= end,loop,write(L2),nl. {L2/b} 2.3 Prolog reaches recursive call – the same sequence of previous steps will be followed: read(L2),L2 \= end,loop,write(L2),nl. {L2/b} formal models of computation

  17. Programming in Prolog • 2nd recursive call: loop:- read(L), L \= end, loop, write(L), nl. loop. 1. Prolog looks for clause (fact or rule) that matches loop – first one top-down is chosen; 2. Prolog creates fresh instance of the clause and tries to prove its body: read(L3),L3 \= end,loop,write(L3),nl. 2.1 Prolog executes built-in, assigning a value to variable L2: read(L3),L3 \= end,loop,write(L3),nl. {L3/c} ?- loop. |: a. |: b. ?- loop. |: a. |: b. |: c. 2.2 Prolog compares value of variable L2: read(L3),L3 \= end,loop,write(L3),nl. {L3/c} 2.3 Prolog reaches recursive call – the same sequence of previous steps will be followed: read(L3),L3 \= end,loop,write(L3),nl. {L3/c} formal models of computation

  18. Programming in Prolog • 3rd (and final!) recursive call: loop:- read(L), L \= end, loop, write(L), nl. loop. 1. Prolog looks for clause (fact or rule) that matches loop – first one top-down is chosen; 2. Prolog creates fresh instance of the clause and tries to prove its body: read(L4),L4 \= end,loop,write(L4),nl. 2.1 Prolog executes built-in, assigning a value to variable L2: read(L4),L4 \= end,loop,write(L4),nl. {L4/end} ?- loop. |: a. |: b. |: c. ?- loop. |: a. |: b. |: c. |: end. 2.2 Prolog compares value of variable L2: read(L4),L4 \= end,loop,write(L4),nl. {L4/end} 2.3 Prolog fails and backtracks, skips over read (no backtrack on built-ins) and tries another clause for loop: loop. 2.4 That’s the end of this proof/run for loop. But this is not the end of the computation!! There are 3 pending computations to be finished!! formal models of computation

  19. Programming in Prolog • 2nd recursive call (termination): loop:- read(L), L \= end, loop, write(L), nl. loop. 2.4 Prolog returns from recursive call and carries on left-to-right: read(L3),L3 \= end,loop,write(L3),nl. {L3/c} 2.5 Prolog finishes the clause, and “pops up” previous recursive call read(L3),L3 \= end,loop,write(L3),nl. NB VariableL3and its value c cease to exist!! ?- loop. |: a. |: b. |: c. |: end. ?- loop. |: a. |: b. |: c. |: end. c formal models of computation

  20. Programming in Prolog • 1st recursive call (termination): loop:- read(L), L \= end, loop, write(L), nl. loop. 2.4 Prolog returns from recursive call and carries on left-to-right: read(L2),L2 \= end,loop,write(L2),nl. {L2/b} 2.5 Prolog finishes the clause, and “pops up” previous recursive call read(L2),L2 \= end,loop,write(L2),nl. NB VariableL2and its value b cease to exist!! ?- loop. |: a. |: b. |: c. |: end. c ?- loop. |: a. |: b. |: c. |: end. c b formal models of computation

  21. Programming in Prolog • End of execution: loop:- read(L), L \= end, loop, write(L), nl. loop. 2.4 Prolog returns from recursive call and carries on left-to-right: read(L1),L1 \= end,loop,write(L1),nl. {L1/a} 2.5 Prolog finishes the clause, and terminates execution! read(L1),L1 \= end,loop,write(L1),nl. NB VariableL1and its value a cease to exist!! ?- loop. |: a. |: b. |: c. |: end. c b ?- loop. |: a. |: b. |: c. |: end. c b a yes ?- formal models of computation

  22. Programming in Prolog • Messages to take home: • Scope of variables: their clauses • No global variables!! • Lifespan of variables: while clauses are being proved • Recursion is the only means to achieve iteration formal models of computation

More Related