CSE 130: Spring 2012

Logic Programming

Ranjit Jhala, UC San Diego

News

PA5, 6 Issues

Final

Logic Programming

Traditional Languages

Prolog

Logic Programming

Prolog History

Prolog : Original Vision "Expert Systems"

Collection of Facts

Collection of Rules

Queries

Deductions

You don't RUN a Prolog Program, you ASK it QUESTIONS

Declarative Programming

Declarative Programming: Ideal for Searching Large Space of Results

Declarative Programming

Ideal for Searching Large Space of Results

Philosophy

Declarative Programming: Super Mainstream Example ???

Declarative Programming: Application Orbitz/Expedia/etc.

Collection of Facts

Collection of Rules

If

Then

Queries

Declarative Programming: "Operations Research"

Specifically Linear Programming (CSE 101)

Collection of Facts

Collection of Rules

Queries

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!    */  

Declarative Programming

Used heavily in many domains (together with statistical methods)

Prolog: New Way To Think About Programming ...

Prolog: ... Programming As Proving!

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Language: Terms

Prolog Program

... but facts and rules about what ?

Terms are Prolog's way of representing Data

"Tree-like" values, similar to Ocaml ADTs

Four Kinds of Terms

  1. Constants
  1. Atoms
  1. Variables
  1. Compound Terms

Prolog Terms: Constants

Constants the simplest term, representing primitive values

Prolog Terms: Atoms

Atom: any identifier starting with lower-case

Prolog Terms: Atoms

Atom: any identifier starting with lower-case

Atoms are not variables

Prolog Terms: Atoms

Atom: any identifier starting with lower-case

Atoms are Uninterpreted Constants (Names)

Prolog Terms: Variables

Variables: any identifier starting with upper-case

Variables are quite special ...

Warning: Upper vs. Lowercase is common source of errors

Prolog Terms: Compound Terms

Compound terms are of form atom(term1, term2, term3, ...)

Where each term is one-of

Prolog Terms: Compound Terms

Compound terms are of form atom(term1, term2, term3, ...)

Where each term is one-of

Examples

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

Prolog Terms ARE NOT function calls

Prolog Compound Terms

Prolog Terms: Compound Terms

Compound terms are of form atom(term1, term2, term3, ...)

Each term is one-of - constant - atom - variable - compound term

An Ocaml Type For Prolog Terms

type term = Constant of int
          | Atom     of string 
          | Variable of string 
          | Compound of string * term list

QUIZ: An Ocaml Type For Prolog Terms

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 Compound Terms

Prolog term parent(alice, Charlie) is represented by:

Ocaml Value

Compound (Atom "parent", [Atom "alice"; Var "Charlie"])

Tree

QUIZ: An Ocaml Type For Prolog Terms

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 Terms

Prolog term factorial(5) is simply the tree

Prolog Terms

Prolog term factorial(5) is simply the tree

Function Symbols

Prolog Terms = (Tree) Structured Data

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Language: Facts

Language: Facts

Example

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).

Language: 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).

Prolog maintains a Database of facts

Predicates = Function Symbols Used For Facts

Language: 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).

Predicates = Function Symbols Used For Facts

Predicates Are Just Names: No Meaning Or Implementation

Language: 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).

Predicates Are Just Names: No Meaning Or Implementation

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Language: Queries

TBD

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Unification

TBD

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Language: Rules

TBD

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming

Programs = Facts + Rules

TBD

Plan

Language

  1. Terms
  2. Facts
  3. Queries (Implementation: Unification)
  4. Rules
  5. Programs (Implementation: Backtracking Search)

Programming