CS61A Lab 7b: Logic Programming

Week 7b, 2013


In Declarative Programming, we aim to define facts about our universe. With these in place, we can make queries in the form of assertions. The system will then check if the query is true, based on a database of facts. It will inform us of what replacements for the variables will make the query true.

The language we will use is called Logic, and an interpreter is already setup for us on the lab machines. To copy the folder to your current directory, run:

cp -r ~cs61a/lib/lab/lab07b/logic .
Just run python3 logic.py after you move your Scheme project files into the folder. Please note that you must have finished up to at least problem 4 on your project in order to do this lab, as this lab depends on your implementation of the Frame class.

Let's review the basics. In Logic, the primitive data types are called symbols: these include numbers and strings. Unlike other languages we have seen in this course, numbers are not evaluated: they are still symbols, but they do not have their regular numerical meaning. Variables in Logic are denoted with a ? mark preceding the name. So for example, ?x represents the variable x. A relation is a named tuple with a truth value.

The next thing we need to do is begin to define facts about our universe Facts are defined using a combination that starts with the fact keyword. The first relation that follows is the conclusion, and any remaining relations are hypotheses. All hypotheses must be satisfied for the conclusion to be valid.

logic> (fact (food-chain ?creature1 ?creature2) (eats ?creature1 ?creature3) (eats ?creature3 ?creature2))

Here we have defined the fact for a food chain: If creature1 eats creature3, and creature3 eats creature2, then creature1 is higher on the food chain than creature2.

Simple facts contain only a conclusion relation, which is always true.

logic> (fact (eats shark big-fish))
logic> (fact (eats big-fish small-fish))
logic> (fact (eats domo kittens))
logic> (fact (eats kittens small-fish))
logic> (fact (eats zombie brains))
logic> (fact (append (1 2) (3 4) (1 2 3 4)))

Here we have defined a few simple facts: that in our universe, sharks eat big-fish, big-fish eat small-fish, Domos eat kittens, kittens eat small-fish, zombies eat brains, and that the list (1 2) appended to (3 4) is equivalent to the list (1 2 3 4). Poor kittens.

Queries are combinations that start with the query keyword. The interpreter prints the truth value (either Success! or Failed.). If there are variables inside of the query, the interpreter will print all possible mappings that satisfy the query.

logic> (query (eats zombie brains))
logic> (query (eats domo zombie))
logic> (query (eats zombie ?what))
what: brains

We're first asking Logic whether a zombie eats brains (the answer is Success!) and if a domo eats zombies (the answer is Failed). Then we ask whether a zombie can eat something (the answer is Success!), and Logic will figure out for us, based on predefined facts in our universe, what a zombie eats. If there are more possible values for what a zombie can eat, then Logic will print out all of the possible values.


1. Within your Logic interactive session, type in the food-chain fact, and enter in the facts mentioned from above. Issue a Logic query that answers the following questions:

More complicated facts

Currently, the food-chain fact is a little lacking. A query (query (food-chain A B)) will only output Success! if A and B are separated by only one animal. For instance, if I added the following facts:

logic> (fact (eats shark big-fish))
logic> (fact (eats big-fish small-fish))
logic> (fact (eats small-fish shrimp))
I'd like the food-chain to output that shark is higher on the food chain than shrimp. Currently, the food-chain fact doesn't do this:

logic> (query (food-chain shark shrimp))

We will define the food-chain-v2 fact that correctly handles arbitrary length hierarchies. We'll use the following logic:

Notice we have two different cases for the food-chain-v2 fact. We can express different cases of a fact simply by entering in each case one at a time:

logic> (fact (food-chain-v2 ?a ?b) (eats ?a ?b))
logic> (fact (food-chain-v2 ?a ?b) (food-chain-v2 ?a ?c) (food-chain-v2 ?c ?b))
logic> (query (food-chain-v2 shark shrimp))

Take a few moments and read through how the above facts work, and how it implements the approach we outlined. In particular, make a few queries to food-chain-v2 -- for instance, try retrieving all animals that dominate shrimp!

Note: In the Logic system, multiple 'definitions' of a fact can exist at the same time (as in food-chain-v2) - definitions don't overwrite each other.

Recursively-Defined Rules

Next, we will define append in the logic style.

As we've done in the past, let's try to explain how append recursively. For instance, given two lists [1, 2, 3], [5, 7], the result of append([1, 2, 3], [5, 7]) is:

In Scheme, this would look like:
(define (append a b) (if (null? a) b (cons (car a) (append (cdr a) b))))
Thus, we've broken up append into two different cases. Let's start translating this idea into Logic! The first base case is relatively straightforward:
logic> (fact (append () ?b ?b))
logic> (query (append () (1 2 3) ?what))
what: (1 2 3)
So far so good! Now, we have to handle the general (recursive) case:
logic> (fact (append (?car . ?cdr) ?b (?car . ?partial)) (append ?cdr ?b ?partial))
This translates to: the list A appended to B is C if C is the result of sticking the CAR of A to the result of appending the CDR of A to B. Do you see how the Logic code corresponds to the recursive case of the Scheme function definition? As a summary, here is the complete definition for append:
logic> (fact (append () ?b ?b ))
logic> (fact (append (?a . ?r) ?y (?a . ?z)) (append ?r ?y ?z))

If it helps you, here's an alternate solution that might be a little easier to read:

logic> (fact (car (?car . ?cdr) ?car))
logic> (fact (cdr (?car . ?cdr) ?cdr))
logic> (fact (append () ?b ?b))
logic> (fact (append ?a ?b (?car-a . ?partial)) (car ?a ?car-a) (cdr ?a ?cdr-a) (append ?cdr-a ?b ?partial))
Meditate on why this more-verbose solution is equivalent to the first definition for the append fact.


1. Using the append fact, issue the following queries, and ruminate on the outputs. Note that some of these queries might result in multiple possible outputs.

logic> (query (append (1 2 3) (4 5) (1 2 3 4 5)))
logic> (query (append (1 2) (5 8) ?what))
logic> (query (append (a b c) ?what (a b c oh mai gawd)))
logic> (query (append ?what (so cool) (this is so cool)))
logic> (query (append ?what1 ?what2 (will this really work)))

2. Define a fact (fact (last-element ?lst ?x)) that outputs Success if ?x is the last element of the input list ?lst. Check your facts on queries such as:

logic> (query (last-element (a b c) c))
logic> (query (last-element (3) ?x))
logic> (query (last-element (1 2 3) ?x))
logic> (query (last-element (2 ?x) (3)))
Does your solution work correctly on queries such as (query (last-element ?x (3)))? Why or why not?

3. Define the fact (fact (contains ?elem ?lst)) that outputs Success if the ?elem is contained inside of the input ?lst:

logic> (query (contains 42 (1 2 42 5)))
logic> (query (contains (1 2) (a b (1) (1 2) bye)))
logic> (query (contains foo (bar baz garply)))