archives

Functions shouldn't be lists, functions should be cast to lists

In Joy, and other similar dynamic languages, values can be evaluated like functions that simply yield themselves. This means that any list can be treated as a function. While elegant, this has several drawbacks:

  • compilation requires an extra indirection step (consider how integers and instructions that place integers on the stack are represented differently in most machine code)
  • it is easy to construct ill-typed functions by concatenating lists
  • you lose referential transparency

I've gone into more detail in this article on my web-site.

As a result of these issues I've decided to differentiate functions from lists of functions in the Cat type system. In order to reclaim some of the elegance of Joy (e.g. using functions and lists interchangeably) I've decided to use implicit function to list casts.

As a general rule I feel implicit conversions are evil, but I think the elegance gained is worth it in this circumstance, because instead of writing:

[1 2 3] list [4 5] list cat

I can now simply write:

[1 2 3] [4 5] cat

This is hopefully a useful insight (if it isn't too obvious) to anyone looking at the relationship between dynamically typed and statically typed languages.

Any thoughts? Am I stating the obvious? Or is this in fact a useful observation? Are there any forseeable problems with the approach?