Logic Programming handout - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

Logic Programming handout

Description:

existential quantifier: (there exists) syntactic sugar, the delimiters: ... variable formula) 'existential formula' E.N.S.E.I.R.B.. January 2002. pyg / Logic ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 60
Provided by: paulg68
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming handout


1
Logic Programminghandout
  • Paul Y Gloess

2
Course Outline
  • logic versus classical programming
  • first order predicate logics
  • syntax and semantics
  • clausal form of formulas
  • proof method resolution by refutation
  • substitutions unification algorithm
  • Pure Prolog subset Horn clauses
  • neutral (?) or Edinburgh (-) syntax
  • clausal form of a problem
  • proof method linear resolution
  • Pure Prolog semantics
  • of a problem or program
  • proof trees, search trees
  • Impure Prolog
  • cut, higher order logic, negation by failure
  • built-in predicates is, , , , writeln,
  • Prolog future (today!)
  • Prolog constraints (CHIP, Prolog III, IV)
  • ? CHIP is a trademark of COSYTEC

3
classical versus logic programming
  • classical procedural or functional programming
  • we write an algorithm to solve a problem
  • we code algorithms into functions or procedures
  • a procedure or function delivers one output for
    each input.
  • logic programming
  • we specify the problem to be solved by writing a
    theory
  • the theory defined relations, not functions
  • there is no input/output distinction among
    relation parameters a given query may yield
    several answers.

4
functional versus logic programmingan example
f u n c t i o n a l
r e l a t i o n a l
5
First Order Predicate Logics(lexical entities)
  • variables
  • V X, Y, Z,
  • extra-logical symbols
  • Boolean operators ? (not), ?(and), ? (or), ?
    (implies),? (equivalent), ? (equivalent)
  • universal quantifier ? (for all)
  • existential quantifier ? (there exists)
  • syntactic sugar, the delimiters ( ) ,
  • basis B of a predicate logic
  • function symbols F f, g, h, F0 ? F1 ? F2
    ?
  • F0 function symbols of arity 0 (constants)
  • F1 function symbols of arity 1 (one argument)
  • F2 function symbols of arity 2 (two arguments)
  • predicate symbols P p, q, r, P0 ? P1 ? P2
    ?
  • P0 predicate symbols of arity 0 (constants)
  • P1 predicate symbols of arity 1 (one argument)
  • P2 predicate symbols of arity 2 (two arguments)

6
First Order Predicate Logics Syntax
  • term variable
  • function_symbol_of_arity_n(term1, , termn)
    ngt0
  • function_symbol_of_arity_0 constant
  • atom predicate_symbol_of_arity_n(term1, ,
    termn) ngt0
  • predicate_symbol_of_arity_0 constant
  • literal atom positive literal
  • ? atom negative literal
  • formula atom
  • (? formula)
  • (formula ? formula) conjunction
  • (formula ? formula) disjunction
  • (formula ? formula) implication
  • (formula ? formula) equivalence
  • (? variable formula) universal formula
  • (? variable formula) existential formula

7
Predicate Logicssome terminology
  • There is a predicate logic for each basis B?F,P?
    of function and predicate symbols.
  • Terms formed on basis B are called B-terms the
    set of all B-terms is denoted TB.
  • Formulas formed on basis B are called B-formulas
    the set of all B-formulas is denoted WFFB.
  • Formulas with all variables bound to a quantifier
    are called closed formulas.
  • Formulas with no quantifier are called
    quantifier free formulas.
  • Formulas with no quantifier and no variable are
    called ground formulas.

8
term semantics
  • assume
  • D is a non empty domain, B ?F, P? a basis
  • Bool t, f
  • a semantics fI Dn ? D, for each f in Fn
  • a semantics pI Dn ? Bool, for each p in Pn
  • S denotes the set of valuations ? V ? D
  • then this semantics I extends to B-terms
  • (t B-term)I S?D
  • XI(?) ?(X)
  • f(t1, , tn)I(?) fI(t1I(?), , tnI(?))
  • example
  • Let D Nat 0, 1, 2, 3, , F2a, m,
  • aI Nat?Nat ? Nat be addition,
  • mI Nat?Nat ? Nat be multiplication,
  • ? ?X,3?, ?Y,7?,
  • Then a(X, m(Y, X))I(?) 3 73 24

9
formula semantics
  • assume
  • D is a non empty domain, B ?F,P? a basis,
    Boolt, f
  • a semantics fI Dn ? D, for each f in Fn
  • a semantics pI Dn ? Bool, for each p in Pn
  • ? denotes the set of valuations ?V?D
  • for ? V ? D, X?V, d?D, ?X/d is identical to ?
    except that ?X/d(X)d.
  • then this semantics I extends to B-formulas
  • (? B-formula)I ? ? Bool
  • (p(t1, , tn))I(?) pI(t1I(?), , tnI(?))
  • (??)I(?) boolean_not(?I(?))
  • (???)I(?) boolean_and(?I(?), ?I(?))
  • (???)I(?) boolean_or(?I(?), ?I(?))
  • (???)I(?) boolean_implies(?I(?),
    ?I(?))
  • (???)I(?) boolean_equivalence(?I(?),
    ?I(?))
  • (?X ?)I(?) BOOLEAN_AND(?I(?X/d))
  • d?D
  • (?X ?)I(?) BOOLEAN_OR(?I(?X/d))
  • d?D

10
Truth, Models, Validity, Satisfiability
  • assume
  • B ?F, P? a basis
  • a B-formula
  • then
  • I, ? ? assuming I ?B,D?-semantics, ? V?D
  • means ?I(?) t
  • reads ? is true in I, ?
  • I ? assuming I ?B,D?-semantics
  • means I, ? ? for each ? V?D
  • reads I is a model of ?
  • or ? is valid in I
  • ?
  • means I ? for each I ?B,D?-semantics
  • reads ? is logically valid
  • or ? is valid
  • ? is satisfiable
  • means ? has a model
  • ? is unsatisfiable
  • means ? has no model

11
Classification of closed B-Formulas
  • The truth value ?I(?) of a closed formula ? does
    not depend on ?!

valid closed B-formulas
?
??
non valid but satisfiable closed B-formulas
?
??
unsatisfiable closed B-formulas
??
?
12
Checking the Validity of Formulas
  • Theorem 1
  • There is no algorithm to determine whether a
    given closed formula ? is
  • - valid,
  • - unsatisfiable,
  • - not valid and satisfiable.
  • Theorem 2
  • The resolution method (J. Robinson) is a complete
    method for checking the validity of valid
    formulas.

Prolog system use the resolution method to answer
user queries. Actually, a simpler form of
resolution suits the needs of Prolog logic.
13
The Resolution Proof Method
  • General Resolution Principle
  • proof by refutation
  • clausal form of formulas
  • resolvent formation
  • Unification of Terms
  • substitution
  • instantiation
  • Applicability of Resolution
  • completeness
  • undecidability
  • practical efficiency

14
The Resolution Method
checking the validity of a valid closed formula ?
??
Only the refutation phase is relevant of Prolog!
15
Clausal Form
  • A clause is a set of literals (implicitly ORed
    together).
  • Every formula is intersatisfiable with a
    conjunction (?) of clauses.

a formula
?X ?Y (mother(X,Y) ? father(X,Y)) ?
ancestor(X,Y)
its clausal form
?mother(X,Y) ? ancestor(X,Y) ?
?father(X,Y) ? ancestor(X,Y)
no quantifiers, no deep nesting, no ?!
16
Resolution Refutation(a unique inference rule)
(a parent clause) ?1?? ?i ? r1 ?? rm
(another parent clause) ??1?? ??j ? r1 ??
rn
s(r1) ?? s(rm) ? s(r1) ?? s(rn) (the
resolvent)
Finding s is the problem of unification.
17
Resolvent Formation(an example)
mother(bill, Z) ? astronaut(Z)
?mother(X,Y) ? ancestor(X,Y)
astronaut(W) ? ancestor(bill, W (the resolvent)
18
Unification Algorithm
unify
a most general unifier of term1 and
term2 s(term1) s(term2)
19
Unification Example
unify
most general unifier
most general common instance
20
Unification Example
unify
21
Substitution and Instanciation
  • Definition
  • A substitution ? is a function from the set of
    terms to the set of terms, such that
  • ?f(t1, ..., tn) f(?t1, ..., ?tn),
  • only a finite set of variables is changed.
  • Given a term t, a substitution ?, ?t is called
    an instance of t.
  • Remark
  • A substitution ? is entirely determined by its
    value on a finite set of variables
  • ? ?X1, t1?, , ?Xk, tk?.

22
partial preorder among terms
  • Definition
  • A term t1 is less general than a term t2 (written
    t1?t2) iff ?s t1 s(t2).

U
23
partial preorder among substitutions
  • Definition
  • A substitution s1 is less general than a
    substitution s2 (written s1 s2) iff ?? s1
    s2 o ? .
  • A renaming is a one to one substitution which
    maps variables into variables.

Theorem If s1 s2 and s2 s1 then s1 s2 o ? for
a renaming ? and s2 s1 o ? -1.
24
Introduction to Prolog
  • Pure Prolog Logic
  • Horn clauses
  • Operational Semantics
  • unification, linear resolution
  • Prolog strategy, search trees
  • Declarative Semantics
  • Impure Prolog
  • cut
  • second order
  • negation by failure
  • built-in predicates and arithmetics

25
Prolog Logic and Predicate Logic
26
Horn Clauses
  • Clause
  • ?1 ? ... ? ?i ? ... ? ?n
  • with each ?i a positive or negative literal.
  • Horn Definite Clause
  • c ? ?h1 ... ? ?hi ? ... ? ?hn
  • with c, h1, ..., hi, ..., hn all positive
    literals.
  • Horn Indefinite Clause
  • ?h1 ? ... ? ?hi ? ... ? ?hn
  • with h1, ..., hi, ..., hn all positive literals.

27
Prolog Notation
  • A (Horn) definite clause
  • c ? ?h1 ... ? ?hi ? ... ? ?hn
  • is intersatisfiable with
  • ?X1 ... ?Xkh1 ? ... ? hi ? ... ? hn ? c
  • Prolog usual notation is either of
  • c ? h1 ? ... ? hi ? ... ? hn
  • c ? h1, ..., hi, ..., hn
  • CHIP (Edinburgh) notation is
  • c - h1, ..., hi, ..., hn .
  • c . if n0

28
Structure of a Prolog Problem
Prolog program ? Prolog question
  • A Prolog program is
  • a conjunction (AND) of definite clauses with
    implicit universal quantification.
  • A Prolog question is
  • a conjunction of positive literals
  • ?1 ? ? ?n
  • with implicit existential quantification.

Prolog program ? ?X1 ?Xk ?1 ? ? ?n
29
Clausal Form of a Negated Prolog Problem
the original problem
Prolog program ? ?X1 ?Xk ?1 ? ? ?n
indefinite clause
definite clauses
30
Logical Semanticsof a Prolog Problem
program ? ?X1 ?Xk ?1 ? ? ?n
  • The logical semantics of the above Prolog problem
    consist in
  • the set of most general substitutions (called
    answer substitutions) s such that the formula
  • program ? ?Y1 ?Ym s?1 ? ? ?n
  • is valid, where Y1, , Ym are all the free
    variables occurring in s?1 ? ? ?n.

31
Edinburgh Prolog Basis
  • function or predicate symbols
  • all sequences made of
  • lower case letters a, b, c, d, ,
  • upper case letters A, B, C, D, ,
  • digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  • the underscore character _,
  • and starting with a lower case letter.
  • examples
  • append, cons, a, a34, father_of
  • no apparent fixed arity but
  • f of arity 2 is internally f/2
  • f of arity 3 is internally f/3.

32
Edinburgh Variables
  • any sequence made of
  • lower case letters a, b, c, d, ,
  • upper case letters A, B, C, D, ,
  • digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  • the underscore character _,
  • and starting with an upper case letter.
  • or the anonymous _ variable
  • just the underscore character _
  • internally, each occurrence of the anonymous
    variable is distinct from all program variable
    other occurrences _/128956, _/128957,

33
Pure Prolog Edinburgh Syntax
  • program clauses
  • clauses clause
  • clause clauses
  • clause head .
  • head - body .
  • head atom see logic syntax
  • body literals
  • literals literal
  • literal , literals
  • literal atom see logic syntax
  • question literals .

34
Prolog Semantics
  • A pure Prolog program P is regarded as a finite
    representation of a finite or infinite set of
    facts (ground atoms), whose set is called the
    semantics of P.
  • A single atom query may be regarded as a means of
    extracting a subset of P semantics the facts
    belonging to P semantics which are also query
    instances.
  • Real Prolog systems are not complete in general
    miss some facts in P semantics.

35
Prolog Semantics
  • There are four equivalent declarative semantics
    for a pure Prolog program P
  • logical semantics,
  • proof tree semantics,
  • least model semantics,
  • least fixed point semantics.
  • They are simple understanding them helps writing
    good programs.
  • Real semantics of real Prolog systems are
    operational, based on
  • SLD resolution,
  • search trees.
  • They are more difficult to understand, but
    exactly describe real system behavior.

36
Logical Semanticsof a Prolog Program P
  • P logical semantics
  • ground atom g P g
  • ground atom g P?g
  • the set of all ground atomic logical consequences
    of formula P
  • These semantics are inherited from the semantics
    of first order predicate logic.

37
Proof Tree Semanticsof a Prolog Program P
  • P proof tree semantics
  • r r is the root of some proof tree on P
  • the set of roots of all proof tree on P

What is exactly a proof tree on P?
38
Proof Tree Definition
  • A proof tree is a finite tree whose
  • all nodes are ground atoms,
  • all leaves are ground instances of program facts,
  • all complete sub trees of depth 1 are ground
    instances of program clauses.
  • A complete sub tree of depth 1 of a tree t is a
    sub tree of depth 1 whose
  • root is some node n in t,
  • root successors are exactly all n successors in t.

39
Proof Tree Example
a PROLOG program
gp(X,Z) ? p(X,Y), p(Y,Z) p(jane,lenny) ? p(jane
,mary) ? p(lenny,steve) ? p(ma
ry,steve) ?
two proof trees of the same atom
40
Proof Tree Semanticsexample
a PROLOG program
natural(zero) ? natural(suc(N)) ? natural(N)
41
A New Definition of B-semanticsfor Prolog Basis
B ?F, P?
A B-semantics I also called interpretation I
true facts
false facts
The Herbrand Base set of all ground atoms
42
Least Model Semantics of a Prolog Program
  • Given a subset I of the Herbrand base (that is,
    an interpretation I), and a closed formula ?, ?I
    is recursively defined similarly to when I is a
    B-semantics as defined in the context of
    predicate logic.
  • A Prolog program P is itself a (closed) formula!
  • PI ??Bool is a constant (either always true, or
    always false).
  • Hence we identify PI with a Boolean constant PI
    Bool .
  • A clause is true in I if all its ground instances
    are true in I.
  • A ground atom a is true in I if a?I.
  • A model of P is an interpretation in which P is
    true, that is, in which all clauses are true.

Theorem and Definition Every Prolog program P has
a model, and a least model (which is the
intersection of all its models) it is called the
least model semantics of P.
43
Least Model Example
44
Least Fixpoint Semanticsof a Prolog Program
45
Inference Operator TPand Least Fixpoint of TP
  • Definition
  • TP H?H, with H the Herbrand Base
  • TP(I) a?H ?s ?(c?h1, , hk)?P,
  • a s(c),
  • s(h1)?I, , s(hk)?I
  • TP?0 ?
  • TP?n1 TP(TP?n)
  • TP?? ?n?Nat TP?n

46
ComparisonofPure Prolog Program Semantics
  • Theorem
  • Let P be a pure Prolog program. Then
  • P logical semantics
  • P proof tree semantics
  • P least model semantics
  • P least fixed point semantics TP ?? .

47
Prolog Standard Linear Derivation
(an indefinite clause) (current goal) ??1? ??2?
? ??k
(a definite clause variant) c ? ?h1 ?? ?hn
?s(h1) ?? ?s(hn) ? ? s(?2)? ? ? s(?k) (the
resolvent is an indefinite clause, becomes
current goal)
LD is complete for Horn clauses.
Prolog SLD is deterministic, depth first, left to
right, not complete.
48
Search Tree of a PROLOG Problem
initial query
There are as many successors of a node as there
are rules applicable to the leftmost literal in
the node. Successors are ordered from left to
right according to rule order in program. Rule
hypotheses come first in a successor node, in
same order as in rule.
49
Search Tree of a PROLOG Problem
(gp) gp(X,Z) ? p(X,Y), p(Y,Z) (p1) p(jane,lenny)
? (p2) p(jane,mary) ? (p3) p(lenny,steve) ? (p4) p
(mary,steve) ?
gp(jane,steve)
50
On the Usefulness of Search Trees
  • The search tree of a PROLOG problem is a
    condensed representation of
  • all possible proof trees of the goal,
  • all finite failures,
  • all infinite branches.
  • Search trees provide an adequate basis for the
    description of real PROLOG operational semantics,
    including extensions such as
  • the cut,
  • negation by failure,
  • ...

51
Real Prolog ProgrammingNeeds
  • Strategic Control
  • to avoid infinite proofs
  • to save time
  • Second Order Logic
  • (replacement of a literal with a variable)
  • to provide a form of negation
  • Built-in Predicates
  • for efficient arithmetic
  • for input/output and graphics
  • for meta-programming

These are necessary extensions to Horn clause
theory found in most Prolog systems.
52
Search Tree Semantics of the Cut
cde
? c - a, !, b. cut introduction
a!bde
g!bde
h!bde
failure
i!bde
!bde
jk!bde
? cut erased
k!bde
bde
failure
53
Good and Bad Things about the Cut
  • Good things
  • means of tuning PROLOG stupid strategy,
  • sometimes prevents PROLOG from exploring infinite
    branches,
  • cutting branches saves time.
  • Bad Things
  • lack of declarative semantics,
  • makes programs less general, sometimes loses
    reversibility,
  • sometimes loses solutions.

54
negation by failure
not(X) - X, !, fail. not(X) .
  • X represents a fact (second order logic),
  • "!" is the Edinburgh syntax for "cut",
  • fail is an undefined predicate.

The false is everything you cannot prove!
55
List Expression Edinburgh Syntax
Edinburgh lists LISP lists nil a
b cons(a, b) a b c cons(a,
cons(b, cons(c, nil))) abbreviation of above a,
b, c (a b c)abbreviation of above
Edinburgh lists are merely a convenient
notation They do not add any expressive power to
pure Prolog!
56
Built-in ArithmeticsX is (YZ)Y
  • crashes (or delays evaluation) if Y or Z is
    unbound.
  • fails if X, Y, Z are bound, and values of X and
    (YZ)Y are different.
  • succeeds if Y, Z are bound, and
  • either X is unbound (it will get bound to the
    value of (YZ)Y,
  • or X is bound to the value of (YZ)Y.

The "is" construction is called a "passive
constraint" in CHIP CHIP does not crash on
unbound expressions, unlike previous Prolog
systems Passive constraints are superseded by
CLP "active constraints" in the case of linear
arithmetics.
57
Built-in ArithmeticsXY gt XY Z
  • crashes (or delays evaluation) if anyone of X, Y,
    Z variables is unbound.
  • fails if X, Y, Z are bound, and the value of XY
    is not strictly greater than the value of XY
    Z.
  • succeeds if X, Y, Z are bound, and the value of
    XY is strictly greater than the value of XY Z.

Passive constraints are superseded by CLP "active
constraints" in the case of linear arithmetics.
58
Input Output Built-in Predicates
  • read(X)
  • reads something into X
  • write(X)
  • outputs the value of X (or X if X is unbound)
  • ln
  • outputs a "line feed"
  • writeln(X)
  • write(X), ln

X may be replaced with a term see CHIP
documentation.
59
References
  • J. W. Lloyd
  • Foundations of Logic Programming
  • Second Extended Edition, Springer Verlag, 1993
  • Krzysztof R. Apt
  • From Logic Programming to Prolog
  • Prentice Hall, 1997, http//www.cwi.nl/apt
  • Leon Stirling Ehud Shapiro
  • The Art of Prolog Programming Techniques
  • MIT Press, 1986
Write a Comment
User Comments (0)
About PowerShow.com