Adar

From Esolang
Jump to: navigation, search

Adar is an esoteric "programming language" created by User:TuxCrafting.

Structure

An Adar program is a list of pairs of integers. Each pair represents a register: the first integer is the initial value of the register, and the second is the value to add to every register (including this one) when the register is greater than or equal to 0. If multiple registers are triggered, their values to add are summed.

Note that the revised version is compatible with the old version. Adar with a trigger value of 0 is the same as with an user specified one, since (a, b, c) in the "old" version becomes (a - b, c) in the revised one, with no change of behavior.

Computational class

Adar is a Finite state machine, since the different registers can be seen as a single register, with its values divided in regions of the range of all positive integers.

Examples

Infinite loop

 [(0, 1), (-1, -2)]

Looping counter

 [(0, 1)]

Counter from 10 to 0:

[(10, -1)]

This counter counts in 2n-1's (and halts after a few iterations). This can also demonstrate that Adar can simulate triggers that are not the same in every iteration:

[(0, 1), (-1, 1), (-2, 1), (-3, 1), (-4, -15)]

Truth-machine

 [(x, 1), (-1, -3)]

Where x is the input, either 0 or 1. Stabilizes if x is 0, and oscillates with period 3 if x is 1.

Alternate version

 [(1, -1), (0, x)]

Output is value of the first register. If x is 0, triggers once to output 0, then no more triggering should occur. On 1, stabilizes to a repeatedly triggering state where the first register is always 1.

Oscillators

Oscillators of any period and step amount can be created as follows:

 [(0, {step}), (-{step * (period - 1)}, -{step * period})]

Oscillates with period 7:

 [(0, 1), (-6, -7)]

Period 6, step 5:

 [(0, 5), (-25, -30)]

Period 3, step 1:

 [(1, 1), (-1, -3)]

Increasing the first register's initial value simply adds an offset. Making the second register more negative delays the oscillation. Increasing it by making it -step * (period - n) where period >= n > 1, prevents oscillation and it will stabilize in period - n + 1 steps.

This is another form of making oscillators (which uses three rather than two registers; n is any integer):

 [(0, {step}), (-{step * (period - 1)}, -{step * period - n}), (-{step * (period - 1)}, {n})]

Stabilizers

A stabilizer is an oscillator of period 1, where registers have non-negative values and therefore trigger but produce the same next state. Note, that this is different from the case where all registers are negative so that no trigger occurs(as in this code snippet: [(-1, 1), (-2, -3)]). Using the formula above:

 [(0, 1), (0, -1)]

but any combination of triggers which sum to zero across all registers are stable.

Cat program

This is very trivial; use x as the input here. This stabilizes without doing anything to x, which is equivalent to outputting x.

  [(x, 0)]

+ interpreter

x is the input for the interpreter; For each iteration, set x as a negative number for a command that is not +, and set x as a natural number for a command that is +.

  [(0, 0), (x, 1)]

APLWSI interpreter

This is created totally as a joke. This is the shortest possible program in Adar:

[]

Simple adder with iteration

[(a, x), (b, y)]

Adds x+y to a and b, which are initial values. There can be an arbitary amount of accumulators for adding. Note that this iterates by default and you will have to stop it explicitly.

Interpreter

The following Python function does one step of Adar:

adar = lambda l: (lambda n: list(map(lambda x: (x[0] + n, x[1]), l)))(sum(map(lambda x: (x[0] >= 0) * x[1], l)))

In fact, adding 1 line of code creates an Adar shell:

adar = lambda l: (lambda n: list(map(lambda x: (x[0] + n, x[1]), l)))(sum(map(lambda x: (x[0] >= 0) * x[1], l)))
while 1: x = input(">"); adar(eval(x))

Here is an implementation that takes an input and evaluates the next step until Adar stabilizes:

adar = lambda l: (lambda n: list(map(lambda x: (x[0] + n, x[1]), l)))(sum(map(lambda x: (x[0] >= 0) * x[1], l)))
x = eval(input())
while 1:
    nxt = adar(x)
    print(nxt)
    if nxt == x:
        break # halt if Adar stabilizes
    x = nxt
    # enter = input("Press enter to evaluate one more step. ") # wait for the user; uncomment this for interactive use

This supports file implementing: try

cat test.adar | python adar.py

Adar-=

Adar-= is a simple derivative Adar, where the trigger condition is equality instead of greater than or equal.

It is pretty obvious that Adar-= is a finite-state automaton, since causing a register to trigger requires shifting every other register by the same amount as is required for the register to trigger, meaning that it is impossible, for example, to have an unbounded counter.