Tuesday 6/12/2012 from 7-10pm
CENTER 105
Stay tuned for review session
- Program = Algorithm + Data Structures
- Execute
- Program = Facts + Rules
- Queried

- 1970s: Logic + Automated Theorem Proving
- Developed for Artificial Intelligence
- Carnitas is Mexican
- Mexican food is delicious
- What is a delicious food ?
- Carnitas!
- Specify what you want
- Specify desired properties of result
- Not how to obtain result
Ideal for Searching Large Space of Results
- It is often hard to specify search algorithm
- But easy to specify the characteristics of the solution.
If
A to B with price (P1), duration ...B to C with price (P2), duration ...Then
A to C with price (P1 + P2), duration ...
- What is cheapest flight from SAN to JFK with duration < 6 Hrs ?
Specifically Linear Programming (CSE 101)
- El Cuervo makes two products
- CA Burrito (profit =
$2)- Fish Taco (profit =
$1)
- Burrito Capacity <
200- Taco Capacity <
500- Total Capacity <
400
- How many burritos and tacos to maximize profit?
max 2.x + 1.y /* profit */
s.t. x < 200 /* burrito capacity */
y < 400 /* taco capacity */
x+y < 300 /* total capacity */
0 < x, y /* must produce! */
Used heavily in many domains (together with statistical methods)
- Scheduling
- Travel, Sports, ...
- Rule-based Anomaly detection
- Credit card fraud!
- SQL (and similar DB Query Languages)
- Find all pairs of stocks, with same price on same day,
- More than 50 times last year
- Many of these are inspired-by / subsets of Prolog ...
- Terms
- Facts
- Queries (Implementation: Unification)
- Rules
- Programs (Implementation: Backtracking Search)
- Numeric Computation
- Data Structures
- Puzzle Solving
Prolog Program
... but facts and rules about what ?
- Terms
"Tree-like" values, similar to Ocaml ADTs
- Constants
- Atoms
- Variables
- Compound Terms
Constants the simplest term, representing primitive values
- Basic types like integers, reals
- Examples:
1,92,4.4
Atom: any identifier starting with lower-case
x,alice,taco,giraffe,appleSauce
Atom: any identifier starting with lower-case
x, alice, taco, giraffe, appleSauce
- Elements of a single mega enum type
- Similar to tags used in ML types (except ML tags are uppercase)
type atoms = x | alice | taco | giraffe | appleSauce | ...
Atom: any identifier starting with lower-case
x, alice, taco, giraffe, appleSauce
- Prolog knows NOTHING about the tags, they are just names
- Each tag is equal to itself (more later...)
alice = alicetaco = taco
- Each tag is disequal to every other tag
alice = taconever holds in Prolog- never
- ever
Variables: any identifier starting with upper-case
X, Y, Z, Head, Tail, Taco, Burrito, Alice, Bob
_ is the wildcard variable, similar to ML
- Even though
x = amakes no sense to Prolog ...
- ...
X = adoes have a meaning but not what you might think!
Compound terms are of form atom(term1, term2, term3, ...)
Where each term is one-of
Compound terms are of form atom(term1, term2, term3, ...)
Where each term is one-of
x(y, z) % y, z are atoms
parent(alice, bob) % alice, bob are atoms
parent(alice, Child) % alice is an atom, Child is a variable
- Terms are NOT Function Calls!
- More like trees
Compound terms are of form atom(term1, term2, term3, ...)
Each term is one-of - constant - atom - variable - compound term
type term = Constant of int
| Atom of string
| Variable of string
| Compound of string * term list
type term = Constant of int
| Atom of string
| Variable of string
| Compound of string * term list
(Hint: atom = lowercase, variable = uppercase)
What Ocaml value of type term represents Prolog term parent(alice, bob) ?
A. parent ("alice", "bob")
B. parent (Atom "alice", Atom "bob")
C. [Atom "parent"; Atom "alice"; Atom "bob"]
D. Compound ("parent", [Atom "alice"; Atom "bob"])
E. Compound (Atom "parent", [Atom "alice"; Atom "bob"])
Prolog term parent(alice, Charlie) is represented by:
Compound (Atom "parent", [Atom "alice"; Var "Charlie"])
type term = Constant of int
| Atom of string
| Variable of string
| Compound of string * term list
(Hint: atom = lowercase, variable = uppercase)
What Ocaml value of type term represents Prolog term factorial(5) ?
A. factorial(5)
B. factorial(Atom 5)
C. 120
D. Constant 120
E. Compound ("factorial", [Constant 5])
Prolog term factorial(5) is simply the tree 
- The term is just a box containing
5labeledfactorial
Prolog term factorial(5) is simply the tree 
5 labeled factorial
factorialjust a label called a function symbol
- Prolog has no idea about implementation of function ...
The following facts specify a list of parent-child relationships
parent(kim, holly).
parent(margaret, kim).
parent(herbert, margaret).
parent(john, kim).
parent(felix, john).
parent(albert, felix).
- Note
kim,holly,margaretetc. are all atoms
- Facts are just terms (typically without variables.)
- Specified by term followed by
.
The following facts specify a list of parent-child relationships
parent(kim, holly).
parent(margaret, kim).
parent(herbert, margaret).
parent(john, kim).
parent(felix, john).
parent(albert, felix).
- You can make up and add new facts to the collection
- We will be able to ask Prolog queries over these facts
The following facts specify a list of parent-child relationships
parent(kim, holly).
parent(margaret, kim).
parent(herbert, margaret).
parent(john, kim).
parent(felix, john).
parent(albert, felix).
- Represent functions that evaluate to a boolean
- e.g.
parentis a predicate of arity 2 (that takes 2 arguments)
The following facts specify a list of parent-child relationships
parent(kim, holly).
parent(margaret, kim).
parent(herbert, margaret).
parent(john, kim).
parent(felix, john).
parent(albert, felix).
parent is a predicate of arity 2 (that takes two arguments)
- Programmer mentally notes that:
parent(kim, holly)meanskimis a "parent-of"hollyparent(margaret, kim)meansmargaretis a "parent-of"kim- etc.
TBD
TBD
TBD
TBD
TBD