Skip to content

Odd Corners

Bill Hails edited this page Jun 2, 2018 · 6 revisions

HM type checking assumes that all functions take a single argument. This may seem like a limitation, but it is overcome by treating multiple arguments as curried, so in

fn add(x, y) {
    x + y
}

the type of add is int -> int -> int, (see Type Notation.) This is cool because the type checker can then correctly type check curried function application.

However there is a bit of a problem. What is the type of fn x () { 12 }? It looks like a function, but there is no lhs for the -> operator, so it's type is just int. And what about function application? calling x() looks like a function application, but there is no argument and its type is also int.

So the type checker allows both of these expressions, and all seems well. The problem is that the type checker also allows

fn x() {
    12
}

1 + x;

Which will cause a run-time error, not acceptable. Similarly 12(); passes type checking but causes a run-time error.

I've taken the decision to make the language behaviour conform with the type-checker's expectations rather than trying to modify the type checker itself. so if a function is defined with no arguments, its body is immediately evaluated and the result is assigned; and if an arbitrary expression is applied to a list of zero arguments, the result is just the expression. All of the above problems go away, at the expense of the language allowing a couple of apparently odd constructs.

Up: Home

Next: To Do

Clone this wiki locally