Guest User

Untitled

a guest
Nov 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. This is a term that performs modulo 32 addition on two 32-bit binary numbers efficiently on the abstract algorithm.
  2.  
  3. On this example, I compute `279739872 + 496122620 = 775862492`.
  4.  
  5. ```
  6. binSize= 32
  7. binZero= (binSize r.a.b.c.(a r) a.b.c.c)
  8. binSucc= (binSize r.x.a.b.c.(x b x.(a (r x)) c) a.a)
  9. binFold= x.a.b.c.(binSize r.x.(x x.f.(a (f x)) x.f.(b (f x)) f.c r) a.c x)
  10. binToNat= (binSize r.n.x.(x x.f.(f x) x.f.(add n (f x)) f.0 (r (mul 2 n))) n.x.0 1)
  11. binAdd= a. b. (binToNat a binSucc (binToNat b binSucc binZero))
  12.  
  13. A= x.a.b.c.(a x)
  14. B= x.a.b.c.(b x)
  15. C= a.b.c.c
  16. X= (A (A (A (A (A (B (B (B (B (A (B (B (B (B (B (A (A (A (B (B (A (B (A (B (A (A (A (A (B (A (A (A C))))))))))))))))))))))))))))))))
  17. Y= (A (A (B (B (B (B (B (B (A (B (A (B (B (B (A (A (A (B (A (A (B (A (A (B (B (A (B (B (B (A (A (A C))))))))))))))))))))))))))))))))
  18.  
  19. (binFold (binAdd X Y))
  20. ```
  21.  
  22. This works by converting both bitstrings to unary (i.e., Nat), and then applying the binary `succ` function `279739872 + 496122620` times to the bitstring `zero`. So, yes, it applies a linear-time function millions of times to a term that shouldn't even fit in your computer's memory, yet it reaches normal form in merely `152715` rewrites! That's amazing, isn't it? This example shows well the power of optimal sharing.
  23.  
  24. Of course, there are faster solutions. By understanding how to make those terms lightweight, you could write this:
  25.  
  26. ```
  27. binAdd= x.y.
  28. (x.(x x)
  29. r.x.y.k.(x
  30. xs.(y
  31. ys.xs.k.f.a.b.c.(k b a (f xs ys a.b.b))
  32. ys.xs.k.f.a.b.c.(k a b (f xs ys k))
  33. xs.k.f.a.b.c.c
  34. xs)
  35. xs.(y
  36. ys.xs.k.f.a.b.c.(k a b (f xs ys k))
  37. ys.xs.k.f.a.b.c.(k b a (f xs ys a.b.a))
  38. xs.k.f.a.b.c.c
  39. xs)
  40. k.f.a.b.c.c
  41. k
  42. (r r))
  43. x
  44. y
  45. a.b.b)
  46.  
  47. binSize= 32
  48. binFold= x.a.b.c.(binSize r.x.(x x.f.(a (f x)) x.f.(b (f x)) f.c r) a.c x)
  49.  
  50. A= x.a.b.c.(a x)
  51. B= x.a.b.c.(b x)
  52. C= a.b.c.c
  53. X= (A (A (A (A (A (B (B (B (B (A (B (B (B (B (B (A (A (A (B (B (A (B (A (B (A (A (A (A (B (A (A (A C))))))))))))))))))))))))))))))))
  54. Y= (A (A (B (B (B (B (B (B (A (B (A (B (B (B (A (A (A (B (A (A (B (A (A (B (B (A (B (B (B (A (A (A C))))))))))))))))))))))))))))))))
  55.  
  56. (binFold (binAdd X Y))
  57. ```
  58.  
  59. It performs the same computation, but on arbitrary bit-strings instead of mod-32, and takes only `7817` total rewrites. It is just the equivalent of an usual Haskellish carry-passing recursive addition, though, so no sharing wizardry.
  60.  
  61. *Not so fun fact: it took me years to develop the right techniques and intuition to be able to write those. Yes, for all that time I wasn't able to code a fast binary addition term; and, trust me, I tried A LOT of things. All my previous attempts had exponential complexity. As expected, it turned out to be amazingly simple.*
  62.  
  63. Gist per request of Anton Salikhmetov, who is doing amazing progress :)
Add Comment
Please, Sign In to add comment