Opcode 4: Increment#
Syntax#
Opcode 4 implements the idiomatic + lus increment operator, which adds 1 to an atom.
*[a 4 b] +*[a b]
Explanation#
Opcode 4 adds 1 to the product of formula b. This is Nock’s only arithmetic primitive—all other arithmetic must be built from increment.
Evaluate
bagainst the subject, which must produce an atom.Return that atom plus 1.
+[a b] +[a b]
+a 1 + a
We can increment atoms, but not cells (which makes sense). Every natural number (atom) has a successor, so there is no possible crash here (as long as our interpreter actually supports arbitrarily sized integers).
Since increment is the only arithmetic operation, other arithmetic operations must be built from it. For instance, decrement requires counting up from 0, and addition requires repeated incrementing. Thus all arithmetic in Nock is theoretically \(O(n)\) or worse, where n is the size of the numbers involved. In practice, interpreters “jet” (accelerate) known arithmetic patterns (see opcode 11 and jet-accelerated code).
:subject 42
Subject set to: 42
[4 0 1]
43
[4 4 4 4 4 4 4 4 4 0 1]
51
[4 1 42]
43
Expect failure on incrementing a cell:
[4 1 [42 43]]
Error: fail: cell
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/nock_kernel/kernel.py", line 201, in do_execute
result = nock(self.subject, formula)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/pinochle/nock.py", line 178, in nock
return lus(result)
^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/pinochle/nock.py", line 69, in lus
raise Exception("fail: cell")
Exception: fail: cell
Atoms are formally unbounded in size, so incrementing the largest possible atom is not a problem in Nock itself. However, practical interpreters may have limits on the size of atoms they can represent.
In this interpreter, which is written in Python, there are only practical limits (the size of RAM) to integers and thus to atom size.
[4 1 999999999999999999999999999999999999999999999999999999999]
1000000000000000000000000000000000000000000000000000000000