Chapter 12 - Logic Programming

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
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
1. An Overview of 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).
Logic Programming Some "declarative" slides on logic programming and Prolog. James Brucker.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
P ROGRAMMING L ANGUAGES : L OGIC P ROGRAMMING. S LIDES R EFERENCES Kenneth C. Louden, Functional Programming, Programming Languages: Principles and Practice,
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Programming Languages Third Edition
Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:
School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
1.  Provides the ability to access individual assertions. e.g. in Predicate calculus we may say: P denotes “It rained on Tuesday” but in predicate calculus.
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.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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.
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.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
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.
CHA2555 Week2 Practical: Lee McCluskey First term:
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
1-1 An Introduction to Logical Programming Sept
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”
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:... :-....
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Horn Clauses and Unification
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Horn Clauses and Unification
Horn Clauses and Unification
Chapter 12 :: Logic Languages
Horn Clauses and Unification
Horn Clauses and Unification
CSE 3302 Programming Languages
Presentation transcript:

Chapter 12 - Logic Programming 4/16/2017 Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden © Kenneth C. Louden, 2003

K. Louden, Programming Languages Logic Programming Uses a set of logical assertions (i.e. statements that are either true or false), as a program (the facts). Execution is initiated by a query or goal, which the system attempts to prove true or false, based on the existing set of assertions. For this reason, logic programming systems are sometimes called deductive databases. Two main "weirdnesses": no explicit functions, no explicit execution control. Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Examples Computing ancestors: A parent is an ancestor. If A is an ancestor of B, and B is an ancestor of C, then A is an ancestor of C. (a typical relation: called ??) A mother is a parent. A father is a parent. Bill is the father of Jill. Jill is the mother of Sam. Bob is the father of Sam. Computing the factorial function: The factorial of 0 is 1. If m is the factorial of n - 1, then n * m is the factorial of n. Note incompleteness! Chapter 12 K. Louden, Programming Languages

(First order) Predicate Calculus Starts with a set of axioms ( true assertions), stated using the following elements: Constants. Usually numbers or names. In the examples, Bill and 0 are constants. Predicates. Names for functions that are true or false, like Boolean functions in a program. Predicates can take a number of arguments. In the examples, ancestor and factorial are predicates. Functions. First-order predicate calculus distinguishes between functions that are true or false—these are the predicates—and all other functions, which represent non-Boolean values. * is a function in the examples. (Factorial - a function - is indirectly expressed as a predicate.) Chapter 12 K. Louden, Programming Languages

Predicate Calculus (continued) Variables that stand for as yet unspecified quantities. In the examples, A and m are variables. Connectives. Operations and, or, and not; implication "" and equivalence " " (derivable from the previous three). Quantifiers. These are operations that introduce variables: "for all" - the universal quantifier, and "there exists" - the existential quantifier. Punctuation symbols: left and right parentheses, the comma, and the period. Note: Arguments to predicates and functions can only be terms: combinations of variables, constants, and functions. Chapter 12 K. Louden, Programming Languages

Examples written in Pred. Calc.: Ancestors: For all X and Y, parent(X,Y)  ancestor(X,Y). For all A, B, and C, ancestor(A,B) and ancestor(B,C)  ancestor(A,C). For all X and Y, mother(X,Y)  parent(X,Y). For all X and Y, father(X,Y)  parent(X,Y). Father(Bill,Jill). Mother(Jill,Sam). Father(Bob,Sam). Factorials: Factorial(0,1). For all n and m, factorial(n-1,m)  factorial(n,n*m). Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Horn Clauses Drop the quantifiers (i.e., assume them implicitly). Distinguish variables from constants, predicates, and functions by upper/lower case: parent(X,Y)  ancestor(X,Y). ancestor(A,B) and ancestor(B,C)  ancestor(A,C). mother(X,Y)  parent(X,Y). father(X,Y)  parent(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). factorial(0,1). factorial(N-1,M)  factorial(N,N*M). Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Prolog Modified Horn clause syntax: write the clauses backward, with :- as the (backward) arrow, comma as "and" and semicolon as "or": ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). factorial(0,1). factorial(N,N*M) :- factorial(N-1,M). Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Unfortunate Errors: ?- ancestor(bill,sam). Yes. ?- ancestor(bill,X). X = jill ; X = sam ; ERROR: Out of local stack ?- ancestor(X,bob). ?- factorial(2,2). No ?- factorial(0,X). X = 1 ; ERROR: Out of global stack Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages What's Wrong???? Immediate recursion ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). factorial(0,1). factorial(N,N*M) :- factorial(N-1,M). No arithmetic is actually computed Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Arithmetic Easy to fix - arithmetic expressions must be explicitly forced: ?- 2*3 = 6. No ?- 2*3 = 2*3. Yes ?- 6 is 2*3. Yes ?- 2*3 is 6. No Rewrite factorial like this: factorial(0,1). factorial(N,P) :- N1 is N-1, factorial(N1,M), P is M*N. "is" forces its right-hand argument But not its left-hand argument Chapter 12 K. Louden, Programming Languages

Stack Problems a bit harder: Stack problems still exist for factorial: ?- factorial(7,X). X = 5040 ; ERROR: Out of local stack To figure this out, we must understand the procedural interpretation of Horn clauses: a :- b, c, d. represents the definition of a (boolean) function named a. The body of a is given by the clauses on the right hand side: b, then c, then d. Note similarity to recursive-descent parsing! Also note sequencing! Chapter 12 K. Louden, Programming Languages

Prolog's Execution Strategy Given a query or goal, Prolog tries to pattern match the goal with the left-hand sides of all clauses, in a sequential top-down fashion. Any lhs that matches causes the rhs terms to be set up sequentially as subgoals, which Prolog immediately tries to match in turn with lhs terms. Thus, Prolog's execution path is top-down, left-to-right, depth-first. All intermediate results are kept for backtracking purposes. Chapter 12 K. Louden, Programming Languages

Solving the stack problems For the ancestor problem, just remove the left recursion: ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y). parent(X,Z) For the factorial problem, it involves preventing reuse of the recursive rule: factorial(0,1). factorial(N,P) :- not(N=0), N1 is N-1, factorial(N1,M), P is M*N. Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Lists Prolog uses almost the same list syntax as Haskell or ML: [1,2,3] Head and tail pattern syntax is different: [H|T] versus (h:t) in Haskell or (h::t) in ML Also, as many elements as desired can be written using commas before the bar: [1,2,3|[4]] is the list [1,2,3,4]. Example: ?- [1,2,3] = [X,Y|_]. X = 1 Y = 2 Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Pattern Matching Pattern matching is more general in Prolog than it is in Haskell: variables can be repeated in patterns, implying that they must be the same value: ?- [1,1] = [X,X]. X = 1 ?- [1,2] = [X,X]. No Other operations can be more general too. Chapter 12 K. Louden, Programming Languages

K. Louden, Programming Languages Final Examples The append list operation: append([],X,X). append([H|T],L,[H|M]) :- append(T,L,M). Quicksort: quicksort([],[]). quicksort([H|T],Result) :- split(H,T,A,B),/* exercise */ quicksort(A,X), quicksort(B,Y), append(X,[H|Y],Result). Chapter 12 K. Louden, Programming Languages