Reflective Programming and Open Implementations
Total Page:16
File Type:pdf, Size:1020Kb
Reflective Programming and Open Implementations Dr. Stéphane Ducasse [email protected] http://www.iam.unibe.ch/~ducasse/ University of Bern 2000/2001 i. Table of Contents 1. Reflective Programming and Open Implementations 1 Class as Objects 34 Goal of this Lecture 2 Some Class Properties 35 Outline of the Lecture 3 Some Method based Properties 36 What we could have made... 4 Metaclass Responsibilities 37 History,Concepts,Definitions and Examples 5 Outline 38 Why Do We Need Reflective Programming? 6 Why ObjVlisp? 39 Why Do We Need Reflective Programming? 7 The Loops Approach 40 Traditional vs Reflective Answers 8 The Smalltalk Pragmatical Approach 41 Role ot Reflective Prog in Software Engineering 9 ObjVlisp in 5 Postulates (i) 42 Definitions (I) 10 How to Stop Infinite Recursion? 43 Consequences 11 ObjVlisp in 5 Postulates (ii) 44 Meta Programming in Programming Language Context 12 Unification between Classes and Instances 45 Three Approaches 13 About the 6th ObjVlisp’s Postulate 46 Infinite Tower of (Meta)Interpreters 14 Instance Structure: Instance Variables 47 Reflective Languages 15 Instance Behavior: Methods 48 Open Implementation and MOPs 16 Minimal Structure of an Object 49 The Basic Claim of Open Implementation 17 Class as an Object: Structure 50 Meta Object Protocols 18 The class Class: a Reflective class 52 Meta Programming in CLOS 19 A Complete Example 53 Infinite Tower vs Open Implementation 20 Outline 54 A Simple Application as Example 21 Message Passing (i) 55 Programming in Explicit Metaclass Context 22 Message Passing (ii) 56 Reusing Meta Programs 23 Object Creation by Example 57 MetaProgramming in OO Context 24 Object Creation: the Method new 58 MetaProgramming by Example 25 Object Allocation 59 Costs of Reflective Programming 26 Object Initialization 60 Designing Reflective Systems 27 Object Creation: the Metaclass Role 61 Meta-Problems 28 Class Creation 62 Meta and Open are not Limited to Programming Languages 29 A Simple Instantiation Graph 63 2. The Study of a Minimal Object-Oriented Reflective Kernel 30 What is the minimal behavior shared by all the objects? 64 Goals of this Lecture 31 Outline 65 Outline 32 Two Forms of Inheritance 66 Recall: Meta Programming in Programming Language Context 33 Dynamic Method Inheritance 67 Table of Contents ii. A Simple Inheritance Graph 68 Derived Metaclasses in SOM 103 Method Lookup Example (i) 69 Limit of Derived Metaclass 104 Method Lookup Example (ii) 70 Class Property Propagation in SOM 105 Semantics of super 71 Smalltalk Approach 106 Let us be Absurb! 72 Deep into it: Smalltalk Metaclasses in 7 points 107 A Simple Uniform Kernel 73 Smalltalk Metaclasses in 7 points (iii) 109 Class initialization: a Two Steps Process 74 Smalltalk Metaclasses in 7 points (iv) 110 Recap: Class class 75 Responsibilities 111 Recap: Object class 76 Class Property Propagation in Smalltalk 112 Outline 77 NeoClasstalk: Compatibility Model 113 Bootstrapping the Kernel 78 Compatibility? Yes! 114 Abstract Classes 79 Refactoring the Smalltalk Boolean Hierarchy 115 Abstract 80 The Complete Architecture 116 Abstract Class and Method Lookup 81 Class Property Composition 117 3. About Metaclass Composition 82 An Example of Class Property Composition 118 On the Road 83 Extending the Smalltalk Kernel with the Compatibility Model 119 Recap: A Simple Uniform Kernel 84 NeoClasstalk Programmers 120 Recap: Method Lookup 85 Summary 121 Sets 86 If You Really Want to Know More 122 The Metaclass Set 87 4. Open Implementation: the CLOS MOP Example 123 Sharing Metaclasses 88 Goals of this Lecture 124 Zooming in: Creation of Memo-Point (i) 89 CLOS 125 Zooming in: Creation of Memo-Point (ii) 90 CLOS in a nutshell 126 On the Road 91 Class Definition 127 Problems with Explicit Metaclass Programming 92 Instance Creation 128 Interlevel Calls -> Interlevel Coupling 93 Encapsulation and Attribute Accesses 129 Relevance of The Problem 94 Inheritance 130 Upward Compatibility 95 Multiple Inheritance Conflict Resolution 131 Upward Compatibility Definition 96 Generic Function 132 Downward Compatibility 97 Method Definition (i) 133 Downward Compatibility Definition 98 (Method) Generic Function Application 134 On the Road 99 Method Selection 135 ObjVlisp 100 Why CLOS MOP? 136 CLOS 101 Meta Programming in CLOS 137 SOM 102 CLOS was too big! 138 Table of Contents iii. 5 MetaObjects 139 Controling What Exactly! 176 Static Elements 140 A Limited Survey 177 Structure Protocols (i) 141 CLOS Example (i) 178 Structure Protocols (ii) 142 CLOS Example (ii) 179 Extension Example 143 CLOS Example (iii) 180 Dynamic Elements 144 A Coverage Tool in Smalltalk 181 Class Definition: Defclass 145 Smalltalk: Do It Yourself Syndrome 183 Instance creation 146 Smalltalk Basic Reflective Tools 184 Method Creation: Defmethod (i) 147 6 Techniques 185 Defmethod (ii) 148 Unknown Messages 186 Method lookup and apply protocol 149 Creating a MinimalObject 187 Apply Protocol Example 150 Wrapping anObject 188 Slot Access Protocol 152 Evaluation 189 Finalize Inheritance 153 Method Wrappers: an Example 190 Open Implementation and Reflective Languages 154 Method Wrappers 191 5. Open Implementation Design Issues 155 Control 192 Goals of this Lecture 156 MethodWrapper Optimization 193 Locality in MOP Design 157 MW method body 194 Open Implementation Design Guidelines 158 Quality in interface designs 159 Installation 195 Set Module: Design A 160 MW Evaluation 196 Set Module: Design B 161 Exploiting VM Lookup Algorithm 197 First Guideline 162 Let’s view it 198 Second Guideline 163 Interceptor: Anonymous Classes 199 Third Guideline 164 Let us think a bit 200 Subject Matter 165 Essential Methods 201 Fourth Guideline 166 Naive Control Implementation (i) 202 Design D 167 Naive Control Implementation (ii) 203 Last Guideline: Layered Interfaces 168 Naive Control Implementation (iii) 204 6. Comparing Reflection in CLOS, Smalltalk and Java 169 Possible Optimization 205 Sorry but this is your work! 170 Evaluation 206 Some Criterias 171 Why A Mop for Smalltalk is Needed? 207 7. Implementing Message Passing Control in Smalltalk: an Analysis 173 Pratice! 208 Outline 174 Selected Bibliography 209 Why Controling Message? 175 Web pages 213 Reflective Programming 1. 1. Reflective Programming and Open Implementations Dr. Stéphane Ducasse Software Composition Group University of Bern Switzerland Winter Semester 2000-2001 Email: [email protected] Url: http://www.iam.unibe.ch/~ducasse/ © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 2. Goal of this Lecture You will learn about ❑ Open Implementations ❑ Reflection: Intercession and Introspection ❑ Reflective Architectures and Kernels (SOM, Smalltalk, CLOS) ❑ Meta Object Protocol: Powering End-Users ❑ Metaclasses ❑ Message Passing Control Side Effects ❑ Program with a reflective system ❑ Let you implement your own micro kernel ❑ Deeply understanding OO ❑ Experiment with different OO models © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 3. Outline of the Lecture ❑ (C) Introduction, Concepts, Definitions, Examples, Meta Object Protocol, Open Implementations ❑ (C) The Study of an Object-Oriented Reflective Kernel (ObjVLisp) ❑ (Lab) ObjVLisp Implementation (1) ❑ (Lab) ObjVLisp Implementation (2) ❑ (C) Metaclass Composition Issues ❑ (Lab) Metaclass Programming with ObjVlisp ❑ ❑ (C) Analysing CLOS and its MOP ❑ You : (C) Reflection in OO Languages (Clos, Smalltalk, Java Comparison) ❑ (Lab) Interface Browser ❑ (C) Message Passing Control in Smalltalk ❑ (Lab) Implementing Actalk ❑ You : (C) Presentation of papers ❑ (Lab) Scaffolding Patterns © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 4. What we could have made... ❑ MetaCircularity and Infinite Tower: Lisp in Lisp ❑ Different reflective paradigm (relational, actors...) ☞ We will focus on OO reflective programming © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 5. History,Concepts,Definitions and Examples © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 6. Why Do We Need Reflective Programming? >Does anyone know why CLOS does not provide a copy protocol? >Has anybody implemented an inheritance method “a la Eiffel”? >We need a method dispatch that take into account an external context of execution? [Tutorial MOP & OI OOPSLA’93] Some problems: ❑ data structure allocation, optimization ❑ control of language entities (feedback, trace, analysis...) ❑ UI and API definition ❑ language semantics In summary ❑ Optimization ❑ Language extensions (control, debugging) ❑ Semantics change © Dr. Ducasse Stéphane -Universität Bern Reflective Programming and Open Implementations Reflective Programming 7. Why Do We Need Reflective Programming? “As a programming language becomes higher and higher level, its implementation in terms of underlying machine involves more and more tradeoffs, on the part of the implementor, about what cases to optimize at the expense of waht other cases.... the ability to cleanly integrate something outside of the language’s scope becomes more and more limited” [Kiczales’92a] ❑ Why instances do have to have the same internal representation? – for Point => maximum speed needed, all instance variables used ☞ array like representation – for Person => minimize space, few instances used ☞ hash-table like representation ❑ Why can’t I control internal representation