esolang

ELEMENTARY

One expression, three operators, no loops. It still solves every problem in NP.

Turing complete: no · ●●○ moderate · esolangs.org

Tested with 5 known programs · Test method

Run it

Expected: 0

The idea

ELEMENTARY is a computational class first, and a language second. A program is a single expression over natural numbers, and the interpreter is handed a value for each variable, works the expression out with unbounded integers and prints the result. There is no control flow, so every program halts.

Read the story

Addition, remainder and 2 to the power of are enough. With them a program can run any polynomial time algorithm, though the class is weaker than the primitive recursive functions. The usual trick is to build one huge number and treat groups of its digits as many small computations running side by side. Python's own +, % and ** already form an ELEMENTARY interpreter.

The machine

OperatorGives
+The sum
%The remainder; by 0 is an error
**The power, with 0 ** 0 = 1
<<The first times 2 to the power of the second
* /Product and floor division
∸Monus, the difference, or 0 when it would go below
& \| ^ >>Bitwise and right shift
< > <= >= == !=1 or 0

The first four are the language; the rest are the optional extras the specification lists. In this implementation the variables take the numbers in the input in the order they first appear. Operators follow Python's precedence, so not every pair of operands needs its own parentheses.

Example

Constants are not needed at all.

Constants are not needed at all. Given any variable x, (x ** x) is never 0, so it modulo itself is 0, and (2** 0) is 1.

Expected: 1

The example program, with a = 3 and b = 5. It works out (3 + 4) % (5 % 3).

Expected: 1

The integers have no bound.

Expected: 1267650600228229401496703205376

Further

Esoteric subset

Unknown year

Write your own in Run Convert a program from ELEMENTARY

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run elementary -e '(2** 0)'
shib manual elementary

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/elementary.js

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

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