Lectures 3 4 PROLOG LOGic PROgramming Logic what

  • Slides: 80
Download presentation
Lectures 3, 4 PROLOG LOGic PROgramming

Lectures 3, 4 PROLOG LOGic PROgramming

Logic: what is it? n Philosophical logic – fundamental questions n Mathematical logic –

Logic: what is it? n Philosophical logic – fundamental questions n Mathematical logic – theory of correct reasoning n Computational logic – effectiveness of reasoning n Language of scientific thinking

Logic Programming n Logic programming uses a form of symbolic logic and a logical

Logic Programming n Logic programming uses a form of symbolic logic and a logical inference process n Languages based on symbolic logic are called logic programming languages or declarative languages n Prolog is the most widely used logic language

Predicate Calculus Formal logic is the basis for logic programming n Key Points n

Predicate Calculus Formal logic is the basis for logic programming n Key Points n – 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 for logic programming

Propositions

Propositions

Atomic propositions n Objects in logic programming propositions are – Constants: symbols that represent

Atomic propositions n Objects in logic programming propositions are – Constants: symbols that represent an object – Variables: symbols that can represent different objects at different times n 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 is an element of the tabular definition of a function

Compound terms n Compound term has two parts – Functor: symbol that names the

Compound terms n 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. n Example: n man (jake) {jake} is a 1 -tuple in relation man like (bob, steak) {bob, steak} is a 2 -tuple in like

Facts and queries Simple terms in propositions – man, jake, bob, and steak –

Facts and queries Simple terms in propositions – man, jake, bob, and steak – are constants n These propositions have no intrinsic semantics n – like (bob, steak) could mean several things n Propositions are stated in two modes – Fact: one in which the proposition is defined to be true – Query: one in which the truth of the proposition is to be determined

Logical operators Name Symbol Example Meaning a conjunction ( ) a b a and

Logical operators Name Symbol Example Meaning 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 implies b b implies a negation not a

Compound propositions n Compound proposition examples: a b c a b d n Precedence

Compound propositions n Compound proposition examples: a b c a b d n Precedence equivalent to (a ( b)) d of logical connectors: highest precedence , , next , lowest precedence

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

Quantifiers n Variables may appear in propositions – only when introduced by symbols called quantifiers Name n 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 Note: The period separates the variable from the proposition

Quantifiers n Examples of propositions with quantifiers – X. (woman(X) human(X)) For any value

Quantifiers n 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 n Note: Quantifiers have a higher precedence than any of the logical operators

Example of Logic Predicate Calculus n 0 is a natural number. natural (0) n

Example of Logic Predicate Calculus n 0 is a natural number. natural (0) n 2 is a natural number. natural (2) n For all X, if X is a natural number, then so is the successor of X. natural (X) natural (successor (X))

Logic Statement to P. C. n. A horse is a mammal. n A human

Logic Statement to P. C. n. A horse is a mammal. n A human is a mammal. n Mammals have four legs and no arms, or two legs and two arms. n A horse has no arms. n A human has no legs.

Solution: First Order Predicate Calculus n mammal (horse). n mammal (human). n X. mammal

Solution: First Order Predicate Calculus n mammal (horse). n mammal (human). n X. mammal (X) legs (X, 4) arms (X, 0) legs (X, 2) arms (X, 2) n arms (horse, 0). n arms (human, 2) or arms (human, 0). n legs (human, 0).

Clausal forms

Clausal forms

General statement n Redundancy is a problem with predicate calculus – there are many

General statement n 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 n Clausal form is one standard form of propositions used for simplification: B 1 B 2 . . . Bn A 1 A 2 . . . Am Meaning: If all As are true, then at least one B is true n

Characteristics n n n Existential quantifiers are not required Universal quantifiers are implicit in

Characteristics n n n 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 The left side is called the consequent The right side is called the antecedent

Examples n Proposition 1: likes (bob, trout) likes (bob, fish) fish (trout) n Meaning:

Examples n Proposition 1: likes (bob, trout) likes (bob, fish) fish (trout) n Meaning: if bob likes fish and a trout is a fish, then bob likes trout n Proposition 2: father(louis, al) father(louis, violet) father(al, bob) mother(violet, bob) grandfather(louis, bob) n 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

Resolution

Resolution

Proving theorems n One of the most significant breakthroughs in automatic theoremproving was the

Proving theorems n One of the most significant breakthroughs in automatic theoremproving was the discovery of the resolution principle by Robinson in 1965 n 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

Basic idea The concept of resolution is the following: Given two propositions: P 1

Basic idea The concept of resolution is the following: Given two propositions: P 1 P 2 Q 1 Q 2 n Suppose P 1 is identical to Q 2 and we rename them as T. Then T P 2 Q 1 T n Since P 2 implies T and T implies Q 1, it is logically obvious that P 2 implies Q 1, that is Q 1 P 2 n

Example Consider the two propositions: older (joanne, jake) mother (joanne, jake) wiser (joanne, jake)

Example Consider the two propositions: older (joanne, jake) mother (joanne, jake) wiser (joanne, jake) older (joanne, jake) n We can conclude that wiser (joanne, jake) mother (joanne, jake) n 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 n

Expanded Example jake) mother(bob, jake) parent (bob, jake) n grandfather(bob, fred) father(bob, jake) father(jake,

Expanded Example jake) mother(bob, jake) parent (bob, jake) n grandfather(bob, fred) father(bob, jake) father(jake, fred) n Resolution process cancels common term on both the left and right sides: n mother(bob, jake) grandfather(bob, fred) parent (bob, jake) father(jake, fred) n father(bob,

Most important laws Unification: The process of determining useful values for variables in propositions

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. n Instantiation: The temporary assigning of values to variables to allow unification. n 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. n

Horn clauses

Horn clauses

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

Basics When propositions are used for resolution, only a restricted kind of clausal form can be used n Horn clauses n – 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

Relationships and facts n Headed Horn clauses are used to state relationships: likes(bob, trout)

Relationships and facts n Headed Horn clauses are used to state relationships: likes(bob, trout) likes (bob, fish) fish(trout) n Headless Horn clauses are used to state facts: father(bob, jake) n Most propositions may be stated as Horn clauses

Logic Programming

Logic Programming

Brief history n Resolution principle (Robinson 1965) n Prolog language (Colmerauer 1972) n Operational

Brief history n Resolution principle (Robinson 1965) n Prolog language (Colmerauer 1972) n Operational semantics (Kowalski 1974) n Prolog compiler (Warren 1977, 1983) n 5 th generation project (Japan 1981 --) n Constraint LP (Jaffar & Lassez 1987)

Brief basics n Robert Kowalski says: Algorithm = Logic + Control n Prolog is

Brief basics n Robert Kowalski says: Algorithm = Logic + Control n Prolog is the most popular logic programming language n A Prolog program is a collection of Horn clauses

Declarative languages Languages used for logic programming are called declarative languages n Programming with

Declarative languages Languages used for logic programming are called declarative languages n Programming with declarative languages is nonprocedural n – 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 relevant information and a method of inference for computing desirable results

Important issues n Program – knowledge about the problem in the form of logical

Important issues n Program – knowledge about the problem in the form of logical axioms n Call of the program – provide a problem description (as a logical statement) n Execution – attempt to prove the given statement (given the program) n Constructive proofs essential – can you prove sort([2, 3, 1], x)? – non-constructive answer: “Yes I can” – constructive answer: “Yes, with x = [1, 2, 3]”

PROLOG

PROLOG

Notation English Predicate Calculus Prolog and ( ) , or ( ) ; only

Notation English Predicate Calculus Prolog and ( ) , or ( ) ; only if ( ) : - not(. . . ) ? ? ? (query specification) ? -

Example n /* simple rules for deductions */ sees(X, Y): person(X), object(Y), open(eyes(X)), in_front_of(X,

Example n /* 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). n /* questions */ /* does the mother of nikos see his graduation? */ ? -sees(mother(nikos), graduation(nikos)).

General characteristics n n n A program uses terms such as constant symbols, variables,

General characteristics n n n 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 system fails to prove P

Basic elements

Basic elements

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

Terms A Prolog term is a constant, a variable, or a structure n A constant is either an atom or an integer n – Atoms are the symbolic values of Prolog – Atoms begin with a lowercase letter n 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 n Structures represent the atomic propositions of predicate calculus – form is functor (parameter list)

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

Fact statements are used to construct hypotheses (database) from which new information may be inferred n Fact statements are headless Horn clauses assumed true n Examples: n male(bill). female(mary). male(jake). father(bill, jake). mother(mary, jake).

Rule statements n Rule statements are headed Horn clauses for constructing the database n

Rule statements n Rule statements are headed Horn clauses for constructing the database n The RHS is the antecedent (if), and the LHS is the consequent (then) n Consequent is a single term because it is a Horn clause n Conjunctions may contain multiple terms that are separated by logical ANDs denoted by commas, e. g. : female(shelley), child (shelley).

Rule statements n General form of the Prolog headed Horn clause consequence_1 : -

Rule statements n General form of the Prolog headed Horn clause consequence_1 : - antecedent_expression n Example: ancestor(mary, shelley) : - mother(mary, shelley). n 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), father(F, X), father(F, Y). universal objects are X, Y, Z, M, and F

Goal statements Because goal and some nongoal statements have the same form (headless Horn

Goal statements Because goal and some nongoal statements have the same form (headless Horn clauses), it is imperative to distinguish between the two n 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 n Some versions of Prolog use ? - for goals n

Goal statements Facts and rules are used to prove or disprove a theorem that

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) n Syntactic form of Prolog goal statement is identical to headless Horn clauses, e. g. n man (fred). to which the system will respond yes or no n Conjunctive propositions and propositions with variables are also legal goals, e. g. father ( X, mike). n When variables are present, the system identifies the instantiations of the variables that make the goal true

Example n Database: likes(george, kate). likes(george, susie). likes(george, wine). likes(susie, wine). likes(kate, gin). likes(kate,

Example n Database: likes(george, kate). likes(george, susie). likes(george, wine). likes(susie, wine). likes(kate, gin). likes(kate, susie).

Example n Query: ? -likes(george, X). X = kate ; X = susie ;

Example n Query: ? -likes(george, X). X = kate ; X = susie ; X = wine ; no

Example n Database: likes(george, kate). likes(george, susie). likes(george, wine). likes(susie, wine). likes(kate, gin). likes(kate,

Example n 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). n Query: ? - friends(george, susie) yes

Inference process

Inference process

General scheme Goals (queries) may be compound propositions; each of facts (structures) is called

General scheme Goals (queries) may be compound propositions; each of facts (structures) is called a subgoal n 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 n 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 n

Matching The process of proving (or satisfying) a subgoal by a proposition-matching process n

Matching The process of proving (or satisfying) a subgoal by a proposition-matching process n Consider the goal or query: n man (bob). If the database includes the fact man(bob), the proof is trivial n If the database contains the following fact and inference n father (bob). man (X) : - father (X) then Prolog would need to find these and infer the truth. This requires unification to instantiate X temporarily to bob

Matching n Consider the goal: man(X). Prolog must match the goal against the propositions

Matching n Consider the goal: man(X). Prolog must match the goal against the propositions in the database n 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 n If there is no proposition with the form of the goal, the system indicates that the goal can’t be satisfied n

Solution search approaches n Depth-first – Finds a complete sequence of propositions (a proof)

Solution search approaches n Depth-first – Finds a complete sequence of propositions (a proof) for the first subgoal before working on the others n Breadth-first – Works on all subgoals of a given goal in parallel n 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 searchmultiple solutions to a subgoal result from different instantiations of its variables

Backtracking example n Suppose we have the following compound goal: ? - male (X),

Backtracking example n 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? n The search n – 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 n More efficient processing possible

Simple arithmetic n Suppose we know the average speeds of several automobiles on a

Simple arithmetic n 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). n We can calculate distance with this rule: distance(X, Y) : speed(X, Speed), time(X, Time), Y is Speed * Time n Query distance(chevy, Chevy_Dist). will instantiate Chevy_Dist as 105*21 = 2205

Tracing

Tracing

Tracing the process n Consider the following example: likes(jake, chocolate). likes(jake, apricots). likes(darcie, licorice).

Tracing the process n Consider the following example: likes(jake, chocolate). likes(jake, apricots). likes(darcie, licorice). likes(darcie, apricots). ? -likes(jake, X), likes(darcie, X). n Trace Process: (1) (1) (2) (2) Call: Exit: Call: Fail: Redo: Exit: Call: Exit: likes(jake, _0)? likes(jake, chocolate) likes(darcie, chocolate)? likes(darcie, chocolate) likes(jake, _0)? likes(jake, apricots) likes(darcie, apricots)? likes(darcie, apricots) X = apricots

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay hesnotokay

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay hesnotokay

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay hesnotokay shesokay

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay :

Example imokay : - youreokay, hesokay youreokay : - theyreokay hesokay. theyreokay. hesnotokay : - imnotokay shesokay : - hesnotokay shesokay : - theyreokay hesnotokay : - shesokay hesnotokay shesokay LOOPS

List structures

List structures

Basics n Lists can be created with a simple structure: new_list([apple, prune, grape]). where

Basics n Lists can be created with a simple structure: new_list([apple, prune, grape]). where new_list is a relation like male(jake) n Prolog uses the special notation [ X | Y ] to indicate head X and tail Y n In query mode, the elements of new_list can be dismantled: new_list([New_List_Head | New_List_Tail]). It instantiates New_List_Head with apple New_List_Tail with [prune, grape]

Append append(([bob, jo], [jake, darcie], Family). produces the output Family = [bob, jo, jake,

Append append(([bob, jo], [jake, darcie], Family). produces the output Family = [bob, jo, jake, darcie] and Yes

Reverse reverse ([bob, jo, jake, darcie], Rev_list). prints Rev_List = [darcie, jake, jo, bob]

Reverse reverse ([bob, jo, jake, darcie], Rev_list). prints Rev_List = [darcie, jake, jo, bob] and Yes

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

Member n Member function is used to determine if a given symbol is in a given list n Example: member( bob, [darcie, jo, jim, bob, alice]). The system responds with Yes

Performance control

Performance control

Resolution order control Prolog always matches in the same order: starting at the beginning

Resolution order control Prolog always matches in the same order: starting at the beginning and at the left end of a given goal n Therefore, the user can affect efficiency by ordering the database statements to optimize a particular application n If certain rules are more likely to succeed than others during an execution, placing those rules first makes the program more efficient n

Problems with control The ability to tamper with control flow in a Prolog program

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 n Using the ability for flow control, clutters the simplicity of logic programs with details of order control to produce solutions n Frequently used for the sake of efficiency n

Negation

Negation

Example n Consider the following: parent(bill, jake). parent(bill, shelley). sibling(X, Y) : - parent(M,

Example n Consider the following: parent(bill, jake). parent(bill, shelley). sibling(X, Y) : - parent(M, X), parent(M, Y). ? -sibling(X, Y). n Prolog’s result is X = jake Y = jake

Example n Consider the following: parent(bill, jake). parent(bill, shelley). sibling(X, Y) : - parent(M,

Example n Consider the following: parent(bill, jake). parent(bill, shelley). sibling(X, Y) : - parent(M, X), parent(M, Y), not(X=Y). ? -sibling(X, Y). n Prolog’s result is X = jake Y = shelley

Problems with „not” Logical „not” cannot be a part of Prolog primarily because of

Problems with „not” Logical „not” cannot be a part of Prolog primarily because of the form of the Horn clauses B : - A 1 A 2 . . . Am n If all the A propositions are true, it can be concluded that B is true. n But regardless of the truth or falseness of any or all of the As, it cannot be concluded that B is false. n Prolog can prove that a given goal is true, but it cannot prove that a given goal is false. n

Applications

Applications

Logic Programming and RDBMS RDBMs store data in tables and queries are often stated

Logic Programming and RDBMS RDBMs store data in tables and queries are often stated in relational calculus n Query languages are nonprocedural n Obvious facility of Prolog to represent tables(facts) and relationships between tables(rules) with the retrieval process being inherent in the resolution operation n Advantages n – only a single language is required – deductive capability is built in to the RDBMS with inference rules

Expert Systems are designed to emulate human expertise. They consist of a database of

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. n 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. n

Natural Language Processing Forms of logic programming have been found to be equivalent to

Natural Language Processing Forms of logic programming have been found to be equivalent to context-free grammars to describe language syntax n Some semantics of natural languages can be modeled with logic programming n Research in logic-based semantics networks has shown that sets of sentences in natural languages can be expressed in clausal form n

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

Education Extensive experiments in using micro-Prolog to teach children logic programming language n Advantages n – introduces computation at an early age – teaches logic which results in clearer thinking and expression – assists students with mathematics and other subjects n Experiments have produced an interesting result: – it is easier to teach logic programming to a beginner than to a programmer with a significant amount of experience in an imperative language