Home

The Grace Programming Language Draft Specification

image

Contents

1. Revision 1261 committed on 2013 08 14 00 06 00Z by kim 33 11 6 Intersection Types option An object conforms to an Intersection type written T1 amp T2 amp amp Tn if and only if that object conforms to all of the component types The main use of intersection types is as bounds on where clauses class Happy lt T gt where T lt Comparable lt T gt amp Printable amp Happyable ip 11 7 Union Types option Structural union types sums written T1 T2 Tn may refer to an object that conforms to any of the component types Unions are mostly included for completeness variant types subsume most uses 11 8 Type subtraction option A type written T1 T2 has the interface of T1 without any of the methods in T2 Kim Need to tighten up this definition 11 9 Singleton Types James gt How does this work with the new pattern matching interpretation think singleton objects must be self matching for this to work reliably The names of singleton objects typically declared in object declarations may be used as types Singleton types match only their singleton object Singleton typs can be distinguised from regular types because Grace type declarations are statically typed def null object method isNull gt Boolean return true type Some lt T gt thing gt T method isNull gt Boolean type Option lt T gt Some lt T gt
2. b xb sqrt b xb sqrt w a x a b b sqrt a a axa tbx b a x a b xb a b c a b c a b c a b c 8 4 Unary Prefix Operator Requests Grace supports unary methods named by operator symbols that precede the explicit receiver Since binary operator methods must also have an explicit receiver there is no syntactic ambiguity Prefix operators bind less tightly than other method requests and there fore need parenthesis to disambiguate Andrew gt This seems backwards in math prefix operators bind more tightly The statement above is also a non sequitur lt Examples 3 4 b squared b squared b squared parses as b squared status ok engine isOnFire amp amp wings areAttached amp amp isOnCourse Andrew By the above rule we are OK only if the wings have fallen off and we are off course lt 4 8 5 Accessing Operator Requests Grace supports operators and which can be defined in libraries e g for indexing and modifying collections 18 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Examples print a 3 requests method on a with argument 3 a 3 Hello requests method on a with arguments 3 and Hello Note Somewhere we need to have a list of reserved operators that cannot be used normally Andrew eh What does that mean lt 8 6 Super Requests
3. The reserved word super may be used only as an explicit receiver In over riding methods method requests with the pseudo receiver super request the prior overridden method with the given name from self Note that no search is involved super requests can be resolved statically unlike other method re quests Examples super value super bar 1 2 6 super doT his 3 timesTo foo super 1 Isuper foo super syntax error 1 super syntax error 8 7 Outer Requests The reserved word outer denotes the object enclosing self it is an error if there is no such enclosing object Moreover outer outer denotes the object enclosing outer and so on for as many outers as there are enclosing objects Andrew minigrace recognizes outer as a method that can be requested of any object and that answers a reference to its enclosing object Do we want to support this lt Examples outer outer value outer bar 1 2 6 outer outer doThis 3 times To foo outer 1 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 19 louter 8 8 Encapsulation Grace has different default encapsulation rules for methods and fields The defaults can be changed by explicit annotations The details are as follows 8 8 1 Methods By default methods are public which means that they can be requested by any client that has access to the object Thus any expression can be the target of a request for a public met
4. 8 11 Precedence of Method Requests Grace programs are formally defined by the language s grammar see ap pendix B The grammar gives the following precedence levels higher num bers bind more tightly 1 Assignment operator as a suffix to a named request or accessing operator 2 Other operators no priority for different operators associate left to right 3 Additive operators and associate left to right 4 Multiplicative operators and associate left to right 5 Prefix operators associate right to left 22 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 6 Named requests with or without arguments the accessing operator Multi part named requests accumulate words and arguments as far to the right as possible 7 Literals numbers strings objects types parenthesized expres sions 9 Control Flow Control flow statements in Grace are syntactically method requests This allows instructors to use libraries to define domain specific control flow con structs that look the same as the rest of Grace 9 1 Basic Control Flow If statements if test then block if test then block else block While statement while test do block For statement for collection do item gt block body for course students do s Student gt print s for 0 n do i gt print i To allow for conventional syntax with a leading keyword if while fo
5. 100 0 01 def x Number 3 means the same as the above def x Number Syntax Error x must be initialised 6 2 Variables Variable definitions are introduced using the var keyword they optionally bind an identifier to the value of an initialising expression optionally at a precise type Variables can be re bound to new values as often as desired using an assignment statement If a variable is declared without an initializ ing expression it is said to be uninitialized any attempt to access the value of an initialized variable is an error This error may be caught either at run time or at compile time depending on the cleverness of your implementor Examples var x Rational 3 explicit type var x Rational ok x must be initialized before being referenced var x 3 x has type dynamic var x x is uninitialised and dynamic 6 3 Fields Constants and variables declared directly inside an object constructor rep resent fields of that object A field declaration of x can be annotated to Revision 1261 committed on 2013 08 14 00 06 00Z by kim 9 create a reader method x and if x is a variable a writer method x _ as described in Section 8 8 1 Because these methods are deliberately syn tactically identical to direct references to x x becomes inaccessible except through use of the methods You can think of the real field as having a unique secret name which is known only to the reader and writer methods In
6. gt print UPPER CASE case O 9 x gt print numeric case Forename A Za z Surname A Za z r2 fn sn gt print Passenger fn first sn With potentially justifiable special cases more literals e g things like tu ples lists could be descructured a b gt a b Although it would be very nice it s hard to see how e g points created with 3 4 could be destruc ted like a b gt print x a y b without yet more bloated special case syntax Discussion This rules try to avoid literal conversions and ambiguous syn tax The potential ambiguity is whether to treat something as a variable dec laration and when as a first class pattern These rules should treat only fresh variables as intended binding instances so a pattern that syntactically matches a simple variable declaration as in this block empty gt print the singleton empty collect will raise an error even though this is unambiguous given Grace s no shad owing rule Match statements that do nothing but match on types must distinguish Andrew something syntactically from a variable declaration e g match rv case FailedMatch gt print failed Revision 1261 committed on 2013 08 14 00 06 00Z by kim 27 case _ SuccessfulMatch gt print succeeded while writing just match rv case FailedMatch gt print failed case SuccessfulMatch gt prin
7. _ acts as a placeholder identifier it is treated as a fresh identifier everywhere it is used 3 4 Reserved Words and Reserved Operators Grace has the following reserved words and reserved operators The indi cates words related to design options not yet chosen class def inherits is method object outer prefix return self Selftype super type var where i l Q gt James gt we should decide how to handle these properly esp names always defined in the standard library true false etc lt 3 5 Tabs and Control Characters Newline can be represented by the unicode newline character by carriage return or by line feed a line feed that immediately follows a carriage return is ignored Tabs and all other non printing control characters except carriage re turn line feed and line separator are syntax errors even in a string literal There are escape sequences for including special characters in string liter als 4 Built in Objects 4 1 Numbers Grace supports a single type Number Number supports at least 64 bit preci sion floats Implementations may support other numeric types a full speci fication of numeric types is yet to be completed Grace has three syntactic forms for numerals that is literals that denote Numbers 1 Decimal numerals written as strings of digits optionally preceded by a minus Revision 1261 committed on 2013 08 14 00 06 00Z by kim 5 2 Explicit radix nume
8. method prefix gt Number 0 self Andrew gt The where T lt Numeric should be T matches Numeric lt 7 Objects and Classes Grace object constructor expressions and declarations produce individual objects Grace provides class declarations to create classes of objects all of which have the same structure Grace s class and inheritance design is complete but tentative We need experience before confirming the design Revision 1261 committed on 2013 08 14 00 06 00Z by kim 11 7 1 Objects Objects are created by object literals The body of an object literal consists of a sequence of declarations object def colour Colour Colour tabby def name String Unnamed var miceEaten 0 method eatMouse miceEaten miceEaten 1 Object literals are lexically scoped inside their containing method or block In particular any object level expressions initializer expressions and method bodies are executed in that lexical context Each time an object literal is executed a new object is created Object literals are expressions that evaluate to an object with the given features Object literals can also contain statements which are executed as a side effect of evaluating the object literal Thus constant can be defined by an object literal like this def unnamedCat object def colour Colour Colour tabby def name String Unnamed var miceEaten 0 method eatMouse miceEaten mic
9. otherOp forbid space after prefix def assignmentMethodHeader rule identifier assign oneMethodFormal def accessingMethodHeader rule IrBrack genericFormals methodFormals def accessingAssignmentMethodHeader rule IrBrack assign genericFormals methodFormals def methodReturnType rule opt arrow nonEmptyTypeExpression def methodFormals rule IParen replsep identifier opt colon typeExpression comma rParen def oneMethodFormal rule IParen identifier opt colon typeExpression rParen def blockFormals rule repsep identifier opt colon typeExpression comma def matchBinding rule identifier literal parenExpression opt colon nonEmptyTypeExpression opt matchingBlockTail def matchingBlockTail rule IParen replsep matchBinding comma rParen def typeDeclaration rule typeld identifier genericFormals equals nonEmptyTypeExpression semicolon whereClause def typeExpression rule opt ws typeOpExpression opt ws opt ws def nonEmptyTypeExpression rule opt ws typeOpExpression opt ws def typeOp rule opsymbol opsymbol amp opsymbol def typeOpExpression rule rep1sep basic TypeExpression typeOp def typeOpExpression rule this complex rule ensures two different typeOps have no precedence var otherOperator Revision 1261 committed on 2013 08 14 00 06 00Z by kim 41 basicT
10. access and assignment 8 9 Private Fields Grace does not have private fields all fields can be accessed from subobjects However the parameters and temporary variables of constructor methods can be used to obtain an effect similar to privacy Examples method newShipStartingAt s Vector2D endingAt e Vector2D returns a battleship object extending from s to e This object cannot be asked its size or its location or how much floatation remains assert s x e x sy ey def size s distanceTo e var floatation size object method isHitAt shot Vector2D if shot onLineFrom s to e then floatation floatation 1 if floatation 0 then self sink Revision 1261 committed on 2013 08 14 00 06 00Z by kim 21 true else false 8 10 Generic Method Requests Methods that have type parameters may optionally be requested with ex plicit type arguments When a method declared with type parameters is requested in a statically typed context without explicit type arguments the type arguments are inferred Andrew We haven t defined statically typed context More recent thinking is that types are always inferred if not given explicitly the inferred types may be Dynamic lt Examples sumSq lt Integer64 gt 10 i64 20 164 sumSq 10 164 20 164 Andrew gt An example that gives the method definition and uses the type in some way would be more useful lt
11. and following the last word For example method drawLineFrom source to destination The name of a method and the position of its argument lists within that name is determined when the method is declared When reading a request of a multi part method name you should continue accumulating words and argument lists as far to the right as possible right as possible If the receiver of a named method is self it may be left implicit i e the self and the dot may both be omitted Grace does not allow the overloading of method names the type and number of arguments in a method request does not influence the name of the method that is being requested Parenthesis may be omitted where they would enclose a single argument that is a string or block literal Examples canvas drawLineFromPoint p1 toPoint p1 canvas drawLineFromPoint origin ofLenthXY 3 5 canvas movePen ToXY x y 16 Revision 1261 committed on 2013 08 14 00 06 00Z by kim canvas movePen ToPoint p print Hello world size 8 2 Assignment Requests An assignment request is a variable followed by or it is a request of a method whose name ends with in each case followed by a single argument Spaces are optional before and after the Examples x 3 y 2 widget active true Assignment methods return Done 8 3 Binary Operator Requests Binary methods have a receiver and one argument Grace allows operator symbols to name binary
12. architec tures with Total Store Ordering TSO 13 Libraries Kim gt Need to put standard libraries in appendices lt 13 1 Collections Grace will support some collection classes Collections will be indexed 1 size by default bounds should be able to be chosen when explicitly instantiating collection classes Acknowledgements Thanks to Josh Bloch Cay Horstmann Micahel Kglling Doug Lea the participants at the Grace Design Workshops and the IFIP WG2 16 Pro gramming Language Design for discussions about the language design Thanks to Michael Homer and Ewan Tempero for their comments on drafts The Scala language specification 2 8 and the Newspeak language specification 0 05 6 were used as references for early versions of this docu ment The design of Grace so far has been influenced by Algol BS AmbientTalk 12 AspectJ 29 BCPL M2 Beta 33 Blue 32 C 28 C 44 Ct A B Dylan 43 Eiffel 35 B6 Emerald 5 Fy KO Revision 1261 committed on 2013 08 14 00 06 00Z by kim 37 Ft 46 FGJ 24 FJv 25 FORTRESS I gBeta 14 Haskell 22 Java 13 18 Kevo 47 Lua 23 Lisp 16 ML 87 Modula 2 51 Modula 3 II Modular Smalltalk 50 Newspeak 8 6 Pascal 27 Perl 49 Racket 15 Scala 401 39 Scheme 45 Self 48 Smalltalk 19 26 9 7 Object Oriented Turing 21 Noney 84 and Whiteoak at least we apologise if we ve missed any languages out All t
13. contrast block and method temporary variables really exist and can be the targets of real assignment statements Assignment statements return the value done of type Done James gt don t like this description field access lt 6 4 Methods Methods are declared with the method keyword a name optionally a pa rameter list potentially repeated optionally a return type declaration and a method body Methods may not be nested Methods may contain one or more return e statements If a return state ment is executed the method terminates with the value of the expression e If the method returns Done then no expression may follow the return If ex ecution reaches the end of the method body without executing a return the method terminates and returns the value of the last expression evaluated Methods can be named by an identifier suffixed with this form of name is conventionally used for writer methods both user written both automatically generated and custom as exemplified by the example value below Prefix operator methods are named prefix followed by the operator character s Methods may have repeated parameters to allow them to take a variable number of parameters that is to provide method of variable arity called varargs in some other languages A repeated parameters if present must be the last parameter in any on the parameter lists following a part of a multi part method
14. data store case e NoSuchFile gt print No Such File return case e PermissionError gt print Permission denied return 28 Revision 1261 committed on 2013 08 14 00 06 00Z by kim case Exception gt print Unidentified Error System exit finally f close Raising an Exception terminates the execution of the expression contain ing the raise request it is not possible to restart or resume that execution Andrew gt continue here catch 1 case 1 finally 1 construct and a do 1 finally 1 construct sup port finalization even through exceptions Following Scala a using 1 do 1 construct supports resource allocation and deallocation using Closable new do stranger gt bound to the new Closable stranger doSomething the close method is automatically requested of the Closable when the block terminates 10 Equality and Value Objects All objects automatically implement the following non overridable methods option Library programmers are able to override these methods 1 and operators implemented as per Henry Baker s egal pred icate 2 That is immutable objects are egal if they are of the same shape have the same methods declared in the same lexical enviorn ments and if their fields contents are egal while mutable objects are only ever egal to themselves 2 hashcode compatible with the egal As a consequence immutable
15. name Repeated parameters are designated by a star x before the name of the parameter Inside the method a repeated parameter has the type of an immutable collection of the declared type for example a param eter declared xargs String has the type args ImmutableCollection lt String gt Methods may be declared with generic type parameters in which case they must be requested with generic type arguments Andrew Why do we call them generic type parameters and generic type arguments Why not just type parameters and type arguments lt Andrew This contradicts which says that the type arguments are optional lt Generic type parameters may be constrained with where clauses 10 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Examples method pi 3 141592634 method greetUser print Hello World method other Point gt Point x other x y other y method other x other x y other y method other return x other x y other y method value n Number gt Done print value currently value now assigned n super value n method either a Block0 lt Done gt or b BlockO lt Done gt gt Done if random nextBoolean then a apply else b apply method print xargs Printable gt Done method sumSq lt T gt a T b T gt T where T lt Numeric a x a b b
16. of class Kim The second is not currently legal though could write def of factory object generating similar items as class lt how do factories etc relate to uninitialized decide what to do about equality operators Kim gt Thiat is impor tant lt Support for identifying static type decltype and dynamic type typeid foo get Type Support for type test like instanceof and static casts More to the point what is the type system Kim We should insert definition We don t support instance of instead use match lt Multiple Assignment Kim Why bother lt Type assertions should they just be normal assertions between types so e g lt could be a normal operator between types Grace needs subclass compatibility rules Kim YES lt BRANDS Brand Brand Brand kim Maybe not needed lt weak references Kim 7 why lt Grammar top level def program rule codeSequence rep ws end def codeSequence rule repdel declaration statement semicolon def innerCodeSequence rule repdel innerDeclaration statement semicolon declarations def declaration rule varDeclaration defDeclaration classDeclaration typeDeclaration methodDeclaration def innerDeclaration rule varDeclaration defDeclaration classDeclaration 40 Revision 1261 committed
17. section draws heavily on the wording of the Modula 3 report LI with apologies to Luca Cardelli et al If B lt A then every object of type B is also an object of type A The converse does not apply If A and B are ground object types then B lt A iff for every method m in A there is a corresponding method m with the same name in B such that e The method m in B must have the same number of arguments as m in A with the same distribution in multi part method names e If the method m in A has signature P P gt R and m in B has signature Q1 Qn gt S then 32 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Argument types must be contravariant P lt Qi Results types must be covariant S lt R Kim gt almost feel the signature should be further subdivided to reflect multi part names but that seems too paintul lt If a class or object B inherits from another class A then B s type should conform to A s type If A and B are generic classes then similar instantions of their types should conform Kim Need to reword and resolve what this means Could allow but make type checker work harder all the way up the inheritance chain or disallow This does not make it clear lt The conformance relationship is used in where clauses to constrain formal generic type parameters of classes and methods Kim Not really must de
18. spec lt ObjectTypes require formal parameter names amp need to fix examples TE Tuples 11 11 Syntax as a type Literal Tuple Syntax Nesting Serialization Include dialect description Pragmatics distribution medium for Grace programs objects and libraries is Grace source code Grace source files should have the file extension grace If for any bizzare reason a trigraph extension is required it should be grc Grace files may start with one or more lines beginning with these lines are ignored 36 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 12 1 Garbage Collection Grace implementations should be garbage collected Safepoints where GC may occur are at any backwards branch and at any method request Grace will not support finalisation Kim gt Seems like we will need it lt 12 2 Concurrency and Memory Model The core Grace specification does not describe a concurrent language Dif ferent concurrency models may be provided as dialects Grace does not provide overall sequential consistency Rather Grace pro vides sequential consistency within a single thread Across threads any value that is read has been written by some thread sometime but Grace does not provide any stronger guarantee for concurrent operations that interfere Kim Semantics of language must be independent of the hardware specs Grace s memory model should support efficient execution on
19. D Hondt and W De Meuter Ambient oriented programming in AmbientTalk In ECOOP pages 230 254 2006 46 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Carlton Egremont III Mr Bunny s Big Cup o Java Addison Wesley 1999 Erik Ernst Family polymorphism In ECOOP 2001 Matthias Felleisen Robert Bruce Findler Matthew Flatt and Shriram Krishnamurthi How To Design Programs MIT Press 2001 Richard P Gabriel LISP Good news bad news how to win big AI Expert 6 6 30 39 1991 Joseph Gil and Itay Maman Whiteoak Introducing structural typing into Java In OOPSLA 2008 Brian Goetz Time Peierls Joshua Block Joesph Bowbeer David Holmes and Doug Lea Java Concurrency in Practice Addison Wesley Professional 2006 Adele Goldberg and David Robson Smalltalk 80 The Language and its Implementation Addison Wesley 1983 C A R Hoare Hints on programming language design Technical Report AIM 224 Stanford Artificial Intelligence Laboratory 1973 Ric Holt and Tom West OBJECT ORIENTED TURING REFER ENCE MANUAL seventh edition version 1 0 Technical report Holt Software Associates Inc 1999 Paul Hudak John Hughes Simon Peyton Jones and Philip Wadler A history of Haskell being lazy with class In History of Programming Languages ITT pages 12 1 12 55 ACM Press 2007 Roberto Ierusalims
20. The Grace Programming Language Draft Specification Version 0 3 1261 Andrew P Black Kim B Bruce James Noble August 14 2013 1 Introduction This is a specification of the Grace Programming Language This specifica tion is notably incomplete and everything is subject to change In particular this version does not address the library especially collection syntax and collection literals nested static type system although we ve made a start module system James should write up from DYLA paper lt metadata Java s annotations C attributes final abstract etc James pe should add this too lt Kim Need to add syntax but not necessarily details of which attributes are in language yet lt immutable data and pure methods reflection assertions data structure invariants pre amp post conditions contracts Kim gt Put into dialects section lt Dialects regexps libraries including more complex Numeric types and testing For discussion and rationale see http gracelang org Where this document gives options we outline choices in the language design that have yet to be made Revision 1261 committed on 2013 08 14 00 06 00Z by kim User Model All designers in fact have user and use models consciously or subconsciously in mind as they work Team design requires explicit models and assumptions Frederick P Brooks The Des
21. c language Apple Computer Inc 1996 Bjarne Stroustrup Why C is not just an object oriented program ming language In OOPSLA Companion ACM Press 1995 Gerald Sussman and Guy Steele SCHEME An interpreter for extended lambda calculus Technical Report AI Memo 349 MIT Artificial Intel ligence Laboratory December 1975 Don Syme The Ff draft language specification Technical report Mi crosoft 2009 Antero Taivalsaari Delegation versus concatenation or cloning is inher itance too OOPS Messenger 6 3 1995 David Ungar and Randall B Smith SELF the Power of Simplicity Lisp and Symbolic Computation 4 3 June 1991 Larry Wall Perl the first postmodern computer language http www wall org larry pm html Spring 1999 Allen Wirfs Brock and Brian Wilkerson Modular Smalltalk In OOP SLA 1998 Niklaus Wirth Modula 2 and Oberon In HOPL 2007
22. ch 9 2 2 Matching Blocks Blocks with a single parameter are also patterns they match any object that can validly be bound to that parameter For example if the parameter is annotated with a type the block will successfully match an object that has that type and will fail to match other objects Matching blocks support an extended syntax for their parameters In addition to being a fresh variable as in a normal block the parameter may also be a pattern Matching blocks are themselves patterns one argument Andrew Here gave up lt matching block with parameter type A and return type R also implements Pattern lt R Done gt A recursive syntax directed translation maps matching blocks into blocks with separate explict patterns non matching blocks that are called via apply only when their patterns match First the matching block is flattened translated into a straightforward non matching block with one parameter for every bound name or place holder For example _ Pair a Pair b c gt a b c is flattened into Revision 1261 committed on 2013 08 14 00 06 00Z by kim 25 a b c gt a b c then the pattern itself is translated into a composite object structure def mypat MatchAndDestructuringPattern new Pair VariablePattern new a MatchAndDestructuringPattern new Pair VariablePattern new b VariablePattern new c Finally the translated pattern and block are glue
23. chy Luiz Henrique de Figueiredo and Waldemar Ce les The evolution of Lua In HOPL ITI 2007 A Igarashi B C Pierce and P Wadler Featherweight Java A minimal core calculus for Java and GJ ACM Transactions on Programming Languages and Systems 23 3 396 450 2001 Atsushi Igarashi and Hideshi Nagira Union types for object oriented programming Journal of Object Technology 6 2 31 45 February 2007 http www jot fm issues issues 2007 02 article3 Daniel H H Ingalls Design principles behind Smalltalk BYTE Maga zine August 1981 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 47 27 Kathleen Jensen and Niklaus Wirth Pascal User Manual and Report Springer 1975 28 Brian W Kernighan and Dennis M Ritchie The C Programming Language Addison Wesley 2nd edition 1993 29 Gregor Kiczales Erik Hilsdale Jim Hugunin Mik Kersten Jeffrey Palm and William G Griswold An overview of AspectJ In ECOOP 2001 30 Michael Kolling Bett Koch and John Rosenberg Requirements for a first year object oriented teaching language In ACM Conference on Computer Science Education SIGCSE 1995 31 Michael K lling and John Rosenberg Blue a language for teaching object oriented programming In ACM Conference on Computer Sci ence Education SIGCSE 1996 32 Michael K lling and John Rosenberg Blue a language for teach ing object oriented programming language specification Technical Re po
24. d together via a LambdaPattern LambdaPattern new mypat _ a b c gt a b c The translation is as follows e Le e Le O WildcardPattern v fresh unbound variable VariablePattern v v bound variable error v e AndPattern new VariablePattern new v e e f g MatchAndDestructuringPattern new e f g literal literal e not otherwise translated e 9 2 3 Implementing Match case Finally the match 1 case N methods can be implemented directly e g method match o Any case b1 Block lt B1 R gt case b2 Block lt B2 R gt for b1 b2 do b gt def rv b match o if rv succeeded then return rv result FailedMatchException raise or because matching blocks are patterns in terms of pattern combinators 26 Revision 1261 committed on 2013 08 14 00 06 00Z by kim method match o Any case b1 Block lt B1 R gt case b2 Block lt B2 R gt def rv b1 b2 match o if rv succeeded then return rv result FailedMatchException raise First Class Patterns While all types are patterns not all patterns are types For example it would seems sensible for regular expressions to be pat terns potentially created via one or more shorthand syntaxes shorthands all defined in standard Grace match myString case gt print null string case Regexp new a z gt print lower case case A Z x r
25. ddExpression rule replsep multExpression addOp def multExpression rule replsep prefixExpression multOp def prefixExpression rule rep otherOp selectorExpression rep1 otherOp superld we can have super def selectorExpression rule primaryExpression rep selector def selector rule dot unaryRequest dot requestWithArgs IBrack replsep expression comma rBrack 42 Revision 1261 committed on 2013 08 14 00 06 00Z by kim def operatorChar CharacterSetParser new amp x gt lt had to be moved up special symbol for operators cannot be followed by another operatorChar method opsymbol s String trim token s not operatorChar def multOp opsymbol x opsymbol def addOp opsymbol opsymbol def otherOp rule guard trim rep1 operatorChar s gt parse s with reservedOp end encompasses multOp and addOp def operator rule otherOp reservedOp def unaryRequest rule trim identifier genericActuals not delimitedArgument def requestWithArgs rule firstRequestArgumentClause repsep requestArgumentClause opt ws def firstRequestArgumentClause rule identifier genericActuals opt ws delimitedArgument def requestArgumentClause rule identifier opt ws delimitedArgument def delimitedArgument rule argumentsInParens blockLiteral stringLiteral def argumentsInPa
26. e in the class declaration is also part of the im plicit object literal This class might be used as follows def fergus aCat ofColour Colour Tortoiseshell named Fergus This creates an object with fields colour set to Colour Tortoiseshell name set to Fergus and miceEaten initialized to 0 prints The Cat Fergus has been created and binds fergus to this object The above declaration for aCat is equivalent to the following nested object declarations James Where does the name CatFactory come from Is this really really what we want If so we need to say so lt def aCat object a cat factory method ofColour c Colour named n String object the cat herself def colour Colour c def name String n var miceEaten 0 method eatMouse miceEaten miceEaten 1 print The Cat name has been created If the programmer wants a factory object with more than one method she is free to build such an object using nested object constructors Revision 1261 committed on 2013 08 14 00 06 00Z by kim 13 Kim gt We also need to discuss whether the CatFactory there really is equivalent to the CatFactory on the previous page As you might expect the problem is extending it especially if there were protected methods lt 4 7 3 Inheritance Grace class declarations supports inheritance with single subclassing mul tiple subtyping like Java by way o
27. eEaten 1 print The Cat name has been created to bind a name to an object Repeated invocations of the reader method unnamedCat return the same object Andrew gt Do all of the names declared in a scope come into existence simultane ously when that scope is first entered lt James That s the aim Minigrace does not do that lt 7 2 Classes Class declarations combine the definition of a factory object with the defini tion of a single object creation method on that object This method creates instances of the class A class declaration is syntactically a combination of an object literal and a method declaration everything in the body of the class declaration can be imagined as being part of an implicit object literal that is executed each time an instance of the class is created 12 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Examples class aCat ofColour c Colour named n String def colour Colour c def name String n var miceEaten 0 method eatMouse miceEaten miceEaten 1 print The Cat name has been created This declares a class binds it to the name aCat and declares an instance creation method on that class called ofColour named This method takes two arguments and returns a newly created object with the fields and meth ods listed Creating the object also has the side effect of printing the given string since executable cod
28. ent a type Mini grace currently implements outer dynamically as a method that resolves to the enclosing object So I think that there is no way to explicitly refer to a shadowed name in an enclosing method lt 4 confirm super or other mechanism for requesting overridden methods 8 6 Kim gt We seem to have settled on super lt 4 5 confirm outer as meaning the object enclosing self Kim Current decision Kim gt My e mail to grace core raises issues with understanding outer Cur rently it is the self for the object enclosing the current self 6 update grammar to incude outer 8 7 7 confirm rules on named method argument parenthesization 8 how are mutually recursive names initialised 9 make the def keyword optional or remove it or return to const Kim gt No leave it as it is lt Revision 1261 committed on 2013 08 14 00 06 00Z by kim 39 10 11 12 13 14 15 16 17 18 19 20 21 support multiple constructors for classes Kim gt Show how it is done but no need for separate syntax lt where should we draw the lines between object constructor expres sions named object declarations class declarations and hand built classes Kim Don t understand what the issue is lt what s the difference between class FOO and def FOO class for various values
29. f an inherits C clause in a class decla ration or object literal A new declaration of a method can override an existing declaration but overriding declarations must be annotated with is override Overrid den methods can be accessed via super calls see 48 6 It is a static error for a field to override another field or a method This example shows how a subclass can override accessor methods for a variable defined in a superclass in this case to always return 0 and to ignore assignments Andrew gt would like the lt override gt annotation to come after the name of the method not before the method keyword lt James gt We need a coherent story about annotations lt class aPedigreeCat ofColour aColour named aName inherits aCat ofColour aColour named aName var prizes 0 method miceEaten is override 0 method miceEaten n Number is override return ignore attempts to change it The right hand side of an inherits clause is restricted to be an expression that creates a new object such as the name of a class followed by a request on its constructor method or a request to copy an exiting object James gt need to talk about inherits on object declarations lt When executing inherited code self is bound to the object under con struction self requests are resolved in the same way as the finally constructed object def and var initialisers and inline code are r
30. f these types I ve listed some here but it would be better to just write out the full type definitions lt e None an uninhabited type None conforms to all other types Kim gt Perhaps we should consider undefined as a member lt Andrew Since one can nerver dereference an in defined variable one can never request a method from undefined lt e Done the type of the object returned by assignments and methods that have nothing interesting to return Andrew gt All other types conform to Done lt The only method on such objects is asDebugString Kim Minigrace includes and That seems reasonable to me lt e Object the common interface of most objects Object can also be written Kim t has methods and asString We should specify that these are part of all types How do we distinguish between Done and Object structurally e Boolean methods for true and false Kim Has operations methods string concatenation amp amp prefix not not ifType asString and match lt e Number numbers Kim Has operations methods l 4 lt lt gt gt asString prefix inBase truncate and match lt 30 Revision 1261 committed on 2013 08 14 00 06 00Z by kim e String strings and individual characters Kim Has
31. fine matches lt 11 5 Variant Types Variables with untagged retained variant types written T1 T2 Tn may refer to an object of any one of their component types No objects actually have variant types only expressions The actual type of an object referred to by a variant variable can be determined using that object s reified type information Andrew reworded the above Is it what we mean a The only methods that may be requested via a variant type are methods with exactly the same declaration across all members of the variant Option methods with different signatures may be requested at the most most specific argument types and least specific return type Kim f we choose this we should write it more carefully Variant types are retained as variants they are not equivalent to the object type which describes all common methods This is so that the ex haustiveness of match case statements can be determined statically Thus the rules for conformance are more restrictive SSI ETES T S lt S amp T lt T gt S T lt S T Kim gt Added this example Does it help a The following example illustrates the limitations Suppose type S m A gt B n C gt D type T m A gt B k E gt F type U m A gt B Then U fails to conform to S T even though U contains all methods continued in both S and T Kim fix font of S T U
32. he good ideas come from these languages the bad ideas are all our fault 20 38 Revision 1261 committed on 2013 08 14 00 06 00Z by kim A To Be Done As well as the large list in Section I of features we haven t started to design this section lists details of the language that remain to be done 1 specify full numeric types Kim Should be single type not types lt 2 Block apply How should we spell apply run Kim decided on apply lt 4 Andrew Because mapping can be represented by Maps and Func tions thee is an argument to spell apply and at he same way at is also shorter than apply lt 3 confirm method lookup algorithm in particular relation between lex ical scope and inheritance see 8 Out then Up Is that enough Does the no shadowing rule work If it does is this a problem Kim gt We seem to have given up on the no shadowing rule The general rule for name lookup is that all paths are explored If two paths give same name then the using occurrence must be annotated with self or some number of outers to disambiguate the use lt Andrew can t see any reason to allow method or block temporaries and parameters to shadow variables from an enclosing lexical scope But method names are different programmers are not free to choose their own method names because they may have to implem
33. hod If a method is annotated is confidential it can be requested only on the target self or super This means that such a method is accessible to the object that contains it and to inheriting objects but not to client objects Andrew gt Here target means the syntactic thing to the left of the dot while re ceiver means the dynamic thing that gets the request I m not sure if these are the right names Methods can be explicitly annotated as is public this has no effect unless a dialect changes the default encapsulation Andrew f this is possible lt Grace does not have private methods Thus all methods can be overrid den 8 8 2 Fields Variables and definitions var and def declarations immediately inside an object constructor create fields in that object These fields behave like other variables and definitions but also behave like methods in that requests on the object can be used to access them A field declared as var x can be read using the request x and assigned to using the assignment request x see q8 2 A field declared as def y can be read using the request y and cannot be assigned By default fields are confidential they can be accessed and assigned from the object itself and from inheriting objects but not from clients Andrew What about from inner objects Can I say outer confidentialField In other words these requests can be made only on self and s
34. ier bothAll ensures parses take the same length def identifier rule both identifierString not reservedidentifier both doesn t ensure parses take the same length def identifier rule guard identifierString s gt parse s with reservedidentifier end probably works but runs out of stack anything in this list needs to be in reservedidentifier below or it won t do what you want def superld symbol super def extendsld symbol extends def inheritsld symbol inherits def classld symbol class def objectld symbol object def typeld symbol type def whereld symbol where def defld symbol def def varld symbol var def methodld symbol method def prefixld symbol prefix 44 Revision 1261 committed on 2013 08 14 00 06 00Z by kim def interfaceld symbol interface def reservedidentifier rule selfLiteral superld extendsld inheritsld classid objectld typeld whereld defld varld methodld prefixld interfaceld more to come def reservedOp rule assign equals dot arrow colon semicolon this is not quite right Revision 1261 committed on 2013 08 14 00 06 00Z by kim 45 References 1 2 3 4 5 6 7 8 9 10 11 12 Eric Allen David Chase Joe Hallett Victor Luchangco Jan Willem Maessen Sukyoung Ryu Guy L Steele Jr and Sam Tobin Hochstadt The Fortre
35. ign of Design 2010 First year university students learning programming in CS1 and CS2 classes that are based on object oriented programming a The courses may be structured objects first or imperative first Is it necessary to support procedures first b The courses may be taught using dynamic types static types or both in combination in either order c We aim to offer some but not necessarily complete support for functional first curricula primarily for courses that proceed rapidly to imperative and object oriented programming University students taking second year classes in programming algo rithms and data structures concurrent programming software craft and software design Faculty and teaching assistants developing libraries frameworks ex amples problems and solutions for first and second year programming classes Programming language researchers needing a contemporary object oriented programming language as a research vehicle Designers of other programming or scripting languages in search of a good example of contemporary OO language design Revision 1261 committed on 2013 08 14 00 06 00Z by kim 3 3 Syntax Much of the following text assumes the reader has a minimal grasp of computer terminology and a feeling for the structure of a program Kathleen Jensen and Niklaus Wirth Pascal User Manual and Report Grace programs are written in Unicode Reserved words are
36. ing is based around the Pattern objects which are objects that respond to a request match anObject The pattern tests whether or not the argument to match matches the pattern and returns a MatchResult which is either a SuccessfulMatch or a FailedMatch An object that has type SuccessfulMatch behaves like the boolean true but also responds to the requests result and bindings An object that has type FailedIMatch behaves like the boolean false but also responds to the requests result and bindings Andrew gt This needs to be completed started using the information in the DLS paper but soon got confused The tone if not the content of much of what follows is not appropriate for the spec lt result is the return value typically the object matched and the bindings are a list of objects that may be bound to intermediate variables generally used for destructuring objects For example in the scope of this Point type type Point x gt Number 24 Revision 1261 committed on 2013 08 14 00 06 00Z by kim y gt Number extract gt List lt Number gt implemented by this class class aCartesianPoint x x Number y y Number gt Point method x x method y y method extract aList with x y these hold def cp aCartesianPoint new 10 20 Point match cp result returns cp Point match cp bindings returns an empty list Point match true returns FailedMat
37. methods but the receiver of a binary method must always be explicit A binary operator method is named by one or more operator characters provided that the operator is not a reserved symbol of the Grace language So for example and are valid operator symbols but is not because it is reserved Andrew This rules out as an operator Do we want to reconsider lt Most Grace operators have the same precedence it is a syntax error for two different operator symbols to appear in an expression without paren thesis to indicate order of evaluation The same operator symbol can be requested more than once without parenthesis such expressions are evalu ated left to right Four simple arithmetic operators do have precedence and over and James gt James and Andrew hate this exception with a vengeance but understand why it is here lt Andrew Do and have higher or lower precedence that the other operators lt Examples 14 2 3 evaluates to 6 1 2 3 evaluates to 7 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 17 1 2 3 evaluates to 9 1 2 3 evaluates to 7 1 4 4 syntax error Named method requests without arguments bind more tightly than op erator method requests The following examples show the Grace expressions on the left and the parse on the right Examples fk 1 2 i a xa b b sqrt re wm N
38. n the number of parameters Requesting the apply method evaluates the block It is an error to provide the wrong number of arguments for 1 10 do i gt print i might be implemented as method for collection do block block apply collection at i Here is another example var sum 0 def summingBlock Block lt Number Number gt i Number gt sum sum i summingBlock apply 4 sum now 4 summingBlock apply 32 sum now 36 Blocks are lexically scoped inside their containing method or block A naked block literal that is a block literal that is neither the target of a method request nor an argument is a syntax error The body of a block consists of a sequence of declarations and expressions 8 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 6 Declarations Declarations of constants and variables may occur anywhere within an ob ject a method or a block their scope is the whole of their defining object method or block Grace has a single namespace for all identifiers this shared namespace is used for methods constants variables and types It is an error to declare a constant or variable that shadows a lexically enclosing constant or variable 6 1 Constants Constant definitions are introduced using the def keyword they bind an identifier to the value of an initialising expression optionally at a precise type Constants cannot be re bound Examples def x 3
39. null Kim Fixed type of thing above Correct Also changed from class to type as can t use classes as types lt 34 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 11 10 Nested Types Type definitions may be nested inside other expressions for example they may be defined inside object class method and other type definitions If they are defined inside object class and other type definitions they are accessed using dot notation written o T In this way a type may be used as a specification module Types may also be imported from modules since modules are objects 11 11 Additional Types of Types Option Grace may support exact types written Type option Grace probably will probably not support Tuple types probably written Tuple lt T1 T2 Tn gt Option Grace may support selftypes written Selftype 11 12 Syntax for Types This is very basic but hopefully better than nothing Type GroundType Type amp Type Type Ground Type BasicType BasicType lt Type gt Selftype BasicType TypelD TypelD 11 13 Reified Type Information Metaobjects and Type Lit erals option Types are represented by objects of type Type Hmm should be Type lt T gt Since Grace has a single namespace so types can be accessed by requesting their names To support anonymous type literals types may be written in expressions type Type This expressi
40. objects objects with no var fields which capture only other immutable objects act as pure value objects without identity This means that a Grace implementation can support value objects using whatever implementation is most efficient either passing by reference always by passing some times by value or even by inlining fields into their containing objects and updating the field if the containing object assigns a new value 1 However implementors should pickle the stack frames that are terminated when an exception is raised so that they can be used in the error reporting machinery debugger stack trace Revision 1261 committed on 2013 08 14 00 06 00Z by kim 29 11 Types Grace uses structural typing 7 Types primarily describe the re quests objects can answer Fields do not directly influence types except in so far as a field with publicly visible accessor methods cause those methods to be part of the type and in general to be visible to unconstrained clients Unlike in other parts of Grace Type declarations are always statically typed and their semantics may depend on the static types The main case for this is determining between identifiers that refer to types and those that refer to constant name definitions introduced by def which are interpreted as Singleton types 11 1 Basic Types Grace s standard prelude defines the following basic types Kim We should specify the signature of each o
41. on 2013 08 14 00 06 00Z by kim typeDeclaration def varDeclaration rule varld identifier opt colon typeExpression opt assign expression def defDeclaration rule defld identifier opt colon typeExpression equals expression def methodDeclaration rule methodld methodHeader methodReturnType Brace innerCodeSequence rBrace def classDeclaration rule classld identifier dot classHeader methodReturnType Brace inheritsClause codeSequence rBrace whereClause whereClause def oldClassDeclaration rule classld identifier IBrace opt genericFormals blockFormals arrow codeSequence rBrace warning order here is significant def methodHeader rule accessingAssignmentMethodHeader accessingMethodHeader assignmentMethodHeader methodWithArgsHeader unaryMethodHeader operatorMethodHeader prefixMethodHeader def classHeader rule methodWithArgsHeader unaryMethodHeader def inheritsClause rule opt inheritsld expression semicolon def unaryMethodHeader rule identifier genericFormals def methodWithArgsHeader rule firstArgumentHeader repsep argumentHeader opt ws def firstArgumentHeader rule identifier genericFormals methodFormals def argumentHeader rule identifier methodFormals def operatorMethodHeader rule otherOp oneMethodFormal def prefixMethodHeader rule opt ws token prefix
42. on returns the type metaobject representing the literal type Kim gt don t understand the need for this Can someone give an example lt 4 11 14 Type Assertions option Type assertions can be used to check conformance and equality of types assert B lt A B conforms to A Revision 1261 committed on 2013 08 14 00 06 00Z by kim 35 assert B lt foo _ C gt D B had better have a foo method from C returning D assert B A C 11 15 Notes 1 2 9 10 12 The Sanity Check these rules Kim What s the relationship between type members across inheritance and subtyping Kim No variance in type definitions when inherit On matching How does destructuring match works What s the pro tocol Who defines the extractor method not sure why this is here Kim gt Doesn t this go back in matching section lt 4 can a type extend another type Kim Yes though we use amp to denote it lt Kim May be an issue and distinction if we allow SelfType lt Structural typing means we neither need nor want any variance anno tations Because Grace is structural programmers can always write an anonymous structural type that gives just the interface they need or such types could be stored in a library Kim Yes Move this somewhere reasonable in the
43. operations size ord at iter substringFrom to replace with hash code indices asString asNumber match lt e Pattern pattern used in match case statements In addition variables can be annotated as being Dynamic Dynamic is not a type but a label that the type system uses when reasoning about the values of expressions Dynamic is also implicitly assigned to parameters and variables for which no type is declared 11 2 Types Types define the interface of objects by detailing their public methods and the types of the arguments and results of those methods Types can also contain definitions of other types The various Cat object and class descriptions see would produce objects that conform to an object type such as the following Notice that the public methods implicitly inherited from Object are implicitly included in all types t colour gt Colour name gt String miceEaten gt Number miceEaten _ Number gt Done For commonality with method declarations method arguments are nor mally named in type declarations These names are useful when writing specifications of the methods If a parameter name is omitted it must be replaced by an underscore The type of a parameter may be omitted in which case the type is Dynamic 11 3 Type Declarations Types and generic types may be named in type declarations type MyCatType color gt Colour name g
44. presented by Strings of length 1 Strings are immutable Grace values see 10 and so may be interned Strings conform to the protocol of an immutable Index ableCollection and Grace s standard library includes mechanisms to support efficient incremental string construction Escape Meaning Escape Meaning backslash single quote yn double quote b backspace n line feed r carriage return t tab l unicode NEL f page down e escape left brace right brace space non breaking space Table 1 Grace string escapes Examples Hello World t The End of the Line n A 4 3 1 String interpolation Within a string literal expressions enclosed in braces are treated specially The expression is evaluated the asString method is requested on the resulting object and the resulting string is inserted into the string literal in place of the brace expression Examples Adding a to b gives a b Revision 1261 committed on 2013 08 14 00 06 00Z by kim 7 Guillemets may be used for strings that don t support the interpolation feature 5 Blocks Grace blocks are lambda expressions they may or may not have parameters If a parameter list is present the parameters are separated by commas and the list is terminated by the gt symbol do something i gt i 1 sum next gt sum next Blocks construct objects containing a method named apply apply n apply n m depending o
45. r etc these methods are treated as if they are sent to outer Methods defined in a dialect are treated as if they are defined on an outer level enclosing object Andrew Add Xref to as yet unwritten section on dialects 9 2 Case The match case construct which attempts to match its first argument against a series of patterns introduced by case Patterns support destructuring Revision 1261 committed on 2013 08 14 00 06 00Z by kim 23 Andrew gt The description of blocks in j3 needs to be enhanced to talk about blocks like the Ist and 3rd examples below lt Examples match x case 0 gt Zero match against a literal constant case s String gt print s typematch binding s identical to block with typed parameter case pi gt print Pi pi match against the value of an expression requires parenthesis case _ Some v gt print v typematch binding a variable looks like a block with parameter case _ gt print did not match match against placeholder matches anything The case arguments are patterns objects that understand the request match and return a MatchResult which is either a SuccessfulMatch object or a FailedMatch object Each of the case patterns is requested to match x in turn until one of them returns SuccessfulMatch v the result of the whole match case construct is v 9 2 1 Patterns Pattern match
46. rals written as a decimal number between 2 and 35 representing the radix a leading x and a string of digits where the digits from 10 to 35 are represented by the letters A to Z in either upper or lower case As a special case a radix of 0 is taken to mean a radix of 16 Explicit radix numerals may optionally be preceded by a minus 3 Base exponent numerals always in decimal which use e as the expo nent indicator Base exponent numerals may optionally be preceded by a minus All literals evaluate to exact rational numbers explicit conversions such as f64 must be used to convert rationals to other objects Examples 1 1 42 3 14159265 13 343e 12 414 45e3 16xF00F00 2x10110100 Oxdeadbeef Radix zero treated as 16 4 2 Booleans The predefined constants true and false denote the only two values of Grace s Boolean type Boolean operators are written using amp amp for and for or and prefix for not Examples P amp amp Q toBe toBe not Short circuit a k a non commutative boolean operators take blocks as their second argument Examples 6 Revision 1261 committed on 2013 08 14 00 06 00Z by kim P andAlso Q toBe orElse toBe 4 3 Strings and Characters String literals in Grace are written between double quotes Strings literals support a range of escape characters such as t b and also escapes for Unicode these are listed in Table I Individual characters are re
47. rens rule Paren replsep drop opt ws expression comma rParen def implicitSelfRequest rule requestWithArgs rep1sep unaryRequest dot def primaryExpression rule literal nonNakedSuper implicitSelfRequest parenExpression def parenExpression rule IParen replsep drop opt ws expression semicolon rParen TODO should parenExpression be around a codeSequence def nonNakedSuper rule superld not not operator Brack generics def genericActuals rule opt IGeneric opt ws replsep opt ws typeExpression opt ws opt ws comma opt ws opt ws rGeneric def genericFormals rule opt IGeneric rep1sep identifier comma rGeneric def whereClause rule repdel whereld typePredicate semicolon def typePredicate rule expression wherever genericFormals appear there should be a whereClause nearby literals def literal rule stringLiteral selfLiteral blockLiteral numberLiteral objectLiteral tupleLiteral typeLiteral def stringLiteral rule opt ws doubleQuote rep stringChar doubleQuote opt ws def stringChar rule drop backslash escapeChar anyChar space def blockLiteral rule Brace opt matchBinding blockFormals arrow innerCodeSequence rBrace def selfLiteral symbol self def numberLiteral trim DigitStringParser new def objectLiteral rule objectld IBrace inhe
48. ritsClause codeSequence rBrace def tupleLiteral rule IBrack repsep expression comma rBrack def typeLiteral rule typeld opt ws nakedTypeLiteral Revision 1261 committed on 2013 08 14 00 06 00Z by kim 43 def nakedTypeLiteral rule IBrace opt ws repdel methodHeader opt ws rBrace methodReturnType semicolon whereClause terminals def backslash token doesn t belong here doesn t work if left below def doubleQuote token def space token def semicolon rule symbol opt trim newLine def colon rule both symbol not assign def newLine symbol n def Paren symbol def rParen symbol def IBrace symbol def rBrace symbol def IBrack symbol def rBrack symbol def IrBrack symbol def arrow symbol gt def dot symbol def assign symbol def equals symbol def IGeneric token lt def rGeneric token gt def comma rule symbol def escapeChar CharacterSetParser new bnrtlfe def azChars abcdefghijklmnopqrstuvwxyz def AZChars ABCDEFGHIJKLMNOPQRSTUVWXYZ def otherChars 12345678907 0 amp _ lt gt def anyChar CharacterSetParser new azChars AZChars otherChars def identifierString trim GraceldentifierParser new def identifier rule bothAll trim identifierString not reservedidentif
49. rt TR97 13 Monash University Department of Computer Science and Software Engineering 1997 33 Ole Lehrmann Madsen Birger Meller Pedersen and Kristen Nygaard Object Oriented Programming in the BETA Programming Language Addison Wesley 1993 34 Donna Malayeri and Jonathan Aldrich Integrating nominal and struc tural subtyping In ECOOP 2008 35 Bertrand Meyer Object oriented Software Construction Prentice Hall 1988 36 Bertrand Meyer Eiffel The Language Prentice Hall 1992 37 Robin Milner Mads Tofte and Robert Harper The Definition of Stan dard ML MIT Press 1990 38 Peter Naur The European side of the development of ALGOL In History of Programming Languages I pages 92 139 ACM Press 1981 39 Martin Odersky The Scala language specification version 2 8 Technical report Programming Methods Laboratory EFPL July 2010 48 40 41 42 43 44 45 46 47 48 49 50 51 Revision 1261 committed on 2013 08 14 00 06 00Z by kim Martin Odersky and Matthias Zenger Scalable component abstractions In OOPSLA 2005 Alan J Perlis The American side of the development of ALGOL In History of Programming Languages I pages 75 91 ACM Press 1981 Martin Richards and Colin Whitby Stevens BCPL the language and its compiler Cambridge University Press 1980 Andrew Shalit The Dylan reference manual the definitive guide to the new object oriented dynami
50. ss language specification version 1 05 Technical report Sun Microsystems Inc March 2007 Henry G Baker Equal rights for functional objects or the more things change the more they are the same OOPS Messenger 4 4 October 1993 Gavin M Bierman Erik Meijer and Mads Torgersen Lost in transla tion formalizing proposed extensions to Cf In OOPSLA 2007 Gavin M Bierman Erik Meijer and Mads Torgersen Adding dynamic types to Ct In ECOOP 2010 Andrew P Black Eric Jul Norman Hutchinson and Henry M Levy The development of the Emerald programming language In History of Programming Languages III ACM Press 2007 Gilad Bracha Newspeak programming language draft specification ver sion 0 0 Technical report Ministry of Truth 2009 Gilad Bracha and David Griswold Stongtalk Typechecking Smalltalk in a production environment In OOPSLA ACM Press 1993 Gilad Bracha Peter von der Ah Vassili Bykov Yaron Kashai William Maddox and Eliot Miranda6 Modules as objects in Newspeak In ECOOP 2010 Tim Budd A Little Smalltalk Addison Wesley 1987 Luca Cardelli Type systems In Allen B Tucker editor Computer Science Handbook chapter 97 CRC Press 2nd edition 2004 Luca Cardelli James Donahue Lucille Glassman Mick Jordan Bill Kalsow and Greg Nelson Modula 3 reference manual Technical Report Research Report 53 DEC Systems Research Center SRC 1995 J Dedecker T Van Cutsem S Mostinckx T
51. t String care only about names and colours type MyGenericType lt A B gt where A lt Hashable B lt DisposableReference Revision 1261 committed on 2013 08 14 00 06 00Z by kim 31 at _ A put _ B gt Boolean j cleanup _ B Grace has a single namespace types live in the same namespace as meth ods and variables Kim There is no advantage to writing constraints on the type parameters in generic type definitions Those really only make sense for classes and methods In particular you can t write a type definition that only makes sense when constrained lt James Declaring generic types with like this is very confusing How do we write an anonymous structural generic type Do we need to Or aren t we able to write such types lt Kim gt suppose we could instead write lt type MyGenericType lt A B gt at _ A put _ B gt Boolean cleanup __ B Kim gt We still do have a problem inserting type definitions in a context How can we distinguish it from a block Perhaps we need to say that only type names possibly with a prefix path and type parameters after can appear in a context expecting a type expression lt 4 11 4 Relationships between Types Conformance Rules The key relation between types is conformance We write B lt A to mean B conforms to A that is that B is a subtype of A A is a supertype of B This
52. t succeeded although closer to the type declaration less gratuitous and perhaps less error prone would result in two errors about variable shadowing Self Matching For this to work the main value types in Grace the main literals Strings Numbers must be patterns that match themselves That s what lets things like this work method fib n Number match n case 0 gt 0 case 1 gt 1 case gt fib n 1 fib n 2 With this design there is a potential ambiguity regarding Booleans true false as an expression is very different from true false as a com posite pattern Unfortunately if Booleans are Patterns then there s no way the type checker can distinguish these two cases If you want to match against objects that are not patterns you can lift any object to a pattern that matches just that object by writing e g LiteralPattern new o option or something shorter like a prefix 9 3 Exceptions under discussion Grace supports exceptions which can be raised and caught Exceptions are raised by requesting the raise method from an Exception object with a string argument explaining the problem userException raise Oops Exceptions are caught by a catch 1 case 1 case 1 construct that syn tactically parallels match 1 case 1 Andrew This is a bad name try catch catch would make more sense catch def f File open
53. t be provided with type ar guments Generic type parameters may be constrained with where clauses Examples class aVector ofSize size lt T gt var contents Array size size method at index Number gt T return contents at method at index Number put elem T class aSortedVector ofSize lt T gt where T lt Comparable lt T gt Revision 1261 committed on 2013 08 14 00 06 00Z by kim 15 Andrew gt That last lt needs to be a matches think lt 8 Method Requests Grace is a pure object oriented language Everything in the language is an object and all computation proceeds by requesting an object to execute a method with a particular name The response of the object is to execute the method We distinguish the act of requesting a method what Smalltalk calls sending a message and executing that method Requesting a method involves only the object receiving the request the method name and possibly some arguments In contrasty executing the method involves the code of the method which is local to the receiver 8 1 Named Requests A named method request is a receiver followed by a dot then a method name an identifier then any arguments in parentheses Parentheses are not used if there are no arguments To improve readability a the name of a method that takes more than one parameter may comprise multiple parts or words with argument lists between the words
54. un in order from the top most superclass down to the bottom subclass Accesses to unitialised vars and defs raise uninitialised exceptions 6 2 7 4 Understanding Inheritance under discussion Grace s class declarations can be understood in terms of a flattening transla tion to object constructor expressions that build the factory object Under 14 Revision 1261 committed on 2013 08 14 00 06 00Z by kim standing this translation lets expert programmers build more flexible facto ries The above declaration for class aPedigreeCat is broadly equivalent to the following nested object declarations not considering types modules and renaming superclass methods so that an object s method names must actually be unique def aPedigreeCat object a cat factory method ofColour c Colour named n String gt PedigreeCat object the cat herself def colour Colour c def name String n var Cat__miceEaten 0 ugly very ugly var prizes 0 method miceEaten 0 method miceEaten n Number return ignore attempts to change it object method object Andrew gt This translation is confusing because it re writes both the class syntax and the inheritance syntax suggest that we do these one at a time in the appropriate places lt 4 7 5 Generic Classes Classes may optionally be declared with generic type parameters The cor responding requests on the factory methods mus
55. uper Andrew gt and outer a However fields can be made available to client objects using annotations The annotation readable can be applied to a def or var declaration and makes the accessor request available to any object The annotation writable can be applied to a var declaration and makes the assignment request avail able to any object It is also possible to annotate a field declaration as 20 Revision 1261 committed on 2013 08 14 00 06 00Z by kim public In the case of a def public is equivalent to readable Andrew Do we want this It s an option In the case of a var public is equivalent to readable writable Fields and methods share the same namespace The syntax for variable access is identical to that for requesting a reader method while the syntax for variable assignment is identical to that for requesting an assignment method This means that an object cannot have a field and a method with the same name and cannot have an assignment method x as well as a var field x Examples object def a 1 Confidential access to a def b is public 2 Public access to b def c is readable 2 Public access to c var d 3 Confidential access and assignment var e is readable Public access and confidential assignment var f is writable Confidential access public assignment var g is public Public access and assignment var h is readable writable Public
56. written in the ASCII subset of Unicode As a matter of policy the names of methods defined in the required libraries are also restricted to the ASCII subset of Unicode James and the character 74 3 1 Layout Grace uses curly brackets for grouping and semicolons as statement termi nators and infers semicolons at the end of lines Code layout cannot be inconsistent with grouping code with punctuation while stream hasNext do print stream read i code without punctuation while stream hasNext do print stream read A line break followed by an increase in the indent level implies a line continuation whereas line break followed by the next line at the same or lesser indentation implies a semicolon if one is permitted syntactically James We need to define this precisely 3 2 Comments Grace s comments start with a pair of slashes and are terminated by the end of the line as in C and Java However comments are not treated as white space Each comment is conceptually attached to the smallest im mediately preceding syntactic unit except that comments following a blank line are attached to the largest immediately following syntactic unit comment to end of line 4 Revision 1261 committed on 2013 08 14 00 06 00Z by kim 3 3 Identifiers Identifiers must begin with a letter which is followed by a sequence of zero or more letters digits and characters An underscore
57. ypeExpression opt ws opt guard typeOp s gt otherOperator s true replsep basicTypeExpression opt ws guard typeOp s gt s otherOperator def basicTypeExpression rule nakedTypeLiteral literal pathTypeExpression parenTypeExpression if we keep this note that in a typeExpression context a is interpreted as type a otherwise as the block a def pathTypeExpression rule opt superld dot replsep identifier genericActuals dot def parenTypeExpression rule IParen typeExpression rParen statements def statement rule returnStatement expression opt assignmentTail do we need constraints here on which expressions can have an assignment Tail could try to rewrite as options including expression arrayAccess assignment Tail expression dot identifier assignmentTail def returnStatement rule symbol return opt ws opt expression doesn t need parens def assignmentTail rule assign expression expressions def expression rule opExpression def opExpression rule rep1sep addExpression otherOp def opExpression rule this complex rule ensures two different otherOps have no precedence var otherOperator addExpression opt ws opt guard otherOp s gt otherOperator s true replsep addExpression opt ws guard otherOp s gt s otherOperator def a

Download Pdf Manuals

image

Related Search

Related Contents

Alliance Laundry Systems 135 Washer User Manual  Infomairie - Site officiel de la ville de Saint  VHF CASQUE SANS FIL - Innovative Technology        User Manual_IQV20 Cordless Impactool  implementação de uma avaliação nacional de  Innergie mCube Mini 90    

Copyright © All rights reserved.
Failed to retrieve file