Home

The Grace Programming Language Draft Specification Version 0.353

image

Contents

1. 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 Id spec tex 323 2012 04 02 21 53 22Z black 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 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 3 1 Layout Grace uses curly brackets for grouping and semicolons as statement termi nators and in
2. MatchAndDestructuringPattern new Pair VariablePattern new b VariablePattern new c Finally the translated pattern and block are glued together via a LambdaPattern LambdaPattern new mypat _ a b c gt a b c The translation is as follows e Le e Le WildcardPattern VariablePattern v v fresh unbound variable v error bound variable v e e f g literal e not otherwise translated AndPattern new VariablePattern new v e MatchAndDestructuringPattern new e f g literal e Id spec tex 323 2012 04 02 21 53 22Z black 21 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 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 patterns potentially created via one or more shorthand syntaxes short hands a
3. BasicType BasicType lt Type gt Selftype BasicType TypelD TypelD TypelD TypelD 11 14 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 expression returns the type metaobject representing the literal type 11 15 Type Assertions option Type assertions can be used to check conformance and equality of types assert B lt A B conforms to A B is a subtype of A assert B lt foo _ C gt D Id spec tex 323 2012 04 02 21 53 22Z black 29 B had better have a foo method from C returning D 11 16 Notes 1 oN o ao 10 11 12 13 14 15 16 17 18 19 Option Classes define a type of the same name currently this is NOT part of Grace Sanity Check these rules To be done add in path types types in objects What s the relationship between type members across inheritance and subtyping Classes are not types are we sure about this Types are patterns need to be to be matched against Reified Generics formals are also patterns see above On matching How does destructuring match works What s the pro
4. b 4 x a sqrt b squared illegal b squared b squared status ok engine isOnFire amp amp wings areAttached amp amp isOnCourse 8 5 Accessing Operator Method Grace supports an operator which can be defined in libraries e g for indexing collections Examples print a 3 requests method on a with argument 3 option Grace supports a two argument accessing operator Examples 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 8 6 Super Requests 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 requests Examples super foo super bar 1 2 6 super doThis 3 timesTo foo super 1 super foo super syntax error 1 super syntax error Id spec tex 323 2012 04 02 21 53 22Z black 17 8 7 Encapsulation The design of Grace s encapsulation system has not yet begun in earnest Grace will use metadata annotations to allow the declaration of confi dential methods that is methods that can be requested only from self or super 8 8 Generic Method Requests Methods may optionally be r
5. self 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 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 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 Id spec tex 323 2012 04 02 21 53 22Z black 11 A constant can be defined by an object literal such as def unnamedCat object def colour Colour Colour tabby def name String Unnamed var miceEaten 0 to bind a name to an object Repeated invocations of the reader method unnamedCat return the same object 7 2 Classes Objects literals have no provision for initializing the constant and variable attributes of the created object other than via lexical scope Class declarations combine the definition of an object with the definition of a factory object where the factory object has a method t
6. Grace s encapsulation system will control the accessibility of each of these methods You can think of the real instance variable as having a unique secret name which is known only to the accessor methods Block and method temporary variables really exist and can be the tar gets of real assignment statements It s a deliberate design decision that assignment to a local variable and requesting an assignment method on an object look identical It is an error to declare a block or method temporary variable that shadows an enclosing method or assignment method see 6 Id spec tex 323 2012 04 02 21 53 22Z black 9 Assignments return Nothing Void None etc 6 3 Methods Methods are declared with the method keyword a name optionally an argument 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 None 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 Assignment methods are named by an identifier suffixed with Prefix operator methods are named prefix followed by the operator character s Methods may have repeated parameters to allow them t
7. X is None For example this Point type type Point x gt Number y gt Number extract gt Tuple lt Number Number gt implemented by this CartesianPoint class class CartesianPoint new x Number y Number gt Point def x x def y y def extract x y then these hold def cp CartesianPoint new 10 20 Point match cp bindings returns 10 20 Point match true returns MatchFailure 20 Id spec tex 323 2012 04 02 21 53 22Z black 9 2 2 Translating Matching blocks Matching blocks are blocks with one formal parameter This parameter may be a pattern rather than just being a fresh variable potentially with a type Matching blocks are themselves patterns one argument matching block with parameter type A and return type R also implements Pattern lt R None 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 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
8. _ SuccessfulMatch gt print succeeded while writing just match rv case FailedMatch gt print failed case SuccessfulMatch gt print 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 i With this design there is a potential ambiguity regarding Booleans true false as an expression is very different from true false as a composite pattern Unfortunately if Booleans are Patterns then there s no way the type checker can distinguish these two cases Id spec tex 323 2012 04 02 21 53 22Z black 23 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 Option matches could also be written as an operator e g p q for p match q succeeded This might be good for scripts but really it s proba bly a very bad idea in general that way lies Thorn 9 3 Exceptions under discussion Grace supports basic uncheck
9. 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 def operatorChar CharacterSetParser new amp t 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 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 argumentsInParens rule IParen replsep drop opt ws expression comma rParen def implicitSelfRequest rule requestWithArgs replsep unaryRequest dot def primaryExpression rule
10. 02 21 53 22Z black 15 operator method is one or more operator characters and may not match a reserved symbol for example is reserved but is not Most Grace operators have the same precedence it is a syntax error for two different operator symbols to appear in an expression without parenthe sis to indicate order of evaluation The same operator symbol can be sent more than once without parenthesis and is evaluated left to right Four simple arithmetic operators do have precedence and over and Examples 14 2 3 evaluates to 6 1 2 3 evaluates to 7 1 2 x 3 evaluates to 9 1 2 3 evalutes to 7 1 4 x 4 syntax error Named method requests without arguments bind more tightly than op erator method requests The following examples show first the Grace ex pressions as they would be written followed by the parse Examples ea Ee a x a b b sqrt a x a b xb sqrt a a b b sqrt a x a b xb sqrt a xa bx b a x a b xb at b c a b c a b c a b c 8 4 Unary Prefix Operator Method Grace supports unary prefix operator methods since Grace does not sup port binary operator methods with implicit receivers there is no syntactic ambiguity Prefix operators bind with the same precedence as method requests with no arguments and therefore need parenthesis to disambiguate 16 Id spec tex 323 2012 04 02 21 53 22Z black Examples
11. 53 22Z black 13 and renaming superclass methods so that an object s method names must actually be unique def aPedigreeCat object the cat factory method ofColour aColour Colour named aName String gt PedigreeCat object the cat herself def colour Colour aColour def name String aName lt lt private gt gt var Cat_miceEaten 0 ugly super ugly var prizes 0 method miceEaten 0 method miceEaten n Number return ignore attempts to change it object method object 7 5 Generic Classes Classes may optionally be declared with generic type parameters The cor responding requests on the factory methods must be provided with type arguments Generic type parametrs may be constriained 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 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 When speaking of Grace we distinguish the act of requesting 14 Id spec tex 323 2012 04 02 21 53 22Z black a method which is exactly what Smallt
12. method requested via Dynamic returns None but the caller attempts to use that None value 11 6 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 variables The actual type of an object referred to by a variant variable can be determined using that object s reified type information The only methods that may be requested via a variant type are methods with exactly the same declaration across all members of the variant Op tion methods with different signatures may be requested at the most most specific argument types and least specific return type Id spec tex 323 2012 04 02 21 53 22Z black 27 Variant types are retained as variants they are not equivalent to the ob ject type which describes all common methods This is so that the exhaus tiveness of match case statements can be determined statically In detail S lt S T T lt S T S lt S amp T lt T gt S T lt S T 11 7 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 p gt 11 8 Union Types option Structural u
13. 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 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 11 Types Grace uses structural typing 11 34 17 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 Object the co
14. 1 The Grace Programming Language Draft Specification Version 0 353 Andrew P Black Kim B Bruce James Noble April 3 2012 Introduction This is a specification of the Grace Programming Language This spec ification is notably incomplete and everything is subject to change In particular this version does not address collection syntax and collection literals tuples vs multiple values vs multiple returns nested static type system although we ve made a start encapsulation system privacy annotations module system metadata Java s annotations C attributes final abstract etc purity and non nulls reflection assertions data structure invariants pre amp post conditions contracts regexps test support libraries including more Numeric types For discussion and rationale see http gracelang org Where this document gives options we outline choices in the lan guage design that have yet to be made Id spec tex 323 2012 04 02 21 53 22Z black 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 Design 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
15. AssignmentMethodHeader rule IrBrack assign genericFormals methodFormals 34 Id spec tex 323 2012 04 02 21 53 22Z black 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 replsep basicTypeExpression typeOp def typeOpExpression rule this complex rule ensures two different typeOps have no precedence var otherOperator basicTypeExpression opt ws opt guard typeOp s gt otherOperator s true replsep basicTypeExpression opt ws guard typeOp s gt s otherOperator def basicTypeExpressio
16. 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 Ct In OOPSLA 2007 Gavin M Bierman Erik Meijer and Mads Torgersen Adding dynamic types to Cy 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 Re port Research Report 53 DEC Systems Research Center SRC 1995 J Dedecker T Van Cutsem S Mostinckx T D Hondt and W De Meuter Ambient oriented programming in A
17. SS 1 gBeta 14 Haskell 22 Java 13 18 Kevo 46 Lua 23 Lisp 16 ML 37 Modula 2 50 Modula 3 11 Modular Smalltalk 49 Newspeak 8 6 Pascal 27 Perl 48 Racket 15 Scala 40 39 Scheme 44 Self 47 Smalltalk 19 26 9 7 Object Oriented Tur ing 21 Noney 34 and Whiteoak 17 at least we apologise if we ve missed any languages out All the good ideas come from these languages the bad ideas are all our fault 20 32 A Id spec tex 323 2012 04 02 21 53 22Z black To Be Done As well as the large list in Section 1 of features we haven t started to design this section lists details of the language that remain to be done 1 2 10 11 12 13 14 15 16 specify full numeric types Block apply 5 How should we spell apply run confirm method lookup algorithm in particular relation between lex ical scope and inheritance 8 Out then Up Is that enough Does the no shadowing rule work If it does is this a problem confirm super or other mechaism for requesting overridden methods 8 6 confirm rules on named method argument parenthesization 8 1 how are mutually recursive names initialised make the def keyword optional or remove it or return to const 86 1 post 10 02 2011 support multiple constructors for classes 7 2 where should we draw the lines between object constructor expres sion
18. ake the same length def identifier rule both identifierString not reservedidentifier def both doesn t ensure parses take the same length identifier rule guard identifierString s gt parse s with reservedldentifier 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 def def def def def def def def def def def de gt de gt superld symbol super extendsld symbol extends inheritsld symbol inherits classid symbol class objectld symbol object typeld symbol type whereld symbol where defld symbol def varld symbol var methodld symbol method prefixld symbol prefix interfaceld symbol interface reservedidentifier rule selfLiteral superld extendsld inheritsld classid objectld typeld whereld defld varld methodld prefixld interfaceld more to come reservedOp rule assign equals dot arrow colon semicolon this is not quite right 38 Id spec tex 323 2012 04 02 21 53 22Z black References 1 Eric Allen David Chase Joe Hallett Victor Luchangco Jan Willem 10 11 12 Maessen Sukyoung Ryu Guy L Steele Jr and Sam Tobin Hochstadt The Fortress language specification version 1 08 Technical report Sun
19. al rule typeld opt ws nakedTypeLiteral def nakedTypeLiteral rule IBrace opt ws repdel methodHeader methodReturnType semicolon whereClause opt ws rBrace 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 IParen 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 ABCDEFGHISJKLMNOPQRSTUVWXYZ def otherChars 12345678907 0 amp _ s lt gt def anyChar CharacterSetParser new azChars AZChars otherChars Id spec tex 323 2012 04 02 21 53 22Z black 37 def identifierString trim GraceldentifierParser new def identifier rule bothAll trim identifierString not reservedidentifier bothAll ensures parses t
20. alkers call sending a message and involves only a method name and some arguments and executing that method which involves the code of the method which is always local to the receiver of the request 8 1 Named Methods 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 long argument list may be interpolated between the words that makes up the method name This is determined by the declaration of the method If the receiver is self it may be left implicit i e the self and the dot may both be omitted canvas drawLineFrom source to destination canvas movePenToXY x y canvas movePenToPoint p print Hello world pt x Grace does not allow overloading of method names Parenthesis may be omitted where they would enclose a single argument provided that argument is a literal other than an object literal 8 2 Assignment Methods A assignment method request is an explicit receiver followed by a dot then an identifier followed by and then a single argument Examples x 3 y widget active true Assignment methods return Nothing 8 3 Binary Operator Methods Grace allows operator symbols sequences of operator characters for binary methods methods with an explicit receiver and one argument A binary Id spec tex 323 2012 04
21. breaking space Table 1 Grace string escapes Examples Hello World t The End of the Line n LEI A 4 4 String interpolation Within a string literal Grace treats expressions enclosed in braces specially The expression is evaluated the asString method is requested on the result ing object and the resulting string is inserted into the string literal in place of the brace expression Examples Adding a to b gives atb Id spec tex 323 2012 04 02 21 53 22Z black 7 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 terminated by the gt symbol do something i gt i 1 sum next gt sum next Blocks construct objects with a single method named apply apply n apply n m depending on 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 summingBlock apply 4 sum is now 4 summingBlock apply 32 sum in now 36 Blocks are lexically scoped inside their containing method or block A naked block literal that i
22. chnical Report AI Memo 349 MIT Artificial Intel ligence Laboratory December 1975 Don Syme The F draft language specification Technical report Mi crosoft 2009 Antero Taivalsaari Delegation versus concatenation or cloning is in heritance 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
23. constant case 0 gt Zero typematch binding a variable looks like a block with parameter case s String gt print s match against the value in an existing variable requiring parenthesis like Scala case pi gt print Pi pi destructuring match binding variables case _ Some v gt print v match against placeholder matches anything case _ gt print did not match 9 2 1 API Design Scary Overkill Monadic Version under dis cussion Pattern matching is based around the Pattern type type Pattern lt R X gt Id spec tex 323 2012 04 02 21 53 22Z black 19 match o Any gt MatchResult lt R X gt where X lt Tuple type MatchResult lt R X gt succeeded gt Boolean next gt N result gt R bindings gt X where X lt Tuple A pattern can test if any object matches the pattern returing a MatchResult which is either a SuccessfulMatch or a FailedMatch From a successful match the result is the return value typically the object matched and the bindings are a tuple of objects that may be bound to intermediate variables gener ally used for destructuring objects If a prefix of the object is matched any unmatched objects are returned in next A type declaration creates a singleton object that acts as a pattern If the type has an extract method that returns a tuple X is the return type of that method if not
24. e Grace class declarations supports inheritance with single subclassing mul tiple subtyping like Java by way of an inherits C clause in a class dec laration or object literal A new declaration of a method can override an existing declaration but overriding declarations must be annotated with lt override gt Overridden methods can be accessed via super calls 8 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 class aPedigreeCat ofColour aColour named aName inherits Cat ofColour aColour named aName var prizes 0 lt override gt method miceEaten 0 lt override gt method miceEaten n Number return ignore attempts to change it The right hand side of an inherits clause is restricted to be a class name followed by a correct request for that class s method 7 4 Understanding Inheritance under discussion Grace s class declarations can be understood in terms of a flattening trans lation to object constructor expressions that build the factory object Un derstanding this translation lets expert programmers build more flexible factories The above declaration for class aPedigreeCat is broadly equivalent to the following nested object declarations not considering types modules Id spec tex 323 2012 04 02 21
25. ed exceptions Exceptions are generated by requesting the raise method from an Exception class UserException raise Oops Exceptions are caught by a catch 1 case 1 construct that syntactically parallels match 1 case 1 catch def f File open data store case e NoSuchFile gt print No Such File return case e PermissionError gt print No Such File return case Exception gt print Unidentified Error System exit finally f close Exceptions can t be restarted However the stack frames that are termi nated when an exception is raised should be pickled so that they can be used in the error reporting machinery debugger stack trace catch 1 case 1 finally 1 construct and a do 1 finally 1 construct support 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 24 Id spec tex 323 2012 04 02 21 53 22Z black 1 and operators implemented as per Henry Baker s egal pred icate 2 That is immutable
26. en 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 26 Id spec tex 323 2012 04 02 21 53 22Z black This section draws heavily on the wording of the Modula 3 report 11 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 e B contains every method in A e Every B method must have the same number of arguments as A with the same distribution in multi part method names e Every method with parameters P P gt R in A must have a corresponding method in B Qj Qn gt S Argument types may be contravariant P lt Q Results types may be covariant S lt R 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 The conformance relationship is used in where clauses to constrain for mal generic type parameters of classes and methods 11 5 Any and None The type Any is the supertype of all types and may also be written as The type None is the subtype of all types There are no instances of None In particular neither undefined and nor any kind of nil is an instance of None What happens if a
27. equested with actual generic type arguments given explicitly Where a method declared with formal generic type parame ters is requested in a statically typed context without explicit actual generic type arguments the actual types arguments are inferred Examples sumSq lt Integer64 gt 10 i64 20 164 sumSq 10 i164 20 164 9 Control Flow Control flow statements in Grace are syntactically method requests While the design of the module system is not complete in fact hardly yet begun we expect that instructors will need to define domain specific control flow constructs in libraries and these constructs should 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 18 Id spec tex 323 2012 04 02 21 53 22Z black 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 for these methods are treated as if they were implicitly sent to self which implies that all objects must inherit the corresponding method 9 2 Case Grace supports a match case construct Match takes one argument and matches it against a series of blocks introduced by case Pattern matching supports destructuring Examples match x match against a literal
28. f 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 Mag azine August 1981 40 27 28 29 34 35 36 37 38 39 Id spec tex 323 2012 04 02 21 53 22Z black Kathleen Jensen and Niklaus Wirth Pascal User Manual and Report Springer 1975 Brian W Kernighan and Dennis M Ritchie The C Programming Language Addison Wesley 2nd edition 1993 Gregor Kiczales Erik Hilsdale Jim Hugunin Mik Kersten Jeffrey Palm and William G Griswold An overview of AspectJ In ECOOP 2001 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 Michael K6lling and John Rosenberg Blue a language for teaching object oriented programming In ACM Conference on Computer Sci ence Education SIGCSE 1996 Michael Kolling and John Rosenberg Blue a language for teach ing object oriented programming language specification Technical Re port TR97 13 Monash University Department of Comp
29. fers semicolons at the end of lines Code layout cannot be inconsistent with grouping code with punctuation while stream hasNext do print stream read 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 syntatically 3 2 Comments Grace s comments delimiters follow C and Java s line comments Comments are not treated as white space each comment is conceptually attached to the smallest immediately preceding syntactic unit comments following a blank line are attached to the largest immediately following syntactic unit comment to end of line 4 Id spec tex 323 2012 04 02 21 53 22Z black 3 3 Identifiers Identifiers in Grace must begin with a letter and consist of letters and digits thereafter Prime characters may be used after the first character of an identifier u D An underscore _ acts as a placeholder identifier it is treated as a fresh identifier everywhere it is used 3 4 Reserved Words and Operators Grace has the following reserved words and reserved operators The indi cates words related to design options not yet chosen assert case catch class const def extends false finally method object outer prefix raise return self Selft
30. hat creates instances of the class A class declaration is syntactically similar to the method declaration for the factory method where the body of the factory method is the body of an object literal Examples class aCat ofColour aColour named aName def colour Colour aColour def name String aName var miceEaten 0 The ofColour named method takes two arguments the object that it returns has the fields and methods listed in the body of the construc tor that follows the name of the factory method So in the above ex ample the constants colour and name are initialized from the parameters aColour and aName which are in turn initialized from the arguments to ofColour named def fergus aCat ofColour tortoiseshell named Fergus Trouble If the programmer wants a factory object with more than one method she is free to build such an object using nested object constructors The above declaration for class aCat is equivalent modulo types and modules to the following nested object declarations def aCat object the cat factory 12 Id spec tex 323 2012 04 02 21 53 22Z black method ofColour aColour Colour named aName String gt Cat object the cat herself def colour Colour aColour def name String aName var miceEaten 0 Notice that the type Cat describes the object returned from aCat ofColour named not the factory object aCat 7 3 Inheritanc
31. inus All literals evaluate to exact rational Numbers explicit conversions such as f64 must be used to convert rationals to other types Examples 1 1 42 3 14159265 13 343e 12 414 45e3 16xF00F00 2x10110100 Oxdeadbeef Radix zero treated as 16 4 2 Booleans The keywords 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 P andAlso Q toBe orElse toBe 6 Id spec tex 323 2012 04 02 21 53 22Z black 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 1 Individual characters are represented 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 Ww single quote te double quote b backspace n line feed r carriage return t tab l unicode newline page down e escape left bracket right bracket space non
32. 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 IBrack 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 replsep identifier comma rGeneric 36 Id spec tex 323 2012 04 02 21 53 22Z black 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 IBrace opt matchBinding blockFormals arrow innerCodeSequence rBrace def selfLiteral symbol self def numberLiteral trim DigitStringParser new def objectLiteral rule objectld IBrace inheritsClause codeSequence rBrace def tupleLiteral rule IBrack repsep expression comma rBrack def typeLiter
33. ll defined in standard Grace match myString case gt print null string case Regexp new a z gt print lower case case A Z s r gt print UPPER CASE case 0 9 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 tuples lists could be descructured a b gt a xb Although it would 22 Id spec tex 323 2012 04 02 21 53 22Z black be very nice it s hard to see how e g points created with 3 4 could be destructed 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 vari able declaration 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 collection will raise an error even though this is unambiguous given Grace s no shadowing rule Match statements that do nothing but match on types must distinguish syntactically from a variable declaration e g match rv case FailedMatch gt print failed case
34. mbientTalk In ECOOP pages 230 254 2006 Id spec tex 323 2012 04 02 21 53 22Z black 39 13 14 15 16 17 18 19 20 21 22 23 24 25 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 Re port 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 III pages 12 1 12 55 ACM Press 2007 Roberto Ierusalimschy Luiz Henrique de Figueiredo and Waldemar Celes The evolution o
35. mmon interface of most objects e Boolean methods for true and false Number numbers e String strings and individual characters e Pattern pattern used in match case statements Id spec tex 323 2012 04 02 21 53 22Z black 25 e Dynamic dynamically typed expressions If no types are provided on method formal parameters the types are taken as dynamic by default There is also a top type which can be written as an empty object type 11 2 Object Types Object types give the type of objects methods The various Cat object and class descriptions see 7 would produce objects that conform to an object type such as the following colour gt Colour name gt String miceEaten gt Number miceEaten _ Number gt None For commonality with method declarations method arguments may be given both names and types within type declarations A single identifier is interpreted as a formal parameter name with type Dynamic 11 3 Type Declarations Types and generic types may be named in type declarations type MyCatType color gt Colour name gt String care only about names and colours type MyGenericType lt A B gt where A lt Hashable where B lt disposable hashStore _ A _ B gt Boolean pity not just A B cleanup _ B Grace has a single namespace types live in the same namespace as methods and variables 11 4 Relationships betwe
36. n 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 assignmentTail 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 replsep addExpression otherOp Id spec tex 323 2012 04 02 21 53 22Z black 35 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 addExpression rule replsep multExpression addOp def multExpression rule replsep prefixExpression multOp def prefixExpression rule
37. nion 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 9 Type subtraction option A type written T1 T2 has the interface of T1 without any of the methods in T2 11 10 Singleton Types 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 class Some lt T gt thing T gt method isNull gt Boolean return false 28 Id spec tex 323 2012 04 02 21 53 22Z black type Option lt T gt Some lt T gt null 11 11 Nested Types Option Types may be nested inside types written T1 T2 In this way a type may be used as a specification module 11 12 Additional Types of Types Option Grace may support nullable types written Type defined as Type null and exact types written Type option Grace probably will support Tuple types probably written Tuple lt T1 T2 Tn gt We re not yet sure how Option Grace may support selftypes written Selftype 11 13 Syntax for Types This is very basic but hopefully better than nothing Type GroundType GroundType amp GroundType GroundType
38. o take a vari able number of parameters that is to provide what is often called variable arity or varargs A repeated parameters if present must be the last parameter in a part of a multipart method 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 parameter declared xargs String has the type args ImmutableCollection lt String gt Methods may be declared with generic type parameters in whcih case they must be requested with generic type arguments Gneric type parame ters may be constrained with where clauses Examples method pi 3 141592634 method greetUser print Hello World method Hother Point gt Point x other x y other y method Hother x other x y other y method Hother return x other x y other y 10 7 Id spec tex 323 2012 04 02 21 53 22Z black method foo n Number gt None print Foo currently foo now assigned n super foo n method choseBetween a Block lt None gt and b Block lt None gt gt None if Random nextBoolean then a apply else b apply method print xargs Printable gt None method sumSq lt T gt a T b T gt T where T lt Numeric a a b b class NumberFactory method prefix gt Number 0
39. pression equals expression def methodDeclaration rule methodld methodHeader methodReturnType whereClause IBrace innerCodeSequence rBrace def classDeclaration rule classld identifier dot classHeader methodReturnType whereClause IBrace inheritsClause codeSequence rBrace def oldClassDeclaration rule classid 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 otherOp forbid space after prefix def assignmentMethodHeader rule identifier assign oneMethodFormal def accessingMethodHeader rule IrBrack genericFormals methodFormals def accessing
40. s named object declarations class declarations and hand built classes 7 3 what s the difference between class FOO and def FOO class for various values of class how do factories etc relate to uninitialized 6 2 decide what to do about equality operators 10 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 Multiple Assignment 6 2 f lt T gt Type assertions should they just be normal assertions between types so e g lt could be a normal operator between types Id spec tex 323 2012 04 02 21 53 22Z black 33 17 Grace needs subclass compatibility rules 18 BRANDS Brand Brand Brand 19 weak references B 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 typeDeclaration def varDeclaration rule varld identifier opt colon typeExpression opt assign expression def defDeclaration rule defld identifier opt colon typeEx
41. s neither the target of a method request nor passed as an argument is a syntax error The body of a block consists of a sequence of declarations and expres sions 6 Declarations Def and var declarations may occur anywhere within a method or block their scope is the whole of their defining block or method 8 Id spec tex 323 2012 04 02 21 53 22Z black It is an error to declare an identifier that shadows a lexically enclosing identifier 6 1 Constants Constant definitions bind an identifier to the value of an initializer expres sion optionally at a precise type Examples def x 3 x 100 0 01 def x Number 3 means the same as the above def x Number Syntax Error x must be initialised Grace has a single namespace for methods and constants and variables and types and A constant declaration of x can be seen as creating a nullary reader method x 6 2 Variables Grace supports variable declarations using the var keyword Uninitialized variables of any type are given a special uninitialized value accessing this value is an error caught either at run time or at compile time depending on the cleverness of your implementor Examples var x 3 type of x is inferred var x Rational 3 explicit type Instance variables are reassigned using assignment methods see 8 2 A variable declaration of x can be seen as creating a reader method x and an assignment method x 1
42. s not describe a concurrent language Dif ferent concurrency models may be provided as dialects Grace does not provide overall sequential consistency Rather Grace provides 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 in terfere Grace s memory model should support efficient execution on architec tures with Total Store Ordering TSO 13 Libraries 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 Id spec tex 323 2012 04 02 21 53 22Z black 31 Thanks to Michael Homer and Ewan Tempero for their comments on drafts The Scala language specification 2 8 39 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 41 38 AmbientTalk 12 AspectJ 29 BCPL 42 Beta 33 Blue 30 31 32 C 28 C 43 Ct 4 3 Eiffel 35 36 Emerald 5 Fi 10 FH 45 FGJ 24 FJV 25 FORTRE
43. tocol Who defines the extractor method not sure why this is here Somehow do classes need to define a type that describes the objects that are created by their factory methods Note that Generic Types use angle brackets viz ImmutableCollection lt Figure gt can a type extend another type where do where clauses go method return types Structural typing means we neither need nor want any variance anno tations Becaue Grace is structural programmers can always write an anonymous structural type that gives just the interface they need or such types could be storted in a library Should ObjectTypes permit formal parameter names or not 11 2 What actually gets returned from None 86 3 11 5 Tuples 11 12 Syntax as a type Literal Tuple Syntax Nesting Serialization 30 Id spec tex 323 2012 04 02 21 53 22Z black 12 Pragmatics The 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 12 1 Garbage Collection Grace implementations should be garbage collected Safepoints where GC may occour are at any backwards branch and at any method request Grace will not support finalisation 12 2 Concurrency and Memory Model The core Grace specification doe
44. uter Science and Software Engineering 1997 Ole Lehrmann Madsen Birger Moller Pedersen and Kristen Nygaard Object Oriented Programming in the BETA Programming Language Addison Wesley 1993 Donna Malayeri and Jonathan Aldrich Integrating nominal and struc tural subtyping In ECOOP 2008 Bertrand Meyer Object oriented Software Construction Prentice Hall 1988 Bertrand Meyer Eiffel The Language Prentice Hall 1992 Robin Milner Mads Tofte and Robert Harper The Definition of Stan dard ML MIT Press 1990 Peter Naur The European side of the development of ALGOL In History of Programming Languages I pages 92 139 ACM Press 1981 Martin Odersky The Scala language specification version 2 8 Technical report Programming Methods Laboratory EFPL July 2010 Id spec tex 323 2012 04 02 21 53 22Z black Al 40 Al 42 43 44 45 46 47 48 49 50 Martin Odersky and Matthias Zenger Scalable component abstrac tions 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 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 Te
45. ype super true type var where i G gt 3 5 Tabs and Control Characters Newline can be represented either by carriage return or by line feed however a line feed that immediately follows a carriage return is ignored Tabs and all other non printing control characters except carriage and line feed are syntax errors even in a string literal There are escape sequences for including special characters in string literals 4 Built in Objects 4 1 Numbers Grace supports a single type Number Number maintains rational compu tations in arbitrary precision and inexact irrational computations approxi mated to at least 64bit precision Implementations may support other numeric types a full specification of numeric types is yet to be completed Grace has three syntactic forms for numerals literals that denote Numbers 1 decimal numerals written as strings of digits optionally preceded by a minus Id spec tex 323 2012 04 02 21 53 22Z black 5 2 explicit radix numerals 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 not be preceded by a minus 3 base exponent numerals always in decimal which use e as the expo nent indicator Base exponent numerals may be preceded by a m

Download Pdf Manuals

image

Related Search

Related Contents

Samsung ML-3560 Series User's Manual  Samsung YP-ST5X Наръчник за потребителя    DX70 Diaphragm Pump  USB device/ iPod    View User`s Manual  TREADMILL USER`S GUIDE  AWG7000 Series Service Manual  6bay の RAID 装置は RAID 6 で小型 RAID では最も効率がよく、安全性  

Copyright © All rights reserved.
Failed to retrieve file