1 / 27

ICS 313: Programming Language Theory

ICS 313: Programming Language Theory. Module 11: CLOS. CLOS. Objectives: Understand basic principles of CLOS Understand similarities and differences between CLOS and other OO systems. Elements of CLOS: Classes, Instances and Slots. Classes: Common Lisp types Instances of a class:

duncan
Download Presentation

ICS 313: Programming Language Theory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 313: Programming Language Theory Module 11: CLOS

  2. CLOS • Objectives: • Understand basic principles of CLOS • Understand similarities and differences between CLOS and other OO systems.

  3. Elements of CLOS: Classes, Instances and Slots • Classes: • Common Lisp types • Instances of a class: • share structure, behavior, and type • Slots: • Determine the structure of a class • Have a name and a value • Can be read and written by slot accessors • Local slots maintain a separate value for each instance • Shared slots maintain a common (class-wide) value

  4. Elements of CLOS: Superclasses • Allow classes to be built from other classes • New class inherits structure and behavior from superclasses • Classes can inherit structure and behavior from multiple superlasses.

  5. Generic functions • Syntactically identical to normal functions • Semantic difference I: • Normal functions specify interface and implementation • Generic functions specify only interface: implementation is distributed a set of methods. • Semantic difference II: • Implementation of a normal function does not vary from call to call. • Implementation of a generic function varies depending upon the class of its arguments. • Behavior of a generic function involves both selection and composition of methods.

  6. Elements of CLOS: Methods • The underlying implementation of generic functions • Methods are similar to ordinary Lisp functions, but are not called directly. • Method “role” determines its part in generic fn: • Primary methods perform bulk of work. • Only one primary method is called each time a generic function is called. • Provides the return value of the generic fn. • Before methods called before the primary • After methods called after the primary • Around methods “sandwich” the primary

  7. Elements of CLOS: Inheritance • Inheritance is the sharing of characteristics and behavior among a set of classes. • Both slots and methods can be shared among classes. • Multiple inheritance creates problem of differently defined methods or slots with the same name in different ancestors. • CLOS computes a class precedence list to determine which method to inherit: • A class always has precedence over its superclasses • The order in which classes are listed in a class definition sets their precedence

  8. Example class hierarchy window window- window- with-label with-border primary method: refresh window Code to Clear Window Generic function after method: refresh Code to window- refresh draw border with-border after method: refresh Code to window- draw label with-label

  9. Example refresh invocation Instance of argument window-with-label refresh implementation side effects value Implementation: primary method: refresh Code to window Clear Window after method: refresh Code to window- draw label with-label

  10. Method combination • The process of determining the implementation associated with a generic function for a given set of arguments is called generic dispatch: • Find all applicable methods • Sort them by order of precedence • Call one or more of them • Method ordering: • All before methods in most-specific-first order • The most specific primary method • All after methods in most-specific-last order

  11. Example: window-with-border instance • before method for window-with-border • before method for window • most specific primary method • after method for window • after method for window-with-border

  12. Slot access and method combination • Slot accessors are simply primary methods: • (defclass triangle (shape) ((side-a :accessor side-a) (side-b :accessor side-b) (side-c :accessor side-c) (area :reader area))) • We can update the area slot automatically whenever a side changes value by defining an :after method: • (defmethod (setf side-a) :after (new-length (tri triangle)) (setf (area tri) <new area>))

  13. Multi-methods • Methods can be written to specialize on multiple parameters. • Multi-methods allow methods to “belong” to more than one class simultaneously. basic- basic- product OS Life Adventure unix windows (defmethod install ((sw basic-product) (os basic-OS)) (restore-product sw os) (compile-product sw os) (configure-site sw os) (verify-product sw os))

  14. Meta-object protocol • Used to define default model of OO above. • Allows implementation of alternative paradigms • Specifies: • What a subclass inherits from its superclasses • How a subclass determines the precedence of superclasses • How generic functions dispatch • Classes are themselves instances of a meta-class • Meta-classes specify the structure and behavior of classes • Meta-classes inherit structure and behavior from their super meta-classes.

  15. CLOS vs. Java • Java does not support generic functions • Java methods analogous to CLOS methods • Java does not explicitly support member function composition • Java does not support multi-methods • Methods owned by a single class • Java does not provide a meta-object protocol • Inheritance and dispatch procedure hardwired. • Java provides access control (public, private..)

  16. CLOS Pizza Party • Objectives: • Understand the basic mechanisms and syntax of CLOS.

  17. Create a pizza • ;;; (editor) • (defclass pizza () ((size :initarg :size • :initform 12 • :accessor pizza-size) • (topping :initarg :topping • :initform nil • :accessor pizza-topping)) • (:documentation "A pizza"))

  18. Try it out • ;;; (listener) • USER(3): (make-instance 'pizza) • #<PIZZA @ #xd22eae> • USER(4):

  19. Improve printing • ;;; (editor) • (defmethod print-object ((pizza-instance pizza) stream) • "Printing procedure for pizza instances." • (format stream "~d inch pizza with toppings: ~s" • (pizza-size pizza-instance) • (pizza-topping pizza-instance)) • pizza-instance)

  20. Example with printing • ;;; (listener) • USER(4): (make-instance 'pizza) • 12 inch pizza with toppings: NIL

  21. A Mistake • ;;; (listener) • USER(5): (make-instance 'pizza :toppings '(anchovies peppers)) • Error: :TOPPINGS are invalid initargs to make-instance of class • #<STANDARD-CLASS PIZZA @ #xd20a9e>. The valid initargs are :SIZE :TOPPING. • [condition type: PROGRAM-ERROR] • Restart actions (select using :continue): • 0: Ignore • [1c] USER(6): :reset

  22. Right way • ;;; (listener) • USER(7): (make-instance 'pizza • :topping '(anchovies peppers)) • 12 inch pizza with toppings: (ANCHOVIES PEPPERS)

  23. Define subclass • ;;; (editor) • (defclass calzone (pizza) • ((crust :initarg :crust • :initform 'light • :accessor calzone-crust)) • (:documentation "A kind of pizza"))

  24. Define printed representatin • ;;; (editor) • (defmethod print-object :after • ((calzone-instance calzone) stream) • "Printing procedure for calzone instances." • (format stream " and a ~s crust." • (calzone-crust calzone-instance)) • calzone-instance)

  25. Try it out • ;;; (listener) • USER(5): (make-instance 'calzone) • 12 inch pizza with toppings: NIL and a LIGHT crust. • USER(6): (setf my-calzone (make-instance 'calzone)) • 12 inch pizza with toppings: NIL and a LIGHT crust. • USER(7): (setf (calzone-crust my-calzone) 'dark) • DARK • USER(8): my-calzone • 12 inch pizza with toppings: NIL and a DARK crust.

  26. Extra Credit Homework • Modify your previous homework assignment to CLOS. • In other words, create a class called “bank” and methods that allow deposits and withdrawals by individual users. • Due on Friday at start of midterm (with other homework).

  27. End of module

More Related