32
Logic Programming (Control and Backtracking)

Logic Programming (Control and Backtracking)

  • Upload
    vlora

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

Logic Programming (Control and Backtracking). Contents. Logic Programming sans Control with Backtracking Streams. Example. (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append q r (cons 1 (cons 2 empty))) Unify: (append empty x x) - PowerPoint PPT Presentation

Citation preview

Page 1: Logic Programming (Control and Backtracking)

Logic Programming (Control and Backtracking)

Page 2: Logic Programming (Control and Backtracking)

Contents

• Logic Programming– sans Control– with Backtracking

• Streams

Page 3: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append q r (cons 1 (cons 2 empty)))– Unify: (append empty x x)

• { q |→ empty, r |→ (cons 1 (cons 2 empty))

Page 4: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append q r (cons 1 (cons 2 empty)))– Unify: (append (cons w x) y (cons w z))

• (append x r (cons 2 empty))

Page 5: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append x r (cons 2 empty))– Unify: (append empty x x)

• { x |→ empty, r |→ (cons 2 empty) }

Page 6: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append x r (cons 2 empty))– Unify: (append (cons w x) y (cons w z))

• (append x r empty)

Page 7: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append x r empty)– Unify: (append empty x x)

• { x |→ empty, r |→ empty }

Page 8: Logic Programming (Control and Backtracking)

Example

• (append empty x x) ←

• (append (cons w x) y (cons w z)) ← (append x y z)

• (append x r empty)– !Unify: (append (cons w x) y (cons w z))

Page 9: Logic Programming (Control and Backtracking)

Logic Programming

• solve– Input: List of Goals– KB: List of Rules– Output: List of mappings

– Problem:• Keeping variables straight

Page 10: Logic Programming (Control and Backtracking)

Logic Programming

• Keeping variables straight– Variable renaming

• Replace:– (append empty x x) ←

• With:– (append empty v0 v0) ←

– Instantiate as needed

Page 11: Logic Programming (Control and Backtracking)

Logic Programming

(define solve

(lambda (goals)

(if (null? goals)

<then-expression>

<else-expression>)))

Page 12: Logic Programming (Control and Backtracking)

Logic Programming

(define solve

(lambda (goals)

(if (null? goals)

<found a solution>

<try to find rule for first goal>)))

• Fake Problem– Output solution when found

Page 13: Logic Programming (Control and Backtracking)

Logic Programming

(define solve

(lambda (goals soln)

(if (null? goals)

(add-to-answer soln)

<try to find all rules for first goal>)))

Page 14: Logic Programming (Control and Backtracking)

Logic Programming

(define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) (for-each (lambda (x) (let ((u (unify (car goals) (head x)))) (if u <call to solve>)))))))

Page 15: Logic Programming (Control and Backtracking)

Logic Programming

(define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) (for-each (lambda (x) (let ((u (unify (car goals) (head x)))) (if u (solve (append (cdr goals) (inst (tail x) u)) (append soln u)))))))))

Page 16: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution

Page 17: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution

(define solve

(lambda (goals soln k)

(if (null? goals)

(k soln)

…)))

Page 18: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution *and be able to

continue*

(define solve

(lambda (goals soln k)

(if (null? goals)

(backtrack k soln)

…)))

Page 19: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution *and be able to

continue*

(define backtrack

(lambda (k soln)

(call-with-current-continuation

(lambda (x) …))))

Page 20: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution *and be able to

continue*

(define backtrack

(lambda (k soln)

(call-with-current-continuation

(lambda (x) (k (cons soln x))))))

Page 21: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Want to return solution, be able to continue

*and return other solutions*

– Current “solution” will use the old continuation to return answers

Page 22: Logic Programming (Control and Backtracking)

Backtracking

(define solve-k (lambda (x) x))

(define solve (lambda (goals soln) (if (null? goals) (backtrack soln) …)))

(define backtrack (lambda (soln) (call-with-current-continuation (lambda (x) (solve-k (cons soln x))))))

Page 23: Logic Programming (Control and Backtracking)

Backtracking

• Problem– Need to return 0 argument function– When executed, function should get the

current-continuation (for return continuation)– Must be able to overwrite old return

continuation

Page 24: Logic Programming (Control and Backtracking)

Backtracking

(define backtrack

(lambda (soln)

(set! solve-k

(call-with-current-continuation

(lambda (x) …)))))

Page 25: Logic Programming (Control and Backtracking)

Backtracking

(define backtrack

(lambda (soln)

(set! solve-k

(call-with-current-continuation

(lambda (x) (solve-k (cons soln …)))))))

Page 26: Logic Programming (Control and Backtracking)

Backtracking

(define backtrack

(lambda (soln)

(set! solve-k

(call-with-current-continuation

(lambda (x) (solve-k (cons soln

(lambda () (call-with-current-continuation (lambda (y) (x y)))))))))))

Page 27: Logic Programming (Control and Backtracking)

Backtracking

• Want to return answer ASAP– Pass your answer forward

• Need return continuation– Will need to be changed with every continue– Make it global

• Remember to return continuation– (cons <answer> <continue>)

Page 28: Logic Programming (Control and Backtracking)

Backtracking

• Book– Control entirely handled by continuations

• This– Mixed control: Backtracking handled by

continuations, function control flow handled by Scheme call stack

Page 29: Logic Programming (Control and Backtracking)

Backtracking

• Book– sk (success continuation) is solve-k

• What to do with answers

– fk (failure continuation) has no direct equivalent

• Essentially says, here’s what to do next• Is related to for-each statements in code

Page 30: Logic Programming (Control and Backtracking)

Streams

• Infinite Lists– (define x (cons 1 1))– (set-cdr! x x)– (eq? x (cdr x)) → #t– (car x) → 1

Page 31: Logic Programming (Control and Backtracking)

Streams

• Infinite List– Consecutive numbers– Primes– Digits of pi– Random numbers

Page 32: Logic Programming (Control and Backtracking)

Streams

• stream = (cons <value> <func:→ stream>)

• Very similar to backtracking returns

• (car x) = (car x)

• (cdr x) = ((cdr x))