Section 5 - Georgia State University

Download Report

Transcript Section 5 - Georgia State University

Formal Logic
Mathematical Structures
for Computer Science
Chapter 1
Copyright © 2006 W.H. Freeman & Co.
MSCS Slides
Formal Logic
Declarative Programming Languages




Section 1.5
A declarative language is based on predicate logic.
A program written in a declarative language consists
only of statements (actually predicate wffs) that are
declared as hypotheses.
Execution of a declarative program allows the user to
pose queries, asking for information about possible
conclusions that can be derived from the hypotheses.
After obtaining the user’s query, the language turns on
its “inference engine” and applies its rules of inference
to the hypotheses to see which conclusions fit the
user’s query.
Logic Programming
1
Prolog




Prolog (PROgramming in LOGic) is a declarative
programming language.
The set of declarations that constitutes a Prolog program is
also known as a Prolog database.
Items in a Prolog database are either facts or rules.
Example of Prolog facts (a binary predicate called “eat”):
eat (bear, fish)
eat (bear, fox)
eat (deer, grass)
“bear,” “fish,” “fox,” “deer,” and “grass” are constants
because they represent specific elements in the domain.




Section 1.5
Logic Programming
2
Prolog

Other facts that we could add to the Prolog database:






animal (bear)
animal (fish)
animal (fox)
animal (deer)
plant (grass)
We can now pose some simple queries.

is (eat (deer, grass))
• yes

is (eat (bear, rabbit))
• no

Section 1.5
“is” asks if the fact exists in the database.
Logic Programming
3
Prolog

Queries may include variables, for example:


produces:




Section 1.5
which(x: eat(bear, x))
fish
fox
The second type of item in a Prolog database is a
Prolog rule.
A rule is a description of a predicate by means of an
implication.
Logic Programming
4
Prolog Rules

For example, we might use a rule to define a predicate
of prey:


This says that x is a prey if it is an animal that is eaten.
If we add this rule to our database, then in response to
the query:


which(x: prey(x))
we would get:


Section 1.5
prey(x) if eat(y, x) and animal(x)
fish
fox
Logic Programming
5
Horn Clauses and Resolution

We can describe the facts in our database by the wffs

E(b, fi)
E(b, fo)
E(d, g)
A(b)
A( fi)
A( fo)
A(d)
P(g)

with the rule: E(y, x) Λ A(x)  Pr (x)








Prolog treats the rule as being universally quantified
and uses universal instantiation to strip off the
universal quantifiers:

Section 1.5
("y)("x)[E(y, x) Λ A(x)  Pr(x)]
Logic Programming
6
Horn Clauses and Resolution


A Horn clause is a wff composed of predicates or the
negations of predicates (with either variables or
constants as arguments) joined by disjunctions, where,
at most, one predicate is unnegated.
Example of Horn clause:


This can be rewritten using DeMorgan’s law as


Section 1.5
[E(y,x) Λ A(x)] V Pr(x)
This is equivalent to:


[E(y, x)] V [A(x)] V Pr(x)
E(y, x) Λ A(x)  Pr(x)
The above is a rule in the Prolog program.
Logic Programming
7
Horn Clauses and Resolution



The rule of inference used by Prolog is called resolution.
Two Horn clauses in a Prolog database are resolved into a new Horn
clause if one contains an unnegated predicate that matches a negated
predicate in the other clause.
For example:
• A(a)
• [A(a)] V B(b)

is equivalent to:
• A(a), A(a)  B(b)

Prolog infers:
• B(b)


Section 1.5
which is just an application of modus ponens.
Therefore, Prolog’s rule of inference includes modus ponens as
a special case.
Logic Programming
8
Recursion




Prolog rules are implications.
Their antecedents may depend on facts or other rules.
The antecedent of a rule may also depend on that rule
itself, in which case the rule is defined in terms of itself.
For example, we can then define a binary relation infood-chain(x, y), meaning “y is in x’s food chain.” This
means one of two things:
1.
2.
2.
Section 1.5
x eats y directly.
x eats something that eats something that eats something ...
that eats y.
This can also be stated as:
x eats z and y is in z’s food chain.
Logic Programming
9
Recursion





Section 1.5
Case (1) is simple to test from our existing facts, but without (2), infood-chain means nothing different than eat.
On the other hand, (2) without (1) sends us down an infinite path of
something eating something eating something and so on, with nothing
telling us when to stop.
Recursive definitions always need a stopping point that consists of
specific information.
The Prolog rule for in-food-chain incorporates (1) and (2):

in-food-chain(x, y) if eat(x, y)

in-food-chain(x, y) if eat(x, z) and in-food-chain(z, y)
is a recursive rule because it defines the predicate in-food-chain
in terms of in-food-chain.
Logic Programming
10
Expert Systems




Section 1.5
Many interesting applications programs have been
developed, in Prolog and similar logic programming
languages, that gather a database of facts and rules about
some domain and then use this database to draw
conclusions.
Such programs are known as expert systems, knowledgebased systems, or rule-based systems.
The database in an expert system attempts to capture the
knowledge (“elicit the expertise”) of a human expert in a
particular field.
This includes both the facts known to the expert and the
expert’s reasoning path in reaching conclusions from those
facts.
Logic Programming
11