Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Table of Contents Solving Linear Systems of Equations - Dependent Systems The goal in solving a linear system of equations is to find the values of the.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Additional Scheme examples
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
PPL Lazy Lists.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CS1371 Introduction to Computing for Engineers
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Env. Model Implementation
Nondeterministic Evaluation
First-class continuations
Lecture 18 Infinite Streams and
Lecture 18.
Lecture #8 מבוא מורחב.
CS 36 – Chapter 11 Functional programming Features Practice
6.001 SICP Streams – the lazy way
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Lisp.
More Scheme CS 331.
Presentation transcript:

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) { q |→ empty, r |→ (cons 1 (cons 2 empty))

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

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

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)

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 }

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

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

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

Logic Programming (define solve (lambda (goals) (if (null? goals) )))

Logic Programming (define solve (lambda (goals) (if (null? goals) ))) Fake Problem –Output solution when found

Logic Programming (define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) )))

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

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

Backtracking Problem –Want to return solution

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

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

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

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

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

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

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

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

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

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

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 )

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

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

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

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

Streams stream = (cons ) Very similar to backtracking returns (car x) = (car x) (cdr x) = ((cdr x))