Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, 2006. (ISBN 9781904987178)

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Advertisements

Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
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.
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.
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).
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Recursion Introduction to Computing Science and Programming I.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 7.
CS 321 Programming Languages and Compilers Prolog part 2.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
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.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
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
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Simple Data structure and computation. Solving equation? sum(X,Y,Z):- Z is X+Y. ?sum(1,R,7). is/2: Arguments are not sufficiently instantiated ^ Exception:
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Artificial Intelligence CS370D Prolog programming List operations.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
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.
Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Section 16.5, 16.6 plus other references
Introduction to Computing Science and Programming I
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Racket CSC270 Pepper major portions credited to
Unification, Recursion and Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
تصنيف التفاعلات الكيميائية
CS 36 – Chapter 11 Functional programming Features Practice
Chapter 12 :: Logic Languages
Lecture 7: Definite Clause Grammars
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, 2006. (ISBN 9781904987178) Also available at http://www.learnprolognow.org

List Syntax Samples: [mia, vincent, jules, yolanda] [mia, robber(honey_bunny), X, 2, mia] [] [mia, [vincent, jules], [butch, girlfriend(butch)]] [[], dead(z), [2, [b, c]], [], Z, [2, [b, c]]]

List Syntax Enclose lists in brackets Any type elements Empty list is [] Lists can contain lists All lists have an implied empty list at the end

Head and Tail Use [ H | T ] to extract the first element:  [Head|Tail]  =  [mia,  vincent,  jules,  yolanda].    Head  =  mia    Tail  =  [vincent,jules,yolanda] Multiple elements: [H1, H2 |Tail]  =  [mia,  vincent,  jules,  yolanda].    H1  =  mia H2 = vincent    Tail  =  [ jules,yolanda] No more elements: [H1 |Tail]  =  [mia].    H1  =  mia    Tail  =  [ ] Empy list has no Head and Tail

Are you a member of a list? Either X is the head Or x is a member of the tail Remember the empty list has no Head and Tail so the empty tail wont fit member(X,[X|T]).  member(X,[X|T]).    member(X,[H|T])  :-  member(X,T). Because you don't care about H in second or T in first, replace with _ member(X,[X|_]).    member(X,[_|T])  :-  member(X,T).

Recurse down a list Change a list of a's to b's Goal: Yes to No to  a2b([a,a,a,a],[b,b,b,b]).  a2b([a,a,a],[b,b,b]).   a2b([],[]). No to a2b([a,c,a,a],[b,b,5,4]). a2b([a,a,a,a],[b,b,b]).

Recurse a2b The empty list is a non-recursive true a2b fact The head of one list being a and the other being b plus the rest of each list fitting a2b a2b([a|Ta],[b|Tb])  :-  a2b(Ta,Tb). a2b([a,a,a,a],X). X  =  [b,b,b,b].  a2b(X,[b,b,b,b]).  X  =  [a,a,a,a]

Translate a list tran(eins,one). tran(zwei,two). tran(drei,three). tran(vier,four). tran(fuenf,five). tran(sechs,six). tran(sieben,seven). tran(acht,eight). tran(neun,nine). Create listtran([eins,neun,zwei],X). That will return the translation: X = [one,nine,two].

Translation Answer Good translation: A list of no items translates to a list of no items listtran([],[]). Another good translation: the head of the german list being part of a tran fact with the head of the english list plus the rest of the lists being a translation as well. listtran([Hg|Tg], [He|Te]):- tran(Hg,He), listtran(Tg,Te).

Exercise: Translate edges Translate a list of starting points of each edge, to ending points of each edge Sample run with the same edges as in #1 endPath([a,b,x],X). X = [b, c, z] ; X = [b, d, z] ; X = [b, e, z].

Append 2 lists Append is true when nothing added to a List yields that List   append([],L,L). Append is true when the first element of the list = the first element of another list, and when the tail of the first list is appended to the tail of the result.    append([H|T],L2,[H|L3])  :-  append(T,L2,L3).

Trace append append([a,  b,  c],  [1,  2,  3],  _G518)    append([b,  c],  [1,  2,  3],  _G587)    append([c],  [1,  2,  3],  _G590)    append([],  [1,  2,  3],  _G593)    append([],  [1,  2,  3],  [1,  2,  3])    append([c],  [1,  2,  3],  [c,  1,  2,  3])    append([b,  c],  [1,  2,  3],  [b,  c,  1,  2,  3])    append([a,  b,  c],  [1,  2,  3],  [a,  b,  c,  1,  2,  3])

prefix How to get all the elements that are prefixes in the list? prefix(X,[a,b,c,d]).        X  =  []  ;        X  =  [a]  ;        X  =  [a,b]  ;        X  =  [a,b,c]  ;        X  =  [a,b,c,d]  ;

prefix    prefix(P,L):-  append(P,_,L).

Exercise: Query for Suffix    ?-  suffix(X,[a,b,c,d]).        X  =  [a,b,c,d]  ;        X  =  [b,c,d]  ;        X  =  [c,d]  ;        X  =  [d]  ;        X  =  []  ;

Reversing a list Naïve: nrev([],[]). nrev([H|T],R):- nrev( T,RevT), append(RevT,[H], R) .

Use an accumulator accRev([],A,A). accRev([H|T],A,R):- accRev(T,[H|A],R). accRev([1,2,3],[],X). Call: (6) accRev([1, 2, 3], [], _G2326) ? creep Call: (7) accRev([2, 3], [1], _G2326) ? creep Call: (8) accRev([3], [2, 1], _G2326) ? creep Call: (9) accRev([], [3, 2, 1], _G2326) ? creep Exit: (9) accRev([], [3, 2, 1], [3, 2, 1]) ? creep Exit: (8) accRev([3], [2, 1], [3, 2, 1]) ? creep Exit: (7) accRev([2, 3], [1], [3, 2, 1]) ? creep Exit: (6) accRev([1, 2, 3], [], [3, 2, 1]) ? creep X = [3, 2, 1].

How the Accumulator Works Seed the accumulator with an empty list It grows as it is called recursively At the base case, transfer the accumulator over to a variable that will pass back.

Accumulate a List of Throws throwUntil_11WList(A,[11|A]):- throw2(11). throwUntil_11WList(PriorRolls, R ):- throw2(Y), Y \= 11, throwUntil_11WList([Y|PriorRolls],R). Call as : throwUntil_11WList([],X). When the throw is 11 (base case), transfer the accumulator to the result. When the throw is not 11, insert the throw at the head of the list.

Accumulating a List Accumulated text: Accumulate a list: Call as: descend2(X,Y, parentChild(X,Y)):- child(X,Y). descend2(X,Y, parentChild(X,I,L)):- child(X,I), descend2(I,Y,L). Accumulate a list: descendList(X,Y,L,[L|child(X,Y) ]):- child(X,Y). descendList(X,Y,L,A):- child(X,I), descendList(I,Y,[L,child(X,I) ],A). Call as: descendList(anne,donna,anne,X).

Accumulating a List Like counting, Need to seed the first item in the list. Need to transfer the accumulation to a result variable.

Exercise: Accumulate the Path you Walk Build a list of the edges you travel connectedPathtoList(a,e, a,X). X = [[a, edge(a, b)]|edge(b, e)] ; X = [[[a, edge(a, b)], edge(b, d)]|edge(d, e)] .

Summary Syntax for list creation [ H | T ] use to split off the first element T is a list Determine whether member Append Reverse using accumulation Build a list using accumulation