Logic Programming March 27, 2012Tom Lever - PowerPoint PPT Presentation

About This Presentation
Title:

Logic Programming March 27, 2012Tom Lever

Description:

Material conditionals (implications ... . ? owns(Person, Pet), animal(Pet, What). Definite Programs and Goals Definite program a set of definite clauses ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 16
Provided by: Giorg59
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming March 27, 2012Tom Lever


1
Logic ProgrammingMarch 27, 2012 Tom Lever
2
What is Logic Programming?
  • Roots in automatic theorem proving
  • Describe the problem, not the solution
  • Let the computer decide how to solve it
  • Describe problem in language native to the field
  • Write declarative sentences in a dialect of FOL
  • Computer follows simple rules to analyze
  • Correct description ? correct solution (?!)

3
What is PROLOG?
  • PROgramming en LOGique
  • Early 1970s work on natural language processing
  • Alain Colmerauer Montreal and Marseille
  • Robert Kowalski Imperial College, London
  • David H D Warren University of Edinburgh
  • Warren Abstract Machine (WAM) interpreted code
  • Declarations are in the form of Horn clauses (see
    section 17.3 of our textbook - clauses in CNF
    with at most one positive literal in each
    disjunction)

4
What do declarations look like?
  • Material conditionals (implications) facing left
    (?)
  • Literal - Literal Literal Literal
  • Each literal takes the form of Predicate(arguments
    )
  • Each declaration has the form Head - Body.
  • A declaration with only a Head is a Fact
  • owns(max,pris).
  • A declaration without a head is a Query
  • ? owns(max,Who).
  • A complete declaration is a Rule
  • ownsCat(Who, Pet) - owns(Who, Pet), animal(Pet,
    cat).

5
Basic Housekeeping
  • Predicates and constants begin with lower-case
  • Variables begin with upper-case
  • A comma between predicates means AND
  • Variables are instantiated with values when a
    predicate is examined (executes)
  • Predicates in the same declaration are examined
    left to right.
  • Facts and rules are preloaded from files.

6
Fact an assertion or premise
  • owns(max, carl).
  • owns(max, pris).
  • owns(claire, scruffy).
  • owns(claire, folly).
  • animal(carl, dog).
  • animal(scruffy, cat).
  • animal(folly, dog).
  • animal(pris, cat).

7
Query interactive way to access data
  • Queries continue executing until the entire
    database has been examined. Continue after
    pauses with Tab or terminate with Enter.
  • ? owns(max, folly).
  • ? owns(Who, folly).
  • ? owns(Who, Pet).
  • ? animal(Pet, dog).
  • ? ownsCat(Person, Pet).
  • ? owns(Person, Pet), animal(Pet, What).

8
Definite Programs and Goals
  • Definite program a set of definite clauses
    (facts rules)
  • Definite goal a query consisting of atomic
    formulas as subgoals which must each be satisfied
  • Query analogous to relational database query,
    posing an existential question whether all
    subgoals can be satisfied
  • Rules containing variables imply universal
    quantification
  • Rules defined recursively can give a model
    infinite depth
  • PROLOG code specifies what exists but not how
    to find the solution

9
Backtracking? animal(Pet, _), owns(Person,
Pet).
  • When a predicate succeeds, it instantiates its
    variables and control passes to the right
  • The rightmost predicate pauses after it succeeds,
    then continues where it left off
  • When the rightmost predicate fails, control
    passes back to its predecessor
  • If it succeeds again, it re-instantiates its
    variables and control passes to the right again
  • The rightmost predicate starts from the top

10
Unification
  • The process of matching up references and
    definitions of predicates (argument passing)
  • Sophisticated pattern matching to instantiate
    variables in two directions. For example
  • T p( X, f(a,b), g(Z,F))
  • U p(t(e), W, g(3,C))
  • Combining these changes both to this
  • T U p(t(e), f(a,b), g(3,C)
  • where only variable C (or F) remains
    uninstantiated

11
Recursion
  • Used in list processing (similar to LISP)
  • Defining something in terms of itself
  • Must define a stop condition
  • append(, List, List).
  • append(HeadTail, List, HeadNewTail) -
    append(Tail, List, NewTail).
  • Sample uses
  • ? append(a,b,c, d,e,f, What).
  • ? append(First, Second, mary, anne, john).

12
Quicksort Pseudocode
  • function quicksort('array')
  • if length('array') 1
  • return 'array' // 0 or 1 elements
    already sorted
  • select and remove a pivot value 'pivot'
    from 'array'
  • create empty lists 'less' and 'greater'
  • for each 'x' in 'array'
  • if 'x' 'pivot' then append 'x' to
    'less'
  • else append 'x' to 'greater'
  • return concatenate(quicksort('less'),
    'pivot',
  • quicksort('greater')) //
    two recursive calls

13
Quicksort in PROLOG
  • partition(, _, , ).
  • partition(XXs, Pivot, Smalls, Bigs) -
  • ( X _at_lt Pivot -gt
  • Smalls XRest,
  • partition(Xs, Pivot, Rest, Bigs)
  • Bigs XRest,
  • partition(Xs, Pivot, Smalls, Rest)
  • ).
  • quicksort() --gt .
  • quicksort(XXs) --gt
  • partition(Xs, X, Smaller, Bigger) ,
  • quicksort(Smaller), X, quicksort(Bigger).

14
Applications
  • Natural language processing
  • Artificial intelligence
  • Theorem proving
  • Ontologies (information frameworks)
  • Expert systems
  • Automated answering systems
  • Control systems
  • Games

15
References
  • Bharath, R. An Introduction to PROLOG, Tab Books
    1986
  • Saint-Dizier, P. An Introduction to Programming
    in PROLOG, Springer-Verlag, 1990
  • http//www.learnprolognow.org/ - a free online
    introductory course to PROLOG, with links to free
    downloadable implementations
  • http//en.wikipedia.org/wiki/Prolog - overview
    with abundant links leading in many directions
  • http//www.cs.bham.ac.uk/pjh/prolog_course/sem223
    _se.html - Logic Programming with Prolog An
    Introduction for Software Engineers - course
    overview
Write a Comment
User Comments (0)
About PowerShow.com