Logic Programming - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Logic Programming

Description:

Logic Programming Tasanawan Soonklang – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 56
Provided by: Eric1251
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming


1
Logic Programming
  • Tasanawan Soonklang

2
Programming paradigms
  • Imperative
  • Object-oriented
  • Functional
  • Logic

Procedural programming
Non-procedural programming or Declarative
programming
3
Non-procedural programming
  • So far programming has been algorithmic.
  • Procedural statements (C, C, FORTRAN)
  • Functional expressions (Postscript, ML, LISP)
  • Now declarative languages logic programming
  • Programs do not state now a result is to be
    computed, but rather the form of the result

4
Title
Logic Programming
  • Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit. Vivamus et magna. Fusce sed sem
    sed magna suscipit egestas.
  • Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit. Vivamus et magna. Fusce sed sem
    sed magna suscipit egestas.
  • Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit. Vivamus et magna. Fusce sed sem
    sed magna suscipit egestas.
  • Refers loosely to the use of
  • facts and rules to represent information.
  • deduction to answer queries.
  • algorithm logic control
  • We supply logic part, Programming Language
    supplies control part.

5
Imperative vs. Logic
fact(0,1) - true.
  • int fact(int n)
  • int i 1
  • for (int j n jgt1 --j)
  • i i j
  • return I

fact(0,1). fact(N,F) - Ngt0, N1 is N-1,
fact(N1,F1), F is N F1.
?- fact(3,W). W6 Click to see the animation
6
Computing
  • Deals with relations rather than functions.
  • Search facts in order looking for matches.
  • Use rules for a logical inferencing process to
    produce results
  • Premise
  • programming with relations is more flexible than
    with functions.

7
Reserve fight
  • Information
  • fight number
  • from city
  • to city
  • departure time
  • arrival time

fight ( fight_number, from_city, to_city,
departure_time, arrival_time ) fight(fight_number,
Bangkok, Los Angeles, departure_time,
arrival_time) fight(fight1,Bangkok, X,
depart1,arrival1), fight(fight2,X,Los
Angeles,depart2,arrival2), depart2 gt
arrive130
8
Logic
9
Proposition
  • A declarative sentence.
  • e.g. Woff is a dog.
  • e.g. Jim is the husband of Mary.
  • A proposition is represented by a logical symbol.
  • P Woff is a dog.
  • R Jim is the husband of Mary.

10
Proposition
  • A logical statement that may or may not be true
  • Consists of
  • objects
  • relationships of objects to each other

11
Symbolic Logic
  • Logic which can be used for the basic needs of
    formal logic
  • Express propositions
  • Express relationships between propositions
  • Describe how new propositions can be inferred
    from other propositions
  • Particular form of symbolic logic used for logic
    programming called predicate calculus

12
Object Representation
  • Objects in propositions are represented by simple
    terms either constants or variables
  • Constant - a symbol that represents an object
  • Variable - a symbol that can represent different
    objects at different times
  • Different from variables in imperative languages

13
Compound Terms
  • Atomic propositions consist of compound terms
  • Compound term one element of a mathematical
    relation, written like a mathematical function
  • Mathematical function is a mapping
  • Can be written as a table

14
Parts of a Compound Term
  • Compound term composed of
  • Functor - function symbol that names the
    relationship
  • Ordered list of parameters (tuple)
  • Examples
  • student(jon)
  • like(seth,OSX)
  • like(nick,windows)
  • like(jim,linux)

15
Forms of a Proposition
  • Fact - proposition is assumed to be true
  • Query - truth of proposition is to be determined
  • Compound proposition
  • Have two or more atomic propositions
  • Propositions are connected by operators

16
Logical Operators
Name Symbol Example Meaning
negation ? ? P not P
conjunction ? P ? Q P and Q
disjunction ? P ? Q P or Q
equivalence ? P ? Q P is equivalent to Q
implication ? ? P ? Q P ? Q P implies Q Q implies P
17
Quantifiers
Name Example Meaning
universal ?X.P For all X, P is true
existential ?X.P There exists a value of X such that P is true
18
Forms of a Proposition
  • Too many ways to state the same thing
  • Use a standard form for propositions
  • Clausal form
  • B1 ? B2 ? ? Bn ? A1 ? A2 ? ? Am
  • means if all the As are true, then at least one B
    is true
  • Antecedent - right side
  • Consequent - left side

19
Predicate Calculus and Proving Theorems
  • A use of propositions is to discover new theorems
    that can be inferred from known axioms and
    theorems
  • Resolution - an inference principle that allows
    inferred propositions to be computed from given
    propositions

20
Resolution
  • Unification finding values for variables in
    propositions that allows matching process to
    succeed
  • Instantiation assigning temporary values to
    variables to allow unification to succeed
  • After instantiating a variable with a value, if
    matching fails, may need to backtrack and
    instantiate with a different value

21
Proof by Contradiction
  • Hypotheses a set of pertinent propositions
  • Goal negation of theorem stated as a proposition
  • Theorem is proved by finding an inconsistency

22
Theorem Proving
  • Basis for logic programming
  • When propositions used for resolution, only
    restricted form can be used
  • Horn clause - can have only two forms
  • Headed single atomic proposition on left side
  • Headless empty left side (used to state facts)
  • Most propositions can be stated as Horn clauses

23
First Order Logic (FOL)
  • Allows the following to be modeled
  • Objects
  • properties of objects
  • relations among the objects
  • Like propositional logic, FOL has sentences
  • Additionally it has terms which allow the
    representation of objects

24
Terms
  • A term is a logical expression which refers to an
    object.
  • Elements of a term
  • Constant Symbols e.g. A, B, John
  • Predicate Symbols - refer to a relation
  • Function Symbols - refer to a relation which is
    a function

25
Sentences
  • Atomic Sentences - state a fact
  • e.g. company(gordon).
  • e.g. location(gordon,usa).
  • Complex Sentences - formed from
  • atomic sentences
  • logical connectives
  • quantifiers

26
Quantifier
  • Universal Quantifier (?)
  • All cats are mammals.
  • ?x (Cat(x) ? Mammal(x))
  • Extensile Quantifier (?)
  • Spot has a sister who is a cat.
  • ?x ( Sister(x,Spot) ? Cat(x))

27
Prolog
  • Programming Logic
  • Prolog is based upon First Order Logic (FOL)
  • A Prolog program consists of a Knowledge Base
    composed of
  • facts
  • rules
  • All facts and rules must be expressed as Horn
    Clauses

28
Applications
  • Relational DBMS
  • Artificial Intelligence
  • Expert systems
  • Natural language processing
  • Automatic theorem proving

29
The Origins of Prolog
  • University of Aix-Marseille
  • Natural language processing
  • University of Edinburgh
  • Automated theorem proving

30
Syntax rules
  • Predicates (functors) must start with lower-case
    letter.
  • Constants begin with a lower-case letter or
    number.
  • Variables begin with an upper-case letter or an
    (_).

31
Syntax rules
  • All clauses have a head and a body.
  • head - body.
  • The symbol - is read if
  • All sentences (clauses) must end with a period.

32
Edinburgh syntax
  • Term a constant, variable, or structure
  • Constant an atom or an integer
  • Atom symbolic value of Prolog
  • Atom consists of either
  • a string of letters, digits, and underscores
    beginning with a lowercase letter
  • a string of printable ASCII characters delimited
    by apostrophes

33
Edinburgh syntax
  • Variable any string of letters, digits, and
    underscores beginning with an uppercase letter
  • Instantiation binding of a variable to a value
  • Lasts only as long as it takes to satisfy one
    complete goal
  • Structure represents atomic proposition
  • functor(parameter list)

34
Fact statements
  • Used for the hypotheses
  • Headless Horn clauses
  • female(shelley).
  • male(bill).
  • father(bill,jake).

35
Rule statements
  • Used for the hypotheses
  • Headed Horn clause
  • Right side body (if part)
  • May be single term or conjunction
  • Left side Head (then part)
  • Must be single term
  • Conjunction multiple terms separated by logical
    AND operations (implied)

36
Rule statements
  • ancestor(mary,shelley)- mother(mary,shelley).
  • Can use variables (universal objects) to
    generalize meaning
  • parent(X,Y)- mother(X,Y).
  • parent(X,Y)- father(X,Y).
  • grandparent(X,Z)-
  • parent(X,Y), parent(Y,Z).
  • sibling(X,Y)- mother(M,X), mother(M,Y),
  • father(F,X), father(F,Y).

37
Goal statements
  • For theorem proving, theorem is in form of
    proposition that we want system to prove or
    disprove
  • Same format as headless Horn
  • man(fred)
  • Conjunctive propositions and propositions with
    variables also legal goals
  • father(X,mike)

38
Simple Prolog facts
  • A database of facts
  • inclass(john, cmsc330).
  • inclass(mary, cmsc330).
  • inclass(george, cmsc330).
  • inclass(jennifer, cmsc330).
  • inclass(john, cmsc311).
  • inclass(george, cmsc420).
  • inclass(susan, cmsc420).
  • Queries Prolog can confirm these facts
  • ?-inclass(john, cmsc330). ? yes
  • ?- inclass(susan, cmsc420). ? yes
  • ?- inclass(susan, cmsc330). ? no

39
Simple Prolog facts rules
  • A database of facts
  • dog(woff).
  • barks(woff).
  • barks(spot).
  • wags_tail(woff).
  • A database of rules
  • dog(X) - barks(X), wags_tail(X).
  • Queries
  • ?- dog(woff) gt yes
  • ?- dog(spot) gt no
  • ?- dog(Y) gt Y woff

40
Inferencing Process
  • Facts and rules are Knowledge base (KB)
  • Prolog uses a goal directed search of the KB
  • Depth first search is used
  • Query clauses are used as goals and searched left
    to right
  • KB clauses are searched in the order they occur
    in the KB
  • Goals are matched to the head of clauses
  • Terms must unify based upon variable substitution
    before they match

41
Inferencing Process
  • Queries are called goals
  • If a goal is a compound proposition, each of the
    facts is a subgoal
  • To prove a goal is true, must find a chain of
    inference rules and/or facts. For goal Q
  • B - A
  • C - B
  • Q - P
  • Process of proving a subgoal called matching,
    satisfying, or resolution

42
Approaches
  • Bottom-up resolution, forward chaining
  • Begin with facts and rules of database and
    attempt to find sequence that leads to goal
  • Works well with a large set of possibly correct
    answers
  • Top-down resolution, backward chaining
  • Begin with goal and attempt to find sequence that
    leads to set of facts in database
  • Works well with a small set of possibly correct
    answers
  • Prolog implementations use backward chaining

43
Subgoal Strategies
  • When goal has more than one subgoal, can use
    either
  • Depth-first search find a complete proof for
    the first subgoal before working on others
  • Breadth-first search work on all subgoals in
    parallel
  • Prolog uses depth-first search
  • Can be done with fewer computer resources

44
Backtracking
  • With a goal with multiple subgoals, if fail to
    show truth of one of subgoals, reconsider
    previous subgoal to find an alternative solution
    backtracking
  • Begin search where previous search left off
  • Can take lots of time and space because may find
    all possible proofs to every subgoal

45
Unification
  • Can use a form of substitution called unification
    to derive other relationships.
  • inclass(susan, X).
  • Prolog searches database and responds
    Xcmsc420.
  • Hitting Enter key, Prolog says No since no
    other fact.
  • inclass(john, Y).
  • Prolog has following responses
  • Ycmsc330.
  • Ycmsc311.
  • no.

46
Unification
  • Can define more complex queries
  • takingboth(X)-
  • inclass(X, cmsc330),
  • inclass(X, cmsc311).
  • ?-takingboth(john)
  • yes
  • ?-takingboth(Y)
  • Yjohn
  • no

47
Simple Arithmetic
  • Prolog supports integer variables and integer
    arithmetic
  • is operator takes an arithmetic expression as
    right operand and variable as left operand
  • A is B / 17 C
  • Not the same as an assignment statement!

48
Data
  • Data
  • Integers 1, 2, 3, 4
  • Reals 1.2, 3.4, 6.7
  • Strings 'abc', '123'
  • Facts lower case names
  • Variables Upper case names
  • Lists a, b, c, d

49
Example
  • speed(ford,100).
  • speed(chevy,105).
  • speed(dodge,95).
  • speed(volvo,80).
  • time(ford,20).
  • time(chevy,21).
  • time(dodge,24).
  • time(volvo,24).
  • distance(X,Y) - speed(X,Speed),
  • time(X,Time),
  • Y is Speed Time.

50
List Structures
  • basic data structure
  • List is a sequence of any number of elements
  • Elements can be atoms, atomic propositions, or
    other terms (including other lists)
  • apple, prune, grape, kumquat
  • (empty list)
  • X Y (head X and tail Y)

51
Append Example
  • append(, List, List).
  • append(Head List_1, List_2, Head List_3)
    -
  • append (List_1, List_2, List_3).

52
Reverse Example
  • reverse(, ).
  • reverse(Head Tail, List) -
  • reverse (Tail, Result),
  • append (Result, Head, List).

53
Reverse Example
  • reverse(, ).
  • reverse(Head Tail, List) -
  • reverse (Tail, Result),
  • append (Result, Head, List).

54
Summary
  • So Prolog is
  • 1. A database of facts.
  • 2. A database of queries.
  • 3. A sequential execution model
  • Search facts in order looking for matches.
  • If failure, back up to last match and try next
    entry in database.
  • Because of this last item, Prolog is not truly
    declarative. It does have an algorithmic
    execution model buried in structure of database.

55
The End.
Write a Comment
User Comments (0)
About PowerShow.com