OSDN Git Service

Fix indentation in library docstrings.
authorSimon Forman <sforman@hushmail.com>
Wed, 20 May 2020 23:09:19 +0000 (16:09 -0700)
committerSimon Forman <sforman@hushmail.com>
Wed, 20 May 2020 23:09:19 +0000 (16:09 -0700)
docs/0._This_Implementation_of_Joy_in_Python.ipynb
joy/library.py

index ffe2838..2313783 100644 (file)
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "When talking about Joy we use the terms \"stack\", \"quote\", \"sequence\",\n",
+      "\"list\", and others to mean the same thing: a simple linear datatype that\n",
+      "permits certain operations such as iterating and pushing and popping\n",
+      "values from (at least) one end.\n",
+      "\n",
+      "There is no \"Stack\" Python class, instead we use the  `cons list`_, a \n",
+      "venerable two-tuple recursive sequence datastructure, where the\n",
+      "empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the\n",
+      "recursive form of a stack with one or more items on it::\n",
+      "\n",
+      "    stack := () | (item, stack)\n",
+      "\n",
+      "Putting some numbers onto a stack::\n",
+      "\n",
+      "    ()\n",
+      "    (1, ())\n",
+      "    (2, (1, ()))\n",
+      "    (3, (2, (1, ())))\n",
+      "    ...\n",
+      "\n",
+      "Python has very nice \"tuple packing and unpacking\" in its syntax which\n",
+      "means we can directly \"unpack\" the expected arguments to a Joy function.\n",
+      "\n",
+      "For example::\n",
+      "\n",
+      "        def dup((head, tail)):\n",
+      "                return head, (head, tail)\n",
+      "\n",
+      "We replace the argument \"stack\" by the expected structure of the stack,\n",
+      "in this case \"(head, tail)\", and Python takes care of unpacking the\n",
+      "incoming tuple and assigning values to the names.  (Note that Python\n",
+      "syntax doesn't require parentheses around tuples used in expressions\n",
+      "where they would be redundant.)\n",
+      "\n",
+      "Unfortunately, the Sphinx documentation generator, which is used to generate this\n",
+      "web page, doesn't handle tuples in the function parameters.  And in Python 3, this\n",
+      "syntax was removed entirely.  Instead you would have to write::\n",
+      "\n",
+      "        def dup(stack):\n",
+      "                head, tail = stack\n",
+      "                return head, (head, tail)\n",
+      "\n",
+      "\n",
+      "We have two very simple functions, one to build up a stack from a Python\n",
+      "iterable and another to iterate through a stack and yield its items\n",
+      "one-by-one in order.  There are also two functions to generate string representations\n",
+      "of stacks.  They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left.  In both functions *internal stacks* are\n",
+      "printed left-to-right.  These functions are written to support :doc:`../pretty`.\n",
+      "\n",
+      ".. _cons list: https://en.wikipedia.org/wiki/Cons#Lists\n"
+     ]
+    }
+   ],
    "source": [
     "import inspect\n",
     "import joy.utils.stack\n",
     "\n",
     "\n",
-    "print inspect.getdoc(joy.utils.stack)"
+    "print(inspect.getdoc(joy.utils.stack))"
    ]
   },
   {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "(1, (2, (3, ())))"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
     "joy.utils.stack.list_to_stack([1, 2, 3])"
    ]
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 2, 3]"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
     "list(joy.utils.stack.iter_stack((1, (2, (3, ())))))"
    ]
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "(3, (2, (1, ())))\n",
+      "[3, 2, 1]\n"
+     ]
+    }
+   ],
    "source": [
     "stack = ()\n",
     "\n",
     "for n in [1, 2, 3]:\n",
     "    stack = n, stack\n",
     "\n",
-    "print stack\n",
-    "print list(joy.utils.stack.iter_stack(stack))"
+    "print(stack)\n",
+    "print(list(joy.utils.stack.iter_stack(stack)))"
    ]
   },
   {
    "cell_type": "code",
    "execution_count": 5,
    "metadata": {},
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "def joy(stack, expression, dictionary, viewer=None):\n",
+      "\t'''Evaluate a Joy expression on a stack.\n",
+      "\n",
+      "\tThis function iterates through a sequence of terms which are either\n",
+      "\tliterals (strings, numbers, sequences of terms) or function symbols.\n",
+      "\tLiterals are put onto the stack and functions are looked up in the\n",
+      "\tdisctionary and executed.\n",
+      "\n",
+      "\tThe viewer is a function that is called with the stack and expression\n",
+      "\ton every iteration, its return value is ignored.\n",
+      "\n",
+      "\t:param stack stack: The stack.\n",
+      "\t:param stack expression: The expression to evaluate.\n",
+      "\t:param dict dictionary: A ``dict`` mapping names to Joy functions.\n",
+      "\t:param function viewer: Optional viewer function.\n",
+      "\t:rtype: (stack, (), dictionary)\n",
+      "\n",
+      "\t'''\n",
+      "\twhile expression:\n",
+      "\n",
+      "\t\tif viewer: viewer(stack, expression)\n",
+      "\n",
+      "\t\tterm, expression = expression\n",
+      "\t\tif isinstance(term, Symbol):\n",
+      "\t\t\tterm = dictionary[term]\n",
+      "\t\t\tstack, expression, dictionary = term(stack, expression, dictionary)\n",
+      "\t\telse:\n",
+      "\t\t\tstack = term, stack\n",
+      "\n",
+      "\tif viewer: viewer(stack, expression)\n",
+      "\treturn stack, expression, dictionary\n",
+      "\n"
+     ]
+    }
+   ],
    "source": [
     "import joy.joy\n",
     "\n",
-    "print inspect.getsource(joy.joy.joy)"
+    "print(inspect.getsource(joy.joy.joy))"
    ]
   },
   {
    "source": [
     "import joy.parser\n",
     "\n",
-    "print inspect.getdoc(joy.parser)"
+    "print(inspect.getdoc(joy.parser))"
    ]
   },
   {
     }
    ],
    "source": [
-    "print inspect.getsource(joy.parser._parse)"
+    "print(inspect.getsource(joy.parser._parse))"
    ]
   },
   {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •\n"
+      "!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •\n"
      ]
     }
    ],
    "source": [
     "import joy.library\n",
     "\n",
-    "print ' '.join(sorted(joy.library.initialize()))"
+    "print(' '.join(sorted(joy.library.initialize())))"
    ]
   },
   {
      "output_type": "stream",
      "text": [
       "@inscribe\n",
-      "@combinator_effect(_COMB_NUMS(), a1, s1)\n",
       "@FunctionWrapper\n",
       "def dip(stack, expression, dictionary):\n",
       "\t'''\n",
     }
    ],
    "source": [
-    "print inspect.getsource(joy.library.dip)"
+    "print(inspect.getsource(joy.library.dip))"
    ]
   },
   {
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "? == dup truthy\n",
-      "*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons\n",
-      "*fraction0 == concat [[swap] dip * [*] dip] infra\n",
-      "anamorphism == [pop []] swap [dip swons] genrec\n",
-      "average == [sum 1.0 *] [size] cleave /\n",
-      "binary == nullary [popop] dip\n",
-      "cleave == fork [popd] dip\n",
-      "codireco == cons dip rest cons\n",
-      "dinfrirst == dip infra first\n",
-      "unstack == ? [uncons ?] loop pop\n",
-      "down_to_zero == [0 >] [dup --] while\n",
-      "dupdipd == dup dipd\n",
-      "enstacken == stack [clear] dip\n",
-      "flatten == [] swap [concat] step\n",
-      "fork == [i] app2\n",
-      "gcd == 1 [tuck modulus dup 0 >] loop pop\n",
-      "ifte == [nullary not] dipd branch\n",
-      "ii == [dip] dupdip i\n",
-      "least_fraction == dup [gcd] infra [div] concat map\n",
-      "make_generator == [codireco] ccons\n",
-      "nullary == [stack] dinfrirst\n",
-      "of == swap at\n",
-      "pam == [i] map\n",
-      "primrec == [i] genrec\n",
-      "product == 1 swap [*] step\n",
-      "quoted == [unit] dip\n",
-      "range == [0 <=] [1 - dup] anamorphism\n",
-      "range_to_zero == unit [down_to_zero] infra\n",
-      "run == [] swap infra\n",
-      "size == 0 swap [pop ++] step\n",
-      "sqr == dup mul\n",
-      "step_zero == 0 roll> step\n",
-      "swoncat == swap concat\n",
-      "ternary == unary [popop] dip\n",
-      "unary == nullary popd\n",
-      "unquoted == [i] dip\n",
-      "while == swap [nullary] cons dup dipd concat loop\n",
+      "? dup truthy\n",
+      "*fraction [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons\n",
+      "*fraction0 concat [[swap] dip * [*] dip] infra\n",
+      "anamorphism [pop []] swap [dip swons] genrec\n",
+      "average [sum 1.0 *] [size] cleave /\n",
+      "binary nullary [popop] dip\n",
+      "cleave fork [popd] dip\n",
+      "codireco cons dip rest cons\n",
+      "dinfrirst dip infra first\n",
+      "unstack ? [uncons ?] loop pop\n",
+      "down_to_zero [0 >] [dup --] while\n",
+      "dupdipd dup dipd\n",
+      "enstacken stack [clear] dip\n",
+      "flatten [] swap [concat] step\n",
+      "fork [i] app2\n",
+      "gcd 1 [tuck modulus dup 0 >] loop pop\n",
+      "ifte [nullary not] dipd branch\n",
+      "ii [dip] dupdip i\n",
+      "least_fraction dup [gcd] infra [div] concat map\n",
+      "make_generator [codireco] ccons\n",
+      "nullary [stack] dinfrirst\n",
+      "of swap at\n",
+      "pam [i] map\n",
+      "tailrec [i] genrec\n",
+      "product 1 swap [*] step\n",
+      "quoted [unit] dip\n",
+      "range [0 <=] [1 - dup] anamorphism\n",
+      "range_to_zero unit [down_to_zero] infra\n",
+      "run [] swap infra\n",
+      "size 0 swap [pop ++] step\n",
+      "sqr dup mul\n",
+      "step_zero 0 roll> step\n",
+      "swoncat swap concat\n",
+      "tailrec [i] genrec\n",
+      "ternary unary [popop] dip\n",
+      "unary nullary popd\n",
+      "unquoted [i] dip\n",
+      "while swap [nullary] cons dup dipd concat loop\n",
       "\n"
      ]
     }
    ],
    "source": [
-    "print joy.library.definitions"
+    "print(joy.library.definitions)"
    ]
   },
   {
   "language_info": {
    "codemirror_mode": {
     "name": "ipython",
-    "version": 2
+    "version": 3
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
-   "pygments_lexer": "ipython2",
-   "version": "2.7.12"
+   "pygments_lexer": "ipython3",
+   "version": "3.8.3"
   }
  },
  "nbformat": 4,
index bcfadd9..bc31f30 100644 (file)
@@ -309,7 +309,7 @@ def inscribe_(stack, expression, dictionary):
        definition is given as a string with a name followed by a double
        equal sign then one or more Joy functions, the body. for example:
 
-                       sqr == dup mul
+               sqr == dup mul
 
        If you want the definition to persist over restarts, enter it into
        the definitions.txt resource.
@@ -350,9 +350,9 @@ def getitem(stack):
        nth position in the quote counting from 0.
        ::
 
-                        [a b c d] 0 getitem
+                  [a b c d] 0 getitem
                -------------------------
-                                                               a
+                   a
 
        '''
        n, (Q, stack) = stack
@@ -371,9 +371,9 @@ def drop(stack):
        n items removed off the top.
        ::
 
-                        [a b c d] 2 drop
+                  [a b c d] 2 drop
                ----------------------
-                                        [c d]
+                      [c d]
 
        '''
        n, (Q, stack) = stack
@@ -395,9 +395,9 @@ def take(stack):
        use reverse if needed.)
        ::
 
-                        [a b c d] 2 take
+                  [a b c d] 2 take
                ----------------------
-                                        [b a]
+                      [b a]
 
        '''
        n, (Q, stack) = stack
@@ -419,14 +419,14 @@ def choice(stack):
        Use a Boolean value to select one of two items.
        ::
 
-                               A B False choice
-                ----------------------
-                                                        A
+                  A B False choice
+               ----------------------
+                  A
 
 
-                               A B True choice
-                ---------------------
-                                                        B
+                  A B True choice
+               ---------------------
+                    B
 
        Currently Python semantics are used to evaluate the "truthiness" of the
        Boolean value (so empty string, zero, etc. are counted as false, etc.)
@@ -442,14 +442,14 @@ def select(stack):
        Use a Boolean value to select one of two items from a sequence.
        ::
 
-                               [A B] False select
-                ------------------------
-                                                               A
+                  [A B] False select
+               ------------------------
+                   A
 
 
-                               [A B] True select
-                -----------------------
-                                                        B
+                  [A B] True select
+               -----------------------
+                     B
 
        The sequence can contain more than two items but not fewer.
        Currently Python semantics are used to evaluate the "truthiness" of the
@@ -479,9 +479,12 @@ def min_(S):
 @inscribe
 @SimpleFunctionWrapper
 def sum_(S):
-       '''Given a quoted sequence of numbers return the sum.
+       '''
+       Given a quoted sequence of numbers return the sum.
+       ::
+
+               sum == 0 swap [+] step
 
-       sum == 0 swap [+] step
        '''
        tos, stack = S
        return sum(iter_stack(tos)), stack
@@ -495,9 +498,9 @@ def remove(S):
        from the the quote.  The item is only removed once.
        ::
 
-                        [1 2 3 1] 1 remove
+                  [1 2 3 1] 1 remove
                ------------------------
-                                               [2 3 1]
+                    [2 3 1]
 
        '''
        (tos, (second, stack)) = S
@@ -531,7 +534,7 @@ def clear(stack):
 
                clear == stack [pop stack] loop
 
-                        ... clear
+                  ... clear
                ---------------
 
        '''
@@ -551,7 +554,8 @@ def disenstacken(stack):
 @inscribe
 @SimpleFunctionWrapper
 def reverse(S):
-       '''Reverse the list on the top of the stack.
+       '''
+       Reverse the list on the top of the stack.
        ::
 
                reverse == [] swap shunt
@@ -566,12 +570,13 @@ def reverse(S):
 @inscribe
 @SimpleFunctionWrapper
 def concat_(S):
-       '''Concatinate the two lists on the top of the stack.
+       '''
+       Concatinate the two lists on the top of the stack.
        ::
 
-                        [a b c] [d e f] concat
+                  [a b c] [d e f] concat
                ----------------------------
-                                        [a b c d e f]
+                      [a b c d e f]
 
        '''
        (tos, (second, stack)) = S
@@ -581,14 +586,15 @@ def concat_(S):
 @inscribe
 @SimpleFunctionWrapper
 def shunt(stack):
-       '''Like concat but reverses the top list into the second.
+       '''
+       Like concat but reverses the top list into the second.
        ::
 
                shunt == [swons] step == reverse swap concat
 
-                        [a b c] [d e f] shunt
+                  [a b c] [d e f] shunt
                ---------------------------
-                                [f e d a b c] 
+                      [f e d a b c] 
 
        '''
        (tos, (second, stack)) = stack
@@ -636,9 +642,9 @@ def pm(stack):
        Plus or minus
        ::
 
-                        a b pm
+                  a b pm
                -------------
-                        a+b a-b
+                  a+b a-b
 
        '''
        a, (b, stack) = stack
@@ -811,9 +817,9 @@ def i(stack, expression, dictionary):
        onto the pending expression for evaluation.
        ::
 
-                        [Q] i
+                  [Q] i
                -----------
-                               Q
+                   Q
 
        '''
        quote, stack = stack
@@ -880,9 +886,9 @@ def infra(stack, expression, dictionary):
        with the list as its stack.  Does not affect the rest of the stack.
        ::
 
-                        ... [a b c] [Q] . infra
+                  ... [a b c] [Q] . infra
                -----------------------------
-                        c b a . Q [...] swaack
+                   c b a . Q [...] swaack
 
        '''
        (quote, (aggregate, stack)) = stack
@@ -919,12 +925,12 @@ def genrec(stack, expression, dictionary):
        For example, given a (general recursive) function 'F':
        ::
 
-                       F == [I] [T] [R1] [R2] genrec
+               F == [I] [T] [R1] [R2] genrec
 
        If the [I] if-part fails you must derive R1 and R2 from:
        ::
 
-                       ... R1 [F] R2
+               ... R1 [F] R2
 
        Just set the stack arguments in front, and figure out what R1 and R2
        have to do to apply the quoted [F] in the proper way.  In effect, the
@@ -932,15 +938,15 @@ def genrec(stack, expression, dictionary):
        the original definition in the else-part:
        ::
 
-                       F == [I] [T] [R1]   [R2] genrec
-                         == [I] [T] [R1 [F] R2] ifte
+               F == [I] [T] [R1]   [R2] genrec
+                 == [I] [T] [R1 [F] R2] ifte
 
        Primitive recursive functions are those where R2 == i.
        ::
 
-                       P == [I] [T] [R] tailrec
-                         == [I] [T] [R [P] i] ifte
-                         == [I] [T] [R P] ifte
+               P == [I] [T] [R] tailrec
+                 == [I] [T] [R [P] i] ifte
+                 == [I] [T] [R P] ifte
 
        '''
        (rec2, (rec1, stack)) = stack
@@ -985,9 +991,9 @@ def primrec(stack, expression, dictionary):
        the data parameter is zero, then the first quotation has to produce
        the value to be returned. If the data parameter is positive then the
        second has to combine the data parameter with the result of applying
-       the function to its predecessor.
+       the function to its predecessor.::
 
-       5  [1]  [*]  primrec
+               5  [1]  [*]  primrec
 
        > Then primrec tests whether the top element on the stack (initially
        the 5) is equal to zero. If it is, it pops it off and executes one of
@@ -995,17 +1001,17 @@ def primrec(stack, expression, dictionary):
        Otherwise it pushes a decremented copy of the top element and
        recurses. On the way back from the recursion it uses the other
        quotation, [*], to multiply what is now a factorial on top of the
-       stack by the second element on the stack.
+       stack by the second element on the stack.::
 
                n [Base] [Recur] primrec
 
-          0 [Base] [Recur] primrec
-       ------------------------------
-             Base
+                  0 [Base] [Recur] primrec
+               ------------------------------
+                     Base
 
-          n [Base] [Recur] primrec
-       ------------------------------------------ n > 0
-          n (n-1) [Base] [Recur] primrec Recur
+                        n [Base] [Recur] primrec
+               ------------------------------------------ n > 0
+                  n (n-1) [Base] [Recur] primrec Recur
 
        '''
        recur, (base, (n, stack)) = stack
@@ -1039,17 +1045,17 @@ def branch(stack, expression, dictionary):
 
        ::
 
-                       branch == roll< choice i
+               branch == roll< choice i
 
        ::
 
-                               False [F] [T] branch
-                --------------------------
-                                                        F
+                  False [F] [T] branch
+               --------------------------
+                         F
 
-                               True [F] [T] branch
-                -------------------------
-                                                                       T
+                  True [F] [T] branch
+               -------------------------
+                            T
 
        '''
        (then, (else_, (flag, stack))) = stack
@@ -1096,9 +1102,9 @@ def cond(stack, expression, dictionary):
 
        It works by rewriting into a chain of nested `ifte` expressions, e.g.::
 
-                                               [[[B0] T0] [[B1] T1] [D]] cond
-                       -----------------------------------------
-                                [B0] [T0] [[B1] [T1] [D] ifte] ifte
+                     [[[B0] T0] [[B1] T1] [D]] cond
+               -----------------------------------------
+                  [B0] [T0] [[B1] [T1] [D] ifte] ifte
 
        '''
        conditions, stack = stack
@@ -1133,9 +1139,9 @@ def dip(stack, expression, dictionary):
        on the rest of the stack.
        ::
 
-                        ... x [Q] dip
+                  ... x [Q] dip
                -------------------
-                                ... Q x
+                    ... Q x
 
        '''
        (quote, (x, stack)) = stack
@@ -1150,9 +1156,9 @@ def dipd(S, expression, dictionary):
        Like dip but expects two items.
        ::
 
-                        ... y x [Q] dip
+                  ... y x [Q] dip
                ---------------------
-                                ... Q y x
+                    ... Q y x
 
        '''
        (quote, (x, (y, stack))) = S
@@ -1167,9 +1173,9 @@ def dipdd(S, expression, dictionary):
        Like dip but expects three items.
        ::
 
-                        ... z y x [Q] dip
+                  ... z y x [Q] dip
                -----------------------
-                                ... Q z y x
+                    ... Q z y x
 
        '''
        (quote, (x, (y, (z, stack)))) = S
@@ -1186,9 +1192,10 @@ def app1(S, expression, dictionary):
        program.
        ::
 
-                                                       ... x [Q] . app1
-                -----------------------------------
-                               ... [x ...] [Q] . infra first
+                        ... x [Q] . app1
+               -----------------------------------
+                  ... [x ...] [Q] . infra first
+
        '''
        (quote, (x, stack)) = S
        stack = (quote, ((x, stack), stack))
@@ -1202,10 +1209,10 @@ def app2(S, expression, dictionary):
        '''Like app1 with two items.
        ::
 
-                                               ... y x [Q] . app2
-                -----------------------------------
-                               ... [y ...] [Q] . infra first
-                                               [x ...] [Q]   infra first
+                      ... y x [Q] . app2
+               -----------------------------------
+                  ... [y ...] [Q] . infra first
+                      [x ...] [Q]   infra first
 
        '''
        (quote, (x, (y, stack))) = S
@@ -1222,11 +1229,11 @@ def app3(S, expression, dictionary):
        '''Like app1 with three items.
        ::
 
-                                               ... z y x [Q] . app3
-                -----------------------------------
-                               ... [z ...] [Q] . infra first
-                                               [y ...] [Q]   infra first
-                                               [x ...] [Q]   infra first
+                    ... z y x [Q] . app3
+               -----------------------------------
+                  ... [z ...] [Q] . infra first
+                      [y ...] [Q]   infra first
+                      [x ...] [Q]   infra first
 
        '''
        (quote, (x, (y, (z, stack)))) = S
@@ -1245,19 +1252,19 @@ def step(S, expression, dictionary):
        Run a quoted program on each item in a sequence.
        ::
 
-                                       ... [] [Q] . step
-                        -----------------------
-                                                                ... .
+                  ... [] [Q] . step
+               -----------------------
+                         ... .
 
 
-                                ... [a] [Q] . step
-                       ------------------------
-                                                        ... a . Q
+                  ... [a] [Q] . step
+               ------------------------
+                        ... a . Q
 
 
-                        ... [a b c] [Q] . step
+                  ... [a b c] [Q] . step
                ----------------------------------------
-                                                                ... a . Q [b c] [Q] step
+                            ... a . Q [b c] [Q] step
 
        The step combinator executes the quotation on each member of the list
        on top of the stack.
@@ -1280,19 +1287,19 @@ def times(stack, expression, dictionary):
        times == [-- dip] cons [swap] infra [0 >] swap while pop
        ::
 
-                        ... n [Q] . times
+                  ... n [Q] . times
                ---------------------  w/ n <= 0
-                                                ... .
+                        ... .
 
 
-                        ... 1 [Q] . times
-               ---------------------------------
-                                                ... . Q
+                  ... 1 [Q] . times
+               -----------------------
+                        ... . Q
 
 
-                        ... n [Q] . times
-               ---------------------------------  w/ n > 1
-                                                ... . Q (n - 1) [Q] times
+                  ... n [Q] . times
+               -------------------------------------  w/ n > 1
+                        ... . Q (n - 1) [Q] times
 
        '''
        # times == [-- dip] cons [swap] infra [0 >] swap while pop
@@ -1329,13 +1336,13 @@ def loop(stack, expression, dictionary):
        Basic loop combinator.
        ::
 
-                        ... True [Q] loop
+                  ... True [Q] loop
                -----------------------
-                                       ... Q [Q] loop
+                     ... Q [Q] loop
 
-                        ... False [Q] loop
+                  ... False [Q] loop
                ------------------------
-                                                       ...
+                         ...
 
        '''
        quote, (flag, stack) = stack
@@ -1352,17 +1359,17 @@ def cmp_(stack, expression, dictionary):
        one of the three depending on the results of comparing the two values:
        ::
 
-                                        a b [G] [E] [L] cmp
-                               ------------------------- a > b
-                                                               G
+                  a b [G] [E] [L] cmp
+               ------------------------- a > b
+                       G
 
-                                        a b [G] [E] [L] cmp
-                               ------------------------- a = b
-                                                                               E
+                  a b [G] [E] [L] cmp
+               ------------------------- a = b
+                           E
 
-                                        a b [G] [E] [L] cmp
-                               ------------------------- a < b
-                                                                                               L
+                  a b [G] [E] [L] cmp
+               ------------------------- a < b
+                               L
        '''
        L, (E, (G, (b, (a, stack)))) = stack
        expression = concat(G if a > b else L if a < b else E, expression)