1 Programming Languages (CS 550) Simple Query Language* Jeremy R. Johnson *Material for this lecture comes from SICP chapter 4.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
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.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Extending Pattern Directed Inference Systems Algo & Data 2.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
Natural deduction of logical proofs Kalish & Montegue: a set of heuristics for doing logical proofs Introduction rules –not introduction, and introduction,
1 Programming Languages (CS 550) Simple Query Language* Jeremy R. Johnson *Material for this lecture comes from SICP chapter 4.
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Section 16.5, 16.6 plus other references
Operational Semantics of Scheme
Functional Programming Languages
Edited by Original material by Eric Grimson
Propositional Calculus: Boolean Functions and Expressions
6.001 SICP Variations on a Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Original material by Eric Grimson
Propositional Calculus: Boolean Functions and Expressions
Nondeterministic Evaluation
Proving Properties of Recursive Functions and Data Structures
Jess Architecture Diagram WORKING MEMORY INFERENCE ENGINE EXECUTION ENGINE PATTERN MATCHER RULE BASE Here you can see how all the parts fit.
Propositional Calculus: Boolean Algebra and Simplification
Mini Language Interpreter Programming Languages (CS 550)
The basics of SICP query language
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
topics mutable data structures
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

1 Programming Languages (CS 550) Simple Query Language* Jeremy R. Johnson *Material for this lecture comes from SICP chapter 4

2 Theme  In this lecture we provide a simple logic programming language (from SICP), called a query language since it can be used to retrieve information - by lookup or deduction - from a database. In addition to studying the language, we will study an implementation of the language. The key concepts in the implementation of this language are pattern-matching, binding, and a generalization of pattern matching called unification that can be used for deduction.

3 Outline  Query Language  Database of assertions  Simple queries  Compound queries  Rules  System architecture  Pattern matching, Unification, Stream of frames  Implementing query language w/o rules  Pattern matcher  Implementing rules  Unifyer

4 Database of Assertions (address (Bitdiddle Ben) (Slumerville (Ridge Road) 10)) (job (Bitdiddle Ben) (computer wizard)) (salary (Bitdiddle Ben) 60000) (address (Hacker Alyssa P) (Cambridge (Mass Ave) 78)) (job (Hacker Alyssa P) (computer programmer)) (salary (Hacker Alyssa P) 40000) (supervisor (Hacker Alyssa P) (Bitdiddle Ben)) (address (Reasoner Louis) (Slumerville (Pine Tree Road) 80)) (job (Reasoner Louis) (computer programmer trainee)) (salary (Reasoner Louis) 30000) (supervisor (Reasoner Louis) (Hacker Alyssa P))

5 Simple Queries ;;; Query input: (job ?x (computer programmer)) ;;; Query results: (job (Hacker Alyssa P) (computer programmer)) (job (Fect Cy D) (computer programmer)) ;;; More queries (job ?x (computer ?type)) (job ?x (computer. ?type)) (supervisor ?x ?x)

6 Processing Simple Queries  The system finds all assignments to variables in the query pattern that satisfy the pattern -- that is, all sets of values for the variables such that if the pattern variables are instantiated with (replaced by) the values, the result is in the data base.  The system responds to the query by listing all instantiations of the query pattern with the variable assignments that satisfy it.

7 Practice Question 1  Enter queries for the following  all people supervised by Ben Bitdiddle  the names and jobs of all people in the accounting division  the names and addresses of all people who live in Slumerville

8 Compound Queries ;;; (and … ) (and (job ?person (computer programmer)) (address ?person ?where)) ;;; Query results: (and (job (Hacker Alyssa P) (computer programmer)) (address (Hacker Alyssa P) (Cambridge (Mass Ave) 78))) (and (job (Fect Cy D) (computer programmer)) (address (Fect Cy D) (Cambridge (Ames Street) 3))) ;;; another compound query – (or … ) (or (supervisor ?x (Bitdiddle Ben)) (supervisor ?x (Hacker Alyssa P)))

9 Compound Queries ;;; (not ) (and (supervisor ?x (Bitdiddle Ben)) (not (job ?x (computer programmer)))) ;;; look out! – not as a filter (not (job ?name (computer programmer))) ;;; (lisp-value … ) (and (salary ?person ?amount) (lisp-value > ?amount 30000))

10 Practice Question 2  Enter queries for the following  the names of all people who are supervised by Ben Bitdiddle, together with their addresses  all people whose salary is less than Ben Bitdiddle's, together with their salary and Ben Bitdiddle's salary  all people who are supervised by someone who is not in the computer division, together with the supervisor's name and job

11 Rules ;;; (rule ) – is pattern, query (rule (lives-near ?person-1 ?person-2) (and (address ?person-1 (?town. ?rest-1)) (address ?person-2 (?town. ?rest-2)) (not (same ?person-1 ?person-2)))) (rule (same ?x ?x)) (lives-near ?x (Bitdiddle Ben)) ;;; Query results: not facts in DB, but rather derived from facts (lives-near (Reasoner Louis) (Bitdiddle Ben)) (lives-near (Aull DeWitt) (Bitdiddle Ben))

12 Rules (and (job ?x (computer programmer)) (lives-near ?x (Bitdiddle Ben))) (rule (outranked-by ?staff-person ?boss) (or (supervisor ?staff-person ?boss) (and (supervisor ?staff-person ?middle-manager) (outranked-by ?middle-manager ?boss)))) ;;; Query input: (outranked-by (reasoner louis) (bitdiddle ben)) ;;; Query results: (outranked-by (reasoner louis) (bitdiddle ben))

Rules ;;; Query input: (outranked-by (reasoner louis) ?name) ;;; Query results: (outranked-by (reasoner louis) (hacker alyssa p)) (outranked-by (reasoner louis) (bitdiddle ben)) (outranked-by (reasoner louis) (warbucks oliver)) 13

Rules ;;; Query input: (outranked-by ?name (bitdiddle ben)) ;;; Query results: (outranked-by (tweakit lem e) (bitdiddle ben)) (outranked-by (reasoner louis) (bitdiddle ben)) (outranked-by (fect cy d) (bitdiddle ben)) (outranked-by (hacker alyssa p) (bitdiddle ben)) 14

15 Logic as Programs ;;; (append-to-form L1 L2 L3) – L1 and L2 append to form L3 (rule (append-to-form () ?y ?y)) (rule (append-to-form (?u. ?v) ?y (?u. ?z)) (append-to-form ?v ?y ?z)) ;;; adding rules/facts to DB ;;; Query input: (assert! (rule (append-to-form () ?y ?y))) Assertion added to data base. ;;; Query input: (assert! (rule (append-to-form (?u. ?v) ?y (?u. ?z)) (append-to-form ?v ?y ?z))) Assertion added to data base.

16 Logic as Programs (cont) ;;; Query input: (append-to-form (a b) (c d) ?z) ;;; Query results: (append-to-form (a b) (c d) (a b c d)) ;;; Query input: (append-to-form (a b) ?y (a b c d)) ;;; Query results: (append-to-form (a b) (c d) (a b c d))

17 Logic as Programs (cont) ;;; Query input: (append-to-form ?x ?y (a b c d)) ;;; Query results: (append-to-form () (a b c d) (a b c d)) (append-to-form (a) (b c d) (a b c d)) (append-to-form (a b) (c d) (a b c d)) (append-to-form (a b c) (d) (a b c d)) (append-to-form (a b c d) () (a b c d))

18 Practice Question 3  Define a rule that says that person 1 can replace person 2 if either person 1 does the same job as person 2 or someone who does person 1's job can also do person 2's job, and if person 1 and person 2 are not the same person. Using your rule, give queries that find the following:  all people who can replace Cy D. Fect  all people who can replace someone who is being paid more than they are, together with the two salaries

19 System Architecture  For each frame match against entire database, producing a stream of extended frames  For a single query, start with single empty frame query (job (? x) (? y)) Input stream of frames Output stream of frames, extended and filtered Stream of assertions from database

20 Compound Queries (and A B) Input stream of frames Output stream of frames, extended and filtered Stream of assertions from database AB

21 Compound Queries (or A B) Input stream of frames Output stream of frames, extended and filtered Stream of assertions from database A B Merge

22 Not as a Filter (not A) Input stream of frames Output stream of frames, extended and filtered Stream of assertions from database A Remove if satisfied

23 Practice Question 4  Do the following two queries produce the same results? If not, why not. (and (supervisor ?x ?y) (not (job ?x (computer programmer)))) (and (not (job ?x (computer programmer))) (supervisor ?x ?y))

24 Simple Queries & Pattern Matching  Match query against assertions in database  (pattern-match pat dat frame)  Attempt to match pattern and datum consistent with bindings in frame  Return extended frame with bindings implied by match  Examples  dat = ((a b) c (a b)), pat = ((? x) c (? x))  x = (a b)  dat = ((a b) c (a b)), pat = ((? x) c (? y))  x = (a b), y = (a b)  dat = ((a b) c (a b)), pat = ((? x) b (? x))  failed

25 Pattern Matcher (define (pattern-match pat dat frame) (cond ((eq? frame 'failed) 'failed) ((equal? pat dat) frame) ((var? pat) (extend-if-consistent pat dat frame)) ((and (pair? pat) (pair? dat)) (pattern-match (cdr pat) (cdr dat) (pattern-match (car pat) (car dat) frame))) (else 'failed)))

26 Pattern Matcher (cont) (define (extend-if-consistent var dat frame) (let ((binding (binding-in-frame var frame))) (if binding (pattern-match (binding-value binding) dat frame) (extend var dat frame))))  binding may be a pattern if stored during unification  E.G. ?x bound to (f ?y)  Augment with ?x bound to (f b)  (pattern-match (f ?y) (f b))  ?y bound to b

27 Streamlining Pattern Matching  Speed up pattern matching by limiting the database entries that must be matched  If car of pattern is constant, match against stream of patterns starting with same constant (define THE-ASSERTIONS the-empty-stream) (define (fetch-assertions pattern frame) (if (use-index? pattern) (get-indexed-assertions pattern) (get-all-assertions))) (define (get-all-assertions) THE-ASSERTIONS) (define (get-indexed-assertions pattern) (get-stream (index-key-of pattern) 'assertion-stream))

28 Implementing Query Evaluation (define (qeval query frame-stream) (let ((qproc (get (type query) 'qeval))) (if qproc (qproc (contents query) frame-stream) (simple-query query frame-stream)))) (define (simple-query query-pattern frame-stream) (stream-flatmap (lambda (frame) (stream-append-delayed (find-assertions query-pattern frame) (delay (apply-rules query-pattern frame)))) frame-stream)) (define (find-assertions pattern frame) (stream-flatmap (lambda (datum) (check-an-assertion datum pattern frame)) (fetch-assertions pattern frame)))

29 Implementing Compound Queries (define (conjoin conjuncts frame-stream) (if (empty-conjunction? conjuncts) frame-stream (conjoin (rest-conjuncts conjuncts) (qeval (first-conjunct conjuncts) frame-stream)))) (define (negate operands frame-stream) (stream-flatmap (lambda (frame) (if (stream-null? (qeval (negated-query operands) (singleton-stream frame))) (singleton-stream frame) the-empty-stream)) frame-stream)) (put 'and 'qeval conjoin) (put 'not 'qeval negate)

30 Applying Rules  Unify the query with the conclusion of the rule to form, if successful, an extension of the original frame.  Relative to the extended frame, evaluate the query formed by the body of the rule.  (lives-near ?x (Hacker Alyssa P))  (rule (lives-near ?person-1 ?person-2) (and (address ?person-1 (?town. ?rest-1)) (address ?person-2 (?town. ?rest-2)) (not (same ?person-1 ?person-2))))

31 Unification  Similar to pattern matching except both the pattern and datum can have variables.  Unifier takes two patterns and determines if it is possible to assign values to variables that makes the two patterns equal. Return most general match  (?x a ?y) and (?y ?z a)  x = y = z = a  (?x ?y a) and (?x b ?y)  fail  (?x ?x) and ((a ?y c) (a b ?z))  y = b, z = c, x = (a b c)  (?x a) and ((b ?y) ?z)  x = (b ?y), z = a  (?x ?x) (?y (a ?y))  recursive dependancy

32 Practice Question 5  What happens when the following query is matched against the following rule?  (lives-near ?x (Hacker Alyssa P))  (rule (lives-near ?person-1 ?person-2) (and (address ?person-1 (?town. ?rest-1)) (address ?person-2 (?town. ?rest-2)) (not (same ?person-1 ?person-2))))

33 Practice Question 6  What happens when you enter the following query? (assert! (married Minnie Mickey)) (assert! (rule (married ?x ?y) (married ?y ?x))) (married Mickey ?who)

34 Unifier (define (unify-match p1 p2 frame) (cond ((eq? frame 'failed) 'failed) ((equal? p1 p2) frame) ((var? p1) (extend-if-possible p1 p2 frame)) ((var? p2) (extend-if-possible p2 p1 frame)) ; *** ((and (pair? p1) (pair? p2)) (unify-match (cdr p1) (cdr p2) (unify-match (car p1) (car p2) frame))) (else 'failed)))

35 Unifier (cont) (define (extend-if-possible var val frame) (let ((binding (binding-in-frame var frame))) (cond (binding (unify-match (binding-value binding) val frame)) ((var? val) ; *** (let ((binding (binding-in-frame val frame))) (if binding (unify-match var (binding-value binding) frame) (extend var val frame)))) ((depends-on? val var frame) ; *** 'failed) (else (extend var val frame)))))

36 Unifier (cont) (define (depends-on? exp var frame) (define (tree-walk e) (cond ((var? e) (if (equal? var e) true (let ((b (binding-in-frame e frame))) (if b (tree-walk (binding-value b)) false)))) ((pair? e) (or (tree-walk (car e)) (tree-walk (cdr e)))) (else false))) (tree-walk exp))

37 Implementing Rule Evaluation (define (apply-a-rule rule query-pattern query-frame) (let ((clean-rule (rename-variables-in rule))) (let ((unify-result (unify-match query-pattern (conclusion clean-rule) query-frame))) (if (eq? unify-result 'failed) the-empty-stream (qeval (rule-body clean-rule) (singleton-stream unify-result))))))

38 Practice Question 7  Study the code for the unifier and try several examples (you might trace the examples from slide 31)