Contents

 

 
Computational Logic
 
A “Hands-on” Introduction to (Pure) Logic Programming
 

Note: slides with executable links. Follow the   run example \longmapsto  links to execute the example code.

Syntax: Terms (Variables, Constants, and Structures)

(using Prolog notation conventions)

Variables, constants, and structures as a whole are called terms (they are the terms of a “first–order language”): the data structures of a logic program.

Syntax: Terms

(using Prolog notation conventions)

Syntax: Rules and Facts (Clauses)

Syntax: Predicates, Programs, and Queries

“Declarative” Meaning of Facts and Rules

The declarative meaning is the corresponding one in first order logic, according to certain conventions:

“Declarative” Meaning of Predicates and Queries

“Execution” and Semantics

Running Programs in a Logic Programming System

See the part on Developing Programs with a Logic Programming System
for more details on the particular system used in the course (Ciao).

Simple (Top-Down) Operational Meaning of Programs

In the following we define a more precise operational semantics.

Unification: uses

Unification

Unification

Unification Algorithm

Unification Algorithm Examples

run example \longmapsto

Unification Algorithm 2 (Just A More Formal Version)

Unification Algorithm 2 - Examples (I)

Unification Algorithm 2 - Examples (II)

A (Schematic) Interpreter for Logic Programs (SLD–resolution)

Input: A logic program PP, a query QQ
Output: μ\mu (answer substitution) if QQ is provable from PP, failure otherwise

  1. Make a copy QQ' of QQ

  2. Initialize the “resolvent” RR to be {Q}\{ Q \}

  3. While RR is nonempty do:

    1. Take a literal AA in RR

    2. Take a clause A:-B1,,BnA' \texttt{:-} B_1, \ldots, B_n (renamed) from PP
      with AA' same predicate symbol as AA

      1. If there is a solution θ\theta to A=AA=A' (unification)

        • Replace AA in RR by B1,,BnB_1, \ldots, B_n

        • Apply θ\theta to RR and QQ

      2. Otherwise, take another clause and repeat

    3. If there are no more clauses, go back to some other choice

    4. If there are no pending choices left, output failure

  4. (RR empty) Output solution μ\mu to Q=QQ = Q'

  5. Explore another pending branch for more solutions (upon request)

A (Schematic) Interpreter for Logic Programs (Standard Prolog)

Input: A logic program PP, a query QQ
Output: μ\mu (answer substitution) if QQ is provable from PP, failure otherwise

  1. Make a copy QQ' of QQ

  2. Initialize the “resolvent” RR to be {Q}\{ Q \}

  3. While RR is nonempty do:

    1. Take the leftmost literal AA in RR

    2. Take the first clause A:-B1,,BnA' \texttt{:-} B_1, \ldots, B_n (renamed) from PP
      with AA' same predicate symbol as AA

      1. If there is a solution θ\theta to A=AA=A' (unification)

        • Replace AA in RR by B1,,BnB_1, \ldots, B_n

        • Apply θ\theta to RR and QQ

      2. Otherwise, take the next clause and repeat

    3. If there are no more clauses, go back to most recent pending choice

    4. If there are no pending choices left, output failure

  4. (RR empty) Output solution μ\mu to Q=QQ = Q'

  5. Explore the most recent pending branch for more solutions (upon request)

A (Schematic) Interpreter for Logic Programs (Contd.)

Running Programs: Alternative Execution Paths

C1_1: pet(X) :- animal(X), barks(X).
C2_2: pet(X) :- animal(X), meows(X).
C3_3: animal(tim).    C6_6: barks(spot).
C4_4: animal(spot).   C7_7: meows(tim).
C5_5: animal(hobbes). C8_8: roars(hobbes).

      

Running Programs: Alternative Execution Paths

C1_1: pet(X) :- animal(X), barks(X).
C2_2: pet(X) :- animal(X), meows(X).
C3_3: animal(tim).    C6_6: barks(spot).
C4_4: animal(spot).   C7_7: meows(tim).
C5_5: animal(hobbes). C8_8: roars(hobbes).

      

The Search Tree Revisited

pet(X) :- animal(X), barks(X).       animal(tim).         barks(spot).
pet(X) :- animal(X), meows(X).       animal(spot).
                                     animal(hobbes).

Characterization of The Search Tree

Depth-First Search (Backtracking)

See the part on Developing Programs with a Logic Programming System
for more details on the particular system used in the course (Ciao).

Control of Search in Depth-First Search (Backtracking)

Conventional programs (no search) execute conventionally.
Programs with search: programmer has at least three ways of controlling search:
1 The ordering of literals in the body of a clause:

Control of Search in Depth-First Search (Backtracking) (Contd.)

2 The ordering of clauses in a predicate:

 
3 The pruning operators (e.g., “cut”), which cut choices dynamically –see later.

Role of Unification in Execution

“Modes”

 

 
Computational Logic
 
Pure Logic Programming Examples
 

Pure Logic Programs (Overview)

(Recall the initial slides for the course.)

Database Programming

Database Programming (Contd.)

Structured Data and Data Abstraction (and the ’=’ Predicate)

Structured Data and Data Abstraction (and The Anonymous Variable)

Terms as Data Structures with Pointers

main :- 
    X=f(K,g(K)),
    Y=a, 
    Z=g(L),
    W=h(b,L), 
% Heap memory at this point-> 
    p(X,Y), 
    q(Y,Z), 
    r(W).

Structured Data and Data Abstraction (Contd.)

Logic Programs and the Relational DB Model

Relational Database Logic Programming
Relation Name \rightarrow Predicate symbol
Relation \rightarrow Procedure consisting of ground facts
(facts without variables)
Tuple \rightarrow Ground fact
Attribute \rightarrow Argument of predicate

“Person”

Name Age Sex
Brown 20 M
Jones 21 F
Smith 36 M
person(brown,20,male).
person(jones,21,female).
person(smith,36,male).


“Lived in”

Name Town Years
Brown London 15
Brown York 5
Jones Paris 21
Smith Brussels 15
Smith Santander 5
lived_in(brown, london,  15).
lived_in(brown, york,     5).
lived_in(jones, paris,   21).
lived_in(smith, brussels,15).
lived_in(smith, santander,5).


The argnames package can be used to give names to arguments:

:- use_package(argnames).
:- argnames person(name,age,sex).
:- argnames lived_in(name,town,years).


run example \longmapsto

Logic Programs and the Relational DB Model (Contd.)

Deductive Databases

Recursive Programming

Types

Recursive Programming: Recursive Types

Recursive Programming: Arithmetic

Recursive Programming: Arithmetic

Recursive Programming: Arithmetic

Recursive Programming: Arithmetic/Functions

Functional Syntax: Packages and Directives (I)

Functional Syntax: Packages and Directives (II)

Recursive Programming: Arithmetic/Functions (Functional Syntax)

Recursive Programming: Arithmetic/Functions (Funct. Syntax, Contd.)

Recursive Programming: Lists

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Lists (Contd.)

Recursive Programming: Binary Trees

Recursive Programming: Binary Trees

Polymorphism

Recursive Programming: Manipulating Symbolic Expressions

run example \longmapsto

polynomial(X,X).
polynomial(Term,X)        :- pconstant(Term).
polynomial(Term1+Term2,X) :- polynomial(Term1,X), polynomial(Term2,X). 
polynomial(Term1-Term2,X) :- polynomial(Term1,X), polynomial(Term2,X).
polynomial(Term1*Term2,X) :- polynomial(Term1,X), polynomial(Term2,X).
polynomial(Term1/Term2,X) :- polynomial(Term1,X), pconstant(Term2).
polynomial(Term1^N,X)     :- polynomial(Term1,X), nat(N).

Recursive Programming: Manipulating Symb. Expressions (Contd.)

Recursive Programming: Graphs

Recursive Programming: Graphs (Exercises)

 

 

Recursive Programming: Automata (Graphs)

Recursive Programming: Automata (Graphs) (Contd.)

Recursive Programming: Towers of Hanoi

Recursive Programming: Towers of Hanoi (Contd.)

Recursive Programming: Towers of Hanoi (Contd.)

Learning to Compose Recursive Programs