Collaboration and Management Dana Fisman 1 Principles of Programming Languages Lesson 21 – An Introduction to Logic Programming – Relational Logic Programming www.cs.bgu.ac.il/~ppl172 Slides by Yaron Gonen and Dana Fisman Based on Book by Mira Balaban and
49
Embed
Principles of Programming Languages - Home - Newppl172/wiki.files/class/... · Principles of Programming Languages ... embodied this observation into Prolog. Prolog and subsets of
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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?
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
Composition of Substitutions
s ◦ s'
1. A variable X for which s(X) is defined, is removedfrom the domain of s‘