Graceful Language Extensions and Interfaces
Total Page:16
File Type:pdf, Size:1020Kb
Graceful Language Extensions and Interfaces by Michael Homer A thesis submitted to the Victoria University of Wellington in fulfilment of the requirements for the degree of Doctor of Philosophy Victoria University of Wellington 2014 Abstract Grace is a programming language under development aimed at ed- ucation. Grace is object-oriented, imperative, and block-structured, and intended for use in first- and second-year object-oriented programming courses. We present a number of language features we have designed for Grace and implemented in our self-hosted compiler. We describe the design of a pattern-matching system with object-oriented structure and minimal extension to the language. We give a design for an object-based module system, which we use to build dialects, a means of extending and restricting the language available to the programmer, and of implementing domain-specific languages. We show a visual programming interface that melds visual editing (à la Scratch) with textual editing, and that uses our dialect system, and we give the results of a user experiment we performed to evaluate the usability of our interface. ii ii Acknowledgments The author wishes to acknowledge: • James Noble and David Pearce, his supervisors; • Andrew P. Black and Kim B. Bruce, the other designers of Grace; • Timothy Jones, a coauthor on a paper forming part of this thesis and contributor to Minigrace; • Amy Ruskin, Richard Yannow, and Jameson McCowan, coauthors on other papers; • Daniel Gibbs, Jan Larres, Scott Weston, Bart Jacobs, Charlie Paucard, and Alex Sandilands, other contributors to Minigrace; • Gilad Bracha, Matthias Felleisen, and the other (anonymous) review- ers of papers forming part of this thesis; • the participants in his user study; • and Roma Klapaukh, Juanri Barnard, Alexandra Donnison, Amy Chard, and Timothy Jones for providing feedback on drafts of this thesis. iii Contents 1 Introduction1 2 Related Work5 2.1 Programming language education ............... 5 2.2 Pattern matching ......................... 14 2.2.1 Scala ............................ 17 2.2.2 Newspeak......................... 18 2.2.3 Other object-oriented languages ............ 20 2.2.4 Functional languages................... 22 2.3 Modules .............................. 26 2.3.1 Classes and objects as modules............. 27 2.3.2 Packages.......................... 30 2.3.3 Foreign objects....................... 33 2.4 Dialects and domain-specific languages ............ 34 2.4.1 Racket ........................... 35 2.4.2 Scala ............................ 36 2.4.3 Ruby ............................ 38 2.4.4 Haskell........................... 39 2.4.5 Cedalion.......................... 40 2.4.6 Converge.......................... 41 2.4.7 Wyvern........................... 42 2.4.8 Protean operators..................... 42 2.4.9 External domain-specific languages .......... 43 2.4.10 Pluggable checkers..................... 45 iv CONTENTS v 2.5 Visual interfaces for novices................... 46 2.5.1 Scratch........................... 46 2.5.2 Blockly........................... 48 2.5.3 Codemancer........................ 49 2.5.4 Lego Mindstorms..................... 49 2.5.5 Calico............................ 50 2.5.6 Alice ............................ 51 2.5.7 Greenfoot ......................... 51 2.5.8 TouchDevelop....................... 52 2.5.9 Graphical overlays on programs ............ 53 3 The Grace Language 55 3.1 Goals of Grace........................... 55 3.2 Variables and literals ....................... 56 3.3 Objects in Grace are closer than they appear.......... 58 3.4 Methods .............................. 59 3.4.1 Operators ......................... 59 3.4.2 Field accesses ....................... 61 3.4.3 Multi-part method names................ 61 3.4.4 Visibility.......................... 62 3.5 Blocks................................ 63 3.6 Classes ............................... 65 3.7 Inheritance............................. 65 3.7.1 Chained inheritance ................... 66 3.8 Types................................ 67 3.8.1 Generic types ....................... 69 3.9 Pattern matching ......................... 69 3.10 Modules .............................. 70 3.11 Dialects............................... 70 4 Patterns as Objects 71 4.1 Introduction............................ 71 v vi CONTENTS 4.2 Conceptual model......................... 73 4.3 Graceful patterns ......................... 74 4.3.1 match()case()...case.................... 74 4.3.2 Matching blocks...................... 75 4.3.3 Literal patterns ...................... 76 4.3.4 Type patterns ....................... 76 4.3.5 Variable patterns ..................... 76 4.3.6 Wildcard pattern ..................... 77 4.3.7 Combinators........................ 77 4.3.8 Destructuring....................... 78 4.3.9 Predicates ......................... 79 4.3.10 Using arbitrary expressions to obtain patterns . 80 4.4 Patterns as objects......................... 81 4.4.1 Patterns as an object framework............. 81 4.4.2 Irrefutable patterns.................... 82 4.4.3 Combinators........................ 85 4.4.4 Types............................ 87 4.4.5 Autozygotic patterns................... 88 4.4.6 Destructuring patterns.................. 89 4.4.7 Destructuring types.................... 92 4.4.8 Lambda patterns and match...case ........... 94 4.5 Types and patterns ........................ 95 4.5.1 Pattern and MatchResult................. 96 4.5.2 Destructuring....................... 97 4.5.3 Combinators........................ 98 4.5.4 Exhaustive matching................... 99 4.6 Generalising patterns.......................100 4.7 Discussion and comparison with related work . 102 4.7.1 Scala ............................103 4.7.2 Newspeak.........................106 4.7.3 Racket ...........................110 vi CONTENTS vii 4.7.4 Gradual and optional typing . 111 4.7.5 Matching as monads...................112 4.7.6 Future work........................112 4.7.7 Alternative approaches..................113 4.7.8 Application ........................119 4.8 Conclusion.............................119 5 Modules as Gradually-Typed Objects 121 5.1 Introduction............................121 5.1.1 What is a module? ....................122 5.2 Modules as objects ........................123 5.2.1 Importing modules....................124 5.2.2 Gradual typing of modules . 125 5.2.3 Recursive module imports . 128 5.3 Design rationale..........................129 5.4 Package management.......................130 5.4.1 Identifying packages...................130 5.4.2 Finding packages.....................132 5.4.3 Installing packages....................132 5.4.4 Publishing packages ...................132 5.5 Extensions and future work ...................133 5.5.1 Foreign objects.......................134 5.5.2 External data........................136 5.5.3 Resource imports.....................136 5.6 Comparison with related work . 139 5.6.1 Python...........................140 5.6.2 Newspeak.........................142 5.6.3 Go..............................143 5.7 Conclusion.............................144 6 Dialects 147 6.1 What is a dialect? .........................149 vii viii CONTENTS 6.1.1 Structure..........................149 6.1.2 Pluggable checkers....................151 6.1.3 Run-time protocol.....................152 6.2 Case studies of dialects......................153 6.2.1 Logo-like turtle graphics . 153 6.2.2 Design by contract ....................156 6.2.3 Dialect for writing dialects . 158 6.2.4 Requiring type annotations . 161 6.2.5 Literal blocks .......................164 6.2.6 Ownership types .....................165 6.2.7 Type checking.......................171 6.2.8 Relations..........................173 6.2.9 Finite state machines...................174 6.2.10 GrAPL ...........................175 6.2.11 GPGPU parallelism....................176 6.3 Discussion .............................179 6.3.1 Inheritance.........................179 6.3.2 Delegation.........................181 6.3.3 Macros...........................182 6.3.4 Local dialects .......................184 6.3.5 Default methods .....................185 6.4 Implementation..........................186 6.4.1 Lexical scoping ......................186 6.4.2 Executing checkers statically . 187 6.4.3 Side effects.........................189 6.4.4 Security concerns.....................190 6.5 Comparison with related work . 190 6.5.1 Racket ...........................190 6.5.2 Scala ............................193 6.5.3 Ruby ............................193 6.5.4 Wyvern...........................194 viii CONTENTS ix 6.5.5 Cedalion..........................194 6.5.6 Haskell...........................195 6.5.7 Xtext ............................196 6.6 Conclusion.............................198 7 Tiled Grace 201 7.1 Tiled Grace ............................202 7.1.1 Implementation......................205 7.2 Motivation.............................208 7.3 Functionality............................210 7.3.1 Handling errors......................211 7.3.2 Overlays..........................213 7.3.3 Dialects...........................214 7.3.4 Type checking.......................217 7.3.5 Hints............................219 7.4 Experiment.............................220 7.4.1 Research questions....................221 7.4.2 Participation........................222 7.4.3 Instruments ........................222 7.4.4 Protocol ..........................222 7.4.5 Data collection.......................225 7.5 Results ...............................234