• Nie Znaleziono Wyników

Lectures 3,4

N/A
N/A
Protected

Academic year: 2021

Share "Lectures 3,4"

Copied!
80
0
0

Pełen tekst

(1)

Lectures 3,4

PROLOG

LOGic PROgramming

(2)

Logic: what is it?

Philosophical logic

– fundamental questions

Mathematical logic

– theory of correct reasoning

Computational logic

– effectiveness of reasoning

(3)

Logic Programming

Logic programming uses a form of

symbolic logic and a logical inference process

Languages based on symbolic logic are called logic programming

languages or declarative languages

Prolog is the most widely used logic

language

(4)

Predicate Calculus

Formal logic is the basis for logic programming

Key Points

– Proposition:

• a logical statement that may or may not be true

– Symbolic logic used for three purposes

• express propositions

• express the relationships between propositions

• describe how new propositions may be inferred

– Predicate calculus is the form of symbolic logic used

(5)

Propositions

(6)

Atomic propositions

Objects in logic programming propositions are

– Constants: symbols that represent an object – Variables: symbols that can represent different

objects at different times

Atomic propositions are the simplest

propositions and consist of compound terms

– Compound term: an element of a mathematical relation, written in form of function notation, which

(7)

Compound terms

Compound term has two parts

– Functor: symbol that names the relation – An ordered list of parameters

Compound term with a single parameter is called a 1-tuple; with two – a 2-tuple, etc.

Example:

man (jake)

{jake} is a 1-tuple in relation man like (bob, steak)

{bob, steak} is a 2-tuple in like

(8)

Facts and queries

Simple terms in propositions – man, jake, bob, and steak – are constants

These propositions have no intrinsic semantics

– like (bob, steak) could mean several things

Propositions are stated in two modes

– Fact:

one in which the proposition is defined to be true

(9)

Logical operators

Name Symbol Example Meaning

negation   a not a

conjunction  () a  b a and b disjunction  () a  b a or b

equivalence  () a  b a is eqv to b implication  ()

 ()

a  b a  b

a implies b b implies a

(10)

Compound propositions

Compound proposition examples:

a  b  c

a   b  d equivalent to (a ( b))  d

Precedence of logical connectors:

 highest precedence

, ,  next

(11)

Quantifiers

Variables may appear in propositions – only when introduced by symbols called quantifiers

Note: The period separates the variable from the proposition

Name Example Meaning

Universal X.P For all X, P is true

existential X.P There exists a value of

X such that P is true

(12)

Quantifiers

Examples of propositions with quantifiers

 X.(woman(X)  human(X))

For any value of X, if X is a woman, then X is human

 X.(mother(mary, X)  male (X))

There exists a value of X, such that mary is the mother of X and X is a male

Note: Quantifiers have a higher precedence

than any of the logical operators

(13)

Example of Logic  Predicate Calculus

0 is a natural number.

natural (0)

2 is a natural number.

natural (2)

For all X, if X is a natural number, then so is the successor of X.

X.natural (X)  natural (successor (X))

(14)

Logic Statement to P.C.

A horse is a mammal.

A human is a mammal.

Mammals have four legs and no arms, or two legs and two arms.

A horse has no arms.

A human has arms.

(15)

Solution:

First Order Predicate Calculus

mammal (horse).

mammal (human).

X. mammal (X)  legs (X,4)  arms (X,0)  legs (X,2)  arms (X,2)

arms (horse, 0).

arms (human, 2) or  arms (human, 0).

legs (human, 0).

(16)

Clausal forms

(17)

General statement

Redundancy is a problem with predicate calculus

– there are many different ways of stating propositions that have the same meaning – not a problem for logicians

For computerized system, this is a problem

Clausal form is one standard form of propositions used for simplification:

B1  B2  ...  Bn  A1  A2  ...  Am

Meaning:

(18)

Characteristics

Existential quantifiers are not required

Universal quantifiers are implicit in the use of variable in the atomic propositions

Only the conjunction and disjunction operators are required

Disjunction appears on the left side of the

casual form and conjunction on the right

side

(19)

Examples

Proposition 1:

likes (bob, trout)  likes (bob, fish)  fish (trout)

Meaning:

if bob likes fish and a trout is a fish, then bob likes trout

Proposition 2:

father(louis, al)  father(louis, violet)  father(al, bob) mother(violet, bob)  grandfather(louis, bob)

Meaning:

if al is bob’s father and violet is bob’s mother and louis is bob’s grandfather, then louis is either al’s father or violet’s father

(20)

Resolution

(21)

Proving theorems

One of the most significant

breakthroughs in automatic theorem- proving was the discovery of the

resolution principle by Robinson in 1965

Resolution is an inference rule that allows inferred propositions to be computed from given propositions

– Resolution was devised to be applied to

propositions in clausal form

(22)

Basic idea

The concept of resolution is the following: Given two propositions:

P1  P2 Q1  Q2

Suppose P1 is identical to Q2 and we rename them as T. Then

T  P2 Q1  T

Since P2 implies T and T implies Q1, it is logically obvious that P2 implies Q1, that is

Q1  P2

(23)

Example

Consider the two propositions:

older (joanne, jake)  mother (joanne, jake) wiser (joanne, jake)  older (joanne, jake)

We can conclude that

wiser (joanne, jake)  mother (joanne, jake)

The mechanics of this resolution construction is one of cancellation of the common term

when both left sides are combined and both

right sides are combined

(24)

Expanded Example

father(bob, jake)  mother(bob,jake)

 parent (bob,jake)

grandfather(bob,fred)

 father(bob,jake)  father(jake,fred)

Resolution process cancels common term on both the left and right sides:

mother(bob,jake)  grandfather(bob,fred)

 parent (bob,jake)  father(jake,fred)

(25)

Most important laws

Unification: The process of determining useful values for variables in propositions to find values for variables that allow the resolution process to succeed.

Instantiation: The temporary assigning of values to variables to allow unification.

Inconsistency detection: A critically important property of resolution is its ability to detect any inconsistency in a given set of propositions. This property allows resolution to be used to prove theorems.

(26)

Horn clauses

(27)

Basics

When propositions are used for resolution, only a restricted kind of clausal form can be used

Horn clauses

– special kind of clausal form to simplify resolution – two forms:

• single atomic proposition on the left side, or

• an empty left side

– left side of Horn clause is called the head

– Horn clauses with left sides are called headed Horn clauses

(28)

Relationships and facts

Headed Horn clauses are used to state relationships:

likes(bob, trout)  likes (bob, fish)  fish(trout)

Headless Horn clauses are used to state facts:

father(bob,jake)

Most propositions may be stated as Horn

(29)

Logic Programming

(30)

Brief history

Resolution principle (Robinson 1965)

Prolog language (Colmerauer 1972)

Operational semantics (Kowalski 1974)

Prolog compiler (Warren 1977, 1983)

5th generation project (Japan 1981--)

Constraint LP (Jaffar & Lassez 1987)

(31)

Brief basics

Robert Kowalski says:

Algorithm = Logic + Control

Prolog is the most popular logic programming language

A Prolog program is a collection

of Horn clauses

(32)

Declarative languages

Languages used for logic programming are called declarative languages

Programming with declarative languages is nonprocedural

– programs do not state exactly how a result is to be computed but rather describe the form of the result – it is assumed that the computer can determine how

the result is to be obtained

– one needs to provide the computer with the

(33)

Important issues

Program

– knowledge about the problem in the form of logical axioms

Call of the program

– provide a problem description (as a logical statement)

Execution

– attempt to prove the given statement (given the program)

Constructive proofs essential

– can you prove sort([2,3,1], x)?

(34)

PROLOG

(35)

Notation

English Predicate Calculus Prolog

and  () ,

or  () ;

only if  () :-

not  not(...)

??? (query specification) ?-

(36)

Example

/* simple rules for deductions */

sees(X,Y):-person(X),object(Y),open(eyes(X)),in_front_of(X,Y).

sees(X,Y):- person(X),event(Y),watching(X,film_of(Y)).

person(mother(nikos)).

event(birthday(kostas)).

event(graduation(nikos)).

watching(mother(nikos),film_of(graduation(nikos))).

person(kostas). open(eyes(kostas)).

in_front_of(kostas,tv). object(tv).

/* questions */

/* does the mother of nikos see his graduation? */

?-sees(mother(nikos),graduation(nikos)).

(37)

General characteristics

A program uses terms such as constant symbols, variables, or functional terms

Queries may include conjunctions,

disjunctions, variables, and functional terms

A program consists of a sequence of sentences, implicitly conjoined

All variables have implicit universal quantification

Prolog uses a negation as failure operator:

a goal not P is assumed proved if the

(38)

Basic elements

(39)

Terms

A Prolog term is a constant, a variable, or a structure

A constant is either an atom or an integer

– Atoms are the symbolic values of Prolog – Atoms begin with a lowercase letter

Variables begin with an uppercase letter

– not bound to types by declarations

– binding of a value and type is an instantiation

– Instantiations last only through completion of goal

Structures represent the atomic propositions

of predicate calculus

(40)

Fact statements

Fact statements are used to construct hypotheses (database) from which new information may be inferred

Fact statements are headless Horn clauses assumed true

Examples:

male(bill).

female(mary).

male(jake).

(41)

Rule statements

Rule statements are headed Horn

clauses for constructing the database

The RHS is the antecedent (if), and the LHS is the consequent (then)

Consequent is a single term because it is a Horn clause

Conjunctions may contain multiple terms that are separated by logical ANDs denoted by commas, e.g.:

female(shelley), child (shelley).

(42)

Rule statements

General form of the Prolog headed Horn clause

consequence_1 :- antecedent_expression

Example:

ancestor(mary, shelley) :- mother(mary, shelley).

Demonstration of the use of variables:

parent(X, Y) :- mother (X, Y).

parent(X, Y) :- father(X, Y).

grandparent(X, Z) :- parent(X, Y), parent (Y, Z).

sibling(X,Y) :- mother(M,X), mother(M,Y),

(43)

Goal statements

Because goal and some nongoal statements have the same form (headless Horn clauses), it is imperative to distinguish between the two

Interactive Prolog implementations do this by simply having two modes, indicated by

different prompts: one for entering goals and one for entering fact and rule statements

Some versions of Prolog use ?- for goals

(44)

Goal statements

Facts and rules are used to prove or disprove a theorem that is in the form of a proposition (called goal or query)

Syntactic form of Prolog goal statement is identical to headless Horn clauses, e.g.

man (fred).

to which the system will respond yes or no

Conjunctive propositions and propositions with variables are also legal goals, e.g.

father ( X, mike).

When variables are present, the system identifies the instantiations of the variables that make the goal true

(45)

Example

Database:

likes(george,kate).

likes(george,susie).

likes(george,wine).

likes(susie,wine).

likes(kate,gin).

likes(kate,susie).

(46)

Example

Query:

?-likes(george,X).

X = kate

;

X = susie

;

X = wine

;

(47)

Example

Database:

likes(george,kate). likes(george,susie).

likes(george,wine). likes(susie,wine).

likes(kate,gin). likes(kate,susie).

friends(X,Y) :- likes(X,Z), likes(Y,Z).

Query:

?- friends(george,susie)

yes

(48)

Inference process

(49)

General scheme

Goals (queries) may be compound

propositions; each of facts (structures) is called a subgoal

The inference process must find a chain of rules/facts in the database that connect the goal to one or more facts in the database

If Q is the goal, then either Q must be found as fact in the database or the inference

process must find a sequence of propositions

that give that result

(50)

Matching

The process of proving (or satisfying) a

subgoal by a proposition-matching process

Consider the goal or query:

man (bob).

If the database includes the fact man(bob), the proof is trivial

If the database contains the following fact and inference

father (bob).

man (X) :- father (X)

(51)

Matching

Consider the goal:

man(X).

Prolog must match the goal against the propositions in the database

The first found proposition with the form of the goal, with an object as its parameter, will cause X to be instantiated with that object’s value and this result displayed

If there is no proposition with the form of the

goal, the system indicates that the goal can’t

be satisfied

(52)

Solution search approaches

Depth-first

– Finds a complete sequence of propositions (a proof) for the first subgoal before working on the others

Breadth-first

– Works on all subgoals of a given goal in parallel

Backtracking

– When the system finds a subgoal it cannot prove, it reconsiders the previous one to attempt to find an alternative solution and then continue the search- multiple solutions to a subgoal result from different

(53)

Backtracking example

Suppose we have the following compound goal:

?- male (X), parent (X, shelley)

Is there an instantiation of X, such that X is a male and X is a parent of shelley?

The search

– Prolog finds the first fact in the database with male as its functor

– It instantiates X to the parameter of the found fact, say john

– It attempts to prove that parent(john, shelley) is true – If it fails, it backtracks to the first subgoal, male(X)

and attempts to satisfy it with another alternative to X

(54)

Simple arithmetic

Suppose we know the average speeds of several automobiles on a particular racetrack and the amount of time they are on the track:

speed(chevy, 105).

speed(dodge, 95).

time(ford, 20).

time(chevy, 21).

We can calculate distance with this rule:

distance(X,Y) :-

speed(X, Speed), time(X,Time), Y is Speed * Time

Query

distance(chevy, Chevy_Dist).

will instantiate Chevy_Dist as 105*21 = 2205

(55)

Tracing

(56)

Tracing the process

Consider the following example:

likes(jake, chocolate).

likes(jake, apricots).

likes(darcie, licorice).

likes(darcie, apricots).

?-likes(jake,X), likes(darcie,X).

Trace Process:

(1) Call: likes(jake, _0)?

(1) Exit: likes(jake, chocolate) (2) Call: likes(darcie, chocolate)?

(2) Fail: likes(darcie,chocolate) (1) Redo: likes(jake, _0)?

(57)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay hesnotokay :- shesokay

shesokay

(58)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay

shesokay

(59)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay hesnotokay :- shesokay

shesokay

hesnotokay

(60)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay

shesokay

hesnotokay

(61)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay hesnotokay :- shesokay

shesokay

hesnotokay

shesokay

(62)

Example

imokay :- youreokay, hesokay

youreokay :- theyreokay hesokay.

theyreokay.

hesnotokay :- imnotokay shesokay :- hesnotokay shesokay :- theyreokay

shesokay

hesnotokay shesokay

LOOPS

(63)

List structures

(64)

Basics

Lists can be created with a simple structure:

new_list([apple, prune, grape]).

where new_list is a relation like male(jake)

Prolog uses the special notation [ X | Y ] to indicate head X and tail Y

In query mode, the elements of new_list can be dismantled:

new_list([New_List_Head | New_List_Tail]).

(65)

Append

append(([bob, jo], [jake, darcie], Family).

produces the output

Family = [bob, jo, jake, darcie]

and

Yes

(66)

Reverse

reverse ([bob, jo, jake, darcie], Rev_list).

prints

Rev_List = [darcie, jake, jo, bob]

and

Yes

(67)

Member

Member function is used to determine if a given symbol is in a given list

Example:

member( bob, [darcie, jo, jim, bob, alice]).

The system responds with

Yes

(68)

Performance control

(69)

Resolution order control

Prolog always matches in the same order:

starting at the beginning and at the left end of a given goal

Therefore, the user can affect efficiency by ordering the database statements to optimize a particular application

If certain rules are more likely to succeed

than others during an execution, placing

those rules first makes the program more

efficient

(70)

Problems with control

The ability to tamper with control flow in a Prolog program is a deficiency because it is detrimental to one of the advantages of logic programming: programs do not specify how to find solutions

Using the ability for flow control, clutters the

simplicity of logic programs with details of

order control to produce solutions

(71)

Negation

(72)

Example

Consider the following:

parent(bill, jake).

parent(bill, shelley).

sibling(X,Y) :- parent(M,X), parent(M,Y).

?-sibling(X,Y).

Prolog’s result is

X = jake

(73)

Example

Consider the following:

parent(bill, jake).

parent(bill, shelley).

sibling(X,Y) :- parent(M,X), parent(M,Y), not(X=Y).

?-sibling(X,Y).

Prolog’s result is

X = jake

Y = shelley

(74)

Problems with „not”

Logical „not” cannot be a part of Prolog primarily because of the form of the Horn clauses

B :- A1  A2  ...  Am

If all the A propositions are true, it can be concluded that B is true.

But regardless of the truth or falseness of any

or all of the As, it cannot be concluded that B

is false.

(75)

Applications

(76)

Logic Programming and RDBMS

RDBMs store data in tables and queries are often stated in relational calculus

Query languages are nonprocedural

Obvious facility of Prolog to represent tables(facts) and relationships between

tables(rules) with the retrieval process being inherent in the resolution operation

Advantages

– only a single language is required

(77)

Expert Systems

Expert Systems are designed to emulate

human expertise. They consist of a database of facts, an inference process, some

heuristics about the domain, and some friendly human interface.

Prolog provides the facilities of using

resolution for query processing, adding facts and rules to provide the learning capability, and using trace to inform the user of the

reasoning behind a given result.

(78)

Natural Language Processing

Forms of logic programming have been found to be equivalent to context-free grammars to describe language syntax

Some semantics of natural languages can be modeled with logic programming

Research in logic-based semantics networks

has shown that sets of sentences in natural

(79)

Education

Extensive experiments in using micro-Prolog to teach children logic programming language

Advantages

– introduces computation at an early age

– teaches logic which results in clearer thinking and expression

– assists students with mathematics and other subjects

Experiments have produced an interesting result:

– it is easier to teach logic programming to a

beginner than to a programmer with a significant

(80)

Cytaty

Powiązane dokumenty

This abstract result provides an elementary proof of the existence of bifurcation intervals for some eigenvalue problems with nondifferentiable nonlinearities1. All the results

A complete probability measure µ on a space X is said to be Radon if it is defined on the Borel subsets of X and has the property that the measure of each Borel set is the supremum

The percentage of newly created genotypes that passed the life test fell from 82 (90% of the deadline set) to 36 (60% of the deadline set). However, it did not influence the

the purpose of improving the Rekers-Sch¨urr graph grammar parser [10], although the authors did not provide a rigorous graph-theoretic and group-theoretic definition of

Besides these the proof uses Borel–Carath´ eodory theorem and Hadamard’s three circles theorem (the application of these last two theorems is similar to that explained in [4], pp..

This generalisation of gauge theory uses the notion of coalgebra ψ-bundles, which was proposed by the author at his lecture delivered at the Banach Center Minisemester on Quantum

If X is a real Hilbert space condition (d) can be replaced by “F ( · , x) has a strongly measurable selection” and the values of F need only be closed convex.. This is Theorem 10.5

W i l k i e, Some model completeness results for expansions of the ordered field of real numbers by Pfaffian functions, preprint, 1991. [10] —, Model completeness results for