© Kenneth C. Louden, 20031 Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.

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
Knowledge & Reasoning Logical Reasoning: to have a computer automatically perform deduction or prove theorems Knowledge Representations: modern ways of.
Standard Logical Equivalences
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
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,
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/
Prolog IV Logic, condensed. 2 Propositional logic Propositional logic consists of: The logical values true and false ( T and F ) Propositions: “Sentences,”
Programming Languages Third Edition
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
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.
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.
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.
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.
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.
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/
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Propositional Logic Predicate Logic
Programming Languages Third Edition Chapter 4 Logic Programming.
Chapter 2 Symbolic Logic. Section 2-1 Truth, Equivalence and Implication.
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.
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
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”
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.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
CSE 341, S. Tanimoto Logic Programming -
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
Horn Clauses and Unification
Logic Programming Languages
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Horn Clauses and Unification
Horn Clauses and Unification
Horn Clauses and Unification
Logic Programming Language
Horn Clauses and Unification
CSE 3302 Programming Languages
CS2136: Paradigms of Computation
Presentation transcript:

© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden

Chapter 12K. Louden, Programming Languages2 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 12K. Louden, Programming Languages3 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 12K. Louden, Programming Languages4 (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 12K. Louden, Programming Languages5 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 12K. Louden, Programming Languages6 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 12K. Louden, Programming Languages7 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 12K. Louden, Programming Languages8 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 12K. Louden, Programming Languages9 Resolution and Unification Resolution is an inference rule for Horn clauses that is especially efficient The process of pattern matching to make statements identical is called unification. The variables that are set equal to patterns are said to be instantiated.

Chapter 12K. Louden, Programming Languages10 Simplest example of resolution b  a. c  b. Then resolution says that we may infer the following clause: c  a. Another way of looking at resolution is to combine lhs and rhs of both Horn clauses and then cancel those statements that match on both sides. b, c  a, b gives c  a

Chapter 12K. Louden, Programming Languages11 Unification for resolution Example 9 (page ) shows an additional requirement when we apply resolution to derive goals: To match statements that contain variables, so that the statements become identical and can be canceled from both sides – this is called unification.

Chapter 12K. Louden, Programming Languages12 Refutation system A refutation system is a logical system that proves a statement by assuming it is false and deriving a contradiction. Horn clause logic with resolution is a refutation system (exercise 12.3). –Prove by contradiction –Construction of deductive tree –Pattern matching, pattern matching with variables (unification, instantiation)

Chapter 12K. Louden, Programming Languages13 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 12K. Louden, Programming Languages14 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 12K. Louden, Programming Languages15 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 12K. Louden, Programming Languages16 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).