1 / 19

Logic Programming Part 3: Control Flow

Explore the importance of clause order, backtracking, and correctness in logic programming. Learn about nondeterminism and the role of "cut." Delve into examples and considerations for efficient and clear program execution. Discover the complexities of merging and sorting algorithms in logic programming.

astrickland
Download Presentation

Logic Programming Part 3: Control Flow

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. Logic Programming Part 3: Control Flow James Cheney CS 411

  2. Declarative Programming • Ideal: Write down logical formulas that define programs clearly & concisely • In logic, clause order doesn’t matter • Control flow, efficiency invisible • Reality: Must know how programs are run • Efficiency (backtracking, clause order) • Correctness (determinism, termination)

  3. Clause order matters p(X) :- p(X). p(a). Has answer X  p(a) But depth-first search doesn’t terminate.

  4. Clause order matters p(a). p(X) :- p(X). Terminates with X  p(a).

  5. Backtracking • Consider a :- b,c,d,e,f. a :- b,c,d,e,g. b. c. d. e. g. • First we solve b,c,d,e, then fail on f. • Then we solve b,c,d,e again and then g. • Duplicate effort!

  6. Order and Backtracking • If instead we do (logically equivalent) a :- f,b,c,d,e. a :- g,b,c,d,e. b. c. d. e. g. • then the failure occurs earlier, and there is less wasted effort.

  7. Nondeterminism • Consider mem(A,A::L). mem(A,B::L) :- mem(A,L). • Then ?- member (1,[1,2,3,1]). can succeed in two different ways.

  8. “Cut” • PROLOG includes a goal called “cut”, written !, for pruning the search space • Removes current backtracking state. • Allows more control over search, efficiency • However, “cut” damages correspondence to logic

  9. “Cut” Example mem(A,A::L) :- !. mem(A,B::L) :- mem(A,L). ?- mem([1,X],[[1,2],[1,3]]). yes X  2; no • Only the first solution is found.

  10. “Cut” Example • But, the definition of mem is no longer “complete”: ?- mem(X,[1,2,3,4]). yes X  1; no • “cut” may exclude desirable solutions

  11. More Reality • In PROLOG, I/O primitives are “impure” predicates • Example: main :- write(“What is your name?”), read(X), write(“Hello, ”), write(“X”). • Now duplication can also change program.

  12. Other LP systems • lProlog: • PROLOG uses FOL, lProlog uses higher-order logic • Typed • “Functional programming” (map, fold) • Powerful, but complex, higher-order unification (l X. F X) a == f a ?

  13. Sorting sorted([]). sorted([A]). sorted([A,B|M]) :- A < B, sorted(B,M). sort(L,M) :- perm(L,M), sorted (L,M). • Complexity?

  14. Mergesort msort([],[]). msort(L,L’) :- split(L,L1,L2), msort(L1,L1’), msort(L2,L2’), merge(L1’,L2’,L’). split([],[]). split([A|L],[A|M],N) :- split(L,N,M).

  15. Mergesort (Cont’d) merge([],L,L). merge(L,[],L). merge([A|L],[B|M],[A|N]) :- A <= B, merge(L,[B|M],N). merge([A|L],[B|M],[B|N]) :- A > B, merge([A|L],M,N).

  16. Other LP systems: Mercury • Mercury: typed, “pure” • ML-style polymorphism, datatypes • Modes & determinism checking • I/O primitives take “world argument” main(W0,W4) :- read(X,W1,W2), write(“Hello World”,W2,W3), write(X,W3,W4).

  17. Other LP systems: lProlog • Idea: Mix functional and logic paradigms • Term language is higher-order typed l-calculus • Requires solving hard (undecidable!) unification problems (lx. F x) a = f a  F  f, F  (lx. a) • Can encode variable binding syntax using l’s

  18. Applications • Artificial intelligence • Natural language processing • Expert systems • Constraint solving/optimization • Logic programs + constraints describe solutions to complex problems • Query languages (eg SQL, XQuery) • declarative in flavor

  19. Summary • Logic programming a powerful paradigm • “Algorithm = logic + control” • Unfortunately, for efficiency reasons, LP programs diverge from this ideal • Mathematical clarity != programming efficiency • “cut”, imperative features lead to opaque programs • Lesson: TANSTAAFL.

More Related