Undo is an esoteric programming language based on Unlambda. The main differences are that Undo uses lazy evaluation instead of eager evaluation, and that the way I/O works is different; instead of using side effects, it uses a system inspried by Haskell's monadic I/O. (The "do" in the name comes from Haskell's
Because of what's similar and what's different, many Unlambda programs will be valid Undo programs but behave differently (e.g. not producing the proper output or terminating early), and vice versa.
Knowing Unlambda and knowing about monads will be helpful in understanding what is going on here.
A program is a single expression. An expression can be any of the following:
b, representing a built-in function.
.) followed by any single character, representing a "print" function.
Yare valid Undo expressions, representing function application.
Whitespace is ignored (except after
# starts a single-line comment (again, except after
.). Aside from the different set of built-in functions, this syntax is the same as Unlambda.
\ starts a single-line indication of the language version. For compatibility with future versions, programs should start with a line
\undo1. Interpreters can use this to show a warning or error for unsupported versions or enter a compatibility mode, or they can treat it as another comment character.
The basic combinators are
1 (one), which satisfy the following:
`YX(that's a one, not an ell)
v are exactly the same as their Unlambda equivalents. This means that many algorithms for Unlambda will also work in Undo. In particular, abstraction elimination works the same, except that you don't need to worry about side effects or nontermination; and algorithms for working with things like numbers and data structures will continue to work as long as they don't involve I/O or the
c function (or
Of these functions,
1 is the only new function. Like
v, it's not strictly necessary; it can also be written
``s`k`si``s`kki. Its purpose will be discussed later, in the section on monad laws.
There are two (classes of) basic I/O actions:
.X, for any character
X, represents an action that prints a single character. Like in Unlambda,
ris an alternative way of writing
.followed by a newline. The result of a
@represents an action that reads one character from the standard input. The result of an
Xis the character read, or
vif the end of the input has been reached.
While these look like Unlambda's I/O functions, they behave differently. Applying them to an argument doesn't perform their side effect; rather, it creates a new I/O action as described in the next section. To actually perform the action, the outermost expression in the program must evaluate to the action. For instance, the minimal program in Unlambda to print an asterisk is
`.*i, whereas in Undo it's
.* (which in Unlambda would do nothing). On the other hand, in Unlambda,
`k`.*i would output an asterisk, whereas in Undo, it would output nothing since the expression evaluates to a partially-applied
k rather than to an I/O action.
If the program evaluates to an expression that is not an I/O action or a continuation action (described in a later section), the program halts.
Note that the equivalence from lambda calculus where λx. f x = f (
F) does not hold when f is an I/O action; if the action is inside a partially-applied function, the interpreter will not see it.
I is an I/O action, then
`IF is an I/O action that performs
I and then evaluates
R is the result of the action, and performs the result of that as an I/O action.
This means that you can perform actions in sequence (ignoring their results) by applying the first action to
`k of the second action, like this:
…or, associating the other way:
For actions where you want the result (like
@), you can instead put a non-constant function. For instance,
`@i reads a character and interprets the result as an action and performs it; since the result of
@ is a printing function, this will read one character from the input and then print it.
According to the Haskell documentation, monads satisfy the following properties:
return a >>= k = k a
m >>= return = m
m >>= (\x -> k x >>= h) = (m >>= k) >>= h
Translating these into Unlambda/Undo notation gives:
``<bind>`<return>AK = `KA
``<bind>M<return> = M
``<bind>M^x``<bind>`K$xH = ``<bind>``<bind>MKH
As I talked about in the previous section, you can combine I/O actions simply by applying it to a function in the same way that Haskell's
>>= operator works; that is,
<bind> for Undo I/O actions is simply
i. This means:
``<return>AK = `KA
`M<return> = M
`M^x``K$xH = ``MKH
Notice that property 1 is just the definition for the
1 combinator; that means that
1 is the
return function for Undo's I/O monad (this is why I defined a new combinator for that). That is,
`1X behaves like an I/O action that does nothing and has the result
If an I/O action is applied to two arguments, then property 3 will be applied before performing the action.
M is an I/O action, then
`M1 will perform the action and then halt (because
`1<anything> is not an action), so
`M1 are the same in isolation.
``M1F simplifies to
`M^x`F$x, so property 2 holds.
= function takes two arguments and evaluates them. If the result of both arguments is an I/O action that prints a character next (as opposed to inputting or halting), and the first character printed by the actions is the same, then the result is
k; otherwise it's
`ki. (Or, thought of another way,
````=XYTF returns T if both X and Y output characters and output the same character, and F otherwise.)
The main intended use for this is when reading a character, where this serves the same purpose as Unlambda's
?X function. For instance, the program
`@ ``s``s``s``s`k=i`k.a`k.y`k.n (
`@ ^c ````=$c.a.y.n) reads a character from the input, and prints
y if the character is
This can also be used for testing a boolean stored with Unlambda's
v convention; since
``=`B.a.a will test if
There's no exit function, unlike Unlambda 2; however, since function application is used for sequencing and
`v<anything> is never an I/O action,
v acts as an action that exits the program.
I'm not sure how useful these are, since where continuations can be used is more restricted and I don't think
c can be used to detect
v. (Also, this hasn't been tested as much.)
c function works similarly to Unlambda;
<cont> is the current continuation. However, both
`<cont>X are treated as I/O actions and
`F<cont> is expected to be an I/O action; the same rules about combining I/O actions apply here, too.
There is also a
b function (bracketed continuation), which takes an expression involving continuations and evaluates it. If the result is
X, then the result of the call to
|pure||Combinators, Monad laws|
|output ||I/O||Basic I/O actions|
|input ||I/O||Basic I/O actions|
Comparison to Unlambda:
|Function||Unlambda 1||Unlambda 2||Undo|
|simplify and run cont on arguments first|
|if there's a rule to simplify X|
|run cont||also include "simplify" rules|
|else||halt||also include "simplify" and "run cont" rules|