1 / 32

Logic Programming (Control and Backtracking)

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)

vlora
Download Presentation

Logic Programming (Control and Backtracking)

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 (Control and Backtracking)

  2. Contents • Logic Programming • sans Control • with Backtracking • Streams

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

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

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

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

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

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

  9. Logic Programming • solve • Input: List of Goals • KB: List of Rules • Output: List of mappings • Problem: • Keeping variables straight

  10. Logic Programming • Keeping variables straight • Variable renaming • Replace: • (append empty x x) ← • With: • (append empty v0 v0) ← • Instantiate as needed

  11. Logic Programming (define solve (lambda (goals) (if (null? goals) <then-expression> <else-expression>)))

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

  13. Logic Programming (define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) <try to find all rules for first goal>)))

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

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

  16. Backtracking • Problem • Want to return solution

  17. Backtracking • Problem • Want to return solution (define solve (lambda (goals soln k) (if (null? goals) (k soln) …)))

  18. Backtracking • Problem • Want to return solution *and be able to continue* (define solve (lambda (goals soln k) (if (null? goals) (backtrack k soln) …)))

  19. Backtracking • Problem • Want to return solution *and be able to continue* (define backtrack (lambda (k soln) (call-with-current-continuation (lambda (x) …))))

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

  21. Backtracking • Problem • Want to return solution, be able to continue *and return other solutions* • Current “solution” will use the old continuation to return answers

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

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

  24. Backtracking (define backtrack (lambda (soln) (set! solve-k (call-with-current-continuation (lambda (x) …)))))

  25. Backtracking (define backtrack (lambda (soln) (set! solve-k (call-with-current-continuation (lambda (x) (solve-k (cons soln …)))))))

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

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

  28. Backtracking • Book • Control entirely handled by continuations • This • Mixed control: Backtracking handled by continuations, function control flow handled by Scheme call stack

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

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

  31. Streams • Infinite List • Consecutive numbers • Primes • Digits of pi • Random numbers

  32. Streams • stream = (cons <value> <func:→ stream>) • Very similar to backtracking returns • (car x) = (car x) • (cdr x) = ((cdr x))

More Related