Associative Database Managment WIlensky Chapter 22

Similar documents
Associative Database Managment

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

Lisp Basic Example Test Questions

Announcements. Today s Menu

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

(penguin-rule (if (animal-x is bird) (animal-x can swim)) (then (animal-x is penguin))) (?x is bird)

Common LISP-Introduction

A little bit of Lisp

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Introduction to Functional Programming and basic Lisp

Pattern Matching WIlensky Chapter 21

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

UMBC CMSC 331 Final Exam

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Functional programming with Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

FP Foundations, Scheme

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

User-defined Functions. Conditional Expressions in Scheme

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Recursion & Iteration

This should prevent residual sexism, racism, favoritism lurking in the unconscious of your professor & TA from biasing grading!!

INF4820. Common Lisp: Closures and Macros

Lisp. Versions of LISP

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

A Brief Introduction to Common Lisp

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

Midterm Examination (Sample Solutions), Cmput 325

John McCarthy IBM 704

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Lecture Notes on Lisp A Brief Introduction

Heap storage. Dynamic allocation and stacks are generally incompatible.

Common Lisp. Blake McBride

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

15 Unification and Embedded Languages in Lisp

Functional Programming with Common Lisp

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Functions, Conditionals & Predicates

Modern Programming Languages. Lecture LISP Programming Language An Introduction

A Genetic Algorithm Implementation

Building a system for symbolic differentiation

ALISP interpreter in Awk

CSCI 2210: Programming in Lisp. Progn. Block. CSCI Programming in Lisp; Instructor: Alok Mehta 1. ANSI Common Lisp, Chapters 5-10

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Documentation for LISP in BASIC

Functional Programming. Pure Functional Languages

Review of Functional Programming

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Common LISP Tutorial 1 (Basic)

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Building a system for symbolic differentiation

Prolog Introduction. Gunnar Gotshalks PI-1

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Functional Programming. Contents

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming - 2. Higher Order Functions

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CMSC 331 Final Exam Section 0201 December 18, 2000

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

Robot Programming with Lisp

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

CS 314 Principles of Programming Languages

CS 842 Ben Cassell University of Waterloo

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Imperative, OO and Functional Languages A C program is

Scheme: Strings Scheme: I/O

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Streams and Lazy Evaluation in Lisp

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

MIDTERM EXAMINATION - CS130 - Spring 2005

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

19 Machine Learning in Lisp

Scheme Basics > (butfirst '(help!)) ()

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

CS61A Midterm 2 Review (v1.1)

by the evening of Tuesday, Feb 6

Functional Programming. Big Picture. Design of Programming Languages

Functional programming techniques

Department of Computer and information Science Norwegian University of Science and Technology

1.3. Conditional expressions To express case distinctions like

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

Scheme: Expressions & Procedures

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Introduction to Functional Programming

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSCI337 Organisation of Programming Languages LISP

CS 314 Principles of Programming Languages

a little more on macros sort of like functions, but..

Introduction to Lisp

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functional programming in LISP

Introduction to lambda calculus Part 3

Transcription:

Associative Database Managment WIlensky Chapter 22 DB-1

Associative Database An associative database is a collection of facts retrievable by their contents» Is a poodle a dog? Which people does Alice manage?» As opposed to retrieving facts by their position in the data base Give me the 10'th fact What is the 10'th fact The facts in a database can be stored as patterns We can use a pattern matcher to search for facts in a database» I.e. match a query pattern against the patterns in the database looking for one or more matches DB-2

Example database facts Simple facts have no variables (dog fido) Fido is a dog (loves John Mary) John loves Mary Can have more complex facts (implies (dog?x) (animal?x)) x is a dog implies x is an animal (loves?x?x) A person loves themselves One has to carefully consider how to represent facts. In the Lisp world it is customary to have the first item on a list be the main predicate and the remaining items be the arguments to the predicate DB-3

Example queries Queries are patterns themselves they can be without variables» Is fido a dog? (dog fido)» Does John love Mary? (loves John Mary) Can have more complex queries» What is fido? (?what fido)» Who does John love? (loves John?who)» Who loves whom? (loves?who?whom) DB-4

Implementation 1 In designing a database we need to consider how the facts will be stored In our first implementation the facts are all stored in a list. (dog fido) (loves John Mary) (implies (dog?x) (animal?x)) (loves?x?x) --> ( (loves (*var* #:var12) (*var* #:var12)) (implies (dog (*var* #:var11)) (animal (*var* #:var11))) (loves john mary) (dog fido) ) DB-5

Add to the database Store the database as the value of a symbol.» Want to pass an unevaluated pattern and unevaluated symbol to our add operation so use a macro» Change the value of the symbol to update the database» Replace the names of the pattern matching variables to be unique (defmacro add-to-data-base (item d-b-name) `(setq,d-b-name (cons (replace-variables (quote,item)),d-b-name))) DB-6

Replace variable names Replace the variables names in item» Replacing variables names needs to be done consistently.» Create a binding list that keeps track of renaming.» Start off with a nil binding» Returns the rebuilt item and the bindings of old and new variable names (defun replace-variables (item) (values (replace-variables-with-bindings item nil))) DB-7

Replace variable names using bindings 1 Use the current bindings to replace variables consistently (defun replace-variables-with-bindings (item bindings) For an atom nothing to replace (cond ((atom item) (values item bindings)) For a pattern variable return a replacement, if necessary ((pattern-var-p item) (let ((var-binding (get-binding item bindings))) (if var-binding ; if on binding list return the binding (values var-binding bindings) ; else generate a new symbol (let ((newvar (list '*var* (gensym "VAR")))) (values newvar (add-binding item newvar bindings)))))) DB-8

Replace variable names using bindings 2» Item is neither an atom nor a pattern variable use recursion» Have to remember bindings from the "car" recursion for the "cdr" recursion (t (multiple-value-bind (newlhs lhsbindings) )) (replace-variables-with-bindings (car item) bindings) (multiple-value-bind (newrhs finalbindings) (replace-variables-with-bindings (cdr item) lhsbindings) (values (cons newlhs newrhs) finalbindings)))) DB-9

Replace variable examples (replace-variables '(loves john mary)) --> (LOVES JOHN MARY) (replace-variables '(loves?x?x)) --> (LOVES (*VAR* #:VAR20) (*VAR* #:VAR20)) DB-10

Start a database (setq DB nil) (add-to-data-base (loves john mary) DB) --> ((loves john mary)) (add-to-data-base (loves?x?x) DB) --> ((loves (*var* #:var22) (*var* #:var22)) (loves john mary)) (add-to-data-base (dog fido) DB) --> ((dog fido) (loves (*var* #:var22) (*var* #:var22)) (loves john mary)) DB-11

Query the data base Use the matcher program to query the database» Returns a list of bindings that match (defun query (request data-base) (mapcan #'(lambda (item) data-base) (multiple-value-bind (flag bindings) (if flag (list bindings)))) (match item request) )» mapcan is like mapcar except it uses nconc in place of append Destructive replacement of the cdr part of a cell for speed DB-12

Example queries (query '(fido dog) DB) ; not in database --> nil (query '(dog fido) DB) ; in DB - no variables --> (nil) (query '(loves john john) DB) ; in DB - hidden variables --> ((((*var* #:var22) john))) (query '(dog?name) DB) ; Variable in query --> ((((*var* name) fido))) (query '(loves?x?y) DB) ; Multiple matches --> ((((*var* x) (*var* y)) ((*var* #:var61) (*var* x))) (((*var* y) mary) ((*var* x) john))) DB-13

Implementation 2 Previous implementation becomes slow as the database increases in size.» Search is O(n) where n is the number of facts Reduce search time by indexing the facts» Put facts with different predicates on different lists» Put facts with the same predicate on the same list» Search significantly shorter lists by only searching lists that match the predicate in the query The fact lists are put on the property list of the predicate with the key being the database symbol» Facts could be in some databases and not in others DB-14

Indexing example Enter the following into the indexed database (index '(loves john mary) 'DB) (index '(loves?x?x) 'DB) (index '(person john) 'DB) (index '(poodle fido) 'DB) Then look at the property lists for the predicates (symbol-plist 'person) --> (db ((person john))) (symbol-plist 'poodle) --> (db ((poodle fido))) (symbol-plist 'loves) --> (db ((loves (*var* #:var13) (*var* #:var13)) (loves john mary))) DB-15

Other index lists The previous examples assumed that facts would begin with an atom that could become a symbol with a propertly list What if a fact begins with a list?» For example, could represent "if x is a woman then x is mortal" as the following ( --> is a valid symbol in Lisp! ) ((?x woman) --> (?x mortal))» Have the special atom *list* to hold such facts What if a fact begins with a variable?» For example could represent "everyone loves Barney" as the following (?x loves Barney )» Have the special atom *var* to hold such facts DB-16

What about searching the entire DB? If we have a query that begins with a variable, then the variable could match a variable, a list or any atom. Hence the entire data base would need to be searched. How can we do this if the database is scattered across the property lists of many symbols? Have to keep track of the index symbols with the symbol for the database» Add to the property list for the database symbol the list of *keys* that have been used as indices.» In the example, several slides back, you could look at the symbol list for DB (symbol-plist 'DB) --> (*keys* (poodle person loves)) DB-17

Index function for a database (defun index (item data-base) place is where we want to store the item use the key for the pattern (let ((place (cond ((atom (car item)) (car item)) ((pattern-var-p (car item)) '*var*) (t '*list*)))) Store the item itself (setf (get place data-base) (cons (replace-variables item) ; rename variables (get place data-base))) Store the key for the item adjoin adds only if not there (setf (get data-base '*keys*) (adjoin place (get data-base '*keys*))))) DB-18

Fast query (defun fast-query (request data-base) (if (pattern-var-p (car request)) (mapcan #'(lambda (key) ; Search entire DB (query request (get key data-base))) (get data-base '*keys*)) (nconc else search under "atom" or *list* (query request (get (if (atom (car request)) data-base) (car request) '*list*) ) Add in search under *var* if "atom" or *list* search (query request (get '*var* data-base))))) DB-19

Deductive retrieval We use backward chaining Store implications in the database in the following form (<- consequent antecedent) In addition to querying the database in the normal way we add the following query (<- request antecedent) If the second query suceeds we recursively query using the returned antecedent as a new request (<- previous-antecedent antecedent) And so on we proceed backwards from the query to the "base facts" DB-20

Deductive retrieval example Let's add the following to the database (index '(<- (mammal?x) (dog?x)) 'DB) (index '(<- (dog?x) (poodle?x)) 'DB) (index '(poodle fido) 'DB) And make the following query (mammal fido)» matches fact 1 using the implication search with antecedent --> (dog fido) Make the recursive query matches fact 2 antecedent --> (poodle fido) Make the recursive query - matches fact 3 - no further recursion necessary» return success DB-21

Deductive retrieval function 1 (defun retrieve (request data-base) Combine a regular seach (nconc (fast-query request data-base) with a recursive search over the implications (mapcan )))... the function to apply to the implication search... Get the next level of implication search note the use of a macro to construct the pattern to use for the search (fast-query `(<-,request?antecedent) data-base) DB-22

Deductive retrieval function 2... the function to apply to the implication search... #'(lambda (bindings) Search for each of the bindings of antecedent and add to the list of bindings ) (mapcar #'(lambda (rbindings) (append rbindings bindings)) Recursive search on an antecedent. Need to replace the variables in antecedent with their values, if any ) (retrieve (substitute-vars (get-binding '?antecedent bindings) bindings) data-base) DB-23

Substituting variables Suppose we have the following binding list ((?antecedent (loves john?y)) (?y?z) (?z mary)) We do not want to search for the more general (loves john?y) Because we have bindings that restrict the value of?y A first level substitution for?z -->?y yields a search pattern of (loves john?z) But this is still too general as we have a binding for?z Need to do a second level,?mary -->?z, recursive substitution to get the pattern we want to search on (loves john mary) DB-24

Substitute variables for deductive retrieval (defun substitute-vars (item bindings) Nothing to do if item is an atom (cond ((atom item) item) Potential substitution if a variable ((pattern-var-p item) (let ((binding (get-binding item bindings))) Substitute only if we have a binding for the item (if binding (substitute-vars binding bindings) item))) Have a list, so recursively substitute on first and rest (t (cons (substitute-vars (car item) bindings) (substitute-vars (cdr item) bindings))))) DB-25