Simple Query Language: A Logic Programming Approach (CS 550) - Prof. Jeremy R. Johnson, Papers of Programming Languages

A simple query language for retrieving information from a database using logic programming. The language includes concepts such as pattern matching, binding, and unification for deduction. The document also covers the implementation of the language, including database of assertions, simple queries, compound queries, rules, and system architecture.

Typology: Papers

Pre 2010

Uploaded on 08/19/2009

koofers-user-o4t
koofers-user-o4t 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Programming Languages
(CS 550)
Simple Query Language*
Jeremy R. Johnson
*Material for this lecture comes from SICP chapter 4
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Simple Query Language: A Logic Programming Approach (CS 550) - Prof. Jeremy R. Johnson and more Papers Programming Languages in PDF only on Docsity!

Programming Languages

(CS 550)

Simple Query Language*^ Jeremy R. Johnson *Material for this lecture comes from SICP chapter 4

Theme

™In this lecture we provide a simple logicprogramming language (from SICP), called aquery language since it can be used to retrieveinformation - by lookup or deduction - from adatabase. In addition to studying the language, wewill study an implementation of the language. Thekey concepts in the implementation of thislanguage are pattern-matching, binding, and ageneralization of pattern matching calledunification that can be used for deduction.

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

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)

Compound Queries

(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 – not as a filter (and (supervisor ?x (Bitdiddle Ben))^ (not (job ?x (computer programmer))))

Rules

(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: (lives-near (Reasoner Louis) (Bitdiddle Ben)) (lives-near (Aull DeWitt) (Bitdiddle Ben))

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

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 fromdatabase

Compound Queries

(or A B)

Input stream of frames

Output stream of frames,extended and filtered A^ B Stream of assertions fromdatabase

Merge

Not as a Filter^ (not A)

Input stream of frames

Output stream of frames,extended and filtered

Stream of assertions fromdatabase

A

Remove if satisfied

Simple Queries & Pattern Matching ™Match query against assertions in database ™(pattern-match pat dat frame)^ ¾^ Attempt to match pattern and datum consistent withbindings 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

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

Streamlining Pattern Matching ™Speed up pattern matching by limiting thedatabase entries that must be matched^ ¾If car of pattern is constant, match againststream 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))

Applying Rules

™Unify the query with the conclusion of the rule toform, if successful, an extension of the originalframe. ™Relative to the extended frame, evaluate the queryformed 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))))