next_inactive up previous







Computational Logic
A ``Hands-on'' Introduction to Pure Logic Programming

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 Pure Logic Programs: the Ciao System's bf/af Packages

Ciao Programming Environment: file being edited and top-level

!0.93Image 005splash

Top Level Interaction Example


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 (I)

Unification Algorithm Examples (II)

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


Input:
A logic program $P$, a query $Q$
Output:
$Q\mu$ (answer substitution) if $Q$ is provable from $P$, failure otherwise
Algorithm:
  1. Initialize the ``resolvent'' $R$ to be $\{ Q \}$
  2. While $R$ is nonempty do:
    1. Take the leftmost literal $A$ in $R$
    2. Choose a (renamed) clause $A'
\leftarrow B_1, \ldots, B_n$ from $P$,
      such that $A$ and $A'$ unify with unifier $\theta$
      (if no such clause can be found, branch is failure; explore another branch)
    3. Remove $A$ from $R$, add $B_1, \ldots, B_n$ to $R$
    4. Apply $\theta$ to $R$ and $Q$
  3. If $R$ is empty, output $Q$ (a solution). Explore another branch for more sol's.

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

Running programs

C$_1$: pet(X) <- animal(X), barks(X).
C$_2$: pet(X) <- animal(X), meows(X).

C$_3$: animal(spot) <-.
C$_4$: animal(barry) <-.
C$_5$: animal(hobbes) <-.
C$_6$: barks(spot) <-.
C$_7$: meows(barry) <-.
C$_8$: roars(hobbes) <-.

Running programs (different strategy)


C$_1$: pet(X) <- animal(X), barks(X).
C$_2$: pet(X) <- animal(X), meows(X).

C$_3$: animal(spot) <-.
C$_4$: animal(barry) <-.
C$_5$: animal(hobbes) <-.
C$_6$: barks(spot) <-.
C$_7$: meows(barry) <-.
C$_8$: roars(hobbes) <-.


The Search Tree

Characterization of The Search Tree

figure=/home/logalg/public_html/slides/Figs/search_cases.eps,width=0.6

Depth-First Search

figure=/home/logalg/public_html/slides/Figs/search_cases_df.eps,width=0.6

Breadth-First Search



figure=/home/logalg/public_html/slides/Figs/search_cases_bf.eps,width=0.6

Role of Unification in Execution and Modes

Database Programming


Database Programming (Contd.)


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


Structured Data and Data Abstraction (and The Anonymous Variable)

Structured Data and Data Abstraction (Contd.)


Logic Programs and the Relational DB Model


Traditional $\rightarrow$ Codd's Relational Model  
File Relation Table
Record Tuple Row
Field Attribute Column

 $$
Example:
Name Age Sex
Brown 20 M
Jones 21 F
Smith 36 M
     


Person
Name Town Years
Brown London 15
Brown York 5
Jones Paris 21
Smith Brussels 15
Smith Santander 5
     



Lived-in

 $$
The order of the rows is immaterial.
 $$
(Duplicate rows are not allowed)

Logic Programs and the Relational DB Model (Contd.)


Relational Database $\rightarrow$ 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

 $$
Example:
person(brown,20,male) <-.
person(jones,21,female) <-.
person(smith,36,male) <-.

 $$
Example:
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) <-.


Name Age Sex
Brown 20 M
Jones 21 F
Smith 36 M
     

Name Town Years
Brown London 15
Brown York 5
Jones Paris 21
Smith Brussels 15
Smith Santander 5
     

Logic Programs and the Relational DB Model (Contd.)

Logic Programs and the Relational DB Model (Contd.)

Deductive Databases

Recursive Programming


Types

Recursive Programming: Recursive Types


Recursive Programming: Arithmetic

 $$
Defining the natural order ($\leq$) of natural numbers:

less_or_equal(0,X) <- nat(X).

less_or_equal(s(X),s(Y)) <- less_or_equal(X,Y).

 $$
Multiple uses: less_or_equal(s(0),s(s(0))), less_or_equal(X,0),$\ldots$

 $$
Multiple solutions: less_or_equal(X,s(0)), less_or_equal(s(s(0)),Y), etc.

 $$
Addition:

plus(0,X,X) <- nat(X).

plus(s(X),Y,s(Z)) <- plus(X,Y,Z).

 $$
Multiple uses: plus(s(s(0)),s(0),Z), plus(s(s(0)),Y,s(0))

 $$
Multiple solutions: plus(X,Y,s(s(s(0)))), etc.

Recursive Programming: Arithmetic

Recursive Programming: Arithmetic

Recursive Programming: Arithmetic/Functions


Recursive Programming: Lists

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: Binary Trees

Recursive Programming: Binary Trees

Creating a Binary Tree in Pascal and LP

Polymorphism

Recursive Programming: Manipulating Symbolic Expressions


Recursive Programming: Manipulating Symb. Expressions (Contd.)

Recursive Programming: Automata (Graphs)


Recursive Programming: Automata (Graphs) (Contd.)


Recursive Programming: Towers of Hanoi


\psfig{figure=/home/logalg/public_html/slides/Figs/hanoi.eps,width=\basewidth}

Recursive Programming: Towers of Hanoi (Contd.)

Recursive Programming: Towers of Hanoi (Contd.)

Learning to Compose Recursive Programs


next_inactive up previous
Last modification: Thu Feb 28 14:38:26 CET 2008 <webmaster@clip.dia.fi.upm.es>[CLIP] [FIM] [UNM]