Home

Seven Languages in Seven Weeks - Io

This blog post is a next article from series related with books "Seven Languages in Seven Weeks" and its sequel. Each post will describe a single language chosen by this book and its most interesting and influencing features, from my point of view and previous experiences. I hope that you will find this series interesting. Do not hesitate with sharing your feedback and comments below!

Introduction

At first you may think that blog post is about io.js, a recent node.js fork - but it is not, we are talking here about programming languages. :wink:

Io is a programming language created by Steve Dekorte in 2002. It is described as second one in the aforementioned book Seven Languages in Seven Weeks. The only thing that it has in common
with io.js / node.js and other JavaScript based platforms is its prototypical nature.

It means, that like in Self (or JavaScript) everything is a clone of another object and like in Smalltalk everything is an object. In other words - there is no distinction between class and instance and you can build classes and its schema during run-time execution.

 1 Car := Object clone
 2 //   Car_0xDEADBEE:
 3 // type            = "Car"
 4 
 5 Car drive := method("Vroom!" println)
 6 // method(
 7 //  "Vroom!" println
 8 // )
 9 
10 Car drive
11 // Vroom!

As you can observe above - syntax is pretty minimal. It is often compared to the Lisp-like languages. Besides that, language has really clear semantics (which is easy to grasp), powerful features in the standard library (also related with the concurrency support) and small, portable virtual machine (which is often used in the embedded systems domain).

I would like to bring some light to the most interesting language features, starting from the basic things.

Slots and Message Passing

1 Car := Object clone
2 Car desc := "A simple car."
3 
4 Car slotNames
5 // list("type", "desc")

After creating new clone, you can create new slots (with an operator :=) or assign value to the existing one (with simple = operator). Getting value from the slot is simple. Underneath everything is a message - even the method invocation is represented as a message passing to the actual object.

Prototype chains

 1 Car := Object clone
 2 Car desc := "A simple car."
 3 
 4 Ferrari := Car clone
 5 Ferrari type
 6 // Ferrari
 7 
 8 testarossa := Ferrari clone
 9 testarossa type
10 // Ferrari
11 
12 testarossa slotNames
13 // list()
14 
15 testarossa desc
16 // "A simple car."

In presented example you can see how the message passing related with method invocation, propagates to the top of the prototype chain. Also, in Io there is a difference between instances and types regarding syntax - capitalized names means types from which you can clone an instance (and its name is in small caps).

Nice examples

Singleton

It is very easy to create a true singleton instance in the prototypical language. We just need to provide our implementation in the clone slot for that instance. Our implementation will return always the same copy. This method requires consistency in the language - it should be only one way to create an object copy.

1 Single := Object clone
2 Single clone := Single

In presented case, each clone invoked on the Single object will return always the same instance.

Concurrency support

 1 a := Object clone
 2 a say := method(
 3     "A" println
 4     yield
 5     "A" println
 6     yield)
 7 
 8 b := Object clone
 9 b say := method(
10     yield
11     "B" println
12     yield
13     "B" println)
14 
15 a @@say; b @@say
16 Coroutine currentCoroutine pause

On the basic level related with concurrency, Io supports coroutines. As in the example, two coroutines are switching back-and-forth thanks the message yield. Last line will wait until all other coroutines will finish, and after it will let the execution flow. Having this piece and message passing it is very simple and intuitive to build on top the actor model. Besides that we also have futures implementation available in the standard library.

Extending interpreter and VM

And finally, most advanced but still concise example - related with extensiveness of the language and a run-time.

 1 OperatorTable addAssignOperator(":", "atPutNumber")
 2 
 3 curlyBrackets := method(
 4     r := Map clone
 5     call message arguments foreach(arg,
 6         r doMessage(arg)
 7     )
 8     r
 9 )
10 
11 Map atPutNumber := method(
12     self atPut(
13         call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""),
14         call evalArgAt(1)
15     )
16 )
17 
18 // Structure of `data.json` file:
19 //
20 // {
21 //    "key": 123456,
22 //    "another_key: 4567890
23 // }
24 
25 content := File with("data.json") openForReading contents
26 
27 data := doString(content)
28 
29 data keys println
30 data values println

Of course, not a full JSON specification is implemented here, but thanks to the ability to overload operator meaning, creating new operators and modifying its precedence (all of this done by manipulating OperatorTable) we can extend our interpreter at run-time. Thanks to that extension, it will meaningfully evaluate data read, directly from the file contents.

Summary

In the next blog post we will talk about another not popular, but powerful and mind cracking programming language from the 70s - Prolog. It provides declarative beauty connected together with a logic nature. Different approach to computation expressed by facts and rules mingled together with relations can really bend your mind and guide you in the really strange, but also entertaining directions - like an annoying, but still interesting puzzle.

Credits

Home