Chapter 16 Logic Programming Languages ISBN 0 321
Chapter 16 Logic Programming Languages ISBN 0 -321 -49362 -1
Chapter 16 Topics • • Introduction A Brief Introduction to Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of Prolog The Basic Elements of Prolog Deficiencies of Prolog Applications of Logic Programming Copyright © 2015 Pearson. All rights reserved. 1 -2
Overview of Logic Programming • Declarative semantics – There is a simple way to determine the meaning of each statement – Simpler than the semantics of imperative languages • Programming is nonprocedural – Programs do not state how a result is to be computed, but rather the form of the result Copyright © 2015 Pearson. All rights reserved. 1 -3
Procedural Languages • Procedural languages are also called third generation languages ( 3 GL ). • This language tells the computer what to do and how to do. • It is difficult to learn as compared to non-procedural. • It is difficult to debug. • It requires a large number of procedural instructions. • It is normally used by professional programmer. • It is typically file-oriented. • Procedural languages provides many programming capabilities. Copyright © 2015 Pearson. All rights reserved. 1 -4
Non - Procedural Languages • It is also called fourth generation languages ( 4 GL ). • Non-procedural languages tells the computer what to do, not how to do. • It is very easy to learn. • It is very easy to debug. • It requires a few non-procedural instructions. • It can be used professional and technical users. • It is typically database oriented. • Non-Procedural languages provides less programming capabilities. Copyright © 2015 Pearson. All rights reserved. 1 -5
Introduction • Programs in logic languages are expressed in a form of symbolic logic • Declarative rather that procedural: – Only specification of results are stated (not detailed procedures for producing them) Copyright © 2015 Pearson. All rights reserved. 1 -6
Symbolic Logic • Logic which can be used for the basic needs of formal logic: 1. Express propositions 2. Express relationships between propositions 3. Describe how new propositions can be concluded from other propositions • Particular form of symbolic logic used for logic programming called predicate calculus Copyright © 2015 Pearson. All rights reserved. 1 -7
Terminology Copyright © 2015 Pearson. All rights reserved. 1 -8
Proposition • Proposition-A logical statement that may or may not be true – Consists of objects and relationships of objects to each other Copyright © 2015 Pearson. All rights reserved. 1 -9
Example “Drilling for oil caused dinosaurs to become extinct. ” is a proposition. “Look out!” is not a proposition. “How far is it to the next town? ” is not a proposition. Propositions: All cows are brown The Earth is further from the sun than Venus. There is life on Mars. Copyright © 2015 Pearson. All rights reserved. 1 -10
Object Representation • Objects in propositions are represented by simple terms: either constants or variables • Constant: a symbol that represents an object • Variable: a symbol that can represent different objects at different times – Different from variables in imperative languages Copyright © 2015 Pearson. All rights reserved. 1 -11
Compound Terms • Atomic propositions consist of compound terms • Compound term: one element of a mathematical relation, written like a mathematical function – Mathematical function is a mapping – Can be written as a table Copyright © 2015 Pearson. All rights reserved. 1 -12
Parts of a Compound Term • Compound term composed of two parts – Functor: function symbol that names the relationship – Ordered list of parameters (tuple) • Examples: student(jon) like(seth, OSX) like(nick, windows) like(jim, linux) Copyright © 2015 Pearson. All rights reserved. 1 -13
Forms of a Proposition • Propositions can be stated in two forms: – Fact: proposition is assumed to be true An example of a fact is: cat (tom); or cat(tom): =true – Query: truth of proposition is to be determined is tom a cat? ? - cat(tom). Yes • Compound proposition: – Have two or more atomic propositions – Propositions are connected by operators Copyright © 2015 Pearson. All rights reserved. 1 -14
Proof by Contradiction • Hypotheses: a set of relevant propositions • Goal: rejection of a proposition • Theorem is proved by finding an inconsistency Copyright © 2015 Pearson. All rights reserved. 1 -15
Theorem Proving • Basis for logic programming • Horn clause - can have only two forms – Headed: single atomic proposition on left side – Headless: empty left side (used to state facts) • Most propositions can be stated as Horn clauses Copyright © 2015 Pearson. All rights reserved. 1 -16
Artificial intelligence • There are primarily two computer languages used in Artificial intelligence work, LISP and PROLOG. • LISP, which is short for List Processing, was created by John Mc. Carthy of Stanford University. Copyright © 2015 Pearson. All rights reserved. 1 -17
Prolog • Prolog is a general-purpose logic programming language associated with artificial intelligence and computational linguistics. [1][2][3] • Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages • Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules. Copyright © 2015 Pearson. All rights reserved. 1 -18
Prolog cont. • A computation is initiated by running a query over these relations. [4] • The language was first conceived by a group around Alain Colmerauer in Marseille, France, in the early 1970 s • The first Prolog system was developed in 1972 by Colmerauer with Philippe Roussel. [5][6] Copyright © 2015 Pearson. All rights reserved. 1 -19
Applications of Prolog • Some applications of Prolog are: – – – – intelligent data base retrieval natural language understanding expert systems specification language machine learning robot planning automated reasoning problem solving Copyright © 2015 Pearson. All rights reserved. 1 -20
Programming in Prolog • declare facts describing explicit relationships between objects and properties objects might have (e. g. Mary likes pizza, grass has_colour green, Fido is_a_dog, Mizuki taught Paul Japanese ) • define rules defining implicit relationships between objects (e. g. the sister rule above) and/or rules defining implicit object properties (e. g. X is a parent if there is a Y such that Y is a child of X). • One then uses the system by: asking questions above relationships between objects, and/or about object properties (e. g. does Mary like pizza? is Joe a parent? ) Copyright © 2015 Pearson. All rights reserved. 1 -21
Terms Prolog's single data type is the term. Terms are either atoms, numbers, variables or compound terms. • • • This book uses the Edinburgh syntax of Prolog 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 Copyright © 2015 Pearson. All rights reserved. 1 -22
Terms: Variables and Structures • Variable: any string of letters, digits, and underscores beginning with an uppercase letter • Instantiation: binding of a variable to a value – Lasts only as long as it takes to satisfy one complete goal • Structure: represents atomic proposition functor(parameter list) Copyright © 2015 Pearson. All rights reserved. 1 -23
Fact Statements • Used for the hypotheses • Headless Horn clauses female(shelley). male(bill). father(bill, jake). Copyright © 2015 Pearson. All rights reserved. 1 -24
Rule Statements • 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) Copyright © 2015 Pearson. All rights reserved. 1 -25
Example Rules ancestor(mary, shelley): - mother(mary, shelley). • Can use variables (universal objects) to generalize meaning: parent(X, Y): - mother(X, Y). parent(X, Y): - father(X, Y). grandparent(X, Z): - parent(X, Y), parent(Y, Z). Copyright © 2015 Pearson. All rights reserved. 1 -26
Approaches • Matching is the process of proving a proposition • Proving a subgoal is called satisfying the subgoal • 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 Copyright © 2015 Pearson. All rights reserved. 1 -27
Subgoal Strategies • When goal has more than one subgoal, can use either – Depth-first search: find a complete proof for the first subgoal before working on others – Breadth-first search: work on all subgoals in parallel • Prolog uses depth-first search – Can be done with fewer computer resources Copyright © 2015 Pearson. All rights reserved. 1 -28
Backtracking • With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking • Begin search where previous search left off Copyright © 2015 Pearson. All rights reserved. 1 -29
Simple Arithmetic • Prolog supports integer variables and integer arithmetic • is operator: takes an arithmetic expression as right operand variable as left operand A is B / 17 + C • Not the same as an assignment statement! – The following is illegal: Sum is Sum + Number. Copyright © 2015 Pearson. All rights reserved. 1 -30
Example speed(ford, 100). speed(chevy, 105). speed(dodge, 95). speed(volvo, 80). time(ford, 20). time(chevy, 21). time(dodge, 24). time(volvo, 24). distance(X, Y) : speed(X, Speed), time(X, Time), Y is Speed * Time. A query: distance(chevy, Chevy_Distance). Copyright © 2015 Pearson. All rights reserved. 1 -31
Example likes(jake, chocolate). likes(jake, apricots). likes(darcie, licorice). likes(darcie, apricots). trace. likes(jake, X), likes(darcie, X). (1) (2) (1) (3) X = 1 Call: likes(jake, _0)? 1 Exit: likes(jake, chocolate) 1 Call: likes(darcie, chocolate)? 1 Fail: likes(darcie, chocolate) 1 Redo: likes(jake, _0)? 1 Exit: likes(jake, apricots) 1 Call: likes(darcie, apricots)? 1 Exit: likes(darcie, apricots) apricots Copyright © 2015 Pearson. All rights reserved. 1 -32
Example • Fact – cat(tom) • An example of a rule is: animal(X) : - cat(X). If we add that rule and ask what things are animals? ? - animal(X). X = tom Copyright © 2015 Pearson. All rights reserved. 1 -33
Example 2 Copyright © 2015 Pearson. All rights reserved. 1 -34
Example 3 Copyright © 2015 Pearson. All rights reserved. 1 -35
Example 4 - DB Copyright © 2015 Pearson. All rights reserved. 1 -36
EX 4 - Here is how you would formulate the following queries: Was George I the parent of Charles I? Query: parent(charles 1, george 1)? Who was Charles I's parent? Query: parent(X, charles 1). Who were the children of Charles I? Query: parent(charles 1, X). Copyright © 2015 Pearson. All rights reserved. 1 -37
Now try expressing the following rules: • M is the mother of X if she is a parent of X and is female • F is the father of X if he is a parent of X and is male • X is a sibling of Y if they both have the same parent. Copyright © 2015 Pearson. All rights reserved. 1 -38
Ex 5 - property of being a man or woman by following PROLOG facts: man(adam). man(peter). man(paul). woman(marry). woman(eve). Copyright © 2015 Pearson. All rights reserved. 1 -39
EX 5 = Relation "parent" which associates parent and child. parent(adam, peter). % means adam is parent of peter parent(eve, peter). parent(adam, paul). parent(marry, paul). Copyright © 2015 Pearson. All rights reserved. 1 -40
EX 5 - Rule • Each rule has its head - name of the defined relation, and its body - a real definition of the relation. father(F, C): -man(F), parent(F, C). mother(M, C): -woman(M), parent(M, C). Copyright © 2015 Pearson. All rights reserved. 1 -41
EX 5 - anonymous variable • If some parameter of the relation is not important for us we can use anonymous variable (denoted _ ) like in these definitions: is_father(F): -father(F, _). is_mother(M): -mother(M, _). Copyright © 2015 Pearson. All rights reserved. 1 -42
EX 5 - query ? -father(X, paul). • which expresses: who is father of paul? • The answer is X=adam, naturally. Copyright © 2015 Pearson. All rights reserved. 1 -43
Example 6 son(S, P): -man(S), parent(P, S). daughter(D, P): -woman(D), parent(P, D). siblings(A, B): -parent(P, A), parent(P, B), A=B. % siblings have at least one common parent % the test A=B preserves that siblings are different persons Copyright © 2015 Pearson. All rights reserved. 1 -44
Example full_siblings(A, B): parent(A, F), parent(B, F), parent(A, M), parent(B, M), A=B, F=M. % full siblings have common parents (both) % the test F=M preserves that full siblings have two different parents (father and mother, naturally) Copyright © 2015 Pearson. All rights reserved. 1 -45
Example uncle(U, N): -man(U), siblings(U, P), parent(P, N). aunt(A, N): woman(A), siblings(A, P), parent. P, N). grand_parent(G, N): -parent(G, X), parent(X, N). Copyright © 2015 Pearson. All rights reserved. 1 -46
Shortages of Prolog • Resolution order control – In a pure logic programming environment, the order of attempted matches is nondeterministic and all matches would be attempted concurrently • The closed-world assumption – The only knowledge is what is in the database • The negation problem – Anything not stated in the database is assumed to be false • Intrinsic limitations – It is easy to state a sort process in logic, but difficult to actually do—it doesn’t know how to sort Copyright © 2015 Pearson. All rights reserved. 1 -47
Applications of Logic Programming • Relational database management systems • Expert systems • Natural language processing Copyright © 2015 Pearson. All rights reserved. 1 -48
Summary • Symbolic logic provides basis for logic programming • Logic programs should be nonprocedural • Prolog statements are facts, rules, or goals • Resolution is the primary activity of a Prolog interpreter • Although there a number of drawbacks with the current state of logic programming it has been used in a number of areas Copyright © 2015 Pearson. All rights reserved. 1 -49
- Slides: 49