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.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
Prolog.
© 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.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
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).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Programming Languages 2e Tucker and Noonan
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
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.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
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.
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
CS 321 Programming Languages and Compilers Prolog part 2.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 9: Functional Programming in a Typed Language.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Programming Languages Third Edition Chapter 4 Logic Programming.
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.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
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.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
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.
Expressions and Data Types Professor Robin Burke.
Section 16.5, 16.6 plus other references
Programming Languages 2nd edition Tucker and Noonan
User-Written Functions
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Programming Techniques
Programming Languages 2nd edition Tucker and Noonan
Chapter 12 :: Logic Languages
Presentation transcript:

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 list element can be a(n) –atom –variable –sublist –etc.

Examples of Lists The empty list: [ ] List with embedded list: [boys, [like, girls]] List with variables: [x, V1, y, V2, [A, B]] –V1, V2, A, and B are variables that may be instantiated with data at a later time. Multi-type lists: [boy, [1, 2, 3], ran] [A, _, Z] –The _ means “don’t care” – sometimes referred to as an unbound variable.

Working with Lists [Head|Tail] notation simplifies processing: –The vertical bar is an operator that separates a list into its Head (the first list element), and its Tail (a list that represents everything else). –Head can be any Prolog term (list, variable, atom, predicate, etc.) –If L = [a, b, c] then Head = a and Tail = [b,c] –Tail is always another list. What is the head of [a]? The tail?

Working with Lists What is the head of [ ]? Of [a]? 4 ?- [ ] = [Head|Tail]. false. 5 ?- [a] = [Head|Tail]. Head = a, Tail = [].

The append Function append is a built-in Prolog function that concatenates two lists. append(A, B, L) concatenates the lists A and B and returns them as L. append([my, cat], [is, fat], L). yields L = [my, cat, is, fat]

The Append Function append(L1, L2, L3): append([ ], X, X). %base case append([Head|Tail], Y,[Head|Tail2]) :- append(Tail, Y, Tail2). This definition says: –The empty list concatenated with any list (X) returns an unchanged list (X again). –Append [H|T] to Y and get a new list that has the same 1 st element but a new tail: the list you would get if you appended Tail to Y –Each recursive call reduces the size of the first list by one element until it becomes the empty list and then the calls terminate and begin the series of returns.

?- Append([english, russian], [spanish], L). H=english, T=[russian], Y=[spanish], L=[english,Z] 1 and Z = [russian, spanish] Append([russian],[spanish], [Z]). H = russian, T=[ ], Y=[spanish], Z=[russian|Z 1 ] 2 Append([ ], [spanish], [Z 1 ]). So Z1= [spanish] X=[spanish], Z 1 =[spanish] 3 Append([ ], [spanish], [spanish]). append([ ], X, X). append([Head|Tail], Y,[Head|Tail2]) :- append(Tail, Y, Tail2)

Using append prefix(X, Z) :- append(X, Y, Z). (finds all prefixes of a list Z) suffix(Y, Z) :- append(X, Y, Z). (finds all suffixes of Z)

Recursion/ member The function returns ‘yes’ or ‘true’ if X is a member of a given list. member(X, [X | _ ]). member(X, [ _ | Y]) :- member(X, Y).

Member(X,Y) The test for membership succeeds if either: –X is the head of the list [X |_] –X is not the head of the list [_| Y], but X is a member of the list Y (the tail). Notes: pattern matching governs tests for equality. Don ’ t care entries (_) mark parts of a list that aren ’ t important to the rule.

Naming Lists Defining a set of lists: a([single]). a([a, b, c]). a([cat, dog, sheep]). When a query such as a(L), prefix(X, L). Is posed, all three lists can be processed. Other lists, such as b([red, yellow, green]), would be ignored.

a([single]). a([a, b, c]). a([cat, dog, sheep]). prefix(X, Z) :- append(X, _, Z). suffix(Y, Z) :- append(_, Y, Z). % To make queries about lists in the database: % suffix(X, [the, cat, is, fat]). % a(L), prefix(X, L). A Sample List Program

?- a(L), prefix(X, L). L = [single] X = [] ; L = [single] X = [single] ; L = [a, b, c] X = [] ; L = [a, b, c] X = [a] ; L = [a, b, c] X = [a, b] ; L = [a, b, c] X = [a, b, c] ; L = [cat, dog, sheep] X = [] Sample Output Based on the program on the previous slide.

35 ?- a(L), append([cat], L, M). L = [single] M = [cat, single] ; L = [a, b, c] M = [cat, a, b, c] ; L = [cat, dog, sheep] M = [cat, cat, dog, sheep] ; Sample Output

Logic Programming : Practical Aspects 15.3: Example Applications

Recursive Factorial Program To see the dynamics of a function call, use the trace function. For example,given the following function: factorial(0, 1). factorial(N, Result):- N > 0, M is N-1, factorial(M, SubRes), Result is N * SubRes. % ‘is’ ~ temporary assignment

Using the Trace Function At the prompt, type “trace.” Then type the query. Prolog will show the rules it uses and the instantiation of unbound constants. Useful for understanding what is happening in a search process, or in a recursive function. NOTE – the following is based on previous versions of Prolog. Current version is a little different.

Tracing Output ?- trace(factorial/2). ?- factorial(4, X). Call: ( 7) factorial(4, _G173) Call: ( 8) factorial(3, _L131) Call: ( 9) factorial(2, _L144) Call: ( 10) factorial(1, _L157) Call: ( 11) factorial(0, _L170) Exit: ( 11) factorial(0, 1) Exit: ( 10) factorial(1, 1) Exit: ( 9) factorial(2, 2) Exit: ( 8) factorial(3, 6) Exit: ( 7) factorial(4, 24) X = 24 These are temporary variables These are levels in the search tree factorial(0, 1). factorial(N, Result):- N > 0, M is N-1, factorial(M, SubRes), Result is N * SubRes.

%remove() removes an element from a list. %To Call: remove(a, List, Remainder). % or remove(X, List, Remainder). % First parameter is the removed item, % 2nd parameter is the original list, % third is the final list remove(X, [X|R], R). remove(X, [H|R], [H|S]):- remove(X, R, S).

18 ?- trace. Yes 18 ?- remove(a, [b, d, a, c], R). Call: (7) remove(a, [b, d, a, c], _G545) ? creep Call: (8) remove(a, [d, a, c], _G608) ? creep Call: (9) remove(a, [a, c], _G611) ? creep Exit: (9) remove(a, [a, c], [c]) ? creep Exit: (8) remove(a, [d, a, c], [d, c]) ? creep Exit: (7) remove(a, [b, d, a, c], [b, d, c]) ? creep R = [b, d, c]

Revisiting The Factorial Function Evaluation of clauses is from left to right. Note the use of is to temporarily assign values to M and Result

Trace of Factorial (4)

Simple Arithmetic Integer “variables” and integer operations are possible, but imperative language “assignment statements” don’t exist.

Sample Program speed(fred, 60). speed(carol, 75). time(fred, 20). time(carol, 21). distance(Person, Dist) :- speed(Person, Speed), time(Person, Time), Dist is Speed * Time. area_square(S, A) :- A is S * S.

Sample Program There are limits imposed by the need to have variables instantiated area_square(S, A) :- A is S * S. 1 ?- area_square(3.5, A). A = ?- area_square(S, 16). ERROR: is/2: Arguments are not sufficiently instantiated 3 ?- S*S isn’t computable since S has not been instantiated.

Prolog Operators is can be used to cause a variable to be temporarily instantiated with a value. Compare to assignment statements in declarative languages, where variables are permanently assigned values. The not operator is used to indicate goal failure. For example not(P) is true when P is false.

Arithmetic Originally, Prolog used prefix notation +(7, X) Modern versions have infix notation X is Y*C + 3. Qualification: Y and C must be instantiated, as in the Speed program, but X cannot be (It’s not a traditional assignment statement). –X = X + Y is illegal. –X is X + Y is illegal. “ Arguments are not sufficiently instantiated ”

More About Arithmetic Example of simple arithmetic, using something similar to Python’s calculator mode (not as part of a program). –?- X is –X = 10 Arithmetic operators: +, -, *, /, ^ ( exponentiation) Relational operators:, =, = =, \=

Symbolic Differentiation Rules Figure 15.9

Prolog Symbolic Differentiator Figure 15.10

Search Tree for the Query d(x, 2*x+1, Ans) Figure d(u*v) = udv + vdu = 2*dx + x * d2) d(u + v) = du + dv

Executing a Prolog Program Create a file containing facts and rules; e.g., familytree.pl or download the sample program on K drive, change the extension from. txt to.pl, and experiment with it. Follow instructions in handout, which is posted in K:\LABS\424.

SWIplEdit “compile” error If SWI-Prolog finds an error in the.pl file it will give a message such as ERROR: c:/temp/prologprogs/remove.pl: 18:0: Syntax error: Illegal start of term (18 is the line number)

Runtime Error Message The function samelength was called with one parameter when it needed 2: 21 ?- samelength(X). ERROR:Undefined procedure: samelength/1 ERROR:However, there are definitions for: samelength/2

Runtime Errors Here, the error is an error of omission: 22 ?- samelength([a, b, c,],[a, b]) | Queries must end with a period. If you hit enter without typing a period SWIpl just thinks you aren’t through.

Using SWI Prolog If there is an error that you can’t figure out (for example you don’t get any answers, you don’t get a prompt, typing a semicolon doesn’t help) try “interrupt” under the Run button. If changes are made to the program, don’t forget to save the file and “consult” again.