The latest issue of the GOTO Conference Magazine includes an article on Grace – along with other articles on Neo4J, NoSQL, and Kanban.
Language Design
James Bond, Logo, and Trees
The latest “James Bond” release of minigrace (version 0.0.7.1010, thus James Bond) includes experimental support, written by Tim Jones, for generating Java code and calling Processing. Unfortunately this support really is very experimental, and will probably go away in the next release.
The main things we learned from the experiment is that while we can just about compile Grace to Java as a prototype, anything more serious either needs a global analysis to map Grace’s structural types to Java’s nominal types (a nice PhD thesis) or to generate byte codes and use invokedynamic to build Grace’s calling conventions (an MSc at least), or perhaps a mixture of the two.
Tim’s current plan – unless anyone volunteers to take over the Java side of things – is to concentrate on the JavaScript backend. Compiling to a dynamically typed substrate will be so much easier, and as a consequence, perhaps quicker too. Since Processing has a JavaScript implementation, if we can bind to that, we hope to be able to port Grace code calling the Processing API over to JavaScript without any change on the Grace side.
While playing with the experimental Java-backend I revisited some examples that was all the rage when I was starting out, fractal graphics done in Logo style. Stealing shamelessly from motivate.maths.org, all the images in this post were generated in Grace via the Processing API hosted on top of Java, running on a Mac OS 10.7.4. If you download the tarballs and install the files, you could play with something like this too – I mean, this is an example of Grace code that might actually be used, someday, to teach programming.
123456789 method gasket(size : Number, limit : Number) -> Void {if (size < limit) then {return}repeat 3 times {gasket(size/2, limit)forward(size/2)right(120)}}
This article is translated to Serbo-Croatian language by Jovana Milutinovich from Webhostinggeeks.com.
The absence of (inessential) difficulty
We recently submitted a paper on Grace to the Onward! conference at SPLASH 2012. You can read a draft: please do send comments or attach them below. To whet your appetite, here is the abstract:
Grace: the absence of (inessential) difficulty
by Andrew Black, Kim B. Bruce, Michael Homer, and James Noble
Loop with exits revisited
I was recently talking to one of programmers moving some Java code into Grace. He was refactoring Java code that uses “break” and “continue” statements, and wondered why Grace doesn’t support them. I answered that even if break and continue aren’t in the standard library the can certainly be written by a programmers.
The only infelicity is that rather than being commands, “break” and “continue” in Grace will be blocks, supplied as arguments into loops — to take one of these exits the program must apply the appropriate block argument. Here’s a simple loop using both “break” and “continue” — not exactly the best style but hopefully illustrativ
123456789 var foo := 0loopWithExits { break, continue ->foo := foo + 1print "A {foo}"if (foo == 2) then {continue.apply}if (foo == 4) then {break.apply}print "B {foo}"}
1
running this code produces:
A 1
B 1
A 2
A 3
B 3
A 4
as you might expect.
How is this implemented? Well first, here’s a simpler case: a “doWithExit” statement that takes a block and exits when the block is applied:
1234567 doWithExit { exit ->print "A"print "B"exit.applyprint "C"}print "Done"
1
this will print “A”, “B”, and then “Done” because “exit.apply” exits the block. The definition of the “doWithExit” statement is quite straightforward:
123 method doWithExit (block) {block.apply { return "doWithExit Return" }}
we just run the block supplied to the method, passing a second block as an argument, this second block calls return and so exits from the method, consequentially exiting the argument block early.
We can then build upon “doWithExit” to build up the “loopWithExits” statement above. The trick here is we inject two blocks into the looping code. The first of these, the “break” exit, returns from the whole “loopWithExits” statement, stopping the loop (just like C or Java’s break) — the implementation here is just the same as the single exit in the “doWithExit” method. The second injected argument comes from a “doWithExit” inside the loop proper to exit from only the code being looped. When that inner code returns, the control flows out of the “doWithExit” block, into the block controlled by the “loop” statement. Since we’re at the end of the loop, the control flows around to the next iteration of the loop — just like C or Java’s continue.
12345678 method loopWithExits (block) {def break = { return "loop return" }loop {doWithExit { continue ->block.apply( break, continue )}}}
This isn’t the simplest Grace code — frankly I wouldn’t teach it at all to first year students — but hopefully it does show the kind of things that programmers can write in a simple, language based on “a small number of underlying principles that can be applied uniformly”. In Grace, “loopWithExits” is just a method, like any other. The “loopWithExits” method is implemented using nothing but objects, blocks, requests, and returns, without any macro processing or explicit metaprogramming
Finally, it’s interesting to realize that we talked about this on the blog over a year ago and outlined what we hoped the basic Grace code for doing this would be. It’s gratifying to realize that the code above, that works on our current prototype Grace systems, is pretty much the same as last year’s sketches. And it’s even more gratifying to see last year’s sketches made flesh and running this year!
Grace at Lang.Next
Andrew Black and Kim Bruce presented an invited talk on Grace at the Lang.Next workshop at Microsoft a few weeks ago. A video of the talk is available on Channel 9 at Microsoft and the slides are here.
The next day Mads Torgersen interviewed Kim about Grace. The video is available.