The basics of SICP query language DANIEL SPEICHERT

The basics of SICP query language DANIEL SPEICHERT CS 550

Basic information SICP query language is designed/implemented in Lisp/Scheme Different from Lisp/Scheme Language described in terms of the same general framework Primitive Means elements of combination

Basic information Logic programming Deductive Simple information retrieval queries – variable Compound Rules queries: and, or, not, … (assertions too, “views”, “predefined queries”)

Database definition Consists of assertions A possible nested list of lists… No required structure (address (Bitdiddle Ben) (Slumerville (Ridge Road) 10)) (job (Bitdiddle Ben) (computer wizard)) (salary (Bitdiddle Ben) 60000)

More database assertions… (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 (Fect Cy D) (Cambridge (Ames Street) 3)) (job (Fect Cy D) (computer programmer)) (salary (Fect Cy D) 35000) (supervisor (Fect Cy D) (Bitdiddle Ben)) (address (Tweakit Lem E) (Boston (Bay State Road) 22)) (job (Tweakit Lem E) (computer technician)) (salary (Tweakit Lem E) 25000) (supervisor (Tweakit Lem E) (Bitdiddle Ben)) (can-do-job (computer wizard) (computer programmer)) (can-do-job (computer wizard) (computer technician)) (can-do-job (computer programmer) (computer programmer trainee)) (can-do-job (administration secretary) (administration big wheel))

Simple queries Query: (job ? x (computer programmer)) Answer: (job (Hacker Alyssa P) (computer programmer)) (job (Fect Cy D) (computer programmer))

How simple queries work? “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. ”

Queries with no variables… If the pattern has no variables, the query reduces to a determination of whether that pattern is in the data base. If so, the empty assignment, which assigns no values to variables, satisfies that pattern for that data base.

WHERE COL 1 = COL 2 Surprisingly simple – that’s why variables are named: (supervisor ? x)

Basic matching – multiple variables Think about the query: (job ? x (computer ? type)) Looks for exactly 3 elements: “job” (exact match) Anything (? x) A list Exactly 2 elements First one is “computer” (exact match) Second is anything (? type)

LIKE… list matching On top of previous query, we now want to match all job titles starting with “computer”: computer wizard computer programmer computer technician computer programmer trainee List size is 2 or 3 – how to match it? WHERE job LIKE ‘computer %’

Dot-notation A pattern such as the third item can be any list beginning with “computer”: (job ? x (computer. ? type)) Now “(computer. ? type))” can correspond to: A list of one element (computer) ? type is ‘() A list of two elements (computer technician) ? type is (technician) A list of three elements (computer technician trainee) ? type is (technician trainee) …

Compound queries: and (and <query 1> <query 2>. . . <queryn>) is satisfied by all sets of values for the pattern variables that simultaneously satisfy <query 1>. . . <queryn> Query: (and (job ? person (computer programmer)) (address ? person ? where)) Result: (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)))

Compound queries: or (or <query 1> <query 2>. . . <queryn>) is satisfied by all sets of values for the pattern variables that satisfy at least one of <query 1>. . . <queryn> Query: (or (supervisor ? x (Bitdiddle Ben)) (supervisor ? x (Hacker Alyssa P))) Result: (or (supervisor (Hacker Alyssa P) (Bitdiddle Ben)) (supervisor (Hacker Alyssa P))) (or (supervisor (Fect Cy D) (Bitdiddle Ben)) (supervisor (Fect Cy D) (Hacker Alyssa P))) (or (supervisor (Tweakit Lem E) (Bitdiddle Ben)) (supervisor (Tweakit Lem E) (Hacker Alyssa P))) (or (supervisor (Reasoner Louis) (Bitdiddle Ben)) (supervisor (Reasoner Louis) (Hacker Alyssa P)))

Compound queries: not (not <query 1>) is satisfied by all assignments to the pattern variables that do not satisfy <query 1> Query: (and (supervisor ? x (Bitdiddle Ben)) (not (job ? x (computer programmer)))) Results: (and (supervisor (tweakit lem e) (bitdiddle ben)) (not (job (tweakit lem e) (computer programmer))))

Compound queries: lisp-value (lisp-value <predicate> <arg 1>. . . <argn>) When lisp-value is the first element of a pattern, it specifies that the next element is a Lisp predicate to be applied to the rest of the (instantiated) elements as arguments. Query: (and (salary ? person ? amount) (lisp-value > ? amount 30000)) Result: (and (salary (scrooge eben) 75000) (lisp-value > 75000 30000)) (and (salary (warbucks oliver) 150000) (lisp-value > 150000 30000)) (and (salary (fect cy d) 35000) (lisp-value > 35000 30000)) (and (salary (hacker alyssa p) 40000) (lisp-value > 40000 30000)) (and (salary (bitdiddle ben) 60000) (lisp-value > 60000 30000))

Rules - definition (rule <conclusion> <body>) where <conclusion> is a pattern and <body> is any query Example: (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))

Rules - usage Query: (lives-near ? x (Bitdiddle Ben)) Result: (lives-near (Reasoner Louis) (Bitdiddle Ben)) (lives-near (Aull De. Witt) (Bitdiddle Ben))

Rules & logic programming We can regard a rule as a kind of logical implication: If an assignment of values to pattern variables satisfies the body, then it satisfies the conclusion. Consequently, we can regard the query language as having the ability to perform logical deductions based upon the rules. Rules are therefore assertions additional information / meaning added to other assertions

Part Two… Part Two will be about: Examples Internal structure Expansion
- Slides: 20