BAM Weblog

Roy - Tuples

Brian McKenna2011-10-16

Tuples could be implemented in Roy a couple of different ways. The first way is using the ADT support:

data Tuple a b = Tuple2 a b

I could add some syntactic sugar on top to completely hide the data type.

The second way would be to use a structure, something like this:

type Tuple2 a b = {0: a, 1: b}

(The above isn't valid Roy code, I don't have type aliases in yet)

I could also add sugar to hide this method. Pretty similar to what ML does.

Because of JavaScript's property access, I can actually compile tuples to JavaScript arrays very easily. That means this:

let x = (1, true, "three")

Can compile to this:

var x = [1, true, "three"];

And still compile down as if it was a structure with numeric keys:

x[0] == 1
x[1] == true
x[2] == "three"

An interesting part of the structural approach is that higher-arity tuples are subtypes of lesser-arity tuples. For example (1, 2, 3) is a subtype of (4, 5). I'm not sure if this is good or bad thing but ML seems to get away with it.

I'm currently running with the structural way of implementing tuples. I think that having two different possibilities shows a problem with Roy - it doesn't make sense to have two ways of constructing product types. I should just go the ML way and have ADT options only take up to a single parameter.

Please enable JavaScript to view the comments powered by Disqus.