COEN 171 - Logic Programming - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

COEN 171 - Logic Programming

Description:

... starting with a lower case letter, or any printable string in single quotes) ... One way to start this is to pass in a list of the row positions, with variables ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 31
Provided by: RonDan8
Learn more at: http://www.cse.scu.edu
Category:

less

Transcript and Presenter's Notes

Title: COEN 171 - Logic Programming


1
COEN 171 - Logic Programming
  • Logic programming and predicate calculus
  • Prolog statements
  • Facts and rules
  • Matching
  • Subgoals and backtracking
  • List operations
  • Arithmetic
  • Controlling backtracking
  • Running Prolog
  • N queens hints

2
Logic Programming
  • Logic programming
  • uses a form of symbolic (mathematical) logic as
    programming language
  • declarative language
  • imperative and functional languages allow
    programs that describe how to compute a solution
  • declarative languages allow programs that
    describe facts about the problem domain and the
    form of the result (what, not how)
  • how to achieve that result is left up to the
    system

3
Predicate Calculus
  • Predicate calculus is one form of symbolic logic
  • Propositions consist of
  • atomic propositions consist of compound terms
  • compound terms have a functor (name or relation)
    and arguments
  • professor (ron)
  • nieces (heidi, sasha, anna)
  • professor (Z), where Z is a variable
  • compound propositions consist of 2 or more atomic
    propositions joined by logical connectors

4
Predicate Calculus (continued)
  • A clausal form is a standard way of writing
    propositions

5
Predicate Calculus (continued)
  • One wants to infer things from propositions
  • Can do so using resolution techniques

6
Predicate Calculus (continued)
  • Unification is the process of selecting values
    for variables in clauses
  • instantiating variables
  • With resolution, can use a restricted kind of
    clause called Horn clauses
  • nothing on the left side (no implication, either)
  • niece (anna, ron) state facts
  • single atomic proposition on the left side
    (headed Horn clause

7
Prolog
  • Prolog is the most prominent example of a
    declarative language
  • Prolog uses resolution and unification on Horn
    clauses
  • Prolog statements consist of terms
  • constants
  • atom (any string of letters, digits, and _
    starting with a lower case letter, or any
    printable string in single quotes)
  • anna
  • miss_JONES
  • South America
  • numbers (integers or reals, usually integers)
  • variables
  • any string of letters, digits, and _ starting
    with an upper case letter or an _
  • X
  • List_2
  • _

8
Prolog (continued)
  • Prolog statements (continued)
  • structures
  • functor and arguments, which may also be
    structures
  • professor(ron)
  • date(4, may, 1995)
  • date (Day, may, 1995)
  • defined by a name (functor) and arity (number of
    arguments)
  • so date (4, may, 1995) and date (124, 1995) are
    different
  • think of these as trees

date
4
may
1995
9
Facts and Rules
  • Prolog programs consist of facts and rules
  • facts are always true. they are unheaded Horn
    clauses
  • they need not be actually true in real life, but
    are in the Prolog universe
  • big (bear). NOTICE .
  • small (cat).
  • big (mouse).
  • mother (ron, eva).
  • rules are full (headed) Horn clauses
  • head is a single term
  • antecedent is a single term or a series of terms
    joined by and
  • brother (X, Y) - male (X), , means
  • parents (X, M, F), and
  • parents (Y, M, F).
  • can also join with meaning or

10
Matching
  • One of the most important operations to do on
    terms is matching
  • two terms match if
  • they are identical
  • the variables in both terms can be instantiated
    to objects so that the terms become identical
  • so date (D, M, 1981) and date (D1, july, Y1)
    match with the instantiations
  • D D1
  • M july
  • Y1 - 1981
  • we say matching succeeds if two terms match,
    otherwise it fails

11
Matching (continued)
  • The formal matching rules are
  • if S and T are constants, then they match only if
    theyre the same object
  • if S is a variable and T is anything, they match
    and S is instantiated to T. Conversely, if T is
    a variable, then T is instantiated to S.
  • if S and T are structures, they match only if
  • S and T have the same principal functor
  • all their corresponding components match
  • triangle(point(1,1), A, point(2,3))
  • triangle(X, point(4,Y), point(2,Z))

triangle
triangle
point
point
point
point
A
X
1
1
2
3
4
Y
2
Z
12
Subgoals and Backtracking
  • Prolog operates by starting with a database of
    facts and rules
  • Then the interpreter is given a goal
  • Prolog searches the database trying to match the
    goal against a fact or the LHS of a rule
  • if match fact
  • done
  • if match LHS of rule
  • RHS of rule becomes set of subgoals, try to match
    subgoals in turn
  • if fail, back up to immediately preceding match,
    try to satisfy the goal that matched with a
    different match
  • subgoals are always satisfied in left-to-right
    order
  • database is always searched beginning to end
  • physical layout of facts and rules determines
    order of processing

13
Subgoals and Backtracking (continued)
  • Example

14
Subgoals and Backtracking (continued)
Control Flow model
call
fail
Goal 1
redo
succeed
call
fail
Goal 2
redo
succeed
  • Failure causes control to return to previous goal
    (redo)
  • Success causes invocation of next goal

15
Backtracking Performance
  • Reordering the clauses and goals in the database
    can have a significant impact on the performance
    of a program
  • consider the following examples

16
Backtracking Performance (continued)
The same logical program, with 4 different
orderings of statements
17
Backtracking Performance (continued)
18
Backtracking Performance (continued)
19
Backtracking Performance (continued)
20
Backtracking Performance (continued)
21
List Operations
  • Lists are one of the basic data structures of
    Prolog
  • the other is structures, which are equivalent to
    records
  • Lists are represented in programs, and printed
    out, as a sequence of items in
  • ann, tom, tennis, skiing

head
tail
ann
tom
tennis
skiing

22
List Operations (continued)
  • Prolog provides a notation to separate head and
    tail of a list
  • head tail
  • ann tom, tennis, skiing
  • Some useful list operations
  • not built in, but some are in libraries
    distributed with various Prologs
  • member (X, L) succeeds if X is in L
  • member (X, X Tail ).
  • member (X, Head Tail ) - member (X,
    Tail).
  • conc (L1, L2, L3) succeeds if L3 L1
    concatenated with L2
  • and returns L3 L1 cat L2
  • conc (, L, L).
  • conc ( X L1, L2, X L3 ) - conc (L1,
    L2, L3).
  • conc ( a, b, c, 1, 2, 3, L).
  • L a, b, c, 1, 2, 3

23
List Operations (continued)
  • Useful operations (continued)
  • add (X, L, X L ). or just X L
  • del (X, L, L1) deletes one occurrence of X from L
    giving L1
  • del (X, X Tail, Tail ).
  • del (X, Y Tail, Y Tail1 ) - del (X,
    Tail, Tail1).
  • ?- del (a, a, b, a, a, L).
  • L b, a, a
  • L a, b, a
  • L a, b, a
  • no

input goal
interpreter response
typed by user causes rematch as if goal had
failed
24
Arithmetic
  • In Prolog, arithmetic is performed by built-in
    predicates that take arithmetic expressions as
    arguments and evaluate them
  • arithmetic expressions (ae) consist of numbers,
    variables, and arithmetic functors
  • arithmetic functors are
  • X Y, X - Y, X Y, X / Y (real division), X //
    Y (integer division), X mod Y, -X
  • variable must be bound to a non-variable
    expression at evaluation time
  • To evaluate an ae, pass as argument to one of
    these predicates
  • Z is X (assignment statement), X Y (numeric
    equality test), X \ Y (not equal), X lt Y, X gt
    Y, X lt Y, X gt Y

25
Arithmetic (continued)
  • Examples
  • suppose database with facts born (name,
    yearborn). Can retrieve anyone born between 1950
    and 1960 by
  • ?- born (Name, Year), Year gt 1950, Year lt 1960.
  • find the length (number of top level nodes) of a
    list
  • length ( , 0).
  • length ( X Tail, N ) - length (Tail, N1),
    N is N1 1.
  • ?- length ( a, b, c, d , e , N).
  • N 4
  • Its important to differentiate between X Y
    (which tries to match X and Y) and X Y, which
    tests numeric equality
  • ?- 12 2 1.
  • yes
  • ?- 1 2 2 1
  • no
  • ?- 1 A B 2 matches and A set to 2, B set
    to 1

26
Controlling Backtracking
  • Another way to control efficiency in a Prolog
    program is to directly control the backtracking
    mechanism
  • Means to do this called the cut (denoted !)
  • cut is a subgoal that is always satisfied, but
    backtracking cant pass through
  • freezes choices made to that point
  • C - P, Q, R, !, S, T, U.
  • C - V.
  • Suppose try to satisfy rule A - B, C, D.
  • satisfy B, try to satisfy C, match first LHS, try
    to satisfy P, Q, R, S, T, U
  • backtracking works freely when trying to satisfy
    P, Q or R
  • once past !, backtracking works freely trying to
    satisfy S, T or U
  • if S eventually fails, wont backtrack to try
    other alternatives for P, Q, R, which means C
    fails. Also wont try rule C - V.

27
Controlling Backtracking (continued)
  • Example
  • max (first, second, larger)
  • max (X, Y, X) - X gt Y.
  • max (X, Y, Y).
  • if first rule succeeds, never have to check
    second, so add cut
  • max (X, Y, X) - X gt Y, !.
  • max (X, Y, Y).
  • important if max is one of several subgoals
  • foo (A, B) - max (A, B, Large),
  • bar (Large).
  • if max is satisfied by A gt B and bar fails, no
    point in trying to satisfy max again.

28
N Queens Hints
  • One way to start this is to pass in a list of the
    row positions, with variables for each of the
    columns
  • variables get instantiated while executing
  • add rule
  • board (1/C1, 2/C2, ... , 8/C8).
  • then to invoke your program
  • ?- board(Soln), nqueens(Soln).
  • Soln 1/4, 2/2, ... , 8/1
  • type to find more than one solution

29
Running Prolog
  • Log onto workstation in design center and type
    Prolog at system prompt.
  • The interpreter provides a top level prompt ?-
  • load a file
  • ?- filename.
  • allow clauses to be entered directly from
    terminal
  • ?- user
  • no way to save - for play only
  • D control D returns to top level
  • ?-
  • to interrupt execution
  • ?- foo (X).
  • C
  • Prolog interrupt - press h for help gets menu of
    choices
  • to exit
  • ?- D

30
Running Prolog (continued)
  • debugging
  • ?- trace.
  • starts extensive tracing mode
  • Enter key single steps
  • ?- notrace. turns off trace
  • ?- spy (rulename).
  • traces only rule
  • ?- spy (member).
  • ?- nospy. turns of spy
  • Use Unix script command to capture screen output
    to hand in
Write a Comment
User Comments (0)
About PowerShow.com