C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.

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.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Chapter 11 :: Logic Languages
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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.
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 Chapter 15 Part 2. Prolog Lists The list is Prolog’s basic data structure Lists are a series of Prolog terms, separated by commas Each.
Programming Languages 2e Tucker and Noonan
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Contents 15.1 Logic and Horn Clauses 15.2 Logic Programming in Prolog
TES3111 October 2001 Artificial Intelligence LISP.
JavaScript, Third Edition
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Formal Models of Computation Part II The Logic Model
Discussion #141/20 Discussion #14 Predicate Calculus.
CS 321 Programming Languages and Compilers Prolog part 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
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.
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.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
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.
Logic Programming Chapter 15 Part 2. Breadth-first v Depth-first Search Suppose a query has compound goals (several propositions must be satisfied) Depth-first.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R N I N E Logic Programming.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Section 16.5, 16.6 plus other references
Prolog a declarative language
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Nondeterministic Evaluation
Chapter 11 :: Logic Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
C H A P T E R N I N E Logic Programming.
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Programming Languages 2nd edition Tucker and Noonan
Lisp.
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

From Last Time: Prolog programs are made up of terms, which can be constants, variables, or structures. A constant is either an atom (foo, who, “Sue”) or a non-negative integer. A variable is a series of letter that begins with a capital letter (Alf, Result). A structure is a predicate with zero or more arguments (speaks(Who,russian)). The number of arguments is called the structure’s arity.

From Last Time: A fact is a term followed by a period (.) as in: speaks(allen, russian). A rule is a term followed by :- and a series of terms sepparated by commas and ended in a period: parent(A,B) :- father(A,B). parent(A,B) :- mother(A,B). Rules are interpreted as “only if” assertions and the commas are treated as logical “and” operators. A Prolog program is a series of facts and rules.

Lists: The basic Prolog data structure is the list, a series of terms separated by commas and enclosed in brackets: [this, is, a, list] Lists can contain “don’t care” terms designated by the underscore character (_). The first element of a list, the head, is distinguished from the remaining elements of the list like this: [X | Y]

List Functions: A Prolog function to do list concatenation is written recursively as follows: append([], X, X). append([H | T], Y, [H | Z]) :- append(T, Y, Z). The first line is the “base case” which says that an empty list concatenated with any other returns the other list. The recursive second case says “if Z is the result of concatenating lists T and Y then concatenating any new list [H | T] with Y yields [H | Z]”. The process goes something like this…

Partial Search Tree for append([english, russian], [spanish], L) Figure 9.5

List Functions: The following functions define prefix and suffix: prefix(X, Z) :- append(X, Y, Z). suffix(Y, Z) :- append(X, Y, Z). This function defines membership in a list: member(X, [X | _]). member(X, [_ | Y]) :- member(X, Y). Note the use of the don’t care terms; the first line says X is a member if it is the head of a list, the second line says that X is a member if it is a member of the tail of a list.

Solving Word Puzzles: Consider the following word puzzle: Baker, Cooper, Fletcher, Miller, and Smith live in a five story building. Baker doesn’t live on the 5 th floor and Cooper doesn’t live on the first floor. Fletcher doesn’t live on the top or bottom floor, and he is not on a floor adjacent to Smith or Cooper. Miller lives on some floor above Cooper. Who lives on what floors? In addition to the member function from before we will define: adjacent(X, Y) :- X =:= Y+1. adjacent(X, Y) :- X =:= Y-1.

Prolog Solution for the Building Problem Figure 9.12 To run the program type: building(X).

Cut, is, not and assert: Inserting a cut (!) in the right-hand terms (sub- goals) of a rule will cause the right-hand side to not be examined if it has evaluated to true once. (See the bsort example in the book). The is operator can be used to force instantiation of a variable: X is Y+3. The not operator negates goal failure, that is not(P) is true when P is false. The assert function is used to add new facts and rules during execution: assert(mother(jane, joe)).

The Factorial Function in Prolog Figure 9.7 Note that we introduced the intermediate variable M in order to force the evaluation of N-1 before the recursive call.

Trace of Factorial (4) Figure 9.8

Symbolic Differentiation Rules Figure 9.9

Prolog Symbolic Differentiator Figure 9.10

Next time… Logic Programming Wrap Up