1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.

Slides:



Advertisements
Similar presentations
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Advertisements

Prolog Nonmonotonic logic.
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Modern Programming Languages
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
”assert” and ”retract”
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).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
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.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Chapter 12 - Logic Programming
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
CS 321 Programming Languages and Compilers Prolog part 2.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
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),
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
1 CS Programming Languages Class 19 November 2, 2000.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
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.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution).
1 TP #4: Control Facilities n Last TP exercises solved; n Don’t care variables « _ »; n Or « ; »; n Cut.
Section 16.5, 16.6 plus other references
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:
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Computer Science 312 Haskell Lists 1.
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Prolog III.
PROGRAMMING IN HASKELL
PROLOG.
Presentation transcript:

1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.

© L.Lúcio, Last TP exercises solved n Ex.1. Trace the « predecessor(joe,clara) » goal in the predecessor program. parent(john,clara). parent(clara,mike). parent(mike,maria). parent(joe,kim). predecessor(X,Y) :- parent(X,Y). predecessor(X,Y) :- parent(X,Z),predecessor(Z,Y). predecessor(joe,clara) parent(joe,clara) False parent(joe,Z), predecessor(Z,clara) parent(joe,kim)predecessor(kim,clara) parent(kim,clara) False parent(kim,Z1), predecessor(Z1,clara) parent(kim,Z1) False Z = kim

© L.Lúcio, Last TP exercises solved (cont) n Ex.2. Trace the « factorial(3) » goal in the factorial program. factorial(0,1). factorial(Y,X) :- Y>0, Y1 is Y-1, factorial(Y1,X1), X is Y*X1. factorial(3,X) 3 > 02 is 3-1factorial(2,X1)6 is 3*2 2 > 01 is 2-1factorial(1,X2)2 is 2*1 1 > 00 is 1-1factorial(0,X3)1 is 1*1 factorial(0,1) X3 = 1 Tru e

© L.Lúcio, Lists n We are now leaving the « pure » logic aspects of Prolog and going into Prolog programming itself; n Some of the predefined Prolog predicates will be taught, others not! Use the Prolog help, the recommended book or the online manual available for reference… n A List is a sequence of any number of items, e.g.: [bottle, coffee, car, John, fishing] [ ] - The empty list

© L.Lúcio, Lists (cont) n A list can be seen either as empty ([ ]) or as a sequence of elements consisting of a head and tail; n The head can be any Prolog object (atom, variable, term, another list); n The tail is a list of Prolog objects; n Syntax example: [bottle, coffee, car, John, fishing] HeadTail ?- L=[ann|[mike,john]]. L = [ann, mike, john] Yes Use the « | » operator to concatenate head and tail

© L.Lúcio, Predefined predicates for Lists n The member operation returns true if the given element is member of a list. ?- member(a,[a,b,c]). Yes ?- member(d,[a,b,c]). No n The member operation is defined as follows: member(X,[X|Tail]). member(X,[Head|Tail]) :- member(X,Tail).

© L.Lúcio, Predefined predicates for Lists (cont) n append: defines the concatenation of two lists; ?- append([a,b],[1,2],List). List = [a, b, 1, 2] Yes ?- append([X,Y],[1,likes(e,f)],[a,b,1,likes(e,f)]). X = a Y = b Yes n The append operation is defined as follows: append([],L,L). append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).

© L.Lúcio, Exercises n There are many more list predicates defined in the SWI Prolog libraries. While doing the TP practical exercises, use them as much as you can instead of redefining every list operation. 1. Write a relation that will retrieve the last element of a list: n Example: my_last(X,[a,b,c,d]) … X=d 2. Write a relation that will give the length of a list: n Example: my_length(X,[e,f,g(h,f)]) … X=3 3. Write a relation that will reverse a list: n Example: my_reverse(X,[a,b,c,d]) … X=[d,c,b,a]

© L.Lúcio, Solutions 1. my_last: my_last(X,[X|[]]). my_last(X,[Head|Tail]):- my_last(X,Tail). 2. my_length: my_length(0,[]). my_length(X,[Head|Tail]):- my_length(X1,Tail), X is X1+1.

© L.Lúcio, Solutions (cont) 3. my_reverse: my_reverse(List,Reversed) :- my_reverse(List,[],Reversed). my_reverse([],Reversed,Reversed). my_reverse([Head|Tail], SoFar, Reversed) :- my_reverse(Tail, [Head|SoFar], Reversed).

© L.Lúcio, Prolog as a Database n In a way Prolog can be seen as a relational database, since: everything can be seen as a relation; we have the operators to manipulate those relations; n In SWI Prolog there are two sorts of relations: u Static: the ones that are read from a file by « consult » and that are not defined as dynamic; u Dynamic: the ones that are either input by « assert » or that are defined in a file as dynamic; n It is possible to add in run-time new facts to a relation that is defined as dynamic; n It is NOT possible to add in run-time new facts to a relation that is defined as static.

© L.Lúcio, Prolog as a Database (cont) /* immigrants.pl */ :- dynamic wasborn/2 :- dynamic livesin/2 wasborn(rui,portugal). wasborn(jo,germany). ?- consult(‘immigrants.pl’). Yes ?- wasborn(mark,england). No ?- assert(wasborn(mark,england)). Yes ?- wasborn(mark,england). Yes n SWI Prolog provides several predefined predicates for fact database manipulation: u assert: adds a fact to the database; u asserta: adds a fact to the beginning of the database; u assertz: adds a fact to the end of the database (same as assert); u retract: removes a fact from the database.

© L.Lúcio, Prolog as a Database – an example maketable :- L=[0,1,2,3,4,5,6,7,8,9], member(X,L), member(Y,L), Z is X*Y, assert(product(X,Y,Z)), fail. ?- maketable. No ?- listing(product). :- dynamic product/3. product(0, 0, 0). product(0, 1, 0). product(0, 2, 0). product(0, 3, 0). product(0, 4, 0). product(0, 5, 0). product(0, 6, 0). product(0, 7, 0). product(0, 8, 0). product(0, 9, 0). product(1, 0, 0). product(1, 1, 1). product(1, 2, 2). product(1, 3, 3). product(1, 4, 4). product(1, 5, 5). product(1, 6, 6). … n Questions: u Why does the program reply « no »? u What is the « fail » doing there?