Reflective Metaprogramming
Total Page:16
File Type:pdf, Size:1020Kb
Reflective Metaprogramming by Weiyu Miao BS, East China Univ. of Sci. and Tech., Shanghai, China, 2005 MS, East China Univ. of Sci. and Tech., Shanghai, China, 2008 A thesis submitted to the Faculty of the Graduate School of the University of Colorado in partial fulfillment of the requirements for the degree of Doctor of Philosophy Department of Electrical, Computer, and Energy Engineering 2013 This thesis entitled: Reflective Metaprogramming written by Weiyu Miao has been approved for the Department of Electrical, Computer, and Energy Engineering Prof. Jeremy Siek Prof. Bor-Yuh Evan Chang Prof. Jaakko J¨arvi Prof. Sriram Sankaranarayanan Prof. Fabio Somenzi Date The final copy of this thesis has been examined by the signatories, and we find that both the content and the form meet acceptable presentation standards of scholarly work in the above mentioned discipline. iii Miao, Weiyu (Ph.D., ECEE) Reflective Metaprogramming Thesis directed by Prof. Jeremy Siek Reflective Metaprogramming is capable of inspecting code, enables us to write templates, and can automatically instantiate templates according to the meta information obtained by reflection. Reflective metaprogramming supports the writing of generic and reusable software components. However, there are the challenges of designing a reflective metaprogramming language. One chal- lenge is the design of the type system: in a language with type-reflective metaprogramming, type expressions in the residual program may be the result of meta computation, making the type sys- tem difficult to guarantee that the residual code in a metaprogram is statically type safe without sacrificing language expressiveness. Another challenge is the deign of reflective metaprogramming language. Some previous work proposed pattern-based reflection for inspecting and generating class members, but with limited ways that generated declarations can be composed. In this thesis, we present incremental type-checking for reflective metaprograms. It detects errors earlier than C++ templates without sacrificing flexibility: we incrementally type check code fragments as they are created and spliced together during meta computation. We use type variables to represent type expressions that are not yet normalized and a new dynamic variation on existential types to represent residual code fragments. A type error in a code fragment is treated as a run-time error of the meta computation. In this thesis, we present pattern-based traits, a language design, embodied in PTFJ (Pattern- based Traits for Featherweight Java), that combines pattern-based reflection with the expressiveness of trait composition. Pattern-based traits can group, name, and manipulate sets of member declara- tions. Parameterizing traits over such sets can further increase the expressiveness of pattern-based reflection. Dedication To my parents and my wife, for all of the support you provided. I could not have done it without you. v Acknowledgements First, I would like to thank my parents, who give me full support to study for my PhD program in the United States. My parents always care about my study and my research. They encouraged me a lot when I was in frustration. Thank my wife, Wenwen Xiang for her support. I studied at University of Colorado at Boulder and she lives at Davis, California. I usually went back to California once every month. I understand that it is difficult for her to take care of our daughter alone, but she is very strong and never complains. Thank my daughter, Cady Miao, who gives more happiness to our family. I am grateful to my advisor, Professor Jeremy Siek, for his support. Professor Siek gave me a lot of freedom in doing research. I used to have some frustration in my research work, but Professor Siek always supported me and helped me overcome the difficulties. Professor Siek gave me many suggestions and advice on how to write papers and do presentations. Thank all of my PhD thesis committee members: Professor Siek, Professor Chang, Professor Sankaranarayanan, Professor Somenzi, and Professor J¨arvi. They were busy working on their academic papers and doing research, but they were able to sequence time reading my thesis and attending my thesis defense. Last but not least, thank Thomas Nelson and Lou Ordorica for editing my thesis. I am not a native English speaker and inevitably, there was many English grammar mistakes in this thesis. Thomas and Lou helped me correct every English grammar mistakes, pointed out many misuse of words, and adjusted some sentence structures in this thesis. vi Contents Chapter 1 Introduction 1 1.1 Reflective Metaprogramming . 2 1.1.1 C++ Templates . 2 1.1.2 Language Features for Code Reuse . 3 1.2 Reflective Metaprogramming in OO Programming Languages . 4 1.2.1 Class Composition . 5 1.2.2 Class Reflection . 5 1.3 Challenges of Implementing Reflective Metaprogramming . 6 1.4 Outline of Contributions . 7 1.5 Dissertation Road Map . 8 2 Type Reflective Metaprogramming 9 2.1 Multi-Staged Programming . 9 2.2 Garcia's Work on Reflective Metaprogramming . 11 2.2.1 Garcia's Calculus . 11 2.2.2 Type Manipulation Example . 12 2.3 Type-Checking Challenges . 13 2.3.1 Modular Type-Checking . 14 2.3.2 Two-Staged Type-Checking . 14 2.4 Summary . 17 vii 3 Incremental Type-Checking 18 3.1 General Concept of Incremental Type-Checking . 18 3.2 Formalization . 23 3.2.1 Syntax . 23 3.2.2 Non-parametric Existential Types . 25 3.2.3 Typing Rules . 29 3.2.4 Meta-evaluation Rules . 35 3.2.5 Soundness . 40 3.3 Implementation . 41 3.3.1 Surface Language . 41 3.3.2 Kernel Language . 42 3.3.3 Language Translation and Type-checking . 45 3.3.4 Meta-evaluation . 52 3.4 Summary . 58 4 Traits 61 4.1 Limitation of Inheritance-Based Code Reuse . 61 4.2 Traits: A Unit of Behaviors . 62 4.2.1 Trait Syntax . 62 4.2.2 A Trait Example . 63 4.2.3 Traits for Metaprogramming . 67 4.3 Summary . 69 5 Pattern-Based Traits 70 5.1 Pattern-Based Traits Syntax . 71 5.1.1 Traits in PTJ . 71 5.1.2 Patterns . 72 5.1.3 Groups . 75 viii 5.1.4 Ranges . 76 5.1.5 Interface Satisfaction Assertion . 78 5.2 Member Access Control . 80 5.2.1 Group Member Accessibility . 80 5.2.2 Modifier Patterns . 83 5.3 Type-Checking of Pattern-based Traits . 84 5.3.1 Static Type Safety vs. Language Expressiveness . 84 5.3.2 Nominal Typing and Structural Typing . 86 5.3.3 Scope-based Type Lookup . 87 5.4 Formalization . 88 5.4.1 FJ Syntax . 88 5.4.2 PTFJ syntax . 89 5.4.3 Typing Rules . 91 5.4.4 Meta-evaluation . 103 5.4.5 Soundness . 105 5.5 Summary . 108 6 Statement-Level Compile-Time Reflection and Metaprogramming 110 6.1 Runtime Reflection in Java . 110 6.2 An Introductory Example . 112 6.3 Language Features . 114 6.3.1 Pattern-Based Reflection . 114 6.3.2 Reified Generics . 118 6.3.3 Member Accessilbility . 120 6.4 Calculus for the Object Language . 122 6.5 Calculus for the Meta Language . 126 6.5.1 Kernel Syntax of the Meta Language . 126 ix 6.5.2 Type System . 128 6.5.3 Meta-evaluation . 134 6.5.4 Soundness . 134 6.6 Summary . 136 7 PtjORM: Object-Relational Mapping Library 137 7.1 Object-Relational Mapping . 138 7.1.1 Property Mapping . 139 7.1.2 Association Mapping . 141 7.1.3 Inheritance Mapping . 141 7.2 Object-Relational Mapping Configuration . 142 7.3 Reflective Metaprogramming for PtjORM . 145 7.3.1 PtjORM: Built of Traits . 145 7.3.2 PtjORM: Compile-Time Reflection . 147 7.4 Evaluation . 149 7.4.1 Object Saving Evaluation . ..