Grace @ ECOOP today

We have arranged for a room for a Grace presentation/discussion at ECOOP tonight at 5:15 p.m. Please come to George Fox lecture theater 5/6 for the presentation. We will go out to dinner somewhere after the session (suggestions on where to go are welcome).

If you would like to learn more about Grace but cannot come to that session, please contact Kim or James at the meetings this week and we will make time to meet with you.

We are anxious for feedback on our language design, so please come if you can.

The Grace workshop to work through details of the language design — particularly regarding types vs classes remains on Saturday, for people staying longer.

Types vs Classes

Like many object-oriented languages, Grace will have classes. Like some object-oriented languages, Grace will have types. Like a few
object-oriented languages, Grace programmers have the option to ignore classes and use only objects, or to ignore static types and use only dynamic types.

The relationships between objects and classes, and static and dynamic types, are well known. So, then, what’s the relationship between
classes and types in Grace? Or rather, what should be the relationship between classes and (static types?

Here’s the problem. Let’s take a simple Grace class:

This class creates a factory object that supports the creation of new Cat instances in response to the method request “new(aColour,aName)” (cognoscenti will notice we’re trying “def x =” syntax to define constants rather than “const x :=”. Sorry Niklaus).

What type does the variable “fergus” have? As in C#, local type inference gives it whatever type the “Cat.new” method returns. What if we want to declare that type explicitly, say for a variable?

Here the name “Cat” is being used as a type, rather than a factory. The key question is: where did that type come from? There seem to be two options in the design here:

  1. The Cat class declaration implicitly creates a Cat type.
  2. The Cat type must be declared explicitly, separate from the class
    declaration:

The first option, a class implicitly creating a type, is what most typed object-oriented languaes do: a class declaration also creates a type (technically the cone type rooted at the class). Implicit class-types lead to more concise programs, and allow “static typing early” courses to have students write and use their own classes without requiring an explicit concept or separate declaration of a static type.

On the other hand, the second option, explicit type declarations, make static types much more explicit. Under this option, Grace programmers couldn’t declare an explicitly typed variable (or more likely, any method, as method arguments are not inferred) without an explicit declaration of the Cat type. But this clarity comes at a price: simple programs are longer, requiring apparently redundant type declarations, declarations that are close to class declarations, but duplicated some information with some mandatory tweaks.

Grace programmers can avoid the price of a separate declaration in a couple of ways. First they can use dynamic types or local type inference — omitting types from variable and constant definitions will find types via local inference (if the type-checker is run) while omitting types from method arguments and results are interpreted as type dynamic. So the costs of declarations (presumably) would only be required whenever a type is to be written explicitly. Still, this is another case where a ”better” program (with explicit types) is longer and more redundant than a ”worse” program (without them). Most Grace programmers may choose to omit the declarations, so the language would fall into being dynamically typed by default.

In fact, the real situation is worse than this: there are about five or six kinds of “class-like” or “type-like” objects in Grace: a good solution here should address all these roles:

  • ”’Factory”’ object that creates new instances of a class

  • ”’Type”’ with which variables, arguments, and methods are declared

  • ”’Reified Type Parameter”’ Since Grace will have “reified”
    generics, what value or object should the reified value be?

    Note that inside the Collection, the reified type argument will have to be bound to the formal type parameter.

  • ”’Pattern”’ object used to match objects of that type in
    match/case statements

  • ”’Mirrors”’ used to reflect on instances of the class

If these are played by different objects — how many different namespaces will Grace need to name them all? If they are accessible in a shared namespace, how are names resolved?

Finally, following C#, Grace will provide constructs to reify the declared static type of an expression (perhaps “decltype(e)”) and the exact dynamic type of an object (“o.dyntype”, or perhaps alternatively “reflect(o).dyntype” via a mirror). The aim here is to let programmers write programs that interrogate the static and dynamic types in their programs. And, whatever the relationship we end up with, do we need better names for static type and dynamic type?

Learning Edge Momentum

Like many computer science or software engineering departments, at VUW we seem cursed with a “bimodal” distribution of marks in first year programming courses. While some students do very well and collect A or A+ grades, about as many do very poorly, taking away only Ds and Es — and with relatively few students in the
middle. This profile is very different from most other courses at the university — and generally from our second and third year courses — which have much more normal distributions.

A number of hypotheses have been proposed for the bimodal distribution — most commonly, that a large proportion of the population are congenitally unable to learn programming, and that our advertising fails to dissuade them from enrollment.

Recently, Anthony Robins, a colleague from Otago University in NZ (the southern-most university in the world, and oldest university in the NZ) has developed a novel rationale to explain these distributions. His paper, “Learning Edge Momentum” , hypothesizes that introductory programming is unlike many other disciplines, in particular, that “success in acquiring one concept makes learning other closely linked concepts easier (whereas failure makes it harder).”

What’s this to do with Grace? Well, one of our main aims in the design of Grace is to reduce the accidental difficulties of learning to program. In Robins’ terms, I think this could be described as “uncoupling the concepts” within the language — partly removing concepts, but mostly trying to make concepts less
closely linked.

Here’s a simple example. In Java 1.0:

To me, this needs a whole collection of tightly-linked concepts:

  • For loop
  • Integers, and “int” type
  • Variables and assignment
  • Length of a collection and that “col.length()” gets the length
  • Less than, and that “< " is less-than
  • ++ increment operator…
  • “col.at(i)” to get i’th member of a collection.
  • System.out.println to print things.

But the “new for loop” in Java 5.0 needs far fewer concepts:

In particular:

  • For loop
  • Variable declaration
  • System.out.println to print things.

Hopefully Grace’s “for” loop:

will be closer to the Java 5.0 loop rather than Java 1.0!

I look forwards to seeing how Robin’s hypothesis is developed and tested over time. I also look forwards to see how much we can ensure a clear and loosely coupled conceptual model under Grace.

Grace Workshop Lancaster – Saturday 30 July

The next Grace Workshop will take place on Saturday 30 July, after the ECOOP 2011 conference.

The Workshop will be held at Lancaster University, in the Computer Science Department (Infolab21 building) in room C60a. Here is a map of the campus (Infolab21 is building 62 on South Drive).

We’ll aim to start around 10am.

These workshops are to report progress on the design, to discuss challenges, and hopefully to involve others in the project. At this workshop, we also hope to show some very early prototypes that we are building to test the specification.

So, if you’re interested, and are able to come to this workshop, please let us know by emailing James, kjx@ecs.vuw.ac.nz so we have some idea who’s coming.

Object Independence Day

We hold these truths to be self-evident, that all objects are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are sole access to their internal representation

An object-oriented language should provide encapsulation: an object should be able to protect its representation from unwanted external access. Programming languages provide a wide range of different encapsulation mechanisms: statically and dynamically checked; encapsulating names or objects; encapsulating within objects, or within classes, or within modules…

Grace will have a module system to support separate development and compilation: to manage coupling between different development units. The design is yet to be finalised (Ok, yet to be started) – but we are thinking along similar lines to Newspeak, gBeta, J&t or Ceylon in that it will be based on classes nested within objects.

But this large-scale encapsulation, within compilation units, doesn’t really address the key independence of objects in object-orientation: that each object should be independent, having sole access to its own internal representation. Now many object-oriented languages don’t in fact support this either. C++, C#, and Java have private and protected modifiers: but these restrict access within classes not objects: an instance of one class may access private or protected fields of any other instance of the same class — and for protected, of any subclasses as well.

For this reason, like Smalltalk, Ruby, and other languages, Grace will provide object-level encapsulation. Encapsulated names – methods and fields – will only be accessible from within the same object — i.e. by method requests on self. Again like Smalltalk and Ruby, encapsulation in Grace can depend on types (or classes) but not on static types or classes: we don’t want programmers to e.g. remove static types from their code just to get around encapsulation!

But there are a number of different options even within this design:

  • private modifier – marks encapsulated attributes; no modifier means no encapsulation
  • public or shared modifier – marks unencapsulated attributes; no modifier means no access except through self
  • textual rules for encapsulation. In Go for example, names beginning with a Capital letter are public, names beginning with a lower-case letter are private. This convention is used throughout .Net programs – but so far we’ve been using a methods-as-lowercase convention, common in Java, Smalltalk, etc. This kind of implicit coding doesn’t work for code in non-Western alphabets that don’t distinguish between upper/lower case
  • “reverse” textual rules – lower case is public, Upper Case is Private. At least this would work better with the Java convention – but that seems to be all it has going for it.
  • sigils – use a non-alphabetic character to start all (encapsulated) identifiers. For example, any private const, var, or method must begin with an underscore (self._myStuff) — identifiers without underscores are public

There are only a few differences between these options: should the default be accessible (easier for novices to get started) or encapsulated (easier to learn good habits). Encoding encapsulation into the names (as in the last three options) makes their relationship with inheritance clearer, but changing visibility means changing names. On the other hand, modifiers on attributes decouples encapsulation status from the text of the name, but requires modifier-consistency rules across inheritance.

We’re in the process of working our way through these options (or at least thinking about them every so often). We’ll hope to have a decision by the time of the next Grace workshop (30 July, in Lancaster, after ECOOP).

If anyone has any random opinions (or even considered thoughts) on this, we’d love to hear them.