principles of programming languages - home - newppl172/wiki.files/class/... · principles of...
TRANSCRIPT
Collaboration and ManagementDana Fisman
1
Principles ofProgrammingLanguages
Lesson 21 – An Introduction to Logic Programming –Relational Logic Programming
www.cs.bgu.ac.il/~ppl172
Slides by Yaron Gonen and Dana FismanBased on Book by Mira Balaban and
3 main paradigms in PL
Imperative Functional Declarative
Turing Machine [Turing 1936]
Lambda Calculus [Church 1936]
Constructive Logic Proofs [Kowalski, 1970,Colmerauer, 1972]
How it Came to be
Kowalski's Observation (early 70s):
An axiom H B1 and B2 ... and Bn
represents a procedure
H is the procedure’s head and the Bi’s are its body
To solve (execute) H, we recursively solve B1 ... Bn
In a quest to automate the process of proving logical statements, Colmerauer and his group embodied this observation into Prolog.
Topics
1. Relational logic programming Programs specify relations among entities
2. Logic Programming Programs with data structures: lists, binary
trees, symbolic expressions, natural numbers.
3. Meta tools Backtracking optimizations (cuts), unify, meta-
circular interpreters.
4. Prolog and more advanced programming Arithmetic, cuts, negation.
Simple Example
?- parent(sarah,Isaac).
true
?- parent(sarah,joseph).
false
?- parent(miriam,joseph).
false
db.pl
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
male(abraham).male(isaac).male(joseph).
female(sarah).
Simple Example
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
male(abraham).male(isaac).male(joseph).
female(sarah).
?- parent(sarah,X).
X = isaac
?- parent(X,isaac).
X = sarah
db.pl
X = abraham
Note the mind switch:
Working with relationsas opposed to functions
We don’t have to define multiple related
functions
Queries with vars
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
male(abraham).male(isaac).male(joseph).
female(sarah).
grand(X,Z) :- parent(Y,Z), parent(X,Y).
?- grand(sarah,jacob).
true
?- grad(X,jacob).
X = sarah
X = abraham
?- grand(sarah,joseph).
false
Adding RulesSimple Example
Fact
sRule
Logic Programming –as a programming language
Every programming language has:
Syntax What expressionsare legal
Formulas (Facts, Rules)
Semantics What is the valueof a given expression
Answer to query: true / false + assignments to the variables
Operational Semantics
An algorithm to compute the semantics
Unification, Backtracking
In Logic Programming:
Logic Programming - Mind Switch
Mind Switch:
◦ Formula (Fact/Rule) ⇔ procedure declaration
◦ Query ⇔ procedure call
◦ Proving ⇔ computation
Relational Logic Programming
A computational model based on Kowalski's interpretation
The Prolog language ('70) - contains RLP + additional programming features
Relational LP –Atomic Formulas Syntax
atomic formula:
predicate_symbol(term1, ..., termn)
Predicate symbols start with lowercase
terms:
◦ symbols (representing individual constants)
◦ variables (start with uppercase or with an underscore ‘_’ for anonymous)
parent(abraham, isaac)
ancestor(adam, Person)
address(City, Street, _Number)
Predicates and constantsare distinguished by
their context
Relational LP –Atomic Formulas Examples
Relational LP –Facts Syntax
A fact is an assertion of an atomic formula
H.
where H is an atomic formula.
Variables in facts are universally quantified.
Makes it a fact
parent(abraham, isaac).
color(red).
shape(square).
ancestor(adam, Person).
related(Person, Person).
Variables in facts are universally quantified
Relational LP –Facts Examples
parent(abraham, isaac). parent(sarah, isaac).
male(abraham).
female(sarah).
?- female(abraham).
true
Capitalization!Simple Example?- female(abraham).
false
parent(abraham, isaac). parent(sarah, isaac).
male(abraham).
female(Sarah).
Rules are formulas that state conditioned relationships between entities
H :- B1 , B2 , ... , Bn .
Meaning:
If B1 and B2 and ... Bn holdthen H also holds.
Relational LP –Rules Syntax
Rule head
Rulebody
facts
H
mother(X,Y) :- parent(X,Y), female(X).father(X,Y) :- parent(X,Y), male(X).
grand(X,Z) :- parent(X,Y), parent(Y,Z).
grandmother(X) :- female(X), grand(X,_).grandfather(X) :- male(X), grand(X,_).
Relational LP –Rules Examples
o Variables in rules are universally quantified
Variables in rule heads are universally quantified
Variables appearing only in rule bodies are existentially quantified
o Variables are bound within a rule
X, Y, Z. parent(X, Y) & parent(Y, Z)
grand(X,Z).
X, Z.( Y parent(Y, Z) & parent(Y, Z))
grand(X,Z)
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
male(abraham).male(isaac).male(joseph).
female(sarah).
grand(X,Z) :- parent(X,Y), parent(Y,Z).
mother(X) :- parent(X, _), female(X).
?- grand(sarah,X).
X = jacob
?- grad(sarah,_X).
true
?- mother(abraham).
false
anonymous varsSimple Example
?- mother(sarah).
true
father(isaac, jacob).
father(isaac, jacob):- true.
Relational LP –Rules Special Case
o Facts can be considered as rules with an empty body.
This fact and this rule have equivalent meaning.
trueis a zero-
arity predicate
Relational LP –Procedures Syntax & Examples
Procedures are ordered collections of rules and facts sharing the same name and the same arity
% Signature: parent(Parent, Child)/2
% Purpose: Parent is a parent of Child
parent(abraham, isaac).
parent(isaac, jacob).
parent(sarah, isaac).
parent(jacob, joseph).
% Signature: male(Person)/1
% Purpose: Person is a male
male(abraham).
male(isaac).
male(joseph).
Relational LP –Procedures Examples
% Signature: rise(CO, Time, City)/3% Purpose: CO is a celestial object that in City at Time % is seen as if it rises.rise(son, six-am, beer-sheva). rise(moon, _, beer-sheva).
% Signature: rise(S,B)/2% Purpose: B is a bigger amount than Srise(milliliter, liter).rise(gram, kilogram).rise(centimeter, decimeter).rise(centimeter, meter).
These are two different procedures.
A query has the syntax
?- Q1 , Q2 , ... , Qn .
Meaning:
Assuming the program facts and rules, do Q1 and Q2 and ... Qn hold?
Variables in queries are existentially quantified
Relational LP –Queries Syntax
Queryinvocation
Relational LP –Queries Syntax
?- parent(abraham, isaac).
?- parent(isaac, X).
?- parent(sarah, _X).
?- parent(sarah, _X), female(sarah).
Is abraham a parent of isaac?
Who are isaac sons?
Is sarah a parent of someone?
Is sarah a parent of someone, and a female?
queriesSimple Example
?- parent(abraham, isaac), parent(isaac, Y),parent(sarah, _X).
?- parent(sarah, _X), female(sarah).
true
?- parent(sarah, X), female(sarah).
X = isaac
Y = jacob
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).parent(jacob, dan).parent(jacob, benjamin).
male(abraham).male(isaac).male(joseph).
female(sarah).
Queries w.mult. answersSimple Example
?- parent(X, issac), parent(Y, isaac).
X = abraham, Y = abraham;X = abraham, Y = sarah; X = sarah, Y = abraham; X = sarah, Y = sarah;false.
?- parent(X, issac), parent(Y, isaac),X \= Y.
X = abraham, Y = sarah; X = sarah, Y = abraham; false.
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).parent(jacob, dan).parent(jacob, benjamin).
male(abraham).male(isaac).male(joseph).
female(sarah).
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).parent(jacob, dan).parent(jacob, benjamin).
male(abraham).male(isaac).male(joseph).
female(sarah).
Queries w.mult. answersSimple Example
?- parent(F, X), parent(F, Y),X /= Y.
F=jacob, X=joseph, Y=dan;F=jacob, X=dan, Y=joseph;F=jacob, X=joseph, Y=benjamin;F=jacob, X=benjamin, Y=joseph;F=jacob, X=dan, Y=benjamin;F=jacob, X=benjamin, Y=dan;false.
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).parent(jacob, dan).parent(jacob, benjamin).
male(abraham).male(isaac).male(joseph).
female(sarah).
Simple Example
?- parent(F, X), parent(F, Y),X /= Y.
F=jacob, X=joseph, Y=dan
F=jacob, X=benjamin, Y=joseph
F=jacob, X=dan, Y=joseph
F=jacob, X=joseph, Y=benjamin
F=jacob, X=dan, Y=benjamin
F=jacob, X=benjamin, Y=dan
Relational LPRecursive rules Syntax
The ancestor relationship - a recursive rule that computes the transitive closure of the parentrelationship.
% Signature: ancestor(A, D)/2% Purpose: A is an ancestor of D.ancestor(A,D) :- parent(A,D).ancestor(A,D) :- parent(A,P), ancestor(P,D).
Variables occurring in the rule body and not in the headare existentially quantified.
A, D.( P parent(A, P) & ancestor(P, D))
ancestor (A, D)
.
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
% Signature: ancestor(A, D)/2%Purpose: A is an ancestor of D.ancestor(A,D) :- parent(A,D).ancestor(A,D) :- parent(A,P),
ancestor(P,D).
Recursive Rules Example?- ancestor(A, joseph).
A = jacob;A = abraham;A = isaac;A = sarah;False.
?- ancestor(sarah, D).
A = isaac;A = jacob;A = joseph;False.
parent(abraham, isaac). parent(isaac, jacob).parent(sarah, isaac).parent(jacob, joseph).
% Signature: ancestor(A, D)/2%Purpose: A is an ancestor of D.ancestor(A,D) :- parent(A,D).ancestor(A,D) :- ancestor(P,D),
parent(A,P).
Recursive Rules Example?- ancestor(A, joseph).
A = jacob;A = abraham;A = isaac;A = sarah;Out of local stack.
?- ancestor(sarah, D).
A = isaac;A = jacob;A = joseph;Out of local stack.
?- ancestor(sarah, isaac).
true;Out of local stack.
ancestor(A,D) :- parent(A,D).ancestor(A,D) :- parent(A,P),
ancestor(P,D).
Recursive Rules Example
ancestor(A,D) :- parent(A,D).ancestor(A,D) :- ancestor(P,D),
parent(A,P).
A D
P
First
A D
P
Then
Ancestor(P,D)Ancestor(P2,D)Ancestor(P3,D)Ancestor(P4,D)…
FirstThen
Relational LP – Concrete Syntax
<program> ==> <procedure>+
<procedure> ==> (<rule> | <fact>)+ with identical predicate and arity
<rule> ==> <head> : - <body> .
<fact> ==> <head> .
<head> ==> <atomic-formula>
<body> ==> (<atomic-formula> ,)* <atomic-formula>
<atomic-formula> ==> <constant> | <predicate> ( (<term>,)* <term> )
<predicate> ==> <constant>
<term> ==> <constant> | <variable>
<constant> ==> A string starting with a lower case letter.
<variable> ==> A string starting with an upper case letter.
<query> ==> ?- (<atomic-formula>,)* <atomic-formula> .
Relational LP – Abstract Syntax
<program>:
Components:
<procedure>
<procedure>:
Components:
Rule: <rule>
Fact: <atomic-formula>
Overall amount of rules and facts: >=1.
Ordered.
<rule>:
Components:
Head: <atomic-formula>
Body: <atomic-formula>
Amount: >=1.
Ordered.
<atomic-formula>:Kinds: <predication>, <constant>
<predication>:Components:
Predicate: <constant>Term: <term>
Amount: >=1. Ordered.
<query>:Components:
Goal: <atomic-formula>Amount: >=1. Ordered.
<term>:Kinds: <constant>,<variable>
Relational LP – Interim Summary
Semantics:
o Quantification of variables (existential/universal)
o Answers are partial substitutions to query variables (or true/false indications)
Concepts:
o Predicate symbol
o Term
o Facts
o Rules
o Query
Operational Semantics for LP
Input: a program P and a query Q
Output: partial substitutions to query variables (or true/false indications)
Method: by trying to build a proof tree for the given query
Proof Tree - Exampleparent(abraham, isaac). %p1parent(isaac, jacob). %p2parent(sarah, isaac). %p3parent(jacob, joseph). %p4parent(jacob, dan). %p5parent(jacob, dinah). %p6
male(abraham). %m1male(isaac). %m2male(jacob). %m3male(joseph). %m4male(dan). %m5
son(X,Y) :- parent(Y,X), %s1male(X).
son(S, jacob)
parent(jacob, X1),male(X1)
%p4
{S=X1, Y1=jacob}
male(joseph) male(dan) male(dinah)
{X1=dan} {X1=dinah}
%s1
%p5 %p6
truetrue false
{X1=joseph}
S=joseph
{}%m4 {}%m5
S=dan?- son(S,jacob).
Proof Tree - DefinitionTree T is a proof tree of query Q in program P if:
o Every node is a query (list of goals)
o The root node is the query Q
o Every edge is labeled by a substitution and a rule fact name
o If there is a transition on edge e = (Subs, Rname) from node N = (G1,…,Gn)
to node N’ then:
There exists rule Rname = (H :- B1, …, Bk) in P such that H can be unified with a goal Gi via substitution Subs
and Node N’ is obtained from node N by substituting the goal Gi with B1, …, Bkand applying the substitution Subs on the result
That is, N’ = (G1,…Gi-1, B1, …, Bk , Gi+1,…Gn) Subs
A proof tree is said to be a complete proof tree of query Q in program P if no more transitions can be added.
Proof Tree –Obtaining Answers
o A leaf with goal true, is a success node
o A leaf with goal false is a failure node
o If a leaf is marked true, in order to extract the result
we can collect all substitutions along the path to the leaf,
compose them,
and return only substitutions to the required vars
son(S, jacob)
parent(jacob, X1),male(X1)
%p4
{S=X1, Y1=jacob}
male(joseph) male(dan) male(dinah)
{X1=dan} {X1=dinah}
%s1
%p5 %p6
truetrue false
{X1=joseph}
{S=X1, Y1=jacob}{X1=joseph}{} = {S=joseph,Y1=jacob,X1=jacob}
{}%m4 {}%m5
{S=X1, Y1=jacob}{X1=dan}{} = {S=dan,Y1=jacob,X1=don}
S=joseph S=dan
Proof Tree –Terminology
A proof tree is a finite tree if it has no infinite paths.
Otherwise it is an infinite tree.
A proof tree is a success tree if it has a success leaf.
Otherwise it is a failure tree.
Thus,
o given a finite tree
we can always determine whether it is a success tree or failure tree.
o given an infinite tree,
if it has a success leaf, we can determine it is a success tree,
otherwise we don’t know…
Operational Semantics for LPMethod: Build a node N for the given query Q
From current node N:
If it is a success leaf (N = true),
o extract answer and return it
o Backtrack to obtain more answers
If it is a failure leaf (N =false)
o Backtrack
If it is not a leaf node and N = (G1,…,Gn)
o select a goal Gi and
o select a rule H :- B1, …, Bk
If H can be unified with Gi via substitution Substhen create a new node N’ = (G1,…Gi-1, B1, …, Bk , Gi+1,…Gn) Subs
Otherwise, create a new node N’ = false.
o Make N’ the current node.
Input: a program P and a query Q
Output: partial subs to query variables (or true/false)
Algorithm uses procedures:• Gsel - for selecting a goal• Grule - for selecting a rule• Unify – for trying to unify
rule head and goal.
Unification
The unification operation:
Given two atomic formulas A1, A2
Return a substitution s such that A1s = A2s.
Examples:
p(3, X), p(Y, 4) ==> {X=4, Y=3}
p(X, 3, X), p(Y, Z, 4) ==> {X=4, Z=3, Y=4}
Substitution
A substitution is a finite mapping, s, from variables to terms, such that s(X)≠X.
Examples: {X=4, Y=4, Z=3}
{X=4, Z=3, U=X}
{X=4, Z=3, U=V}
{X=4, Z=3, Y=Y}
{X=4, Z=3, X=Y}
Application of substiutionatomic formula ◦ substitution ==> atomic formula‘
The application of substitution s to an atomic formula A, denoted A◦s, simultaneously replaces the terms for their variables.
Examples:
p(X, 3, X, W) ◦ {X = 4, Y = 4} =
p(4, 3, 4, W)
p(X, 3, X, W) ◦ {X = 4, W = 5} =
p(4, 3, 4, 5)
p(X, 3, X, W) ◦ {X = W, W = X} =
p(W, 3, W, X)
Instantiation and Generalization
An atomic formula A’ is an instance of an atomic formula A if there exists a substitution s such that A◦s = A’
A is more general than A’ if A’ is an instance of A
Examples:
p(X,3,X,W) is more general than p(4,3,4,W), which is more general than p(4,3,4,5).
p(X,3,X,W) is more general than p(W,3,W,W), which is more general than p(5,3,5,5).
p(X,3,X,W) is more general than p(W,3,W,X), which is more general than p(X,3,X,W).
Unifier
A unifier of atomic formulas A and B is a substitution s, such that A◦s = B◦s.
For example, the following substitutions are unifiers of p(X,3,X,W) and p(Y,Z,4,W):
{X=4, Z=3, Y=4}
{X=4, Z=3, Y=4, W=5}
{X=4, Z=3, Y=4, W=0}
The mgu of atomic formulas A and B is a unifier s of A and B such that A◦s = B◦s is more general than all other instances of A and B obtained by applying a unifier