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; • David Streader, John Grundy, and Laurence Tratt, examiners of the thesis; • and Alexandra Donnison, Amy Chard, Juanri Barnard, Roma Kla- paukh, 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.1.1 Educational psychology................. 6 2.1.2 Programming pedagogy................. 8 2.1.3 Educational languages.................. 11 2.2 Pattern matching ......................... 15 2.2.1 Scala ............................ 17 2.2.2 Newspeak......................... 19 2.2.3 Other object-oriented languages ............ 21 2.2.4 Functional languages................... 23 2.3 Modules .............................. 27 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 ............................ 37 2.4.3 Ruby ............................ 38 2.4.4 Haskell........................... 40 2.4.5 Cedalion.......................... 41 2.4.6 Converge.......................... 41 2.4.7 Wyvern........................... 42 iv CONTENTS v 2.4.8 Protean operators..................... 43 2.4.9 External domain-specific languages .......... 43 2.4.10 Pluggable checkers..................... 46 2.5 Visual interfaces for novices................... 46 2.5.1 Scratch........................... 47 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 ......................... 52 2.5.8 TouchDevelop....................... 52 2.5.9 Droplet........................... 54 2.5.10 Graphical overlays on programs ............ 54 3 The Grace Language 57 3.1 Goals of Grace........................... 57 3.2 Variables and literals ....................... 58 3.3 Objects in Grace are closer than they appear.......... 60 3.4 Methods .............................. 61 3.4.1 Operators ......................... 61 3.4.2 Field accesses ....................... 63 3.4.3 Multi-part method names................ 63 3.4.4 Visibility.......................... 64 3.5 Blocks................................ 65 3.6 Classes ............................... 67 3.7 Inheritance............................. 67 3.7.1 Chained inheritance ................... 68 3.8 Types................................ 69 3.8.1 Generic types ....................... 71 3.9 Pattern matching ......................... 72 v vi CONTENTS 3.10 Modules .............................. 72 3.11 Dialects............................... 72 4 Patterns as Objects 73 4.1 Introduction............................ 73 4.2 Conceptual model......................... 75 4.3 Graceful patterns ......................... 76 4.3.1 match()case()...case.................... 76 4.3.2 Matching blocks...................... 77 4.3.3 Literal patterns ...................... 78 4.3.4 Type patterns ....................... 78 4.3.5 Variable patterns ..................... 78 4.3.6 Wildcard pattern ..................... 79 4.3.7 Combinators........................ 79 4.3.8 Destructuring....................... 80 4.3.9 Predicates ......................... 81 4.3.10 Using arbitrary expressions to obtain patterns . 82 4.4 Patterns as objects......................... 83 4.4.1 Patterns as an object framework............. 83 4.4.2 Irrefutable patterns.................... 84 4.4.3 Combinators........................ 87 4.4.4 Types............................ 89 4.4.5 Autozygotic patterns................... 90 4.4.6 Destructuring patterns.................. 91 4.4.7 Destructuring types.................... 94 4.4.8 Lambda patterns and match...case ........... 96 4.5 Types and patterns ........................ 97 4.5.1 Pattern and MatchResult................. 98 4.5.2 Destructuring....................... 99 4.5.3 Combinators........................100 4.5.4 Exhaustive matching...................101 vi CONTENTS vii 4.6 Generalising patterns.......................102 4.7 Discussion and comparison with related work . 103 4.7.1 Scala ............................105 4.7.2 Newspeak.........................108 4.7.3 Racket ...........................112 4.7.4 Gradual and optional typing . 113 4.7.5 Matching as monads...................114 4.7.6 Future work........................114 4.7.7 Alternative approaches..................115 4.7.8 Application ........................121 4.8 Conclusion.............................122 5 Modules as Gradually-Typed Objects 123 5.1 Introduction............................123 5.1.1 What is a module? ....................124 5.2 Modules as objects ........................125 5.2.1 Importing modules....................126 5.2.2 Gradual typing of modules . 127 5.2.3 Recursive module imports . 130 5.3 Design rationale..........................131 5.4 Package management.......................132 5.4.1 Identifying packages...................132 5.4.2 Finding packages.....................134 5.4.3 Installing packages....................134 5.4.4 Publishing packages ...................134 5.5 Extensions and future work ...................135 5.5.1 Foreign objects.......................136 5.5.2 External data........................138 5.5.3 Resource imports.....................138 5.6 Comparison with related work . 141 5.6.1 Python...........................142 vii viii CONTENTS 5.6.2 Newspeak.........................144 5.6.3 Go..............................145 5.7 Conclusion.............................147 6 Dialects 149 6.1 What is a dialect? .........................151 6.1.1 Structure..........................151 6.1.2 Pluggable checkers....................153 6.1.3 Run-time protocol.....................154 6.2 Case studies of dialects......................155 6.2.1 Logo-like turtle graphics . 155 6.2.2 Design by contract ....................158 6.2.3 Dialect for writing dialects . 160 6.2.4 Requiring type annotations . 163 6.2.5 Literal blocks .......................166 6.2.6 Ownership types .....................167 6.2.7 Type checking.......................173 6.2.8 Relations..........................175 6.2.9 Finite state machines...................176 6.2.10 GrAPL ...........................177 6.2.11 GPGPU parallelism....................178 6.3 Discussion .............................181 6.3.1 Inheritance.........................181 6.3.2 Delegation.........................183 6.3.3 Macros...........................184 6.3.4 Local dialects .......................186 6.3.5 Default methods .....................187 6.4 Implementation..........................188 6.4.1 Lexical scoping ......................188 6.4.2 Executing checkers statically . 189 6.4.3 Side effects.........................191 viii CONTENTS ix 6.4.4 Security concerns.....................192 6.5 Comparison with related work . 192 6.5.1 Racket ...........................192 6.5.2 Scala ............................195 6.5.3 Ruby ............................195 6.5.4 Wyvern...........................196 6.5.5 Cedalion..........................196 6.5.6 Haskell...........................197 6.5.7 Xtext ............................198 6.6 Conclusion.............................201 7 Tiled Grace 203 7.1 Tiled Grace ............................204 7.1.1 Implementation......................207 7.2 Motivation.............................210 7.3 Functionality............................212 7.3.1 Handling errors......................213 7.3.2 Overlays..........................215 7.3.3 Dialects...........................216 7.3.4 Type checking.......................219 7.3.5 Hints............................221 7.4 Experiment.............................222