esolang

Joy

Not an esolang: a serious functional language with no variables at all. Programs are glued together, and gluing two programs composes their functions.

Manfred von Thun, 2001 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

The idea

Joy is an ancestor, not a joke. Manfred von Thun, at La Trobe University in Melbourne, designed it as a functional language that avoids the lambda calculus. There are no named parameters. Every function takes a stack and returns a stack.

Read the story

The consequence is the property that gives the family its name: concatenation is composition. If P and Q are programs, then P Q is the program that does P and then Q. You can cut any program at any space and both halves are still programs.

The second idea is the quotation. [dup *] is not run; it is pushed as a value, a program on the stack. Then combinators such as map, ifte and linrec take quotations and run them. That is how Joy does loops and conditionals without any special syntax. The influences are John Backus's FP and, in spirit, Forth.

The machine

PartWhat it is
StackNumbers, truth values, lists, and quotations (lists are quotations too)
[ … ]Push a quotation without running it
DEFINE name == … .Give a program a name

Some combinators in this implementation.

WordStack effect
i[P] i runs P
dipx [P] dip runs P under x, then puts x back
map[1 2 3] [P] map runs P on each element
ifte[B] [T] [E] ifte runs T if B is true, otherwise E (B sees the stack but does not change it)
timesn [P] times runs P n times
linrec[P] [T] [R1] [R2] linrec: if P then T, else R1, recurse, then R2

Example

Square every element of a list:

Square every element of a list:

[1 2 3] [dup *] map .              prints [1 4 9]

Factorial with no recursion in sight, using linrec:

DEFINE fact == [null] [succ] [dup pred] [*] linrec .
[1 2 3 4 5] [fact] map .           prints [1 2 6 24 120]

Read fact as: if the number is zero (null), make it one (succ). Otherwise keep a copy and subtract one (dup pred), compute the factorial of that, then multiply (*).

The interpreter is a subset: integers, lists, quotations and about 30 words. No strings or sets.

Patterns it teaches

  • Concatenative: the defining example. The word grew up around Joy.
  • Stack machines: every function maps a stack to a stack.
  • Combinators: map, ifte and linrec are combinators over programs, the useful cousins of S and K.
  • Code as data: a quotation is a list, and a list can be run.

Try this

  1. Predict what 3 2 [dup *] dip + . prints, then run it.
  2. Define sum with linrec or times and use it on [1 2 3 4]. (This implementation has first, rest, null.)
  3. Hard: write fib with linrec. Why is it awkward? (Joy's binrec exists for this.)

Further

  • Joy on Wikipedia
  • Manfred von Thun's papers on Joy, such as "Mathematical foundations of Joy".
  • Underload is Joy squeezed to eight commands; Forth is the practical ancestor.

Functional paradigm

Write your own in Run Convert a program from Joy

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: joy.

Command line

shib run joy -e '[1 2 3] [dup *] map .'
shib manual joy

Source

Module
langs/museum/ports.js
Interpreter
langs/museum/joy.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org