CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11 :: Logic Languages
1 A formula in predicate logic An atom is a formula. If F is a formula then (~F) is a formula. If F and G are Formulae then (F /\ G), (F \/ G), (F → G),
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Automated Reasoning Systems For first order Predicate Logic.
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 Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
ISBN Chapter 16 Logic Programming Languages.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CSE 452: Programming Languages Logical Programming Languages Part 2.
Inference and Resolution for Problem Solving
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS 330 Programming Languages 12 / 09 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 05 / 2006 Instructor: Michael Eckmann.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
CPS 506 Comparative Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Propositional Resolution Computational LogicLecture 4 Michael Genesereth Spring 2005.
CS 330 Programming Languages 12 / 04 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
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.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
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.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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.
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.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
© 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.
CS 330 Programming Languages 11 / 25 / 2008 Instructor: Michael Eckmann.
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.
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.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
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”
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
Section 16.5, 16.6 plus other references
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 Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
CS2136: Paradigms of Computation
Logic Programming & Prolog
Presentation transcript:

CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions / comments? Prolog

Michael Eckmann - Skidmore College - CS Fall 2008 Logical connectors a implies b b implies a a  b a  b  implication a is equivalent to b a  b  equivalence a or b a  b  disjunction a and b a  b  conjunction not a  a  negation MeaningExampleSymbolName

Michael Eckmann - Skidmore College - CS Fall 2008 Logic Languages To use a variable in a proposition we need to preface it with either “for all” or “there exists” These are quantifiers –For all is represented by an upside-down A –There exists is represented by a backwards E Predicate calculus consists of the logical connectors and the two quantifiers above.

Michael Eckmann - Skidmore College - CS Fall 2008 Logic Languages Clausal form: B 1  B 2  …  B n  A 1  A 2  …  A m means if all the As are true, then at least one B is true Antecedent: right side Consequent: left side

Michael Eckmann - Skidmore College - CS Fall 2008 Logic Languages The A's and B's in the clausal form can be variables or constants. When they are variables, the “for all” quantifier is implied. We can use clausal form exclusively because: –All predicate calculus propositions can be algorithmically converted to clausal form. So, we do not lose generality by restriction to this form of propositions.

Michael Eckmann - Skidmore College - CS Fall 2008 Predicate Calc and proving theorems The presence of variables in propositions requires resolution to find values for those variables that allow the matching process to succeed. This is called unification. Instantiation is the temporary assigning of values to variables. During some more complex resolutions, assigning some temporary values might cause the matching process to fail, at which time, backtracking must occur to instantiate a different value to the variable.

Michael Eckmann - Skidmore College - CS Fall 2008 Predicate Calc and proving theorems Horn clauses are a restricted kind of clausal form (used when propositions are used for resolution.)‏ –A single atomic proposition on left side –Or –Empty left side Left side is called the head. If left side is not empty the Horn clause is called a headed Horn clause.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Prolog implementations use backward chaining Backward chaining –begin with goal and attempt to find sequence that leads to set of facts in database When goal has more than one subgoal, Prolog uses depth-first search Depth-first search – find a complete proof for the first subgoal before working on others backtrackingFor a goal with multiple subgoals, if fail to show truth of one of subgoals, Prolog uses backtracking Backtracking –reconsider previous subgoal to find an alternative solution

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog An example. “Generate and test.” integer_divide(N1, N2, Result) :- is_integer(Result), Product1 is Result * N2, Product2 is (Result + 1) * N2, Product1 =< N1, Product2 > N1. Note: each evaluation of is_integer(Result) gives an integer from the “database” of integers starting at 0. Recall that Prolog instantiates variables by going through the facts in order from the beginning. Query: integer_divide(7,2,Answer).

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog List Structures are another basic data structure in Prolog besides atomic propositions. A List is a sequence of any number of elements Elements can be atoms, atomic propositions, or other terms (including other lists)‏ [apple, prune, grape, kumquat] [] ( empty list)‏ [X | Y] ( head X and tail Y)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog [X | Y] ( head X and tail Y)‏ Head X is like Scheme's car of a list Tail Y is like Scheme's cdr of a list | can be used to dismantle a list into head and tail in query mode | can be used to construct a list from a head and a tail

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog [X | Y] ( head X and tail Y)‏ –my_list([apple, prune, grape]). –my_list([apricot, peach, pear, lime]). These signify that the lists are new elements of the relation my_list. Just like parent(jane). stated that jane is an element of the relation parent. my_list is not a variable --- it is a relation. Example query: –my_list([ My_head | My_tail ] )‏ –Results in my_head instantiated with apple, and my_tail instantiated with [prune, grape]

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog [X | Y] ( head X and tail Y)‏ –my_list([apple, prune, grape]). –my_list([apricot, peach, pear, lime]). –my_list([ My_head | My_tail ] )‏ If this query was part of a goal and it backtracked, the next value for my_head would be: apricot and the next value for my_tail would be: [peach, pear, lime]

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog [X | Y] ( head X and tail Y)‏ –also used for constructing lists Construction example: –[apricot, peach | [pear, lime]]

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Append –The first two parameters to append are appended together to produce the result which parameter 3 gets instantiated with. –Note: we will “call” append with only the third parameter being a variable! append([], List, List). append([Head | List_1], List_2, [Head | List_3])‏ :- append (List_1, List_2, List_3). Order matters! The termination step (the headless clause) is first (just like in Scheme.) The recursion step is second. Prolog will try to match the propositions in order because it uses depth-first search. How do we read these propositions?

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog append([], List, List). append([Head | List_1], List_2, [Head | List_3])‏ :- append (List_1, List_2, List_3). So it says, if the first parameter is the empty list, the resulting list (parameter 3) is instantiated to the value of the second parameter. Otherwise, list3 is list1 and list2 appended together if the head of the first parameter is the head of parameter 3 (the result) and the tail of the 3 rd parameter is the tail of the 1 st parameter appended to the second parameter. Or in other words: appending [Head | List_1] to List_2 results in [Head | List_3] only if, List_3 is List_1 appened to List_2. It'll become clearer with an example.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Similar to scheme's: (cons (car list1) (append (cdr list1) list2))‏ append([], List, List). append([Head | List_1], List_2, [Head | List_3])‏ :- append (List_1, List_2, List_3). append( [bob, jo], [jake, darcie], Family). Let's trace through on the board how Prolog will instantiate Family. (page 705.)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Example: member(Element, [Element | _ ] ). member(Element, [ _ | List] ) :- member(Element, List). /* _ is anonymous variable, use when we won't use the variable in the proposition. */ These member propositions state that if first parameter is the head of the second parameter, then member is satisfied. If not, then Element is a member of the List if Element is in the tail of the List.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Cut (!) to prevent backtracking (to earlier subgoals before the cut during a resolution.)‏ Recall, that prolog backtracks when a goal (or subgoal) fails. Let's say we have something like this on the right hand side of a proposition: –member(X, Lis), do_something(X, Lis, Y). If member is satisfied and then do_something is attempted and fails, backtracking will occur and cause member to be reattempted. Inside member's implementation, it will pick up where it left off and possibly not find another match. To make sure it doesn't backtrack, we can use the Cut. member(Element, [Element | _ ] ) :- !. member(Element, [ _ | List] ) :- member(Element, List). Now, Prolog cannot backtrack to reevaluate member once Element is found in the list.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Cut (!) to prevent backtracking (to earlier subgoals before the cut during a resolution.)‏ In this goal: –x, y, z, !, w, q. Once x y and z are satisfied (w/ possible backtracking) then w is attempted. If w fails, then the whole goal fails (because can't backtrack over the !)‏

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Negation problem parent(bill, jake). parent(bill, shelly). sibling( X, Y) :- parent(M, X), parent(M, Y). goal sibling(X, Y). results in: X = jake Y = jake Because it goes through the facts first with parent(M, X) and instantiates them to bill and jake. Then parent(M,Y) starts at the beginning of the facts and tries to find a fact with bill as parent. It finds bill and jake again.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Negation problem solution parent(bill, jake). parent(bill, shelly). sibling( X, Y) :- parent(M, X), parent(M, Y), X <> Y. goal sibling(X, Y). results in: X = jake Y = shelly Because it goes through the facts first with parent(M, X) and instantiates them to bill and jake. Then parent(M,Y) starts at the beginning of the facts and tries to find a fact with bill as parent. It finds bill and jake again. But then X <> Y becomes jake <> jake which fails, so backtracking occurs to parent(M, Y). Prolog remembers where it left off in the list of parent facts, so it gets the next one (bill, shelly) that's good, so then X <> Y becomes jake <> shelly which is satisfied. Done.

Michael Eckmann - Skidmore College - CS Fall 2008 Prolog Fail Can be used (as a subgoal) to force a goal to fail, thus forcing backtracking. Only makes sense as the last subgoal of a goal (because any subgoals after it would never get tested.)‏