# Lecture: A Simple Query Language

### Background Material

• Chapter 4 (sec. 4.4) SICP

### Theme

As programmers, we are used to describing "how to" do things and writing programs that explicitly show how to compute values. Most programming languages are biased towards uni-directional computation - given an input compute the corresponding output. In logic programming the emphasis is on specifying knowledge and the underlying control structure carries out computation. Moreover, it is possible to compute multiple results (relations rather than functions) that satisfy given constraints.

A good example of this is the append function that we saw in Lecture on scheme which stated how to recursively compute the concatenation of two lists. This function can be used to answer questions such as what is the concatenation of two given lists. The basis for this function can be interpreted as two rules: 1) what is the concatenation an empty list with a list and 2) how the concatenation of two lists is related to the concatenation of the list obtained by removing its first element and a second list. These rules specify when three lists are such that the third is the concatenation of the first two. A logic programming language can use these rules to obtain a list that concatenates with a given list to obtain a given third list in addition to concatenating two given lists. Moreover, the same rules, with an appropriate underlying control mechanism, can be used to find all pairs of lists that concatenate to obtain a third given list.

In this lecture we provide a simple logic programming language, 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 implemenation of this language are pattern-matching, binding, and a generalization of pattern matching called unification that can be used for deduction.

### Topics

1. Query language without rules
1. Assertions and queries (Sec. 4.4.1 of SICP)
• Simple queries
• Dot notation
• Compound queries
2. Architecture (Sec. 4.4.2 of SICP)
• Pattern matching
• Streams of frames
• Compound queries (concatentation, merge, and filter)
3. Implementation (Sec. 4.4.4 of SICP)
• query-driver-loop and instantiate
• qeval (simple-query, conjoin, disjoin, negate, list-value)
• find-assertions, check-an-assertion, fetch-assertions
• pattern-match, extend-if-consistent
2. Query language with rules
1. Rules and unification (Sec. 4.4.1 of SICP)
2. Logic as programs (Sec. 4.4.1 of SICP)
3. Applying rules (Sec. 4.4.2 of SICP)
4. Implementing rules and unification (Sec. 4.4.4 of SICP)
3. Problems with logic program (logic programming &ne mathematical logic) (Sec. 4.4.3. of SICP)
1. Not as a filter
2. Infinite loops

### Exercises

• See slides.
Created: May 28, 2008 (revised May 26, 2010) by jjohnson AT cs DOT drexel DOT DOT edu