⍝   J a p a n e s e   A b a c u s         See →notes.soroban←

    ← /()       ⍝ beam (upper/lower deck separator).
    ← .()       ⍝ digit (column) separator.

    ← +() -()   ⍝ add sub

      ⍺() ⍵()   ⍝ soroban ←→ decimal     (mnemonic: ⍺bacus ←→ ⍵estern)

      h :: 0 1                              ⍝ upper deck (heaven) values.
      e :: 0 1 2 3 4                        ⍝ lower deck (earth)  values.
      d :: 0 1 2 3 4 5 6 7 8 9              ⍝ decimal digit.
    p q ::                                  ⍝ match any values.

⍝ Soroban from Decimal:

    ⍺ e   = 0/e                             ⍝ values 0..4
    ⍺ d   = 1/(⌊d)                          ⍝ values 5..9
    ⍺ p.q = (⍺ p).(⍺ q)                     ⍝ multiple digits.

⍝ Decimal from Soroban:

    ⍵ 0/e =  e                              ⍝ values 0..4
    ⍵ 1/e = ⌈e                              ⍝ values 5..9
    ⍵ p.q = (⍵ p).(⍵ q)                     ⍝ multiple digits.

⍝ Simplifications:

    0/0.q = q                               ⍝ ignore leading 0/0 columns.

⍝ Addition:

    h/e + 0 = h/e                           ⍝ + 0
    h/0 + e = h/e

    h/1 + 1 = h/2                           ⍝ + 1
    h/2 + 1 = h/3
    h/3 + 1 = h/4
    h/4 + 1 = (h+1)/0

    h/1 + 2 = h/3                           ⍝ + 2
    h/2 + 2 = h/4
    h/3 + 2 = (h+1)/0
    h/4 + 2 = (h+1)/1

    h/1 + 3 = h/4                           ⍝ + 3
    h/2 + 3 = (h+1)/0
    h/3 + 3 = (h+1)/1
    h/4 + 3 = (h+1)/2

    h/1 + 4 = (h+1)/0                       ⍝ + 4
    h/2 + 4 = (h+1)/1
    h/3 + 4 = (h+1)/2
    h/4 + 4 = (h+1)/3

    h/e + d = (h+1)/e + (⌊d)                ⍝ + 5..9

   p.(h/e) +   d =        p .(h/e + d)      ⍝ multiple digits:
      h/e  + q.d = (0/0 + q).(h/e + d)      ⍝ add columns in parallel.
   p.(h/e) + q.d = (p   + q).(h/e + d)

⍝ Overflow / carry:

      (0+1)/e = 1/e
      (1+1)/e = 0/1.0/e
    p.(0/1.q) = (p+1).q

⍝ Subtraction:

    h/e - 0 = h/e                           ⍝ - 0
    h/e - e = h/0                           ⍝ e-e

    h/0 - 1 = (h-1)/4                       ⍝ - 1
    h/2 - 1 = h/1
    h/3 - 1 = h/2
    h/4 - 1 = h/3

    h/0 - 2 = (h-1)/3                       ⍝ - 2
    h/1 - 2 = (h-1)/4
    h/3 - 2 = h/1
    h/4 - 2 = h/2

    h/0 - 3 = (h-1)/2                       ⍝ - 3
    h/1 - 3 = (h-1)/3
    h/2 - 3 = (h-1)/4
    h/4 - 3 = h/1

    h/0 - 4 = (h-1)/1                       ⍝ - 4
    h/1 - 4 = (h-1)/2
    h/2 - 4 = (h-1)/3
    h/3 - 4 = (h-1)/4

    h/e - d = (h-1)/e - (⌊d)                ⍝ - 5..9

   p.(h/e) -   d =      p .(h/e - d)        ⍝ multiple digits:
   p.(h/e) - q.d = (p - q).(h/e - d)        ⍝ sub columns in parallel.

⍝ Underflow / carry:

       (1-1)/e    = 0/e
    p. (0-1)/e    = (p-1).(1/e)
    p.((0-1)/e-d) = (p-1).1/e - d

⍝ Residue / Over:

    ⌊()        ⌈()

    ⌊5 = 0  ⋄  ⌈0 = 5
    ⌊6 = 1  ⋄  ⌈1 = 6
    ⌊7 = 2  ⋄  ⌈2 = 7
    ⌊8 = 3  ⋄  ⌈3 = 8
    ⌊9 = 4  ⋄  ⌈4 = 9

⍝ Equivalence

    ← =()           ⍝ reduction equivalence

⍝ Test cases:
⍝
⍝     ⍵ ⍺ 1.2.3.4.5.6.7.8.9 -> 1.2.3.4.5.6.7.8.9    ⍝ abacus ←→ decimal.
⍝     ⍺ ⍵ 0/1.1/0.1/2.1/3   -> 0/1.1/0.1/2.1/3      ⍝ decimal ←→ abacus.
⍝                   0/0.0/0 -> 0/0                  ⍝ trim leading 0-cols.
⍝
⍝           ⍵ 0/0+7         -> 7                    ⍝ addition.
⍝           ⍵ 0/0+7+7       -> 1.4
⍝           ⍵ 0/0+7+7+7     -> 2.1
⍝           ⍵ 0/0+7+7+7+7   -> 2.8
⍝           ⍵ 0/0+7+7+7+7+7 -> 3.5
⍝   ⍵ 0/0+1+2+3+4+5+6+7+8+9 -> 4.5
⍝   ⍵ 0/0+9+8+7+6+5+4+3+2+1 -> 4.5
⍝               ⍵ 0/0 + 1.2 -> 1.2
⍝   ⍵ 0/0 + 1.2 + 3.4 + 5.6 -> 1.0.2
⍝
⍝  ⍵ 0/0 + 1.2.3.4.5.6.7.8.9 + 9.8.7.6.5.4.3.2.1 -> 1.1.1.1.1.1.1.1.1.0
⍝
⍝       ⍵ 0/3.1/0-7         -> 2.8                  ⍝ subtraction
⍝       ⍵ 0/3.1/0-7-7       -> 2.1
⍝       ⍵ 0/3.1/0-7-7-7     -> 1.4
⍝       ⍵ 0/3.1/0-7-7-7-7   -> 7
⍝       ⍵ 0/3.1/0-7-7-7-7-7 -> 0
⍝  0/1.0/0.0/2 -1.2-3.4-5.6 -> 0/0
⍝
⍝   ⍵ (⍺ 1.1.1.1.1.1.1.1.1.0) - 1.2.3.4.5.6.7.8.9 -> 9.8.7.6.5.4.3.2.1
⍝   ⍵ (⍺ 1.1.1.1.1.1.1.1.1.0) - 9.8.7.6.5.4.3.2.1 -> 1.2.3.4.5.6.7.8.9
⍝
⍝  0/0 - 1   -> (0-1)/4                             ⍝ no negative numbers.
⍝  1/1 - 2.3 -> 1/1-2.3
⍝   
⍝   Back to: Contents