Principal Typings for Interactive Ruby Programming
Total Page:16
File Type:pdf, Size:1020Kb
Principal Typings for Interactive Ruby Programming A Thesis Submitted to the College of Graduate Studies and Research in Partial Fulfillment of the Requirements for the Degree of Master of Science in the Department of Computer Science University of Saskatchewan Saskatoon, Saskatchewan, Canada by Andriy Hnativ Copyright Andriy Hnativ, December 2009. All rights reserved. Permission To Use In presenting this thesis in partial fulfillment of the requirements for a Postgraduate degree from the University of Saskatchewan, I agree that the Libraries of this University may make it freely available for inspection. I further agree that permission for copying of this thesis in any manner, in whole or in part, for scholarly purposes may be granted by the professor or professors who supervised my thesis work or, in their absence, by the Head of the Department or the Dean of the College in which my thesis work was done. It is understood that any copying or publication or use of this thesis or parts thereof for financial gain shall not be allowed without my written permission. It is also understood that due recognition shall be given to me and to the University of Saskatchewan in any scholarly use which may be made of any material in my thesis. Requests for permission to copy or to make other use of material in this thesis in whole or part should be addressed to: Head of the Department of Computer Science 176 Thorvaldson Building 110 Science Place University of Saskatchewan Saskatoon, Saskatchewan, Canada S7N 5C9 i Abstract A novel and promising method of software development is the interactive style of development, where code is written and incrementally tested simultaneously. Interpreted dynamic languages such as Ruby, Python, and Lua support this interactive development style. However, because they lack semantic analysis as part of a compilation phase, they do not provide type-checking. The programmer is only informed of type errors when they are encountered in the execution of the program–far too late and often at a less- informative location in the code. We introduce a typing system for Ruby, where types will be determined before execution by inferring principal typings. This system overcomes the obstacles that interactive and dynamic program development imposes on type checking; yielding an effective type-checking facility for dynamic programming languages. Our development is embodied as an extension to irb, the Ruby interactive mode, allowing us to evaluate principal typings for interactive development. ii Acknowledgements I am sincerely thankful to my supervisor, Dr. Christopher Dutchyn, for his invaluable advices and ideas, and substantial support in many ways throughout my research. Also, greatest thanks to my lab mates from Software Lab for being part of the great working environment. Finally, biggest gratitude to all of those who helped me in any way during the completion of the project. iii Table of Contents Permission to Use ............................................................................................................... i Abstract.............................................................................................................................. ii Acknowledgements ..........................................................................................................iii Table of contents .............................................................................................................. iv List of Figures.................................................................................................................. vii List of Tables .................................................................................................................... ix List of Symbols .................................................................................................................. x Chapter 1. Introduction ................................................................................................... 1 Chapter 2. Background .................................................................................................. 11 2.1 Ruby........................................................................................................................ 11 2.1.1 Classes and Modules........................................................................................ 12 2.1.2 Functions.......................................................................................................... 14 2.1.3 Variables .......................................................................................................... 16 2.1.4 Assignments..................................................................................................... 19 2.1.5 Iteration............................................................................................................ 19 2.1.6 Conditionals ..................................................................................................... 21 2.1.7 Types................................................................................................................ 22 2.1.8 Exceptions........................................................................................................ 23 2.1.9 Constants.......................................................................................................... 24 2.1.10 Reflection....................................................................................................... 24 2.1.11 Other .............................................................................................................. 25 2.2 Type Inference ........................................................................................................ 25 2.2.1 Constraint Generation ...................................................................................... 28 2.2.2 Solving Type Constraints................................................................................. 29 2.3 Principal Typings.................................................................................................... 33 2.4 Type Inference for Dynamic Languages................................................................. 38 2.4.1 Psyco................................................................................................................ 39 2.4.2 Starkiller........................................................................................................... 39 2.4.3 Brett Cannon’s System .................................................................................... 39 2.5 Summary................................................................................................................. 40 Chapter 3. Typing for Ruby........................................................................................... 41 3.1 Challenges for Typing Ruby................................................................................... 42 3.1.1 Variables Shift Type ........................................................................................ 43 3.1.2 Control-flow Statements Branches are not Required to be Type-consistent ... 43 3.1.3 Ruby Exceptions Occur in Unpredictable Places ............................................ 45 3.1.4 Reflection Constructs are Impossible to Type................................................. 45 iv 3.2 Errors....................................................................................................................... 46 3.2.1 Branches in Control-flow Statements are not Type-consistent........................ 46 3.2.2 Local/Global/Instance Variable Changes Type ............................................... 48 3.2.3 Numbers of Targets and Values in Multiple Assignment Are Different......... 48 3.2.4 Inappropriate Use of Break, Redo, Next Statement......................................... 48 3.2.5 Function Called with Wrong Parameters......................................................... 49 3.2.6 Parameterized Types May Only Contain Values of a Single Type ................. 50 3.2.7 Classes, Modules, and Constants Redefined to Another ................................. 50 3.2.8 Ordinary Functions Called as Class/module Functions................................... 50 3.3 Situations Containing Assumptions........................................................................ 51 3.3.1 Use of Functions Before Declarations ............................................................. 51 3.3.2 A Global/Instance/Class Variable is used Before Definition .......................... 52 3.3.3 Reference to an Undefined Class/Constant...................................................... 52 3.3.4 Functions are not Type-consistent ................................................................... 53 3.3.5 Definitions of Functions with the Type Inconsistent to Expected................... 53 3.4 Related Ruby-Typing Work.................................................................................... 54 3.4.1 DRuby.............................................................................................................. 39 3.4.2 Kristensen’s Master Thesis.............................................................................. 39 3.4.3 Duby................................................................................................................. 39 3.4 Types in Ruby......................................................................................................... 55 3.4.1 Fixed Types.....................................................................................................