Prolog Notes

Prolog

Origins of Prolog

  • Developed at the University of Aix-Marseille (Calmerauer & Roussel) for natural language processing.

  • Also developed at the University of Edinburgh (Kowalski) for automated theorem proving.

Terms

  • Uses the Edinburgh syntax.

  • Term: can be a constant, variable, or structure.

  • Constant: an integer or an atom.

  • Atom: symbolic value.

    • Consists of:

      • A string of letters, digits, and underscores beginning with a lowercase letter.

      • A string of printable ASCII characters delimited by apostrophes.

Variables and Structures

  • Variable: a string of letters, digits, and underscores beginning with an uppercase letter.

  • Instantiation: Binding of a variable to a value; lasts only as long as it takes to satisfy one complete goal.

  • Structure: Represents atomic proposition using the format: functor(parameter list).

Fact Statements

  • Used for hypotheses.

  • Also known as Headless Horn clauses.

  • Examples:

    • female(shelley).

    • male(bill).

    • female(mary).

    • male(jake).

    • father(bill, jake).

    • father(bill, shelley).

    • mother(mary, jake).

    • mother(mary, shelley).

Rule Statements

  • Used for hypotheses and also known as Headed Horn clause.

  • Right side: antecedent (if part); can be a single term or conjunction.

  • Left side: consequent (then part); must be a single term.

  • Conjunction: multiple terms separated by logical AND operations (implied).

Example Rules

  • ancestor(mary,shelley):- mother(mary,shelley).

  • Using variables to generalize meaning:

    • parent(X,Y):- mother(X,Y).

    • parent(X,Y):- father(X,Y).

    • grandparent(X,Z):- parent(X,Y), parent(Y,Z).

Goal Statements

  • Used for theorem proving where the theorem is in the form of a proposition that we want the system to prove or disprove.

  • Also known as goal statement.

  • Same format as headless Horn:

    • man(fred)

  • Conjunctive propositions and propositions with variables are legal goals:

    • father(X, mike)

Inferencing Process

  • Queries are called goals.

  • If a goal is a compound proposition, each of the facts is a subgoal.

  • To prove a goal is true, must find a chain of inference rules and/or facts.

  • For goal Q:

    • P<em>2:−P</em>1P<em>2 :- P</em>1

    • P<em>3:−P</em>2P<em>3 :- P</em>2

    • ⋮\vdots

    • Q:−PnQ :- P_n

  • Matching, satisfying, or resolution: The process of proving a subgoal.

Matching Approaches

  • Matching is the process of proving a proposition.

  • Proving a subgoal is called satisfying the subgoal.

  • Bottom-up resolution, forward chaining:

    • Begin with facts and rules of the database and attempt to find a sequence that leads to the goal.

    • Works well with a large set of possibly correct answers.

  • Top-down resolution, backward chaining:

    • Begin with the goal and attempt to find a sequence that leads to a set of facts in the database.

    • Works well with a small set of possibly correct answers.

  • Prolog implementations use backward chaining because it’s efficient for most cases.

Matching Approaches: Example

  • Knowledge Base (in Prolog):

    • parent(alice, bob).

    • parent(bob, charlie).

    • grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

  • Query:

    • ?- grandparent(alice, charlie).

Matching Approaches: Examples

  • Bottom-up resolution, forward chaining:

    • Step 1: Known facts

      • parent(alice, bob)

      • parent(bob, charlie)

    • Step 2: Apply rules

      • grandparent(alice, charlie) :- parent(alice, bob), parent(bob, charlie)

    • Step 3: Derived

      • grandparent(alice, charlie)

Matching Approaches: Examples

  • Top-down resolution, backward chaining:

    • Goal: ?- grandparent(alice, charlie).

    • Step 1: Try to prove grandparent(alice, charlie)

      • Matches rule: grandparent(X, Y) :- parent(X, Z), parent(Z, Y)

    • Step 2: Bind X=alice, Y=charlie; need to check:

      • parent(alice, Z) and parent(Z, charlie)

    • Step 3: Find Z

      • parent(alice, bob) → Z = bob

    • Step 4: Check parent(bob, charlie) → true

    • Success: Proved grandparent(alice, charlie)

Subgoal Strategies

  • When a goal has more than one subgoal, can use either:

    • Depth-first search: Find a complete proof for the first subgoal before working on others.

    • Breadth-first search: Work on all subgoals in parallel.

  • Prolog uses depth-first search because it can be done with fewer computer resources.

Backtracking

  • With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking.

  • Begin search where previous search left off.

  • Example: male(X), parent(X, shelley).

  • Can take lots of time and space because may find all possible proofs to every subgoal.

Simple Arithmetic

  • Prolog supports integer variables and integer arithmetic.

  • is operator: takes an arithmetic expression as the right operand and a variable as the left operand.

    • A is B / 17 + C

  • Not the same as an assignment statement.

    • The following is illegal: Sum is Sum + Number because Sum is not instantiated before the evaluation of the expression.

    • Solution: NewSum is OldSum + Number.

Example

  • Facts:

    • speed(ford,100).

    • speed(chevy,105).

    • speed(dodge,95).

    • speed(volvo,80).

    • time(ford,20).

    • time(chevy,21).

    • time(dodge,24).

    • time(volvo,24).

  • Rule:

    • distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time.

  • Query:

    • distance(chevy, Chevy_Distance).

Trace

  • Built-in structure that displays instantiations at each step.

  • Tracing model of execution - four events:

    • Call: Beginning of attempt to satisfy goal.

    • Exit: When a goal has been satisfied.

    • Redo: When backtrack occurs.

    • Fail: When goal fails.

Example

  • Facts:

    • likes(jake,chocolate).

    • likes(jake, apricots).

    • likes(darcie, licorice).

    • likes(darcie, apricots).

  • Query:

    • trace.

    • likes(jake, X), likes(darcie, X).

  • Trace Output:

    • (1) 1 Call: likes(jake, _0)?

    • (1) 1 Exit: likes(jake, chocolate)

    • (2) 1 Call: likes(darcie, chocolate)?

    • (2) 1 Fail: likes(darcie, chocolate)

    • (1) 1 Redo: likes(jake, _0)?

    • (1) 1 Exit: likes(jake, apricots)

    • (3) 1 Call: likes(darcie, apricots)?

    • (3) 1 Exit: likes(darcie, apricots)

    • X = apricots

List Structures

  • Other basic data structure (besides atomic propositions).

  • List is a sequence of any number of elements.

  • Elements can be atoms, atomic propositions, or other terms (including other lists).

  • Examples:

    • [apple, prune, grape, kumquat]

    • [] (empty list)

    • [X | Y] (head X and tail Y)

Deficiencies of Prolog

  • Resolution order control: In a pure logic programming environment, the order of attempted matches is nondeterministic and all matches would be attempted concurrently.

  • The closed-world assumption: The only knowledge is what is in the database, anything not stated is assumed to be false.

  • The negation problem: Anything not stated in the database is assumed to be false.

Applications of Logic Programming

  • Relational database management systems

  • Expert systems

  • Natural language processing

Summary

  • Symbolic logic provides the basis for logic programming.

  • Logic programs should be nonprocedural.

  • Prolog statements are facts, rules, or goals.

  • Resolution is the primary activity of a Prolog interpreter.

  • Although there are a number of drawbacks with the current state of logic programming, it has been used in a number of areas.