CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Declarative Programming Lists in PROLOG Autumn 2014.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
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).
AI – Week 5 Implementing your own AI Planner in Prolog – part II : HEURISTICS Lee McCluskey, room 2/09
1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.
AI – Week 11 AI and Games (3) Lee McCluskey, room 2/09
F28PL1 Programming Languages Lecture 17: Prolog 2.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CHA2555 Week 7: Prolog: Some Tips Planners: Some Tips Lee McCluskey First term:
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
Formal Aspects of Computer Science – Week 12 RECAP Lee McCluskey, room 2/07
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
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
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
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.
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)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
CHA2555 Week2 Practical: Lee McCluskey First term:
COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
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;
From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
COSC 2P93 Prolog: Debugging
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Chapter 11 :: Logic Languages
Prolog Lists.
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Programming Paradigms and Languages
Prolog Lists.
Chapter 12 :: Logic Languages
Presentation transcript:

CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:

Artform Research Group Resume n You should know Basic Prolog operation, matching, basic backtracking, variable instantiation (to end of section 2 in notes.tex) This week: n The Cut, list processes, recursive functions (to end of notes - sections 3,4, and 5) n Also consult the “extendednotes” flie in the same link as notes.tex

Artform Research Group Lists Lists are a high level data structure a. [ bank, moorgate, old_street ] b. [ john, [ X, mary] ] c. placing( red_rum, [ 1, 4, u, 2, 1, f, 3 ] ) d. [ [ [ x, y ], z ] ] n A List is a sequence of TERMS n It has a HEAD (the first term in the list) and a TAIL (the LIST without the first term) n In Prolog we use a MIXFIX op [ _ | _ ] to decompose lists n The empty list is an ODD list – [ ]

Artform Research Group Recursion n Typical recursive procedures have the following form: u Base Case to stop recursion u Recursive Case: process data, and recursively call procedure with data nearer the Base Case descendant(X,Y) :- parent(X,Y). /* - X has descendant Y if X is the parent of Y */ descendant(X,Y) :- parent(X,Z), descendant(Z,Y). /* - X has descendant Y if X is the parent of someone who has decendant Y */

Artform Research Group List Processing n Virtually all list processing carried out in prolog contains the following Recursive Case: 'Break the input list down into its head and tail with an expression of the form '[ X | Y ]'. Process the head X. Recursively call this procedure with input list = tail Y.‘ Example: arg2 is the list arg1 with its elements doubled.. double_list([],[]). double_list([Head|Tail], [HeadD|TailD):- HeadD is 2*Head, double_list(Tail, TailD).

Artform Research Group List Processing common functions % Succeeds if arg1 is a member of arg2, otherwise fails: member(X, [X|T]). member(X, [_|T]) :- member(X,T). % concatenates two lists together, arg3 = arg1 ++ arg2 append([],L, L). append([H|T],L, [H|CL]) :- append(T,L,CL). “_” is the anonymous variable – it matches like a variable, but we don’t need to remember what it matches with!

Artform Research Group The Cut The cut is written '!'. It is a predicate with the following properties: n It always succeeds the first time it is invoked in an activation of a rule. n If on backtracking the cut is re-invoked, it not only fails the current rule activation BUT ALSO FAILS THE GOAL THAT ACTIVATED THE RULE. n It may be placed anywhere (as a predicate) in the body of a rule.

Artform Research Group The Cut Consider a program with rules of the form (where A, B, C, X, Y,Z are arbitrary predicates): X :-... Y, Z,... /* rule 1 */ A :-... B, !, C,... /* rule 2 */ Assume the following takes place: rule 1 is activated and its sub-goals succeed up to Y; Z is invoked and matches A, thereby activating rule 2; rule 2's sub-goals succeed past B and the cut; C fails (or succeeds but subsequently fails on backtracking); then the cut will cause the goal that activated the rule it is in, i.e. Z, to fail. Control will next backtrack in rule 1 and re- invoke Y.

Artform Research Group The Cut – main use The procedures factorial(N, Fact), double_list(L1,L2), member(X,L), append(L1,L2,L3) are FUNCTIONAL – not relational. So any procedure which calls them should NEVER backtrack within them => we use the CUT to make them functional. factorial(0,1):-!. factorial(N,Fact) :- X is N-1, factorial(X,Fact1), Fact is N*Fact1,!. append([],L, L). append([H|T],L, [H|CL]) :- append(T,L,CL).!.

Artform Research Group Assert and Retract “assert(X)” will assert term X as a clause at the end of the program. “retract(X)” will succeed once it has removed a clause that matches with X. Thus Prolog programs can change themselves ;-) Eg Assert all the terms in a list as facts.. assert_list([]). Assert_list([H|T) :- assert(H), assert_list(T).

Artform Research Group Formative Assessment I would like to see everyone in class do the following exercises before we move on: 1. sort(L,SL): Write a prolog program to sort a list of numbers into ascending order 2. appendall(LL,L): Write a prolog program to join a list of lists into one big list NB do not get a solution from somewhere else – do it yourself!

Artform Research Group Summary n List processing functions are very important in Prolog – a list is a special kind of term. n The Cut stops backtracking within a clause. It is particularly useful to make procedures only give out ONE answer. n Prolog programs can store/remove ‘global data’ using assert and retract. n PRACTICAL: finish the “notes”