PROLOG - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

PROLOG

Description:

Works exactly as it would in logic, Prolog interprets ' ... food(burrito). language(postscript);food(wood). yes. parent(Parent,Child):- mother(Parent,Child) ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 34
Provided by: terrie2
Category:
Tags: prolog | burrito

less

Transcript and Presenter's Notes

Title: PROLOG


1
PROLOG
  • PRAGMATICS
  • Chapter 7

2
  • There are a wide variety of topics covered in
    this chapter.
  • Well address only the most important.
  • Read the entire chapter to be aware of these
    capabilities.

3
Disjunction ()
  • Works exactly as it would in logic, Prolog
    interprets "" to mean "or
  • When a "" is placed between two lines
    (expressions), Prolog tries to solve the first
    expression. If it cannot, then it attempts to
    solve the second expression.
  • Not commonly used.

4
Example Disjunction - Query
  • language(postscipt).
  • food(burrito).
  • language(postscript)food(wood).
  • yes.

5
Example Disjunction - Predicate
  • parent(Parent,Child)-
  • mother(Parent,Child)
  • father(Parent,Child).
  • The traditional style is to write two versions of
    the predicate
  • parent(Parent,Child)-
  • mother(Parent,Child).
  • parent(Parent,Child)-
  • father(Parent,Child).
  • Each version is treated as OR

6
If-then and if-then-else (-gt)
  • Two forms
  • Condition -gt Truegoal
  • Condition -gt Truegoal Falsegoal
  • For the first form, if the condition is true,
    execute Truegoal.
  • For the second form, if the condition is true,
    execute Truegoal if the condition is false,
    execute Falsegoal.
  • Can accomplish the same logic by defining two
    predicates, and adding nots and ands.

7
Cut (!)
  • Cut tells the program to discard the choices it
    has made up to that point.
  • Recall for backtracking, Prolog places markers at
    all decision points which are then used to
    backtrack. When cut is encountered, all markers
    are deleted.
  • Reduces the search space, often quite
    dramatically. Making a program much more
    efficient in terms of search time and memory
    requirements.
  • Sometimes required for a program to work the way
    you want it to.

8
Cut Example
  • old(X)- age(X,A),A gt 65 .
  • age(jo,30).
  • age(bill,76).
  • age(zeke,98).
  • The query old(Names). causes Prolog to search
    through the data base to find all the old people.

9
Cut Example (continued)
  • old(jo).
  • Prolog will respond "no", but it will also
    attempt to backtrack, searching the knowledge
    base for all of jos ages.
  • However, as the programmer, you know that each
    individual has only one age and backtracking is
    useless.
  • To control backtracking, alter the predicate

10
Cut Example (continued)
  • old2(X)- age(X,A), !, A gt 65 .
  • To execute the query, Prolog will instantiate X
    and then remove the markers made previous to !
  • This has the effect of forcing Prolog to consider
    only the choices in effect when it reached the
    !.
  • old2(jo). - no longer backtracks
  • old2(Names). - cannot find the names of the old
    people, because it is unable to backtrack.

11
Cut Cautions
  • Be careful when using cut!
  • It may work fine when you provide values for
    certain arguments, but how the predicate will
    behave when you provide values for different
    arguments (or no values at all) can be tricky.
  • Test predicates that use ! thoroughly and
    document the limits on the use of the predicate.

12
Application of Cut, Disjunction and Condition
  • Cut is used in applications. In some cases it is
    the only way to implement the needed behavior.
  • I recommend against the use of disjunction and
    condition.

13
Fail
  • Means just what it says - Fail (false).
  • Often used with cut.

14
Fail Example
  • Define a predicate "canFly(X)" which only
    succeeds when X can fly.
  • If X is a bird, but not an ostrich or penguin it
    should succeed.

15
Fail Example (continued)
  • is_a(bird,ostrich).
  • is_a(bird,penguin).
  • is_a(bird,parakeet).
  • is_a(ostrich,ted).
  • is_a(penguin,bill).
  • is_a(parakeet,kim).
  • canFly(X)- is_a(ostrich,X),!,fail.
  • canFly(X)- is_a(penguin,X),!,fail.
  • canFly(X)- is_a(Y,X),is_a(bird,Y).

16
Fail Example (continued)
  • canFly(bill).
  • canFly(ted).
  • canFly(kim).
  • canFly(X).
  • What happens if the !fail combination is not
    included
  • The query succeeds via the third version of the
    predicate.

17
Built in Predicates
  • Negation (not)
  • Assert
  • Retract
  • ..
  • Arg

18
Negation (not)
  • not (Goal)
  • not Goal
  • Reverses the truth value of the goal. (Think in
    terms of logic!)

19
Assert
  • asserta(clause) - adds clause the first clause
    with this name in the database (i.e., puts it at
    the top).
  • assertz(clause) - make this the last clause with
    this name in the database (i.e., put it at the
    bottom)
  • The parameter can be almost anything, another
    rule, a fact, etc.
  • VERY useful. Provides the ability to alter the
    database during execution.

20
Retract
  • retract(clause) - removes the first clause whose
    head matches from the database.
  • Frequently used in conjunction with assert to
    save and recall values from the database.

21
Abolish
  • Removes all versions of the predicate from the
    database.
  • Example
  • abolish(clause)

22
..
  • Term .. List
  • Converts a list (on the right) to a term (on the
    left)
  • To allow conversion between lists and facts.
  • Sets "List" to a list whose first element is the
    predicate of X and whose other terms are the
    arguments of X.

23
.. Examples
  • loves(quince,lox).. Y?
  • Y loves,quince,lox
  • X..a,b,c,d,e
  • Xa(b,c,d,e)

24
arg(N,Structure,Argument)
  • Unifies Argument with the Nth argument of
    Structure
  • N must be a positive integer, and Structure
    should be a compound term with arity greater than
    or equal to N. Must specify values for N and
    Structure.

25
Arg Examples
  • arg(2, related(john,mother(jane) ),X).
  • X mother(jane)
  • arg(1, a(bc),X).
  • X a

26
Checking the Types of Terms
  • Refer to text page 191
  • atom(Term) Term is an atom.
  • atomic(Term) Term is an atom or number.
  • float(Term) Term is a floating point number.
    Only examines the internal representation.
  • integer(Term) Term is an integer.
  • integer(2.0) is true
  • integer range is -2 27 to 227-1

27
Types of Terms
  • number(Term) Term is integer or floating point.
  • var(Term) Term is an unbound variable.
  • nonvar(Term) Term is an instantiated variable.
  • Term is instantiated if it has been assigned a
    value, or if Prolog has assigned a value during
    processing).

28
Input and Output
  • write
  • read
  • see
  • tell
  • ratom

29
write and writeq
  • write(X) were X is the term to be written to the
    output stream.
  • writeq(X) places quotes around X
  • Other handy terms
  • nl - tells Prolog to move to a new line.
  • tab(N) - tells Prolog to output N spaces (ie.
    tab over)

30
read
  • read(Term) reads the next Term.
  • A term ends with a period or question mark.
  • get(C) gets the next printable Character in the
    input stream.
  • Geto(C) gets the next Character.

31
See
  • see, seeing, and seen are ways to define,
    determine and change the current input stream.
    By default the I/O stream is the screen.
  • see(File) tells Prolog to set the input stream to
    File.
  • seeing(File) has Prolog return the name of the
    current input stream.
  • seen closes the current input stream.

32
Tell
  • tell, telling, and told are ways to define,
    determine, and close the current output stream.
  • tell(File) tells Prolog to direct output to File.
  • telling(File) has Prolog return the name of the
    current output stream.
  • told closes the current output stream (close also
    works).

33
Ratom
  • Ratom(X) reads an atom (input token, single
    character).
  • Does not read .
Write a Comment
User Comments (0)
About PowerShow.com