Alok Mehta - Programming in Lisp - Lecture 6 1 66-2210-01 Programming in Lisp Lecture 6 - Structure; Case Study: Blocks World.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
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)
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
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)
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Generics "It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient.
1 LISP III. 2 Functional programming l Definition From the "comp.lang.functional FAQ" Functional programming is a style of programming that emphasizes.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
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.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
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.
Stacks, Queues, and Deques
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Functional Programming 02 Lists
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
11 Speed & Debug.  Lisp is really two languages:  A language for writing fast programs  A language for writing programs fast  In the early stage,
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Linked List by Chapter 5 Linked List by
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Example of formula (defun roots (a b c) (list
CS 1114: Implementing Search
Queues Queues Queues.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
Pointers and Linked Lists
Stacks, Queues, and Deques
Stacks.
Stacks, Queues, and Deques
Lesson Objectives Aims
J.E. Spragg Mitthögskolan 1997
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Modern Programming Languages Lecture 20 Fakhar Lodhi
Stacks.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Announcements Quiz 5 HW6 due October 23
Stacks, Queues, and Deques
Common Lisp II.
Lisp.
The general format of case is the following: (case <key form>
Presentation transcript:

Alok Mehta - Programming in Lisp - Lecture Programming in Lisp Lecture 6 - Structure; Case Study: Blocks World

Alok Mehta - Programming in Lisp - Lecture 6 2 Structure  Lisp's use of pointers  Let you put any value anywhere  Details are taken care of by the Lisp interpreter  What goes on "under the hood"?  It's useful to know this sometimes  Some functions let you get down to low level details of pointers

Alok Mehta - Programming in Lisp - Lecture 6 3 Shared Structure  Lists can share conses in common > (setf part (list 'b 'c)) (B C) > (setf whole (cons 'a part)) (A B C) > (tailp part whole) T ABC PART WHOLE

Alok Mehta - Programming in Lisp - Lecture 6 4 Tailp  Definition of tailp  (tailp object list) –returns T if object is a tail of list  Example implementation > (defun our-tailp (x y) (or (eql x y) (and (consp y) (our-tailp x (cdr y)))))  An object is a tail of itself (base case)  NIL is a tail of every proper list –Recursively take cdr's of the list, until you test for (eql NIL NIL)

Alok Mehta - Programming in Lisp - Lecture 6 5 Shared Structure  Lists can share structure without either being a tail of the other > (setf part (list 'b 'c) whole1 (cons 1 part) whole2 (cons 2 part)) 1 BC PART WHOLE1 2 WHOLE2

Alok Mehta - Programming in Lisp - Lecture 6 6 Top-Level List Structure  Sometimes want to treat data structure as a list  Include only the conses that make up the list  Don't recurse into conses that make up elements  This is the top-level list structure  Other times, want to treat data structure as a tree  All conses are important and treated uniformly AD BC

Alok Mehta - Programming in Lisp - Lecture 6 7 Copy-list vs. Copy-tree AD BC Original Copy-list AD BC Copy-tree

Alok Mehta - Programming in Lisp - Lecture 6 8 Copy-list vs. Copy-tree (code)  Example implementation of copy-list > (defun our-copy-list (lst) (if (null lst) nil (cons (car lst) (our-copy-list (cdr lst)))))  Example implementation of copy-tree > (defun our-copy-tree (lst) (if (atom tr) tr (cons (our-copy-tree (car tr)) (our-copy-tree (cdr tr)))))  Lisp handles pointers automatically  Then… why do we care?  Because some Lisp functions can modify structures

Alok Mehta - Programming in Lisp - Lecture 6 9 Setf revisited > (setf whole (list 'a 'b 'c) tail (cdr whole)) (B C) > (setf (second tail) 'e) E > tail (B E) > whole (A B E) ABC TAIL WHOLE

Alok Mehta - Programming in Lisp - Lecture 6 10 Operators  Avoid modifying lists  Operators like setf, pop, rplaca, …  If you need to (or want to) modify a list  Make sure it's not shared  Or, make sure the shared update works as expected  Or, make changes to a copy of the list > (setf whole (list 'a 'b 'c) tail (cdr whole)) (B C) > (setf tail (cons (first tail) (cons 'e (rest (rest tail))))) (B C) > tail (B C) > whole (A B E)

Alok Mehta - Programming in Lisp - Lecture 6 11 Parameter passing  Parameters are passed by value  The value is copied into the function  If the value is a list…  The entire list is not copied  The reference to the list is copied  A function can permanently alter a list passed in as a parameter!  Similar to parameter passing in Java  Can cause unintentional errors  But, can also be useful

Alok Mehta - Programming in Lisp - Lecture 6 12 Queues  Queue data structure  First in, First out  Compare to stacks (last in, first out)  Stacks are easy in Lisp –Insert (push) / Retrieve (pop) happen at the same end of the list  Queues are harder –Insert (enqueue) / Retrieve (dequeue) happen at opposite ends ABC Q1

Alok Mehta - Programming in Lisp - Lecture 6 13 Sample implementation  Implementation of Queue (Inefficient) > (defmacro dequeue (q) `(pop,q)) > (defmacro enqueue (o q) `(setf,q (append,q (cons,o nil))))  Implementation of Queue using CLOS (Inefficient) > (defclass queue () ((front :accessor front :initform nil))) > (defmethod dequeue ((q queue)) (pop (front q))) > (defmethod enqueue (o (q queue) &aux (node (cons o nil))) (setf (front q) (append (front q) node))) –Example usage > (setf a (make-instance 'queue)) > (enqueue 'a a) > (enqueue 'b a) > (dequeue a)

Alok Mehta - Programming in Lisp - Lecture 6 14 Efficient Queue Implementation  Efficient Implementation of Queue using CLOS > (defclass queue () ((front :accessor front :initform nil) (back :accessor back :initform nil))) > (defmethod enqueue (o (q queue) &aux (node (cons o nil))) (if (eql (front q) nil) ; first one? (setf (front q) node (back q) node) (progn ; not first time (setf (cdr (back q)) node) (setf (back q) (cdr (back q)))) )) > (defmethod dequeue ((q queue)) (if (eql (cdr (front q)) nil) (setf back nil)) ;last one (pop (front q)))

Alok Mehta - Programming in Lisp - Lecture 6 15 Destructive Functions  Several functions update the lists passed to them  Examples  Delete (destructive version of remove) > (setf a '(a b a d a)) (A B A D A) > (remove 'a a) ; Doesn't change A (B D) > (delete 'a A) ; Changes A (B D)  Nconc (destructive version of append) > (defun our-nconc (x y) (if (consp x) (progn (setf (cdr (last x)) y) x) y))

Alok Mehta - Programming in Lisp - Lecture 6 16 Case Study: The Blocks World  Rules  There are three kinds of movable objects: bricks, wedges, balls.  Robot has one hand. It can grasp any movable block that has nothing on top of it.  Every block is either held by the hand or supported by exactly one brick or the table. No block can overhang from its support.  Although a movable block can be moved to the top of a wedge or a ball, neither wedges nor balls can support anything.  Supporting bricks can support more than one block, as long as there is room.  The table is wide enough for all of the blocks to fit on it at once.

Alok Mehta - Programming in Lisp - Lecture 6 17 Case Study: The Blocks World Table B1 B4 B2 B3 W7 W5 B6 L8  Several moves are required to put block B1 on block B2  Sample path  Move W7; Move B4; Move B1 onto B2  <=== Robotic Hand

Alok Mehta - Programming in Lisp - Lecture 6 18 Class Hierarchy Basic-block Load-bearing-blockMovable-block TableBrickWedgeBall Hand

Alok Mehta - Programming in Lisp - Lecture 6 19 Class Definitions  Basic-Block > (defclass basic-block() ((name :accessor block-name :initarg :name) (width :accessor block-width :initarg :width) (height :accessor block-height :initarg :height) (position :accessor block-position :initarg :position) (supported-by :accessor block-supported-by :initform nil))) Supported-by => What the block is supported by (what's underneath it)  Movable-block > (defclass movable-block (basic-block) ())  Load-bearing-block  Has new field > (defclass load-bearing-block (basic-block) ((support-for :accessor block-support-for :initform nil))) Support-for => What the block is a support for (what's on top of it)

Alok Mehta - Programming in Lisp - Lecture 6 20 Class Definitions (cont.)  Brick, wedge, ball, table > (defclass brick (movable-block load-bearing-block) ()) > (defclass wedge (movable-block) ()) > (defclass ball (movable-block) ())  Table > (defclass table (load-bearing-block) ())  Robotic Hand > (defclass hand () ((name :accessor hand-name :initarg :name) (position :accessor hand-position :initarg :position) (grasping :accessor hand-grasping :initform nil)))

Alok Mehta - Programming in Lisp - Lecture 6 21 Creating Blocks in Blocks World (defvar *blocks* (list (make-instance 'table :name 'table :width 20 :height 0 :position '(0 0)) (make-instance 'brick :name 'b1 :width 2 :height 2 :position '(0 0)) (make-instance 'brick :name 'b2 :width 2 :height 2 :position '(2 0)) (make-instance 'brick :name 'b3 :width 4 :height 4 :position '(4 0)) (make-instance 'brick :name 'b4 :width 2 :height 2 :position '(8 0)) (make-instance 'wedge :name 'w5 :width 2 :height 4 :position '(10 0)) (make-instance 'brick :name 'b6 :width 4 :height 2 :position '(12 0)) (make-instance 'wedge :name 'w7 :width 2 :height 2 :position '(16 0)) (make-instance 'ball :name 'L8 :width 2 :height 2 :position '(18 0)) )) (defvar *hand* (make-instance 'hand :name '*hand* :position '(0 6))) Table B1B4B2 B3 W7 W5 B6 L8 

Alok Mehta - Programming in Lisp - Lecture 6 22 Initializing Blocks World  Create other global variables for convenience > (dolist (l *blocks*) (set (block-name l) l)) This sets the global variables TABLE, B1, B2, … W7, L8 to their respective block  All blocks rest on the table initially > (dolist (l (rest *blocks*)) ; For each, except table (setf (block-supported-by l) table) (push l (block-support-for table)))  Load-bearing-block has a method block-support-for  This was automatically generated  Create a dummy stub for this method in basic-block > (defmethod block-support-for ((object basic-block)) nil) By default, basic-blocks do not have anything on top of them. Only Load-bearing- blocks may have something on top of them.

Alok Mehta - Programming in Lisp - Lecture 6 23 Block-Support-For Basic-block Block-Support-For Load-bearing-block Block-Support-For Movable-block TableBrickWedgeBall Hand Returns Nil Returns value of Slot Support-For

Alok Mehta - Programming in Lisp - Lecture 6 24 Put-On  Want a method, PUT-ON, that places one object on another  Get space for the object (may have to move things around)  Grasp object (may have to remove things on top of the object)  Move object  Ungrasp object  Basic Prototype > (defmethod put-on ((object movable-block) (support basic-block)) … )

Alok Mehta - Programming in Lisp - Lecture 6 25 Put-On  Implementation > (defmethod put-on ((object movable-block) (support basic-block)) (if (get-space object support) (and (grasp object) (move object support) (ungrasp object)) (format t "~&Couldn't get space to put ~a on ~a." (block-name object) (block-name support))))  get-space, grasp, move, ungrasp have yet to be defined  Get-space either finds space or makes space > (defmethod get-space ((object movable-block) (support basic-block)) (or (find-space object support) (make-space object support)))  find-space, make-space have yet to be defined (do this later)

Alok Mehta - Programming in Lisp - Lecture 6 26 Grasp  Grasp  Puts desired object in robot's hand > (defmethod grasp ((object movable-block)) (unless (eq (hand-grasping *hand*) object) ; already holding? ; Make sure nothing else is on top of object (when (block-support-for object) (clear-top object)) ; Make sure robot isn't holding anything else (when (hand-grasping *hand*) (get-rid-of (hand-grasping *hand*))) (format t "~&Move hand to pick up ~a at location ~a." (block-name object) (top-location object)) (setf (hand-position *hand*) (top-location object)) (format t "~&Grasp ~a." (block-name object)) (setf (hand-grasping *hand*) object)) t)  Need to define: Clear-top, top-location  Function returns T if successful

Alok Mehta - Programming in Lisp - Lecture 6 27 Ungrasp  Ungrasp  Releases object, if object is being supported by something else > (defmethod ungrasp ((object movable-block)) (when (block-supported-by object) (format t "~&Ungrasping ~a" (block-name object)) (setf (hand-grasping *hand*) nil) T))  Get-rid-of  puts object on the table (out of the way) > (defmethod get-rid-of ((object movable-block)) (put-on object table))

Alok Mehta - Programming in Lisp - Lecture 6 28 Make-space  Clear-top  Gets rid of everything on top of an object > (defmethod clear-top ((support load-bearing-block)) (dolist (obstacle (block-support-for support) t) (get-rid-of obstacle)))  Make-space  Clears away just enough room to put block –Algorithm keeps getting rid of things on top of block –Until the find-space function returns that there is enough space available > (defmethod make-space ((object movable-block) (support basic-block)) (dolist (obstruction (block-support-for support)) (get-rid-of obstruction) (let ((space (find-space object support))) (when space (return space)))))

Alok Mehta - Programming in Lisp - Lecture 6 29 Move  Moves an object on top of a support > (defmethod move ((object movable-block) (support basic-block)) (remove-support object) (let ((newplace (get-space object support))) (format t "~&Move ~a to top of ~a at location ~a." (block-name object) (block-name support) newplace) (setf (block-position object) newplace) (setf (hand-position *hand*) (top-location object))) (add-support object support) t)  Calls remove-support, add-support –These are methods for bookkeeping –They maintain the bi-directional links (support-for, supported-by) –Remove-support removes the bi-directional links of an object –Add-support adds bi-directional links for an object that is to be placed on top of a support

Alok Mehta - Programming in Lisp - Lecture 6 30 Remove-support  Remove-support  This is a bookkeeping method that removes bi-directional links > (defmethod remove-support ((object movable-block)) (let ((support (block-supported-by object))) (when support (setf (block-support-for support) (remove object (block-support-for support))) (setf (block-supported-by object) nil) t)))

Alok Mehta - Programming in Lisp - Lecture 6 31 Add-support  Adding support, for general case  This is just a stub (does no operation)  Can't really put an object on top of any basic-block > (defmethod add-support ((object movable-block) (support basic-block)) t)  Adding support, for specific cases  For load-bearing blocks, we can put an object on top  Bookkeeping needed for this is to update bi-directional pointers > (defmethod add-support ((object movable-block) (support load-bearing-block)) (push object (block-support-for support)) (setf (block-supported-by object support)))

Alok Mehta - Programming in Lisp - Lecture 6 32 Top-location  Returns the location of the top (center) of a block  Example Usage > (block-position b3) (4 0) <-- Lower left position of block > (block-width b3) 4 > (block-height b3) 4 > (top-location b3) (6 4) <-- X = Pos.X + (width/2.0); Y = Pos.Y  Implementation > (defmethod top-location ((object basic-block)) (list (+ (first (block-position object)) (/ (block-width object) 2)) (+ (second (block-position object)) (block-height object))))

Alok Mehta - Programming in Lisp - Lecture 6 33 Find-space  Find-space  Basic algorithm –For each possible location on top of support that the object can be placed, – Is the position occupied? If not, we've found the space; else continue > (defmethod find-space ((object basic-block) (support basic-block)) (dotimes (offset (+ 1 (- (block-width support) (block-width object)))) (unless (intersections-p object offset (first (block-position support)) (block-support-for support)) (return (list (+ offset (first (block-position support))) (+ (second (block-position support)) (block-height support)))))))  Intersections-p is to be defined next B6 B4 B6 B4

Alok Mehta - Programming in Lisp - Lecture 6 34 Intersections-p  Intersections-p  Checks for intersections (only checks the X dimension) > (defun intersections-p (object offset base obstacles) (dolist (obstacle obstacles) (let* ((ls_proposed (+ offset base)) (rs_proposed (+ ls_proposed (block-width object))) (ls_obstacle (first (block-position obstacle))) (rs_obstacle (+ ls_obstacle (block-width obstacle)))) (unless (or (>= ls_proposed rs_obstacle) (<= rs_Proposed ls_obstacle)) (return t))))) Obstacle Proposed Object Location

Alok Mehta - Programming in Lisp - Lecture 6 35 Blocks World Usage  Sample usage of blocks world > (put-on b4 b1) > (put-on w7 b2) > (put-on b1 b2)

Alok Mehta - Programming in Lisp - Lecture 6 36 Final Exam  Next time  Open book, open notes  No calculators, computers, etc.  No lisp interpreter!  Mostly programming type questions  Write a program to …  Exam may contain material from  Chapters 1-12, plus case studies: Expert Systems, Blocks World  Anything from lecture notes