#!/usr/bin/sed -f
# Ackermann function
:ack
# Show what we are doing:
p
# Rewrite expression according to the definition:
s/ack(0,\([0-9]*\))/#\1+/g
s/ack(\([0-9]*\),0)/ack(#\1-,1)/g
s/ack(\([0-9]*\),\([0-9]*\))/ack(#\1-,ack(\1,#\2-))/g
# Perform decrements:
:dec
s/0-/-9/g
t dec
s/1-/=0/g
s/2-/=1/g
s/3-/=2/g
s/4-/=3/g
s/5-/=4/g
s/6-/=5/g
s/7-/=6/g
s/8-/=7/g
s/9-/=8/g
# Perform increments:
:inc
s/9+/+0/g
t inc
s/0+/=1/g
s/1+/=2/g
s/2+/=3/g
s/3+/=4/g
s/4+/=5/g
s/5+/=6/g
s/6+/=7/g
s/7+/=8/g
s/8+/=9/g
s/#+/#=1/g
# Prepare for next rewriting step:
s/#\([0-9][0-9]*\)=/#=\1/g
s/#=0\([0-9][0-9]*\)/#=\1/g
# Have we rewritten anything?
s/#=//g
# If so, start over again:
t ack
# If not, we're done:
q
