Saturday, March 21, 2015

Sophisticated primitives

I mentioned built-in types (aka primitives) in my last post. It turns out, pattern matching lets Effes be a bit more expressive than the standard “here’s an int, go do int things with it” operations. For instance, imagine a world where divide-by-zero exceptions can’t happen! (Big disclosure: I don’t think I’ve ever actually triggered one, so they’re not actually that big a deal to me. Still, I like the idea of getting rid of them at compile time.)

Integers in Effes work something like this:

``````type Int = IntZero | InvValue

type IntValue @builtin:
+ (other: Int) -> Int: @builtin
- (other: Int) -> Int: @builtin
* (other: Int) -> Int: @builtin
/ (other: IntValue) -> Int: @builtin

type IntZero: @builtin
...
``````

As you can see, there are actually two int primitives, one for zero and one for everything else. `Int` is just an alias for the disjunction of those two types, and most of the basic math operations take two Ints (`this` and `other`). Division is the exception: the denominator must be an `IntValue` specifically. That means it can’t be an `IntZero` — and thus that divide-by-zero errors are caught at compile time.

Here’s how you’d use it:

``````hitsPerMinute = case minutes of
IntZero: Error -- or whatever
IntValue: hits / minutes
``````

In this snippet, `minutes` comes in as a standard `Int`. We can’t divide by `Int`, so we throw `minutes` into a `case` expression. If `minutes` is an `IntZero`, the result is explicitly some sort of error; if it’s `IntValue`, we can divide by it.

I’m still not sure if I want to do any such trickery for other primitives. I think I won’t, because other primitives don’t have operations that are undefined (ie, throw an exception) for certain inputs. Floating points, for instance, let you divide by zero, add infinity, or do anything else and always get a value back. It may be `NaN`, but it’s still a value.

It’s actually a bit interesting to me that other languages don’t have this sort of behavior; all you really need to make it work is pattern matching. My guess is that it’s just not a very compelling problem (as I mentioned earlier, I don’t think I’ve ever actually gotten tripped up by it), so it’s not worth the work to catch it. Effes’ type scheme lets me catch it with minimal compiler trickery, which is probably about as much as it’s worth.

Friday, March 20, 2015

Embracing efficient exceptions in Effes

Generics are wrapping up, and I’ve just implemented built-in types at last, so I’m starting to think ahead to next tasks. One of them is exception handling, and I have an idea that combines throwables and standard return types in a way that should settle the “checked vs unchecked” exceptions battle for good. Take that, Java!

Checked exceptions in Java are useful for defining edge cases at API boundaries. For instance, all sorts of things can go wrong in a SQL query, and it makes sense for the entry point to the SQL API to declare, “hey, this method can throw a `SqlException`, and you should know that.”

But sometimes the best you can do with that exception is to propagate it. This results in a whole bunch of methods declaring `throws SqlException` (or `throws IOException`, or, if the programmer is a bit lazy, the infamous `throws Exception`). Eventually you get to a method like `Runnable::run` that can’t throw any checked exceptions, so you just handle the exception generically, probably by wrapping it in a `RuntimeException` and throwing that. Yo dawg, I heard you like exceptions.

So, the problem is that one piece of code wants to treat `SqlException` as a checked exception, while another wants to treat it as an unchecked exception. Java doesn’t let you do that.

In Effes, there’ll be two ways to handle an exception: by throwing it, or by returning it. This is where the dynamic nature of disjunctive types comes into play.

All exceptions will be unchecked in Effes, meaning that you can throw them willy-nilly:

``````executeQuery (query: String) -> QueryResult:
throw SqlException("dummy SQL engine") -- unchecked
``````

But a method can also include an exception as one of its return type disjunctions:

``````runQuery (query: String) -> QueryResult | SqlException:
return SqlException("dummy SQL engine") -- "checked"
``````

The latter method essentially turns the exception into a checked exception, because the resulting variable is a disjunction that has to be picked apart with a case statement:

``````query = runQuery "SELECT * FROM bobby_tables"
summary = case query of:
SqlException(msg): throw query
SqlResults: summarize query
``````

Note that in this snippet, we converted `SqlException` from a “checked” exception (in that it was a disjunction in the result type) to unchecked, just by throwing it.

Moreover, if a method declares an exception as part of its return type, then it’ll never throw it. Trying to throw it from the method directly results in a compile-time exception, and if it’s thrown from down the stack, it’ll be returned immediately. It’s essentially shorthand for:

``````runQuery (query: String) -> QueryResult | SqlException:
try:
<whatever>
catch (SqlException e)
return e
``````

This lets us easily convert unchecked `SqlException`s thrown from `<whatever>` to the equivalent checked exceptions — thus providing that API border we wanted.