MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Formal Models of Computation Part II The Logic Model
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
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.
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.
Chapter Three: Lists, Operators, Arithmetic CS 461.
© 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.
SECTION 21.5 Eilbroun Benjamin CS 257 – Dr. TY Lin INFORMATION INTEGRATION.
1 COMP313A Programming Languages Logic Programming (3)
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
SECTIONS 21.4 – 21.5 Sanuja Dabade & Eilbroun Benjamin CS 257 – Dr. TY Lin INFORMATION INTEGRATION.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Intro to Robots Lists in Python. Intro to Robots What is a List? An ordered set of values: –Ordered: 1 st, 2 nd, 3 rd, … –Values: can be anything, integers,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
Chapter 3: Arrays, Linked Lists, and Recursion
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
Formal Models of Computation Part II The Logic Model
LING 388: Language and Computers Sandiway Fong Lecture 4.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Construction Techniques (1) - 1ICOM 4075 (Fall, 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez 2005.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
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.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Naïve Set Theory. Basic Definitions Naïve set theory is the non-axiomatic treatment of set theory. In the axiomatic treatment, which we will only allude.
Module Code MA1032N: Logic Lecture for Week Autumn.
F28PL1 Programming Languages Lecture 18: Prolog 3.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
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),
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
10.4 Warm up – No Calc. Section 10.4 – Determinant of a SQUARE Matrix With and Without Calculator By the end of this lesson, you should be able to: Calculate.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lecture 6 of Computer Science II
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
CSCE 314: Programming Languages Dr. Dylan Shell
Lisp.
Chapter 12 :: Logic Languages
Presentation transcript:

MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2

MB: 5 March 2001CS360 Lecture 42 Prolog Lists n The elements of a list can be anything including other lists. n Remember that atoms could be made from a sequence of special characters, the empty list is the special atom “[ ]”.

MB: 5 March 2001CS360 Lecture 43 Lists n A non-empty list always contains two things, a head and the tail. It is a structured data object. The functor name is “.” and its arity is 2. n The list consisting of the item “3” is:.(3,[ ])

MB: 5 March 2001CS360 Lecture 44 Lists cont’d n The list consisting of the two items “3” and “x” is:.(3,.(x,[ ])) n Lists are one of the most pervasive data structures in Prolog, so there is a special notation for them: [3, x]

MB: 5 March 2001CS360 Lecture 45 Lists cont’d n Often want to describe beginning of list without specifying the rest of it. For example,.(3,.(x, T)) describes a list whose first two items are 3 and x, and whose remaining items could be anything (including empty). n Prolog provides a special notation for doing this, “|”, e.g., [3,x |T]

MB: 5 March 2001CS360 Lecture 46 Lists n [3, x | T] matches : – [3,x], – [3,x,y(5)], – and [3,x,56, U, name(mike, barley)] (among others) n with T = – [ ], – [y(5)], – and [56,U,name(mike, barley)]

MB: 5 March 2001CS360 Lecture 47 Definition of List n List definition: – list([ ]). – list([I|L1]) :- list(L1).

MB: 5 March 2001CS360 Lecture 48 List Operations n Since lists are an inductively defined data structure, expect operations to be inductively defined. n One common list operation is checking whether something is a member of the list. – member(Item, List)

MB: 5 March 2001CS360 Lecture 49 List Membership n If defining list membership inductively, need to figure out base case for list variable. n Base case for defn of list is [ ], but not appropriate for base case of membership. Why? n Need to look elsewhere. What’s the simplest case for deciding membership?

MB: 5 March 2001CS360 Lecture 410 List Membership n It’s the first item in the list. Maybe this can be our base case. – member(Item, List) :- List = [Item | _ ]. n Prolog is pattern-directed, i.e., does pattern matching, can use this to simplify base case: – member( I, [ I | _ ]).

MB: 5 March 2001CS360 Lecture 411 List Membership n What if item is not the first one in list, then what? Then need to check if it’s in the tail. – member(I, [ _ | T ]) :- member(I, T). n Don’t we have to check for empty list case? – No, because if we hit the empty list, then I is not in the list, so we should fail.

MB: 5 March 2001CS360 Lecture 412 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ ]). Response: no Execution Trace: [member(x,[ ])] 

MB: 5 March 2001CS360 Lecture 413 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(x, [ w, x]). Response: Partial Execution Trace: [member(x,[ w, x ])] 2. I=x, T=[x] [member(x, [x])] continue trace

MB: 5 March 2001CS360 Lecture 414 List Membership KB: 1. member(I, [ I | _ ]). 2. member(I, [_| T] :- member(I, T). Query: member(X, [ w, x]). Response: X=w ? ; Partial Execution Trace: [member(X,[ w, x ])] 1. X=I, I=w [ ] continue trace

MB: 5 March 2001CS360 Lecture 415 List Concatenation n Define relation conc(L1, L2, L3) where L3 is the result of concatenating L1 onto front of L2. n What is the base case? n Go back to defn of list, what is base case?

MB: 5 March 2001CS360 Lecture 416 List Concatenation n List defn base case is [ ], should this be our base case for defining concatenation? n conc([ ], ?, ?) - what is the result of concatenating [ ] onto anything? Are there special cases? n conc([ ], L2, L2).

MB: 5 March 2001CS360 Lecture 417 List Concatenation n What should the inductive step be? n What was the inductive step for list defn? n What should the head look like? n conc([ I | L1], L2, [ I | L3]) n What’s the relation between L1, L2, and L3? n conc(L1, L2, L3)

MB: 5 March 2001CS360 Lecture 418 List Concatenation n Full definition: – conc([ ], L, L). – conc([ I | L1], L2, [I|L3]) :- conc(L1, L2, L3). n Try doing an execution trace for the query: – conc(L1, L2, [1, 2, 3]). n What are the bindings for L1 and L2 if keep asking for alternatives?

MB: 5 March 2001CS360 Lecture 419 Multi-Way Uses of Relations n We have seen that one nice feature of logic programming is its absence of control. n This means we can define one central relation and use it in a number of different ways. What it means depends upon which arguments are variables, partially variablized and/or constants. n conc/3 is an example of such a central relation.

MB: 5 March 2001CS360 Lecture 420 Some Uses of Concatenation n member(I, L) :- conc(_, [ I | _ ], L). n last( Item, List) :- conc(_, [Item], List). n sublist(SubList, List) :- conc(L1, L2, List), conc(SubList, _, L2).

MB: 5 March 2001CS360 Lecture 421 Clarity n We don’t really need to write defns for member/2 and last/2, could just use conc/3. n What have we gained by writing those definitions? n We write their definitions because we want it to be obvious what we’re trying to do!

MB: 5 March 2001CS360 Lecture 422 List Operations n Adds item to front of list: – add(Item, List, [Item | List]). n Given the following code: add(1,[ ],L1), add(2, L1,L2), add(3,L2,L3). What would be the binding for L3?

MB: 5 March 2001CS360 Lecture 423 List Operations n Deletes item from list: – del(Item, [Item| Tail], Tail). – del(Item, [Y | Tail], [Y | Tail1]) :- del(Item, Tail, Tail1). n del/2 is non-deterministic, what would del(1,[1,2,1,3,1],L). What would be the bindings for L if we repeatedly asked for new alternatives?

MB: 5 March 2001CS360 Lecture 424 n Insert item into list: – insert(I,List,NewList) :- del(I, NewList, List). n insert/3 also non-deterministic, what would insert(x, [1,2,3], L) bind to L if repeatedly ask for alternatives?

MB: 5 March 2001CS360 Lecture 425 Permutation of a List n Let’s define the “permutation” relation: – perm(List, Permutation). n Are we clear about what is a permutation? – Look at examples. n What type of definition will we look for?

MB: 5 March 2001CS360 Lecture 426 Defining Permutation Relation n Where do we look for our cases? n What should be our base case?

MB: 5 March 2001CS360 Lecture 427 Defining Permutation Relation n What should be our inductive case? n What should the head look like? n What’s the relationship between the different parts?

MB: 5 March 2001CS360 Lecture 428 Permutation Defined n permutation([ ],[ ]). n permutation([X | L1], Perm) :- permutation(L1, L2), insert(X, L2, Perm).

MB: 5 March 2001CS360 Lecture 429 Homework Quiz n Write definitions for the following relations: – reverse(List, ReverseList) – subSet(Set, SubSet) – flatten(List, FlatList)

MB: 5 March 2001CS360 Lecture 430 Summary n If data structure defined inductively then usually operations are defined inductively. n However, sometimes the data structure base case does not make sense for the operation, then need to find new base case. n First part of coming up with inductive case is finding what the head should be, often part of head is data structure inductive case.

MB: 5 March 2001CS360 Lecture 431 Summary cont’d n Defining relations in pure Prolog allows definitions to be used in many ways. n However, when some uses have common name (e.g., “last”) then should define new relation from old using the common name.