Fully-Parameterized, First-Class Modules with Hygienic Macros
Total Page:16
File Type:pdf, Size:1020Kb
Fully-parameterized, First-class Modules with Hygienic Macros Dissertation der Fakult¨at fur¨ Informations- und Kognitionswissenschaften der Eberhard-Karls-Universit¨at Tubingen¨ zur Erlangung des Grades eines Doktors der Naturwissenschaften (Dr. rer. nat.) vorgelegt von Dipl.-Inform. Josef Martin Gasbichler aus Biberach/Riß Tubingen¨ 2006 Tag der mundlichen¨ Qualifikation: 15. 02. 2006 Dekan: Prof. Dr. Michael Diehl 1. Berichterstatter: Prof. Dr. Herbert Klaeren 2. Berichterstatter: Prof. Dr. Peter Thiemann (Universit¨at Freiburg) Abstract It is possible to define a formal semantics for configuration, elaboration, linking, and evaluation of fully-parameterized first-class modules with hygienic macros, independent compilation, and code sharing. This dissertation defines such a semantics making use of explicit substitution to formalize hygienic expansion and linking. In the module system, interfaces define the static semantics of modules and include the definitions of exported macros. This enables full parameterization and independent compilation of modules even in the presence of macros. Thus modules are truly exchangeable components of the program. The basis for the module system is an operational semantics for hygienic macro expansion—computational macros as well as rewriting-based macros. The macro semantics provides deep insight into the nature of hygienic macro expansion through the use of explicit substitutions instead of conventional renaming techniques. The semantics also includes the formal description of Macro Scheme, the meta-language used for evaluating computational macros. Zusammenfassung Es ist m¨oglich, eine formale Semantik anzugeben, welche die Phasen Konfiguration, syntak- tische Analyse mit Makroexpansion, Linken und Auswertung fur¨ ein vollparametrisiertes Mo- dulsystem mit Modulen als Werten erster Klasse, unabh¨angiger Ubersetzung¨ und Code-Sharing beschreibt. Diese Dissertation beschreibt eine solche Semantik. Dabei formalisieren explizite Substitutionen die hygienische Makroexpansion und das Linken. Im Modulsystem beschreiben Schnittstellen die statische Semantik von Modulen und enthalten die Definitionen der exportierten Makros. Dies erm¨oglicht volle Parametrisierung und unabh¨angige Ubersetzung¨ sogar in Kombi- nation mit Makros. Module sind damit echte austauschbare Komponenten eines Programms. Die Grundlage fur¨ das Modulsystem bildet eine operationelle Semantik fur¨ hygienische Makroexpan- sion die berechnende Makros ebenso beschreibt wie regelbasierte Makros. Durch die Verwendung expliziter Substitutionen anstelle konventioneller Umbenennung gibt die Semantik fur¨ Makro- expansion tiefe Einblicke in das Wesen hygienischer Makroexpansion. Die Semantik beschreibt außerdem Makro Scheme, die Metasprache fur¨ die berechnenden Makros. Contents 1 Introduction 1 1.1 Combining Modules and Macros . 2 1.2 Representation of Identifiers . 4 1.3 Explicit Substitutions . 5 1.4 Structure of the Dissertation . 6 2 Fully-Parameterized Module Systems 7 2.1 Fully-Parameterized Module Systems and Scheme . 7 2.2 The Programmer’s Point of View . 8 2.3 Terminology . 13 2.4 Phase Overview . 15 2.5 The Missing Link Revisited . 17 2.6 Transformation with Code Sharing . 20 2.7 Configuration Language . 20 2.8 Semantics of the Configuration Language . 21 3 A Semantics for Hygienic Macros 25 3.1 Hygienic Macros . 25 3.2 The λv Calculus . 29 n, 3.3 The λv Calculus . 31 3.4 Parsing$% Scheme Without Macros . 39 3.5 The Core Macro Expander . 44 3.5.1 Time Complexity of the Macro Expander . 53 3.6 Computational Macro Transformers . 53 3.6.1 The Semantics of Macro Scheme . 57 3.6.2 Additional Primitives . 63 3.6.3 Expanding Macro Applications Using es-transformer . 66 3.7 Parsing Scheme with Macros . 72 3.8 Parsing and Macro Expansion for Macro Scheme . 75 3.8.1 Scoping Issues Between Object And Meta-Language . 76 3.9 Semantics of syntax-rules ............................... 79 3.9.1 Macro Expansion for syntax-rules ...................... 81 3.9.2 Elimination for syntax-rules Forms . 83 3.9.3 Elimination Rules for Patterns . 89 3.9.4 Parsing syntax-rules .............................. 91 3.10 Future Work Towards Full Scheme . 92 3.11 Comparison with the Work of Bove and Arbilla . 94 4 Semantics for Modules 97 4.1 Identifier Representation and Linking for Modules . 97 Module 4.2 The Λn Calculus . 104 4.2.1 Abstract Syntax . 105 i ii CONTENTS 4.2.2 Evaluation of Programs . 107 4.2.3 Evaluation of Module Expressions . 110 4.3 Parsing and Importing for Modules and Interfaces . 113 4.4 Macro Expansion for Interfaces and Modules . 120 4.5 Independent Compilation and Code Sharing . 125 4.6 Future Work . 126 5 Implementation 129 5.1 Configuration Phase . 129 5.1.1 The Backend for Scheme 48 . 130 5.1.2 The PLT Backend . 130 5.1.3 Configuration Language . 131 5.2 Implementation of the Rewriting Systems . 131 Module 5.2.1 Rewriting System for Λn and Macro Scheme . 135 5.2.2 A Short Review of Using PLT redex . 135 5.3 Direct Implementation of the Macro Expander . 136 5.3.1 Implementation of Macro Scheme . 138 5.4 Generation of LATEX Output . 139 6 Related Work 143 6.1 First-Class Macros . 143 6.2 Fully-Parameterized Module Systems . 144 6.3 Macro Expansion Algorithms . 145 6.4 Module Systems with Macros . 147 6.5 Meta-Languages for Macros . 147 7 Conclusions 149 7.1 Review . 149 7.2 Insights Gained from the Macro Expansion Semantics . 149 7.3 Future Work . 150 7.4 Closing Words . 151 A Notation 153 List of Figures 2.1 Overview of phases and entities . 16 2.2 The configuration language . 21 2.3 Semantics of the configuration language . 23 2.4 Partial order among definition clauses . 24 3.1 The language Λ . 29 3.2 Abstract syntax of Λn .................................. 32 3.3 Elimination of ..................................... 33 3.4 Concrete Syntax$% based on s-expressions . 39 3.5 Mixture of abstract and concrete syntax . 40 3.6 Parsing reduction without macros . 41 3.7 Reduction → without macros . 42 3.8 Mixture syntaxLM for expansion . 47 3.9 Reduction rules for the core macro expander . 48 3.10 Reduction for definitions . 50 3.11 Elimination of the ↑ operator . 51 3.12 Elimination of the operator . 51 3.13 Elimination of the ♂↓ operator . 52 3.14 Abstract syntax for evaluating Macro Scheme . 60 3.15 Application of syntax-lambda ............................. 61 3.16 Evaluation for the explicit substitutions for parsing and expansion . 62 3.17 Evaluation for primitives . 63 3.18 Evaluation for additional primitives . 65 3.19 Mixture syntax for expanding es-transformer macros . 66 3.20 Expansion for es-transformer forms . 67 3.21 Elimination of and normalization of meta-substitutions . 69 3.22 Elimination of identifier*+ substitutions . 70 3.23 Elimination of ↑ and for terms containing meta-variables . 71 3.24 Elimination and parsing♂ for es-transformer ..................... 71 3.25 Extension of the mixture syntax for parsing . 72 3.26 Parsing reduction →Parse and symbol resolution reduction →SymRes . 73 3.27 Elimination of substitutions . 75 3.28 Parsing syntaxLMand syntax-lambda .......................... 75 3.29 Expanding Macro Scheme special forms . 76 3.30 Elimination of the expansion substitutions and the ↓ operator for syntax and syntax-lambda ..................................... 76 3.31 Mixture syntax for expanding syntax-rules ..................... 81 3.32 Expansion for syntax-rules macros . 82 3.33 Helper functions for macro expansion . 84 3.34 Elimination of for syntax-rules ........................... 86 3.35 Reduction forLM syntax-rules ............................. 88 *+ iii iv LIST OF FIGURES 3.36 Elimination of the shift operator, the mark operator, and identifier substitutions for syntax-rules forms . 89 3.37 Normalization of patterns within syntax-rules .................... 89 3.38 Reduction rules for patterns . 90 3.39 Parsing reduction for syntax-rules .......................... 91 3.40 Mixture syntax, parsing, expansion, elimination and stripping for quoted forms . 94 4.1 Abstract syntax for programs with modules . 106 4.2 Program reduction . 108 4.3 Module reduction . 108 4.4 Linking reduction . ..