Logic Programming Part 3: Control Flow - PowerPoint PPT Presentation

About This Presentation
Title:

Logic Programming Part 3: Control Flow

Description:

Ideal: Write down logical formulas that define programs clearly ... Nondeterminism. Consider. mem(A,A::L). mem(A,B::L) :- mem(A,L). Then ?- member (1,[1,2,3,1] ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: jche6
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming Part 3: Control Flow


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 doesnt 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 doesnt 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,AL).
  • mem(A,BL) - 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,AL) - !.
  • mem(A,BL) - 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,BM) - A lt 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(AL,AM,N)
  • - split(L,N,M).

15
Mergesort (Contd)
  • merge(,L,L).
  • merge(L,,L).
  • merge(AL,BM,AN)
  • - A lt B, merge(L,BM,N).
  • merge(AL,BM,BN)
  • - A gt B, merge(AL,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 ls

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.
Write a Comment
User Comments (0)
About PowerShow.com