1 Logic Programming Foundations; Prolog In Text: Chapter 15.

Slides:



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

First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Chapter 11 :: Logic Languages
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
ISBN Chapter 16 Logic Programming Languages.
Propositional Calculus A propositional calculus formula is composed of atomic propositions, which area simply statements that are either true or false.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CSE 452: Programming Languages
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
CSE 452: Programming Languages
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
CPS 506 Comparative Programming Languages
Formal Models of Computation Part II The Logic Model
ARTIFICIAL INTELLIGENCE [INTELLIGENT AGENTS PARADIGM] Professor Janis Grundspenkis Riga Technical University Faculty of Computer Science and Information.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
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.
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.
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.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Prolog Harry R. Erwin, PhD COMM2M University of Sunderland.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© 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.
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
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
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:... :-....
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
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Logic Programming Foundations; Prolog
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Logic Programming Languages
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Presentation transcript:

1 Logic Programming Foundations; Prolog In Text: Chapter 15

2 Chapter 15: LP Foundations, Prolog Logic Programming -- Basic Principles LP languages are declarative Declarative => uses “declarations” instead of assignment statements + control flow Declarative semantics: there is a simple way to determine the meaning of each statement; doesn’t depend on how the statement might be used to solve a problem much simpler than imperative semantics Logic programming languages are nonprocedural Instead of specifying how a result is to be computed, we describe the desired result and let the system figure out how to compute it

3 Chapter 15: LP Foundations, Prolog Logic Programming Example To see declarative vs. procedural differences, consider this logic pseudocode for sorting a list: sort(old_list, new_list)  permute(old_list, new_list) and sorted(new_list) sorted(list)   j such that 1  j < n: list(j)  list(j+1) Prolog is an example of a logic programming language.

4 Chapter 15: LP Foundations, Prolog Prolog Name Value System Prolog is case sensitive Object names (atoms) starting with a lower case letter Literals include integers, reals, strings “Variable” identifiers start with an upper case letter Predicate names (functions) start with lower case letters (like objects, but distinguishable by context): ( )

5 Chapter 15: LP Foundations, Prolog Prolog Name Value System (cont.) “Latent” typing, as in Scheme Types — atoms, integers, strings, reals Structures — lists, similar to LISP (see later) Scope Atoms and predicate names are all global Predicate parameters and “variables” are local to rule in which they are used No global variables or state State of the program does not include value memory “Variables” in Prolog don’t change value once they are bound (like mathematical variables)

6 Chapter 15: LP Foundations, Prolog Prolog Statements Three kinds: Fact statements Rule statements Goal statements Typically, facts + rules define a program Goal statements cause execution to begin You give a goal to run your program

7 Chapter 15: LP Foundations, Prolog Prolog -- Imperatives Prolog maintains a database of known information about its “world” in the form of facts and rules: Fact statements: female(shelley). male(bill). father(bill, shelley). Rule statements: ancestor(mary, shelley) :- mother(mary,shelley). grandparent(x,z) :- parent(x,y), parent(y,z). A Prolog program is a collection of such facts and rules.

8 Chapter 15: LP Foundations, Prolog Giving goals Given a collection of facts and rules, you can specify theorems or propositions to prove in the form of a goal statement: grandfather(bill, mary). A theorem-proving model is used to see if this proposition can be inferred from the database. “yes” or “success” means it is true (according to the database facts and rules) “no” or “failure” means that it could not be proven true (given the facts and rules in the database)

9 Chapter 15: LP Foundations, Prolog Math Foundations: Predicate Calculus A symbolic form of logic that deals with expressing and reasoning about propositions Statements/queries about state of the “universe” Simplest form: atomic proposition Form: functor (parameters) Examples: man (jake) like (bob, redheads) Can either assert truth (“jake is a man”) or query existing knowledge base (“is jake a man?”) Can contain variables, which can become bound man (x)

10 Chapter 15: LP Foundations, Prolog Compound Propositions Contain two or more atomic propositions connected by various 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 equivalent to b implication  a  b a implies b  a  b b implies a

11 Chapter 15: LP Foundations, Prolog Predicate Calculus Quantifiers -- used to bind variables in propositions universal quantifier:   x.P -- means “for all x, P is true” existential quantifier:   x.P -- means “there exists a value of x such that P is true” Examples:  x.(woman(x)  human(x))  x.(mother(mary,x))  male (x))

12 Chapter 15: LP Foundations, Prolog Clausal Form A canonical form for propositions : B 1  B 2 ...  B n  A 1  A 2 ...  A m means: if all of the A’s are true, at least one of the B’s must be true right side is the antecedent; left side is the consequent Examples: likes(bob, mary)  likes(bob, redheads)  redhead(mary) father(louis, al)  father(louis, violet)  father(al, bob)  mother(violet, bob)  grandfather(louis,bob)

13 Chapter 15: LP Foundations, Prolog Horn Clauses A proposition with zero or one term in the consequent is called a Horn clause. If there are no terms it is called a Headless Horn clause: man(jake) If there’s one term, it is a Headed Horn clause: person(jake)  man(jake)

14 Chapter 15: LP Foundations, Prolog Resolution The process of computing inferred propositions from given propositions Example: if we know: older(joanne, jake)  mother(joanne, jake) wiser(joanne, jake)  older(joanne, jake) we can infer the proposition: wiser(joanne, jake)  mother(joanne, jake) There are several logic rules that can be applied in resolution. In practice, the process can be quite complex.

15 Chapter 15: LP Foundations, Prolog PROLOG Control The right hand sides of predicates are “evaluated” left to right On a right hand side, a false predicate causes the system to return to the last predicate to its left with a true value; a true result allows the evaluation of the right hand side to continue to the right. Collections of predicates are “examined” in their lexical (textual) order — top to bottom, first to last Recursion!

16 Chapter 15: LP Foundations, Prolog PROLOG Control (cont.) A reference to a predicate is much like a function call to the collection of predicates of that name State of the program contains markers to last successful (i.e. True) instantiation in collections of facts or rules so as to support backtracking in recursion When all markers are beyond end of all applicable predicate collections, result is “no”

17 Chapter 15: LP Foundations, Prolog Prolog — Modularity and Abstraction Facts and predicates of the same name are collected by a Prolog system to form modules — the components do not have to be textually contiguous Collections of facts and rules may be stored in separate named files Files are “consulted” to bring them into a workspace

18 Chapter 15: LP Foundations, Prolog Imperatives Continued Comparison Operators =,, >=, =< (check for which!), \= Expressions most Prologs support integer arithmetic generally safest if expressions are contained in parentheses check it out in your implementation Assignment (local) “is” operator, infix assigns right hand side value to variable on left X is (3+4)

19 Chapter 15: LP Foundations, Prolog Prolog — Input/Output The output to a goal statement (query) can be: The truth value of the resulting evaluation, or The set of values that cause the goal to be true (instantiation) read(X). write(Y).

20 Chapter 15: LP Foundations, Prolog Prolog — Input/Output The output to a goal statement (query) can be: The truth value of the resulting evaluation, or The set of values that cause the goal to be true (instantiation) read(X). write(Y). read(X), Y is (X + 1), write(Y) X = 3 Y = 4 ; no read(X), Y = (X + 1), write(Y) X = 6 Y = 6+1 ; no

21 Chapter 15: LP Foundations, Prolog Prolog Programs Declare facts about objects and their inter- relationships Define rules (“clauses”) that capture object inter- relationships Ask questions (goals) about objects and their inter-relationships

22 Chapter 15: LP Foundations, Prolog Facts facts are true relations on objects Michael is Cathy’s father Chuck is Michael’s and Julie’s father David is Chuck’s father Sam is Melody’s father Cathy is Melody’s mother Hazel is Michael’s and Julie’s mother Melody is Sandy’s mother facts need not make sense The moon is made of green cheese father(michael, cathy). father(chuck, michael). father(chuck, julie). father(david, chuck). father(sam, melody). mother(cathy, melody). mother(hazel, michael). mother(hazel, julie). mother(melody, sandy). made_of(moon, green_cheese).

23 Chapter 15: LP Foundations, Prolog Rules A person’s parent is their mother or father A person’s grandfather is the father of one of their parents A person’s grandmother is the mother one of their parents parent(X, Y) :- father(X, Y). parent(X, Y) :- mother(X, Y). /* could also be: parent(X, Y) :- father(X, Y); mother(X, Y). */ grandfather(X, Y) :- father(X, A), parent(A, Y). grandmother(X, Y) :- mother(X, A), parent(A, Y).

24 Chapter 15: LP Foundations, Prolog Goals: Questions or Queries Who is father of cathy ? ?- father(X, cathy). Who is chuck the father of ? ?- father(chuck, X). Is chuck the parent of julie ? ?- parent(chuck, julie). Who is the grandmother of sandy ? ?- grandmother(X, sandy). Who is the grandfather of whom ? ?- grandfather(X, Y).

25 Chapter 15: LP Foundations, Prolog Prolog Names Revisited atoms: Symbolic values father(bill, mike). Strings of letters, digits, and underscores starting with lower case letter Variable: unbound entity father(X, mike). Strings of letters, digits, and underscores starting with UPPER CASE letter Variables are not bound to a type by declaration

26 Chapter 15: LP Foundations, Prolog Prolog Facts & Rules Facts: unconditional assertion assumed to be true contain no variables mother(carol, jim). stored in database Rules: assertion from which conclusions can be drawn if given conditions are true: parent(X, Y) :- father(X, Y);mother (X, Y). Contain variables for instantiation Also stored in database

27 Chapter 15: LP Foundations, Prolog Prolog Instantiation Instantiation: binding of a variable to value (and thus, a type): color (apple, red). FACTS color (banana, yellow). ?- color (X, yellow). } question (goal) X = apple color (apple, yellow) instantiation no matching pattern X = banana color (banana, yellow) yes

28 Chapter 15: LP Foundations, Prolog Prolog Unification Unification: Process of finding instantiation of variable for which “match” is found in database of facts and rules Developed by Alan Robinson about 1965, but not applied until the 1970s to logic programming The key to Prolog

29 Chapter 15: LP Foundations, Prolog Prolog Example color(banana, yellow). color(squash, yellow). color(apple, green). color(peas, green). FACTS fruit(banana). fruit(apple). vegetable(squash). vegetable(peas). bob eats green colored vegetables RULE eats(bob, X) :- color(X, green), vegetable(X). bob eats X if X is green and X is a veggie

30 Chapter 15: LP Foundations, Prolog Does Bob Eat Apples? Bob eats green vegetables: eats(bob, X) :- color(X, green), vegetable(X). Does bob eat apples ? ?- eats(bob, apple). color(apple, green) => match vegetable(apple) => no

31 Chapter 15: LP Foundations, Prolog What Does Bob Eat? ?- eats(bob, X). color(banana, green) => no color(squash, green) => no color(apple, green) => yes vegetable(apple) => no color(peas, green) => yes vegetable(peas) => yes Therefore: eats(bob, peas) true X = peas

32 Chapter 15: LP Foundations, Prolog Prolog And/Or/Not Conjunctive rules: X if Y and Z father(X, Y) :- parent(X, Y), male(X). Disjunctive rules: X if Y or Z parent(X, Y) :- mother(X, Y). parent(X, Y) :- mother(X, Y). /* or */ parent(X, Y) :- father(X, Y); mother(X, Y). Negation rules: X if not Y good(X) :- \+ bad(X). mother(X, Y) :- parent(X, Y), \+ male(X). Use Parentheses for grouping

33 Chapter 15: LP Foundations, Prolog “Older” Example older(george, john). older(alice, george). older(john, mary). older(X, Z) :- older(X, Y), older(Y, Z). Now when we ask a query that will result in TRUE, we get the right answer: ?- older(george, mary). yes But a query that is FALSE goes into an endless loop: ?- older(mary, john). Left recursion: the last element in older is the predicate that is repeatedly tried

34 Chapter 15: LP Foundations, Prolog Solving Left Recursion Problems Remove the older rule and replace with: is_older(X, Y) :- older(X, Y). is_older(X, Z) :- older(X, Y), is_older(Y, Z). Now: ?- is_older(mary, john). no

35 Chapter 15: LP Foundations, Prolog Don’t Care! Variables can also begin with an underscore Any such variable is one whose actual value doesn’t matter: you “don’t care” what it is, so you didn’t give it a real name Used for aguments or parameters whose instantiated value is of no consequence ?- is_older(george, _). Succeeds, Indicating that there does exist an argument which will cause the query to be true, but the value is not returned

36 Chapter 15: LP Foundations, Prolog Prolog Lists Lists are represented by [...] An explicit list [a,b,c], or [A,B,C] As in LISP, we can identify the head and tail of a list through the use of the punctuation symbol “|” (vertical bar) in a list pattern: [H|T] or [_|T] There are no explicit functions to select the head or tail (such as CAR and CDR) Instead, lists are broken down by using patterns as formal arguments to a predicate

37 Chapter 15: LP Foundations, Prolog Sample List Functions /*Membership*/ member(H, [H | _]). member(H, [_ | T]) :- member(H, T). /*Concatenation of two lists*/ concat([], L, L). concat([H | T], L, [H | U]) :- concat(T, L, U). /*Reverse a list*/ reverse([], []). reverse([H | T], L) :-reverse(T, R), concat(R, [H], L). /*Equality of Lists*/ equal_lists([], []). equal_lists([H1 | T1], [H2 | T2]) :- H1 = H2, equal_lists(T1, T2).

38 Chapter 15: LP Foundations, Prolog A Logic Puzzle Three children, Anne, Brian, and Mary, live on the same street Their last names are Brown, Green, and White One is 7, one is 9, and one is 10. We know: 1. Miss Brown is three years older than Mary. 2. The child whose name is White is nine years old. What are the children's ages?

39 Chapter 15: LP Foundations, Prolog State the Facts /*----- Facts -----*/ child(anne). child(brian). child(mary). age(7). age(9). age(10). house(brown). house(green). house(white). female(anne). female(mary). male(brian).

40 Chapter 15: LP Foundations, Prolog Define the Rules /*----- Rules -----*/ clue1(Child, Age, House, Marys_Age) :- House \= brown; House = brown, female(Child), Marys_Age =:= Age - 3. clue2(_Child, Age, House) :- House \= white ; Age = 9. are_unique(A, B, C) :- A \= B, A \= C, B \= C.

41 Chapter 15: LP Foundations, Prolog Guess A Solution guess_child(Child, Age, House) :- child(Child), age(Age), house(House). solution(Annes_Age, Annes_House, Brians_Age, Brians_House, Marys_Age, Marys_House) :- /* Guess an answer */ guess_child(anne, Annes_Age, Annes_House), guess_child(brian, Brians_Age, Brians_House), guess_child(mary, Marys_Age, Marys_House), are_unique(Annes_Age, Brians_Age, Marys_Age), are_unique(Annes_House, Brians_House, Marys_House), …

42 Chapter 15: LP Foundations, Prolog Test It For Veracity Solution(…) :- … /* filter against clue 1 */ clue1(anne, Annes_Age, Annes_House, Marys_Age), clue1(brian, Brians_Age, Brians_House, Marys_Age), clue1(mary, Marys_Age, Marys_House, Marys_Age), /* filter against clue 2 */ clue2(anne, Annes_Age, Annes_House), clue2(brian, Brians_Age, Brians_House), clue2(mary, Marys_Age, Marys_House).

43 Chapter 15: LP Foundations, Prolog Prolog Issues Efficiency—theorem proving can be extremely time consuming Resolution order control Processing is always top to bottom, left to right. Indirect control by your choice of ordering Uses backward chaining; sometimes forward chaining is better Prolog always searches depth-first, though sometimes breadth-first can work better

44 Chapter 15: LP Foundations, Prolog Prolog Limitations “Closed World”—the only truth is that recorded in the database Negation Problem—failure to prove is not equivalent to logically false not(not(some_goal)) is not equivalent to some_goal