Chapter 16 Logic Programming Languages - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Chapter 16 Logic Programming Languages

Description:

In logic programming only a restricted form for propositions, called Horn clauses, is used ... [ H | T ] - head (CAR) H and tail (CDR) T. 33. Example the append ... – PowerPoint PPT presentation

Number of Views:145
Avg rating:3.0/5.0
Slides: 42
Provided by: david2548
Category:

less

Transcript and Presenter's Notes

Title: Chapter 16 Logic Programming Languages


1
Chapter 16Logic Programming Languages
  • CS 350 Programming Language Design
  • Indiana University Purdue University Fort Wayne

2
Chapter 16 topics
  • Introduction to logic programming languages
  • A brief introduction to predicate calculus
  • Predicate calculus and proving theorems
  • An overview of logic programming
  • The basic elements of Prolog
  • Deficiencies of Prolog
  • Applications of logic programming

3
Introduction
  • Logic programming languages . . .
  • Express programs in a form of symbolic logic
  • Use a logical inferencing process to produce
    results
  • Are declarative rather than procedural
  • Only a specification of the desired results is
    stated
  • Not a detailed procedure for producing them
  • Logic programming languages are also called
    declarative languages
  • The syntax of logic programming languages is
    remarkably different from imperative and
    functional languages

4
Introduction to predicate calculus
  • A proposition is a logical statement that may or
    may not be true
  • Consists of objects and relationships among
    objects
  • Symbolic logic can be used for the three basic
    needs of formal logic
  • Express propositions
  • Express relationships between propositions
  • Describe how new propositions can be inferred
    from other propositions
  • The particular form of symbolic logic used for
    logic programming called predicate calculus

5
Analogy with Euclidean geometry
  • Axioms are initial propositions assumed to be
    true
  • Initial facts
  • A theorem expresses a relationship between
    propositions that is known to be true
  • One proposition is the hypothesis
  • A second proposition is the conclusion
  • A theorem states that if the hypothesis is true
    then the conclusion is true

6
Analogy with Euclidean geometry
  • If the hypothesis is a fact then the conclusion
    becomes a fact
  • The hypothesis is a fact if it is an axiom or can
    be proven to be true from the axioms using other
    theorems
  • All the facts that can be derived in this way
    become our knowledge about Euclidean geometry

7
Propositions
  • The simplest propositions are called atomic
    propositions
  • An atomic proposition takes the form
  • functor( list of parameters )
  • Examples
  • bird( canary )
  • father( bob, sally )
  • siblings( fred, sue, joe )
  • Propositions can be stated as . . .
  • Facts assumed to be true
  • Queries truth to be determined

8
Propositions
  • Compound propositions . . .
  • Have two or more atomic propositions
  • Are connected by operators

9
Quantifiers
  • Variables permit propositions about hypothetical
    objects
  • Quantifiers indicate how the variable is to be
    interpreted
  • In the table, X is a variable and P is a
    proposition
  • ?X.( square(X) ? sides_equal(X) )
  • All sides of each square have equal length
  • ?X.( integer(X) ? (X2 32 42 ) )
  • There exists an integer X whose square is 25

10
Clausal form
  • A proposition may be stated many different ways
    using predicate calculus
  • This is not desirable for automated processing
  • Fortunately, all propositions may be stated
    uniquely in clausal form
  • B1 ? B2 ? ? Bn ? A1 ? A2 ? ? Am
  • The meaning is that if all the As are true, then
    at least one B is true
  • The antecedent is the right side
  • The consequent is the left side
  • The existential quantifier is not needed
  • The universal quantifier is implicit for any
    variables
  • No operators are needed other than conjunction
    and disjunction

11
Predicate calculus and theorem proving
  • The resolution principle is an inference
    principle that allows a new proposition to be
    inferred from given propositions
  • Example
  • Given propositions
  • A?B ? C
  • D ? A?E
  • . . . the resolution principle gives the new
    proposition
  • B?D ? C?E
  • Technique
  • OR the propositions on the LHS
  • AND the propositions on the RHS
  • Cancel any proposition common to both sides from
    result

12
Resolution
  • Resolution was devised to be applied to
    propositions in clausal form
  • When variables are involved, resolution must
    search for values for the variables that allow
    matching to succeed
  • Unification is finding values for variables in
    propositions that allows the matching process to
    succeed
  • Instantiation is the assignment of trial values
    to variables during unification
  • If the matching fails after instantiating a
    variable with a value, unification may need to
    backtrack and instantiate with a different value

13
Overview of logic programming
  • In logic programming only a restricted form for
    propositions, called Horn clauses, is used
  • This simplifies resolution
  • A Horn clause can have only two forms
  • Headed Horn clause
  • Single atomic proposition on left side
  • The left side of a clausal form is called the
    head
  • Headless Horn clause
  • Empty left side
  • Used to state facts
  • Most propositions can be stated as Horn clauses

14
Overview of logic programming
  • Logic programming languages are characterized by
    declarative semantics
  • There is a simple way to determine the meaning of
    each statement
  • Meaning does not depend on how the statement is
    used
  • Semantics of imperative languages depend on the
    state of local variables, scoping rules, run-time
    context, etc.
  • Programming is nonprocedural
  • Programs do not state how a result is to be
    computed, but rather the characteristics defining
    the result

15
Example sorting a list of size n
  • Describe the characteristics of a sorted list
  • Not the process of rearranging a list
  • The problem with this carrying this out is
    machine efficiency (there are n! permutations of
    the list)
  • We need to be content with simpler goals using
    Prolog

sort(old_list, new_list) ? permute (old_list,
new_list) ? sorted (new_list) sorted ( list ) ?
?k such that 1? k lt n, list( k ) ? list ( k1 )
16
The basic elements of Prolog
  • We consider the dialect called Edinburgh syntax
  • A term is a constant, variable, or structure
  • A constant is an atom or an integer
  • An atom is symbolic value of Prolog consisting of
    ...
  • A string of letters, digits, and underscores
    beginning with a lowercase letter
  • A string of printable ASCII characters delimited
    by apostrophes

17
The basic elements of Prolog
  • A variable is any string of letters, digits, and
    underscores beginning with an uppercase letter
  • Instantiation is the binding of a variable to a
    value
  • Instantiation lasts only as long as it takes to
    satisfy one complete goal
  • A structure is an atomic proposition
  • siblings( fred, sue, joe )
  • This is a headless Horn clause
  • The functor is an atom used to identify the
    structure
  • A structure represents
  • A fact
  • A predicate when its context makes it a query

18
Rules
  • A headed Horn clause is known as a rule
  • Rules correspond to theorems
  • The left side is the consequent (then part)
  • The consequent may be only a single term
  • (because it is a Horn clause)
  • The right side is the antecedent (if part)
  • May be a single term or a conjunction of terms
  • Separated from the left side by -
  • A conjunction consists of multiple terms
    separated by commas (implied logical AND
    operations)
  • Example grandfather(X,Z) - father(X,Y),
    father(Y,Z)

19
Rules
  • Rules usually involve variables to generalize
    their meanings
  • Each variable has an implicit universal
    quantifier
  • Example
  • If there are any instantiations of X, Y, and Z
    such that father(X,Y) is true and father(Y,Z) is
    true, then the rule
  • grandfather(X,Z) - father(X,Y),
    father(Y,Z)
  • asserts that that grandfather(X,Z) is true for
    the same instantiations of X and Z

20
Goal statements
  • A goal statement is a query
  • grandfather( bob, fred )
  • The system will respond with yes or no
  • Yes means the system established the goal from
    the given database of facts and relationships
  • No means only the system was unable to prove that
    the goal was true
  • A Prolog system has two modes
  • Entry mode for entering facts
  • Such as grandfather( bob, fred )
  • Query mode for asking the system to prove goals
  • Such as grandfather( bob, fred )

21
Goal statements
  • Propositions with variables are also legal goals
  • grandfather( bob, X )
  • To prove that this goal is true, the system
    must find at least one instantiation of X that
    makes the goal true
  • If the system answers yes, it also provides all
    instantiations of X that make the goal true
  • Example

The query father( bob, X ). returns X
joe X sue
Entered facts father( bob, joe ).
father( joe, bill ). father( bob, sue ).
The query father( X, bill ). returns X
joe
22
Inferencing process of Prolog
  • Sometimes establishing a goal (query) involves
    establishing subgoals
  • To prove a goal is true, the system must find a
    chain of inference rules and/or facts in the
    database that connect the goal to facts in the
    database
  • For example, to establish goal Q, the system
    might find a fact A and a sequence of subgoals B,
    C, , such that
  • B - A
  • C - B
  • Q - P
  • Process of proving a subgoal called matching,
    satisfying, or resolution

23
Inferencing process example
Entered facts and rules father( bob, joe
). father( joe, bill ). father( bob,
sue ). older( X, Y ) - father( X, Y ).
older( X, Z ) - older( X, Y ), older( Y, Z ).
  • The rule for older is found
  • X is instantiated to bob
  • Y is instantiated to sue
  • father( bob, sue ) is found in the database
  • older( bob, sue ) returns yes

The query older( bob, sue ). returns yes
  • The rules for older are found
  • X is instantiated to bob
  • Z is instantiated to bill
  • Eventually, Y is instantiated to joe
  • father(bob,joe) is found in the database and
    older(bob,joe) returns yes
  • father(joe,bill) is found in the database and
    older(joe,bill) returns yes
  • older(bob,bill) returns yes
  • The query
  • older( bob, bill ).
  • returns yes

24
Inferencing process
  • How does the system attempt to do matching?
  • Bottom-up resolution (or forward chaining)
  • Begin with the facts and rules of the database
    and attempt to find sequence that leads to goal
  • Works well with a large set of possibly correct
    answers
  • Top-down resolution (or backward chaining)
  • Begin with the goal and attempt to find sequence
    that leads to the set of facts in database
  • Works well with a small set of possibly correct
    answers
  • Prolog implementations use backward chaining

25
Inferencing process
  • When there are multiple subgoals, the search for
    the sequence of matches might proceed as a . . .
  • A depth-first search
  • Find a complete proof for the current subgoal
    before working on others
  • A breadth-first search
  • Work on proofs of the various subgoals in
    parallel
  • Prolog uses the depth-first search
  • Can be done with fewer computer resources

26
Inferencing process
  • In searching for a chain of subgoals, what
    happens if one of the subgoals fails and is
    abandoned?
  • The system reconsiders a previous subgoal and
    tries to find an alternative solution
  • This is backtracking
  • This may simply involve instantiating a variable
    to another value
  • Backtracking can take a great deal of time and
    space because it may involve finding all possible
    proofs to every subgoal

27
Simple arithmetic
  • Prolog supports integer variables and integer
    arithmetic with the is operator
  • The is operator takes an arithmetic expression as
    right operand and a variable as left operand
  • Example
  • A is B / 10 C
  • This instantiates A
  • B and C must already be instantiated
  • This is not the same as an assignment statement!
  • More like a solution to
  • is( A, B/10 C)

28
Simple arithmetic
  • Example

speed( ford, 70 ). speed( chevy, 65 ). time(
ford, 8 ). time( chevy, 13 ). dist( X,Y ) -
speed( X, Speed ), time( X, Time ), Y is Speed
Time. --------------------------------------------
----------------------------------------------- Qu
ery dist( ford, D ) returns D 560
29
Simple arithmetic
  • Example
  • Note the conjunction of terms on the RHS is
    matched in a left-to-right order

factorial( 1, 1 ). factorial( N, Fac ) - M is
N-1, factorial( M, G ), Fac is NG. --------------
--------------------------------------------------
------------- Query factorial( 5, Fac
) returns Fac 120
30
Trace
  • Trace is a built-in structure that displays the
    instantiations at each step
  • Activated by
  • Tracing model of execution - four events
  • Call (beginning of attempt to satisfy goal)
  • Exit (when a goal has been satisfied)
  • Redo (when backtrack occurs)
  • Fail (when goal fails)

trace.
31
Trace
likes( jake, chocolate ). likes( jake, apricots
). likes( darcie, licorice ). likes( darcie,
apricots ). ----------------------------------
?- trace. ?- likes( jake, X ), likes( darcie, X
). 1 1 Call likes(jake,_16) ? 1
1 Exit likes(jake,chocolate) ? 2 1
Call likes(darcie,chocolate) ? 2 1
Fail likes(darcie,chocolate) ? 1 1
Redo likes(jake,chocolate) ? 1 1
Exit likes(jake,apricots ) ? 2 1
Call likes(darcie,apricots ) ? 2 1
Exit likes(darcie,apricots ) ? X apricots
  • Example
  • What is the thing that both jake and darcie like?

32
List structures
  • Prolog supports the list data structure in
    addition to atomic propositions
  • List is a sequence of any number of elements
  • Elements can be atoms, atomic propositions, or
    other terms (including other lists)
  • Examples
  • bob, sue, jake, bill
  • 1, 2, 3, 4, 5, 6, 7
  • - the empty list
  • H T - head (CAR) H and tail (CDR) T

33
Example the append predicate
append( , List, List ). append( Head List1
, List2, Head List3 ) - append ( List1,
List2, List3 ).
  • Note The stopping case must come first
  • Matching considers propositions in the order
    entered

The query append( 1,2,3, 4,5,6,7, List
). returns List 1,2,3,4,5,6,7 ------
--------------------------------------------------
--------------------- The query append(
1,2,3, List, 1,2,3,4,5,6,7 ). returns
List 4,5,6,7 -------------------------------
----------------------------------------------
The query append( List, 4,5,6,7,
1,2,3,4,5,6,7 ). returns List
1,2,3
34
Example the reverse predicate
reverse( , ). reverse( Head Tail ,
List ) - reverse(Tail, Result), append (Result,
Head , List).
The query reverse( 1,2,3,4,5, List
). returns List 5,4,3,2,1 ---------------
------------------------ The query reverse(
List, 1,2,3,4,5 ). returns List
5,4,3,2,1
35
Two built-in predicates
  • Two built-in predicates that gather instances of
    variables that make a proposition true are bagof
    and setof
  • bagof(V,P,B) gathers all instances of V that make
    P true into a list B
  • That is, variable B is instantiated to the list
    of all instances of V that make P true
  • setof(X,P,S) is similar except the bag is sorted
  • Sorting removes all duplicates

36
Example
  • Consider the facts at left and the queries
  • The Y is read there is an Y such that
  • Y is the existential qualifier

likes( jake, chocolate ). likes( jake, apricots
). likes( darcie, apricots ). likes( jake,
licorice ). likes( darcie, grapes ).
37
Deficiencies of Prolog
  • Resolution order control
  • The order in which propositions were entered
    matters
  • Also, the order of terms in a Horn clause matters
  • These orders should be nondeterministic
  • It is common to write inadvertent infinite loops
  • Similar to the problem with left-recursive
    production rules and recursive descent parsing
  • Explicit control of backtracking is also
    sometimes necessary
  • This involves a cut operator specified by !
  • This complexity should not be imposed on the
    programmer

38
Example the member predicate
member( Element, Element _ ) - !. member(
Element, _ List ) - member( Element, List
).
  • The underscore means dont care
  • The cut operator is needed in the first
    proposition so that if some subsequent
    proposition fails and backtracking returns to
    member, then member will not try to find an
    alternate solution (there is none)
  • It takes experience to realize this

The query member( 3, 5, 2, 3, 6
). returns yes
39
Deficiencies of Prolog
  • The closed-world assumption
  • If there is not enough information in the
    database to prove that a proposition is true, it
    will be rejected
  • Prolog is a true-fail system instead of a
    true-false system
  • The negation problem
  • The logical NOT cannot be part of Prolog
  • This is related to the closed-world assumption
  • Also related to use of the Horn clause
  • Intrinsic limitations
  • Such as the need to go through all permutations
    of a list to find a sorted arrangement of the
    list
  • See slide 15

40
Deficiencies of Prolog
  • On the other hand, Prolog . . .
  • Is capable of dealing with many useful
    applications
  • Is based on an intriguing concept
  • Is intellectually interesting
  • Processing is naturally parallel, so Prolog
    interpreters can take advantage of
    multi-processor machines
  • It is possible that improved logic programming
    systems will be developed which are capable of
    dealing with larger classes of problems

41
Applications of logic programming
  • Relational database management systems
  • Expert systems
  • Natural language processing
  • Education
Write a Comment
User Comments (0)
About PowerShow.com