22
Logic Programming Table of Contents 1. Application Domains .................................................. 2 2. Definitions................................................................... 2 3. Basic Definitions ......................................................... 4 4. Logic Programs ........................................................... 5 5. Inferencing Process ..................................................... 7 6. Programming Languages vs. IPL & LP .................... 13 7. Drawbacks of Logic programming ........................... 13 8. Prolog ........................................................................ 14 9. Getting Started .......................................................... 15 10. Create a Prolog program ........................................... 17 A. Bellaachia Page: 1

Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

Logic Programming

Table of Contents 1. Application Domains .................................................. 2

2. Definitions................................................................... 2

3. Basic Definitions......................................................... 4

4. Logic Programs........................................................... 5

5. Inferencing Process..................................................... 7

6. Programming Languages vs. IPL & LP.................... 13

7. Drawbacks of Logic programming ........................... 13

8. Prolog........................................................................ 14

9. Getting Started .......................................................... 15

10. Create a Prolog program ........................................... 17

A. Bellaachia Page: 1

Page 2: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

1. Application Domains

• Logic programming language application areas include natural language processing, expert systems, specifications checking, theorem proving, and control systems (among others).

2. Definitions • Logic programming languages, also called declarative

languages, differ substantially from the other programming languages we have discussed.

• Declarative rather that procedural: Only specification of results are stated (not detailed procedures for producing them)

• Most importantly, logic programs do not specify the

execution sequence as in most in the case of other language paradigms. User specifies the specifications of a solution and the computer derives the execution sequence for that solution:

Let us have airline flight information of the form: flight(flight_number, from_city, to_city, departure_time, arrival_time) Then all the flights from Washington DC to Santa Clara can be specified as either direct flights or as flights with an intermediate stop:

A. Bellaachia Page: 2

Page 3: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

flight(flight_number, DC, Santa Clara, departure_time, arrival_time) or flight(flight_number, DC, X, depart1, arrive1) flight(flight_number, X, Los Angles, depart2, arrive2) depart2>=arrive1+30

• Unlike imperative and functional programming, where

implementation is done using a mapping, logic programming uses relations:

In imperative and functional, we say

• Given a value x compute mapping(a), i.e.

square(4)

In logic programming, we have • Given a value x and y, determine whether

x is related to y is true

• Consider the relation ParentOf as follows:

ParentOf(X,Y) where X is parent of Y

ParentOf(john, Mary) - John is parent of Mary

A. Bellaachia Page: 3

Page 4: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

3. Basic Definitions • Horn clause:

Horn clause - can have only two forms o Headed: single atomic proposition

on left side o Headless: empty left side (used to

state facts)

A Horn clause is an IF clause and not iff, formally,

A0 if A1 and A2 and A3 and … and An

Each Ai is a simple assertion of the form Ri(…) where Ri is a relation name.

Informally, this clause means that, if A1, …, and An are all true, then we can infer that A0 is also true. But, we cannot conversely infer that A0 is false just because some Ai turns out to be false.

• A logic program is a collection of Horn clauses.

A. Bellaachia Page: 4

Page 5: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

4. Logic Programs • Basic elements:

Term: • a constant, variable, or structure

Constant: • an atom or an integer

Atom: • symbolic value of Prolog

Atom consists of either: • A string of letters, digits, and

underscores beginning with a lowercase letter

• a string of printable ASCII characters delimited by apostrophes

• Statement: Fact Statements Rule Statements Query (or Goal) Statements

• Fact statements: Concrete relations among objects Used for the hypotheses Structure:

functor(parameter list)

Headless Horn clauses o student(Paul). o junior(Mary).

A. Bellaachia Page: 5

Page 6: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Rule Statements A pattern of relationships among the

database objects. Used for the hypotheses Headed Horn clause Right side: antecedent (if part)

• May be single term or conjunction Left side: consequent (then part)

• Must be single term Conjunction: multiple terms separated by

logical AND operations (implied) Example:

• parent(kim,kathy):- mother(kim,kathy).

Can use variables (universal objects) to generalize meaning:

parent(X,Y):- mother(X,Y). sibling(X,Y):- mother(M,X), mother(M,Y), father(F,X), father(F,Y).

• Query (or Goal) Statements It is the theorem is in form of proposition

that we want a system to prove or disprove Same format as headless Horn

student(james) • A logic program consists of a database of the

following: o Fa

o R

A. Bellaachia

ct statements ule Statements

Page: 6

Page 7: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

5. Inferencing Process

• User query: Computation in a logic program consists of

testing a given assertion (query) A. The testing of a query A can be implemented

by a process called matching, satisfying, or resolution.

The facts and the rules of the program are used to determine which substitutions for variables in the query (called unification) are consistent with the information in the database.

If we can infer from the clauses of the program that A is true, then we say that A succeeds. Otherwise, we say that A fails. Note that this does not mean that A is false.

For example, Prolog as an interpreter, prompts the user for an input query and outputs:

the truth value (“yes”) or falsity (“no”) of that query and an assignment to the variables of the query that make the query true (that unifies the query).

• If a goal is a compound proposition, each of the facts is a subgoal.

A. Bellaachia Page: 7

Page 8: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• To prove a goal is true, must find a chain of inference rules and/or facts. For query Q:

B :- A C :- B … Q :- P

• Process of answering a query is called matching, satisfying, or resolution.

• Resolution types:

Bottom-up resolution, forward chaining: Begin with facts and rules of database and attempt to find sequence that leads to goal works well with a large set of possibly correct answers

Top-down resolution, backward chaining: begin with goal and attempt to find sequence that leads to set of facts in database works well with a small set of possibly correct answers

Prolog implementations use backward chaining

A. Bellaachia Page: 8

Page 9: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Query Processing:

If the program contains a fact ‘A0’, such that A0 matches A, then we immediately conclude that A succeeds.

If the program contains a clause ‘A0 if A1 and … and An’ such that A0 matches A, then we proceed by testing A1.. and An separately as (sub)queries: o Two approaches:

Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search)

Breadth-first search: work on all conditions in parallel

o If all succeed, then we conclude that A succeeds. If one of the Ai fails, then we must backtrack, i.e., give up the attempt to use this particular clause and try another clause instead.

o Only we have exhausted all clauses whose left-hand sides match A can we conclude that A fails.

Example:

A. Bellaachia Page: 9

Page 10: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Resolution uses Unification:

Unification: o It is the process of finding values for

variables in propositions that allows matching process to succeed

o To unify two expressions U and V, we need to find substitutions for the variables occurring in U and V that makes the two expressions identical.

o Generally, it is denoted by σ and written as Uσ = Vσ

Example 1:

o To unify the two expressions:

R1(X,John) with f(g(John), Z) Bind X to g(John) and Z to John to get f(g(John), John) as the unified instance of both expressions.

If the database contains the fact ParentOf(John, Mary) then the unification of the following query:

ParentOf(X,Mary)=ParentOf(John,Y) Would be

ParentOf(John,Mary)

where X is substituted by John and Y is substituted by Mary.

A. Bellaachia Page: 10

Page 11: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

Example 2: grandmother(X,Y) :- mother(X,Z), mother(Z,Y). grandfather (X,Y) :- father (X,Z), father(Z,Y). mother(anna,peter). mother(anna,clara). father(joe,peter). father(jim,clara). mother(mary,anna). father(tom,joe).

Answer:

?- grandfather(tom,X). ?- grandmother(X,peter).

• For example, the programmer supplies a list of facts and rules:

Rules: Eagle has feather. Eagle has beak. Eagle has two legs. Betty eats vegetables. Chicken eats seeds. Chicken has two legs.

Facts: If someone eats meat then it is a

carnivore.

If someone has feather then it is a bird.

If someone eats X then X is food.

A. Bellaachia Page: 11

Page 12: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Finally, the programmer can ask questions based on those facts and rules, such as:

Is eagle a bird?

Is chicken a bird?

• The language engine then tries to apply the rules to answer the questions, e.g.:

Yes (meat is a food).

Yes (eagle and chicken are bird). • Note that the answers are based only on the

available information.

A. Bellaachia Page: 12

Page 13: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

6. Programming Languages vs. IPL & LP In a logic programming language, the

programmer does not identify the computations or functions necessary to derive an answer.

• The great advantage of a declarative language is

that the programmer only needs to supply the information that is known (facts and rules), and ask questions - NOT worry about the ways the rules must be applied to answer those questions.

• From a programmer's perspective, this is a much

more high-level approach to problem solving than in imperative or functional languages.

7. Drawbacks of Logic programming

• The query process can be quite slow and inefficient • The programmer must supply sufficient facts • Rules to allow the engine to answer the queries • The programmer must understand how to phrase the

facts, rules, and queries

A. Bellaachia Page: 13

Page 14: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

8. Prolog • University of Aix-Marseille

Natural language processing • University of Edinburgh

Automated theorem proving • Prolog (PROgramming LOGic) (1972): was the

first widespread formal logic language. It is a non-procedural, goal oriented language.

• Computation takes place by trying to infer the response to queries through accessing a database of facts and rules.

• The user supplies the facts, rules, and queries (expressed in predicate calculus), while the computer carries out resolution to try and infer the replies.

• By far the dominant logic programming language is Prolog, so that is the language we will examine in some detail.

• Prolog is based predominantly on the Horn clause,

A :- B, C, D, ... This tells us A is true if B is true and C is true and ... etc. It does not tell us the circumstances under which a is false

A. Bellaachia Page: 14

Page 15: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

9. Getting Started • In fact, in Prolog all the queries are expressed like

yes/no questions, and the answers come back either as o a simple "No" (meaning no way could be

found to make the query true), or o "Yes", plus all the ways in which the query

could be made true.

• Core components:

o The basic elements of a Prolog "program" are:

Entities: terms, constants, atoms. Variables: as with more traditional

programming languages, we can declare a variety of variables in Prolog

Properties: we can assign properties to individual entities, for example fred would have the property of being a carnivore properties look like a C function call - the name of the property then the entity that has that property is given in brackets, e.g. carnivore (fred), or food (vegetables)

Relationships (facts) : we can assign relationships between entities, for example fred eats meat, or wilma eats vegetables relationships in Prolog again look like a C++

A. Bellaachia Page: 15

Page 16: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

function call, we give the relationship name first, then in brackets the two entities that are related, e.g. eats(wilma,meat), eats(wilma,vegetables)

rules: we can establish rules that describe how one relationship or property is true if some other properties or conditions are true. For example, a rule could state that if someone eats meat and they eat vegetables then they are an omnivore.

• Rules in Prolog have three parts:

The result: this is the relationship or

property which results from the rule, and goes on the left hand side of our rule, e.g. omnivore(Individual) Note that the entity here will usually be a variable.

The "if" symbol: :- which separates the result from

the clauses

The clauses: the set of relationships and properties which determine if the rule is to be applied, this goes on the right hand side of the rule.

A. Bellaachia Page: 16

Page 17: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• There are several logic symbols which can be used in the right hand side:

The comma represents logical AND,

The semi-colon represents logical OR,

The word NOT in some ways functions like logical NOT, but there are some important differences that we'll consider later

10. Create a Prolog program

• Create our set of properties, relationships, and rules. Typically we will enter these into a text file ending with the extension ".pl" (e.g. "flint.pl").

• Start Prolog and load our database from the file.

• Begin issuing queries based on the loaded

information.

• From this point on, everything you type is treated as a query, not a new fact or rule:

eats(fred,vegetables). is now asking whether or not fred eats vegetables, not stating it as a new fact.

A. Bellaachia Page: 17

Page 18: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• SYNTAX NOTE: all statements of properties, relationships, rules, and queries must end with a period -- the Prolog interpreter will not regard any of the above as completed until you enter a period.

A. Bellaachia Page: 18

Page 19: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Example: (http://intra.engr.uark.edu/~wessels/lang/spring00/prolog.html) Create a file, called flint.pl, containing the

following facts and rules: eats(fred,meat). eats(wilma,meat). eats(betty,vegetables). eats(wilma,vegetables). eats(barney,meat). eats(barney,vegetables). carnivore(Individual) :- eats(Individual,meat). omnivore(Individual) :- eats(Individual,meat),eats(Individual,vegetables). food(Thing) :- eats(Individual,Thing).

Once that is created, we can load the database of

facts and rules from the file, then begin issuing queries and getting answers.

In the example below assume > is the prompt: >['flint.pl']. flint.pl compiled Yes >food(meat). Yes >carnivore(fred). Yes >food(dirt). No >omnivore(Individual). Individual = wilma Yes

A. Bellaachia Page: 19

Page 20: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• How queries are answered:

Suppose we are given the query "is fred an omnivore?"

omnivore(fred).

In trying to solve this query, Prolog does the following: - search for all the relationships which match the

goal, i.e. everything in our database that begins with omnivore(...)

- if we encounter a fact in our database that explicitly states omnivore(fred). then we can quit and return Yes as soon as we encountered it.

- if we encounter a rule of the form omnivore(...variable...) :- ... then we can say fred is an omnivore if we can substitute fred for the variable (throughout the rule) and still satisfy all the clauses on the right side of the rule

- In the case of our example, the rule with this form is

omnivore(Individual) :- eats(Individual,meat),eats(Individual,vegetables).

and after substituting we want to check if eats(fred,meat) AND eats(fred,vegetables).

A. Bellaachia Page: 20

Page 21: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

Thus we have two new goals to try to satisfy to check out this rule, and we begin searching the database for facts and rules beginning with eats(..., trying to substitute "fred" and "meat" into the rules to continue the query. If we cannot prove eats(fred,meat) then that clause fails, and hence the rule fails as well, so we must continue on checking for other facts/rules defining omnivores.

As soon as we satisfy one of the facts or rules that prove fred is an omnivore we can stop and return “Yes”.

If we go through all our facts and rules and cannot prove fred is an omnivore then we return “no”.

A. Bellaachia Page: 21

Page 22: Logic Programmingbell/csci210/lectures/lp.pdf · 9 Depth-first search: find a complete proof for the first condition before working on others (Prolog uses depth-first search) 9 Breadth-first

• Another example: suppose our database is: rainy(yesterday). rainy(today). cloudy(Day) :- rainy(Day).

Again, let us make the query:

cloudy(today).

In this case, the first applicable rule (i.e. that has cloudy as a head and one parameter) is: cloudy(Day) :- rainy(Day). Because Day is a variable, and we are looking for the specific value today, we can instantiate the variable Day with the value today, giving us the rule:

cloudy(today) :- rainy(today).

From this point the process continues by trying to satisfy subgoal rainy(today) and succeed because of the applicable fact in the database.

A. Bellaachia Page: 22