+ All Categories
Home > Documents > C H A P T E R N I N E Logic Programming Part 2 Programming Languages Principles and Paradigms by...

C H A P T E R N I N E Logic Programming Part 2 Programming Languages Principles and Paradigms by...

Date post: 18-Jan-2018
Category:
Upload: shauna-carter
View: 222 times
Download: 0 times
Share this document with a friend
Description:
From Last Time: A fact is a term followed by a period (.) as in: speaks(allen, russian). A rule is a term followed by :- and a series of terms sepparated by commas and ended in a period: parent(A,B) :- father(A,B). parent(A,B) :- mother(A,B). Rules are interpreted as “only if” assertions and the commas are treated as logical “and” operators. A Prolog program is a series of facts and rules.
15
C H A P T E R N I N E C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan
Transcript
Page 1: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

C H A P T E R N I N EC H A P T E R N I N E

Logic ProgrammingPart 2

Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

Page 2: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

From Last Time:

• Prolog programs are made up of terms, which can be constants, variables, or structures.• A constant is either an atom (foo, who, “Sue”) or a non-negative integer.• A variable is a series of letter that begins with a capital letter (Alf, Result).• A structure is a predicate with zero or more arguments (speaks(Who,russian)).• The number of arguments is called the structure’s arity.

Page 3: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

From Last Time:

• A fact is a term followed by a period (.) as in:speaks(allen, russian).

• A rule is a term followed by :- and a series of terms sepparated by commas and ended in a period:

parent(A,B) :- father(A,B).parent(A,B) :- mother(A,B).

• Rules are interpreted as “only if” assertions and the commas are treated as logical “and” operators.• A Prolog program is a series of facts and rules.

Page 4: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Lists:

• The basic Prolog data structure is the list, a series of terms separated by commas and enclosed in brackets:

[this, is, a, list]• Lists can contain “don’t care” terms designated by the underscore character (_).• The first element of a list, the head, is distinguished from the remaining elements of the list like this:

[X | Y]

Page 5: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

List Functions:

• A Prolog function to do list concatenation is written recursively as follows:

append([], X, X).append([H | T], Y, [H | Z]) :- append(T, Y, Z).

The first line is the “base case” which says that an empty list concatenated with any other returns the other list.The recursive second case says “if Z is the result of concatenating lists T and Y then concatenating any new list [H | T] with Y yields [H | Z]”.The process goes something like this…

Page 6: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Partial Search Tree for append([english, russian], [spanish], L)Figure 9.5

Page 7: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

List Functions:

• The following functions define prefix and suffix:prefix(X, Z) :- append(X, Y, Z).suffix(Y, Z) :- append(X, Y, Z).

• This function defines membership in a list:member(X, [X | _]).member(X, [_ | Y]) :- member(X, Y).

Note the use of the don’t care terms; the first line says X is a member if it is the head of a list, the second line says that X is a member if it is a member of the tail of a list.

Page 8: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Solving Word Puzzles:

• Consider the following word puzzle:Baker, Cooper, Fletcher, Miller, and Smith live in a five story building. Baker doesn’t live on the 5th floor and Cooper doesn’t live on the first floor. Fletcher doesn’t live on the top or bottom floor, and he is not on a floor adjacent to Smith or Cooper. Miller lives on some floor above Cooper. Who lives on what floors?

•In addition to the member function from before we will define:

adjacent(X, Y) :- X =:= Y+1.adjacent(X, Y) :- X =:= Y-1.

Page 9: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Prolog Solution for the Building ProblemFigure 9.12

To run the program type: building(X).

Page 10: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Cut, is, not and assert:

• Inserting a cut (!) in the right-hand terms (sub-goals) of a rule will cause the right-hand side to not be examined if it has evaluated to true once. (See the bsort example in the book).

• The is operator can be used to force instantiation of a variable:

X is Y+3.• The not operator negates goal failure, that is not(P) is true when P is false.• The assert function is used to add new facts and rules during execution:

assert(mother(jane, joe)).

Page 11: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

The Factorial Function in PrologFigure 9.7

Note that we introduced the intermediate variable M in order to force the evaluation of N-1 before the recursive call.

Page 12: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Trace of Factorial (4)Figure 9.8

Page 13: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Symbolic Differentiation RulesFigure 9.9

Page 14: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Prolog Symbolic DifferentiatorFigure 9.10

Page 15: C H A P T E R N I N E Logic Programming Part 2 Programming Languages  Principles and Paradigms by Allen Tucker, Robert Noonan.

Next time…

Logic Programming

Wrap Up


Recommended