Logic Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Logic Programming

Description:

Note that trace is called with no parameters. %remove() removes an element from a list. ... To tell Prolog to fail a particular goal without trying other solutions: ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: uahscie
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming


1
Logic Programming
  • 15.2.2 Practical Aspects
  • 15.3 Example Applications

2
Simple Arithmetic
  • Integer variables and integer operations are
    possible, but imperative language assignment
    statements dont exist.

3
Prolog Operators
  • is can be used to cause a variable to be
    temporarily instantiated with a value.
  • Does not have the same effect as an assignment
    statement, because the effect isnt permanent.
  • The not operator is used to indicate goal
    failure. For example not(P) is true when P is
    false.

4
Arithmetic
  • Originally, used prefix notation (7, X)
  • Modern versions have infix notationX is Y C
    3.
  • Qualification Y and C must be instantiated, as
    in the Speed program, but X cannot be (Its not a
    traditional assignment statement).
  • X X Y is illegal.
  • X is X Y is illegal. Arguments are not
    sufficiently instantiated

5
More About Arithmetic
  • Example of simple arithmetic
  • ?- x is 3 7.
  • x 10
  • Yes
  • Arithmetic operators , -, , /,
    (exponentiation)
  • Relational operators lt, gt, , lt, gt, \

6
Using the Trace Function
  • At the prompt, type trace.
  • Then type the query.
  • Prolog will show the rules it uses and the
    instantiation of unbound constants.
  • The following example was run on the SWI-Prolog
    on the CS Department system. Note that trace is
    called with no parameters.

7
remove() removes an element from a list. To
Call remove(a, List, Remainder). or remove(X,
List, Remainder). First parameter is the
removed item, 2nd parameter is the original
list, third is the final list remove(X, XR,
R). remove(X, HR, HS)- remove(X,
R, S).
8
18 ?- trace. Yes 18 ?- remove(a, b, d, a, c,
R). Call (7) remove(a, b, d, a, c, _G545) ?
creep Call (8) remove(a, d, a, c, _G608) ?
creep Call (9) remove(a, a, c, _G611) ?
creep Exit (9) remove(a, a, c, c) ?
creep Exit (8) remove(a, d, a, c, d, c) ?
creep Exit (7) remove(a, b, d, a, c, b, d,
c) ? creep R b, d, c
9
Revisiting The Factorial Function
Evaluation of clauses is from left to right. Note
the use of is to temporarily assign values to
M and Result
10
Trace of Factorial (4)
11
The cut not Operators
  • The cut (!) is used to control backtracking.
  • It tells Prolog not to retry the series of goals
    that precede the cut symbol (if the goals have
    succeeded once).
  • Reasons Faster execution, saves memory
  • Not(P) will succeed when P fails.
  • In some places it can replace the ! Operator.

12
Example Revised Factorial
factorial(N, 1)- N lt 1, !. factorial(N,
Result)- M is N 1, factorial(M,
P), Result is N P.
factorial(N, 1)- N lt 1. factorial(N, Result)-
not(N lt 1), M is N1, factorial(M,
P), Result is N P.
13
When Cut Might Be Used(Clocksin Mellish)
  • To tell Prolog that it has found the right rule
  • if you get this far, you have picked the correct
    rule for this goal.
  • To tell Prolog to fail a particular goal without
    trying other solutions
  • if you get to here, you should stop trying to
    satisfy the goal.
  • if you get to here, you have found the only
    solution to this problem and there is no point in
    ever looking for alternatives.

14
Assert - Adding Facts
  • ?- assert(mother(jane, joe)).adds another fact
    to the database.
  • More sophisticated assert can be embedded in a
    function definition so new facts and rules can be
    added to the database in real time.
  • Useful for learning programs, for example.

15
Symbolic Differentiation Rules Figure 15.9
16
Prolog Symbolic Differentiator Figure 15.10
17
Search Tree for the Query d(x, 2x1, Ans) Figure
15.11
18
Executing a Prolog Program
  • Create a file containing facts and rules e.g.,
    familytree.pl
  • Under the file drop-down menu in SWiplEdit select
    open to load the file into the interpreter.
  • From the Start menu select consult
  • Type queries and the interpreter will try to find
    answers
  • ?- talkswith(P1, P2).

19
SWIplEdit compile error
  • If SWI-Prolog finds an error in the .pl file it
    will give a message such asERROR
    c/temp/prologprogs/remove.pl180 Syntax
    error Illegal start of term(18 is the line
    number)

20
Runtime Error Message
  • The function samelength was called with one
    parameter when it needed 221 ?-
    samelength(X).ERRORUndefined procedure
    samelength/1ERRORHowever, there are
    definitions for samelength/2

21
Runtime Errors
  • Here, the error is an error of omission22 ?-
    samelength(a, b, c,,a, b)
  • Queries must end with a period. If you hit
    enter without typing a period SWIpl just thinks
    you arent through.

22
Using SWI Prolog
  • If there is an error that you cant figure out
    (for example you dont get any answers, you dont
    get a prompt, typing a semicolon doesnt help)
    try cancel under the Start button.
  • If changes are made to the program, dont forget
    to consult again.
Write a Comment
User Comments (0)
About PowerShow.com