Logic Programming Languages featuring Prolog, your new favorite language.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Advertisements

Some Prolog Prolog is a logic programming language
Chapter 11 :: Logic Languages
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
Logic.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
Inference and Resolution for Problem Solving
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
CPS 506 Comparative Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Automated Reasoning Early AI explored how to automate several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Logical Agents Chapter 7. Outline Knowledge-based agents Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logical Agents. Outline Knowledge-based agents Logic in general - models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability.
Logic Programming Languages
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Logic Programming Languages
Logic Programming Languages
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Back to “Serious” Topics…
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Logic Programming Languages featuring Prolog, your new favorite language

2 Prolog PROgramming in LOGic It is the most widely used logic programming language Its development started in 1970 What’s it good for? Knowledge representation Natural language processing State-space searching (Rubik’s cube) Expert systems, deductive databases, Agents

3 Overview of Logic Programming Main idea: Ask the computer to solve problems using principles of logic: Program states the known facts To ask a question, you make a statement and ask the computer to search for a proof that the statement is true Additional mechanisms are provided to guide the search to find a proof

4 Declarative vs. Imperative Languages used for logic programming are called declarative languages because programs written in them consist of declarations rather than assignment and flow-of-control statements. These declarations are statements, or propositions, in symbolic logic. Programming in imperative languages (e.g., Pascal, C) and functional languages (e.g., Lisp) is procedural, which means that the programmer knows what is to be accomplished by the program and instructs the computer on exactly how the computation is to be done.

5 Logic Programming Programming in logic programming languages is non-procedural. Programs in such languages do not state how a result is to be computed. Instead, we supply the computer with: relevant information (facts and rules) a method of inference for computing desired results. Logic programming is based on the predicate calculus.

6 Logic background Horn clauses General form: IF (A1 and A2 and A3 …) THEN H Head = H Body = A1 and A2 and …. E.g. “If X is positive, and Y is negative, then Y is less than X”

7 The Predicate Calculus: Proposition Proposition: A proposition is a logical statement, made up of objects and their relationships to each other, which may or may not be true. Examples: man(john) likes(pizza, baseball) functor parameters

8 The Predicate Calculus: Logical Connectors A compound proposition consists of 2 or more propositions connected by logical connectors, which include Negation:  a(“not a”) Conjunction: a  b(“a and b”) Disjunction: a  b(“a or b”) Equivalence:a  b(“a is equivalent to b”) Implication:a  b(“a implies b”) a  b(“b implies a”)

9 The Predicate Calculus: Quantifiers Variables may appear in formulas, but only when introduced by quantifiers: Universal quantifier:  (for all) Existential quantifier:  (there exists) Examples:  X (woman(X)  human(X)) “All women are human”  X (likes(bill,X)  sport(X)) “There is some sport that bill likes”

10 Resolution and Unification Resolution: how we do logical deduction from multiple horn clauses: If the head of horn clause #1 matches one of the terms in horn clause #2, then we can replace that term with the body of clause #1 Unification: How to determine when the hypotheses are satisfied

11 The Predicate Calculus: Resolution Resolution is an inference rule that allows inferred propositions to be computed from given propositions. Suppose we have two propositions A  B (B implies A) and C  D (D implies C) and that A is identical to D. Suppose we rename A and D as T: T  B and C  T From this we can infer: C  B

12 The Predicate Calculus: Unification and Instantiation Unification is the process of finding values for variables during resolution so that the matching process can succeed Instantiation is the temporary binding of a value (and type) to a variable toallow unification. A variable is instantiated only during the resolution process. The instantiation lasts only as long as it takes to satisfy one goal.

13 Prolog syntax and terminology clause = Horn clauses assumed true, represented “head :- [term [,term]*]” comma represents logical “and” clause is fact if: no terms on right of :- head and term are both structures: structure = functor (arg1, arg2,..) Represents a logical assertion, e.g teaches(Barbara, class)

14 Prolog syntax and terminology Constants are numbers or represented by strings starting with lower-case Variables start with upper-case letters A goal or query is a clause with no left- hand side: ?- rainy(seattle) Tells the Prolog interpreter to see if it can prove the clause.

15 Facts, Rules And Queries A collection of facts and rules is called a Knowledge Base. Prolog programs are Knowledgebases You use a Prolog program by posing queries.

16 Using KnowledgeBase Woman(janet) Woman(stacy) playsFlute(stacy) We can ask Prolog ?- woman(stacy). Prolog Answers yes

17 Using KnowledgeBase ?- playsFlute(stacy) Prolog Answers yes ?- playsFlute(mary) Prolog Answers Are you kidding me?

18 Elements of Prolog Fact statements—propositions that are assumed to be true, such as female(janet). male(steve). brother(steve, janet). Remember, these propositions have no intrinsic semantics--they mean what the programmer intends for them to mean.

19 Rules combine facts to increase knowledge of the system son(X,Y):- male(X),child(X,Y). X is a son of Y if X is male and X is a child of Y Elements of Prolog

20 Elements of Prolog Rule statements take the form: :- The consequent must be a single term, while the antecedent may be a single term or a conjunction. Examples: parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z).

21 Elements of Prolog Goal—a proposition that we want the system to either prove or disprove. When variables are included, the system identifies the instantiations of the variables which make the proposition true. As Prolog attempts to solve goals, it examines the facts and rules in the database in top-to- bottom order.

22 Ask the Prolog virtual machine questions Composed at the ?- prompt Returns values of bound variables and yes or no ?- son(bob, harry). yes ?- king(bob, france). no Elements of Prolog

23 Can bind answers to questions to variables Who is bob the son of? (X=harry) ?- son(bob, X). Who is male? (X=bob, harry) ?- male(X). Is bob the son of someone? (yes) ?- son(bob, _). No variables bound in this case! Elements of Prolog

24 Backtracking How are questions resolved? ?- son(X,harry). Recall the rule: son(X,Y):- male(X),child(X,Y).

25 Forward chaining (bottom-up) (starts with each rule and checks the facts) Forward chaining Use database to systematically generate new theorems until one matching query is found Example: father(bob). man(X) :- father(X) Given the goal: man(bob) Under forward chaining, father(bob) is matched against father(X) to derive the new fact man(bob) in the database. This new fact satisfies the goal.

26 Backward chaining (Top-Down) (start with the facts, use the rules that apply) Use goal to work backward to a fact Example: Given man(bob) as goal Match against man(X) to create new goal: father(bob). father(bob) goal matches pre-existing fact, thus the query is satisfied.

27 Forward vs. Backward chaining Bottom-Up Resolution (Forward Chaining) Searches the database of facts and rules, and attempts to find a sequence of matches within the database that satisfies the goal. Works more efficiently on a database that does not hold a lot of facts and rules. Top-Down Resolution (Backward Chaining) It starts with the goal, and then searches the database for matching sequence of rules and facts that satisfy the goal.

28 2 nd Part of Resolution Process If a goal has more than one sub-goal, then the problem exists as to how to process each of the sub-goals to get the goal. Depth-First Search – it first finds a sequence or a match for the first sub-goal, and then continues down to the other sub-goals. Breath-First Search – process all the sub- goals in parallel. Prolog designers went with a top-down (backward chaining), depth-first resolution process.

29 Backward Chaining Example uncle(X,thomas):- male(X), sibling(X,Y), has_parent(thomas,Y). To find an X to make uncle(X,thomas) true: 1.first find an X to make male(X) true 2.then find a Y to make sibling(X,Y) true 3.then check that has_parent(thomas,Y) is true Recursive search until rules that are facts are reached. This is called backward chaining

30 A search example Consider the database: mother (betty, janet). mother (betty, steve). mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z). and the goal ? grandparent (X, adam).

31 ? grandparent (X, adam). Prolog proceeds by attempting to match the goal clause with a fact in the database. Failing this, it attempts to find a rule with a left-hand-side (consequent) that can be unified with the goal clause. It matches the goal with grandparent (X, Z) where Z is instantiated with the value adam to give the goal grandparent (X, adam). To prove this goal, Prolog must satisfy the sub-goals parent (X,Y) and parent (Y,adam) mother (betty, janet). mother (betty, steve) mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z).

32 Sub-goal: parent (X,Y) Prolog uses a depth-first search strategy, and attempts to satisfy the first sub-goal. The first “parent” rule it encounters is parent (X,Y) :- mother (X,Y). To satisfy the sub-goal mother(X,Y), Prolog again starts at the top of the database and first encounters the fact mother (betty, janet). This fact matches the sub-goal with the instantiation X = betty, Y = janet mother (betty, janet). mother (betty, steve) mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z).

33 Sub-goal: parent (X,Y) The instantiation X = betty, Y = janet is returned so that the 2 sub-goals of the grandparent rule are now: grandparent (betty, adam) :- parent (betty, janet), parent (janet, adam). The sub-goal parent(betty, janet) was inferred by Prolog. Next, Prolog must solve the sub- goalparent(janet, adam). Once again, Prolog uses the first matching rule it encounters: parent (X,Y) :- mother (X,Y). with the instantiation X = janet, Y = adam mother (betty, janet). mother (betty, steve) mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z). X = betty Y = janet Z = adam

34 Sub-goal: parent (janet,adam) Substituting the values X = janet, Y = adam in the rule parent (X, Y) :- mother (X, Y). results in parent (janet, adam) :- mother (janet, adam). The consequent of this rule matches the 3 rd fact in the database. Since both sub-goals are now satisfied, the original goal grandparent(X, adam) is now proven with the instantiation X=betty Prolog returns with success: Yes X = betty mother (betty, janet). mother (betty, steve) mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z).

35 Solving grandparent(X,adam) mother (betty, janet). mother (betty, steve) mother (janet, adam). father (steve, dylan). parent (X, Y) :- mother (X, Y). parent (X, Y) :- father (X, Y). grandparent (X, Z) :- parent (X, Y), parent (Y, Z). grandparent(X, adam) parent(X, Y) mother(X, Y) mother(betty, janet) X = betty, Y = janet mother(betty, janet) parent(betty, janet) betty parent(janet, adam) janet mother(janet, adam)

36 Prolog lists A Prolog list consists of 0 or more elements, separated by commas, enclosed in square brackets: Example: [1,2,3,a,b] Empty list: [] Prolog list notation: [H | T] H matches the first element in a list (the Head) T matches the rest of the list (the Tail) For the example above, H = 1 T = [2,3,a,b]

37 Prolog lists To further illustrate the Prolog list notation, consider the following goals: ?- [H | T] = [1, 2, 3, 4]. H = 1 T = [2, 3, 4] Yes ?- [H1, H2 | T] = [1, 2, 3, 4]. H1 = 1 H2 = 2 T= [3, 4] Yes

38 The append predicate (1) ?- append([1, 2, 3], [a, b], X). X = [1, 2, 3, a, b] Yes ?- append([1, 2, 3], X, [1, 2, 3, a, b]). X = [a, b] Yes ?- append(X,[a,b],[1,2,3,a,b]). X = [1, 2, 3] Yes

39 The append predicate (2) ?- append(X, Y, [1,2,3]). X = [] Y = [1, 2, 3] ; X = [1] Y = [2, 3] ; X = [1, 2] Y = [3] ; X = [1, 2, 3] Y = [] Yes Semicolon instructs Prolog to find another solution.

40 Defining myappend % myappend.pl myappend([], List, List). myappend([H | T], List, [H | List2]) :- myappend(T, List, List2).

41 Execution of myappend ?- consult('myappend.pl'). % myappend.pl compiled 0.00 sec, 588 bytes Yes ?- myappend([1,2,3],[a,b],X). X = [1, 2, 3, a, b] Yes ?- myappend([1,2,3],X,[1,2,3,a,b]). X = [a, b] Yes ?- myappend(X,[a,b],[1,2,3,a,b]). X = [1, 2, 3] Yes

42 Defining myreverse % myreverse.pl :- consult('myappend.pl'). myreverse([], []). myreverse([H | T], X) :- myreverse(T, R), myappend(R, [H], X).

43 Execution of myreverse ?- consult('myreverse.pl'). % myappend.pl compiled 0.00 sec, 524 bytes % myreverse.pl compiled 0.00 sec, 524 bytes Yes ?- myreverse([1, 2, 3], X). X = [3, 2, 1] Yes ?- myreverse(X, [1, 2, 3, 4]). X = [4, 3, 2, 1] Yes

44 The Eights Puzzle The Eights Puzzle is a classic search problem which is easily solved in Prolog. The puzzle is a 3 x 3 grid with 8 tiles numbered 1 – 8 and an empty slot. A possible configuration is shown at right.

45 A sample problem The 8s Puzzle problem consists of finding a sequence of moves that transform a starting configuration into a goal configuration. Possible start and goal configurations are shown in the figure at right.

46 One solution to the problem Move left Move up Move right Move down Move left Move up

47 A partial depth-first search tree

48 The Eights Puzzle ?- solve. Enter a starting puzzle: |: |: |: Enter a goal puzzle: |: |: |: Enter a depth bound (1..9): |: Yes

49 Solving the problem We represent the puzzle using a list of 9 numbers, with 0 representing the “empty tile”. = [1,2,3,4,0,5,6,7,8]

50 Solving the problem We make moves using rules of the form move(puzzle1, puzzle2). There are 24 of these in all. For example, the following two rules describe all moves that can be made when the empty tile is in the upper left corner: move([0,B2,B3,B4,B5,B6,B7,B8,B9], % move right [B2,0,B3,B4,B5,B6,B7,B8,B9]). move([0,B2,B3,B4,B5,B6,B7,B8,B9], % move down [B4,B2,B3,0,B5,B6,B7,B8,B9]).

51 The solve rule The workhorse of the program is the solve rule, with form: solve(S, G, SL1, SL2, Depth, Bound) Where S = start puzzle G = goal puzzle SL1 is a list of states (input) SL2 is a list of states (output) Depth is the current depth of the search Bound is the depth bound

52 The solve rule solve(S,S,L,[S|L],Depth,Bound). solve(S, G, L1, L2, Depth,Bound) :- Depth < Bound, not(S == G), move(S, S1), not(member(S,L1)), D is Depth+1, solve(S1,G,[S|L1],L2,D,Bound).

53 Prolog arithmetic Arithmetic expressions are evaluated with the built in predicate is which is used as an infix operator : variable is expression Example, ?- X is 3 * 4. X = 12 yes Prolog has standard arithmetic operators: +, -, *, / (real division), // (integer division), mod, and ** Prolog has relational operators: =, \=, >, >=, <, =<

54 Applications Intelligent systems Complicated knowledge databases Natural language processing Logic data analysis

55 Conclusions Strengths: Strong ties to formal logic Many algorithms become trivially simple to implement Weaknesses: Complicated syntax Difficult to understand programs at first sight

56 Issues What applications can Prolog excel at? Is Prolog suited for large applications? Would binding the Prolog engine to another language be a good idea?

57 End of Lecture