esolang

Push

A stack language designed to be written by evolution. Every type gets its own stack, and any jumble of tokens is a valid program.

Lee Spector, 2001 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

The idea

Lee Spector designed Push at Hampshire College for genetic programming: evolving programs by mutation, crossover and selection against test cases. He presented it in 2001 ("Autoconstructive evolution: Push, PushGP, and Pushpop", GECCO-2001); the standard introduction is Spector and Robinson's 2002 journal paper. Push3 (2003 to 2004, with Chris Perry, Jon Klein and Maarten Keijzer) is the version most systems use.

Read the story

Two choices make it friendly to evolution. First, the syntax is almost nothing: a program is an instruction, a literal, or a parenthesised list of programs. Any cut-and-splice of two programs is still a program. Second, each type has its own stack: integers, floats, booleans, code, and the execution stack itself. An instruction takes its arguments from the right stacks, and if they are not there, it does nothing. Nothing ever crashes.

Push came with two systems. PushGP is ordinary genetic programming over Push programs. Pushpop is stranger: programs make their own children, using Push's code-manipulating instructions, so the way offspring are made evolves too. Spector calls this autoconstructive evolution.

The machine

StackHolds
EXECWhat is still to be run (Push3)
CODEPrograms as data, to be inspected, changed, or run
INTEGER, FLOAT, BOOLEANValues
NAMENames that have no meaning yet

Running a program: push it on EXEC, then repeatedly pop the top item. A literal goes to its stack. An instruction runs. A list is opened, its items pushed back so that its first item is on top.

Some instructions:

InstructionDoes
INTEGER.+ INTEGER.* ...Arithmetic on the top two integers (INTEGER.- is second minus top)
INTEGER.< INTEGER.=Compare, push the result on BOOLEAN
CODE.QUOTEMove the next item from EXEC to CODE instead of running it
CODE.DORun the top of CODE
CODE.IFRun the second or the top item of CODE, depending on a boolean
EXEC.DO*RANGELoop the next item, pushing a counter each time
EXEC.DEFINEBind the name on top of NAME to the next item: a new instruction

An identifier that is not an instruction and not yet defined is pushed on the NAME stack. Once a DEFINE binds it, the name runs its value.

Example

These are the examples from the Push3 specification, run through this implementation.

These are the examples from the Push3 specification, run through this implementation.

( 2 3 INTEGER.* 4.1 5.2 FLOAT.+ TRUE FALSE BOOLEAN.OR )  []
  -> INTEGER: ( 6 )  FLOAT: ( 9.3 )  BOOLEAN: ( TRUE )
( 5 1.23 INTEGER.+ ( 4 ) INTEGER.- 5.67 FLOAT.* )  []
  -> INTEGER: ( 1 )  FLOAT: ( 6.9741 )  BOOLEAN: (  )
( DOUBLE EXEC.DEFINE ( INTEGER.DUP INTEGER.+ ) DOUBLE DOUBLE )  [5]
  -> INTEGER: ( 20 )  FLOAT: (  )  BOOLEAN: (  )
( CODE.QUOTE ( INTEGER.POP 1 ) CODE.QUOTE ( CODE.DUP INTEGER.DUP 1 INTEGER.- CODE.DO INTEGER.* ) INTEGER.DUP 2 INTEGER.< CODE.IF )  [5]
  -> INTEGER: ( 120 )  FLOAT: (  )  BOOLEAN: (  )
( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )  [5]
  -> INTEGER: ( 120 )  FLOAT: (  )  BOOLEAN: (  )

The second one is the point of Push. Integer and float code are mixed. INTEGER.+ finds only one integer, so it does nothing. The parentheses around 4 change nothing. Every step either does something sensible or nothing at all.

The fourth is a recursive factorial. The interpreter puts the whole program on CODE before it starts, so CODE.DUP ... CODE.DO runs the program again on n − 1: recursion with no function names at all. The last line does the same with a loop.

Patterns it teaches

Try this

  1. Predict, then run: shib run push -e '( 3 INTEGER.DUP INTEGER.* FLOAT.+ TRUE INTEGER.- )'.
  2. Write a Push program that leaves the sum 1 + 2 + ... + n on the INTEGER stack, with n preloaded.
  3. Hard: write a program that leaves a copy of itself on the CODE stack, plus one random change. That is the seed of autoconstructive evolution.

Further

  • Spector and Robinson, "Genetic programming and autoconstructive evolution with the Push programming language", Genetic Programming and Evolvable Machines 3(1), 2002.
  • Push 3.0 language description
  • Lee Spector, "Autoconstructive evolution: Push, PushGP, and Pushpop", GECCO-2001.

Stack-based

Write your own in Run Convert a program from Push

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run push -e '( 1 INTEGER.MAX 1 EXEC.DO*RANGE INTEGER.* )' --input '5'
shib manual push

Source

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

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

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