ICFP Contest 2006
Team: Abstraction Anonymous

yang_README

Download

  1. ,-'~~~'-,
  2. .~ `. ~.
  3. / 8 | \
  4. : ,' :
  5. | .--~ |
  6. ! ; !
  7. \ | 8 /
  8. `. ', .'
  9. `-.___.-`
  10.  
  11. B A L A N C E
  12. User's Manual
  13.  
  14.  
  15. I. Introduction
  16.  
  17. Night and day. Beauty and truth. Oxygen and phlogiston. Everywhere we
  18. look there are perfect opposites. Balance is a programming language
  19. based on the concept of harmoniously coexisting duals. In this
  20. language, every operation has an equal and opposite reaction. This
  21. ensures that the machine state does not stray from equilibrium.
  22. Because it performs two operations for each instruction, the language
  23. is also twice as fast as single-operation languages.
  24.  
  25. Balance programs are run inside an 8-bit machine with the following
  26. features:
  27. * CODE: an arbitrarily long immutable stream of bytes
  28. * M[0..255]: 256 8-bit bytes of memory
  29. * IP: the instruction pointer (an arbitrary non-negative value)
  30. * IS: the instruction speed (ranging from -16 to +15)
  31. * sR[0..3]: four 8-bit source registers
  32. * dR[0..1]: two 8-bit destination registers
  33. * four instructions
  34.  
  35. Each instruction is specified by a single 8-bit byte. The bits
  36. are numbered from most to least meaningful as follows:
  37.  
  38. lmb
  39. .--------.
  40. |76543210|
  41. `--------'
  42. mmb
  43.  
  44. * The bits 7,6,5 specify the opcode
  45. and depending on the instruction:
  46. * bits 4,3,2,1,0 specify an immediate value IMM
  47. or
  48. * bit 4 denotes a destination register D
  49. * bits 3,2 denote the first source register S1
  50. * bits 1,0 denote the second source register S2
  51.  
  52. Every problem in computer science can be solved by an additional layer
  53. of indirection. Balance thus provides this facility automatically:
  54. Each of the source and destination registers is an indirect register.
  55. For instance, most instructions do not work on the contents of
  56. registers S1 and S2 directly, but use the contents of S1 and S2 as
  57. indices into the memory M. The result is not stored in D, but rather
  58. stored in the memory location indicated by the current contents of D.
  59.  
  60. The machine begins with IP = 0 and IS = 1. At each step of the
  61. machine, an instruction is fetched from CODE[IP] (where CODE[0] is the
  62. first instruction, CODE[1] the second, etc.). The instruction is
  63. executed, and then IP is increased (or decreased) by the value of IS,
  64. modulo the length of CODE.
  65.  
  66.  
  67. II. Instruction Reference
  68.  
  69. The four instructions of Balance are MATH, LOGIC, SCIENCE, and
  70. PHYSICS. The following is their specification; some examples are
  71. given in a separate section below.
  72.  
  73.  
  74. Opcode (bits) Description
  75. MATH 001
  76. MATH performs addition and its dual, subtraction.
  77. These act on different registers so that the math is
  78. not undone. All operations are modular with respect
  79. to the number of relevant bits. Source registers are
  80. represented with two bits, so if S1 is 3, then
  81. sR[S1+1] is sR[0]. Similarly, dR[1+1] is dR[0].
  82. Quantities in memory are eight bits, so 250 + 20 is
  83. 14.
  84.  
  85. M[ dR[D+1] ] <- M[ sR[S1+1] ] - M[ sR[S2+1] ]
  86. M[ dR[D] ] <- M[ sR[S1] ] + M[ sR[S2] ]
  87.  
  88. LOGIC 010
  89. LOGIC performs bitwise 'and' as well as its perfect
  90. dual, bitwise 'exclusive or.'
  91.  
  92. M[ dR[D+1] ] <- M[ sR[S1+1] ] XOR M[ sR[S2+1] ]
  93. M[ dR[D] ] <- M[ sR[S1] ] AND M[ sR[S2] ]
  94.  
  95. SCIENCE 000
  96. SCIENCE tests a hypothesis and determines the speed
  97. at which the program progresses. When executed, it
  98. sets the instruction speed IS to immediate value IMM,
  99. as long as the memory cell indicated by sR[0] does
  100. not contain 0. Because this instruction behaves
  101. specially when the memory cell contains 0, it also
  102. behaves specially if IS is set to zero: the machine
  103. then halts. The value IMM is treated as a signed
  104. five-bit number in two's complement form, so it can
  105. take on values from -16 to +15.
  106.  
  107. if M[ sR[0] ] = 0 then (nothing)
  108. otherwise IS <- IMM
  109.  
  110. if IS = 0 then HALT
  111. else (nothing)
  112.  
  113. PHYSICS 011
  114. PHYSICS changes what the registers reference, in both
  115. a linear and angular way. The immediate value IMM,
  116. treated as a signed five-bit number, is added to the
  117. register sR[0] so that it may reference a different
  118. memory cell. The instruction also rotates the values
  119. between some subset of the registers, according to a
  120. bitmask derived from IMM. The source register sR[0]
  121. is always part of the rotated set, so the bitmask
  122. used is a 6 bit number where the lowest 5 bits are
  123. the same as IMM and the sixth bit is always 1.
  124. sR[0] <- sR[0] + (IMM as signed 5-bit number)
  125. let L=L0,...,L4 be the registers
  126. dR[1], dR[0], sR[3], sR[2], sR[1]
  127. then let C be the list of n elements Li
  128. such that bit i is set in IMM
  129. (bit 0 is the least significant,
  130. bit 4 is the most significant)
  131. then let Cs be the list (sR[0], C0, ..., C(n-1))
  132. and let Cd be the list (C0, ..., C(n-1), sR[0])
  133. then, simultaneously
  134. Cd0 <- Cs0
  135. ...
  136. Cdn <- Csn
  137.  
  138. Any other opcode stands for the instruction BAIL, which will cause the
  139. machine to terminate in failure. Programmers sometimes insert such
  140. bugs deliberately in order to quickly halt the interpreter during
  141. testing.
  142.  
  143.  
  144. III. Examples
  145.  
  146. If M[sR[0]] = 0, IP = 3, IS = 6, length(CODE) = 100,
  147. and CODE[IP] = SCIENCE 12,
  148. then in the next cycle IP = 9 and IS = 6.
  149.  
  150. If M[sR[0]] = 9, IP = 3, IS = 6, length(CODE) = 100,
  151. and CODE[IP] = SCIENCE 12,
  152. then in the next cycle IP = 15 and IS = 12.
  153.  
  154. If sR = {0, 1, 2, 3}, dR = {4, 5}, M = {2, 3, 5, 7, 11, 13, 17, ...},
  155. and CODE[IP] = MATH (0, 3, 1)
  156. then in the next cycle M = {2, 3, 5, 7, 10, 253, 17, ...}.
  157.  
  158. If sR = {0, 1, 2, 3}, dR = {4, 5}, M = {2, 3, 5, 7, 11, 13, 17, ...},
  159. and CODE[IP] = LOGIC (0, 3, 1)
  160. then in the next cycle M = {2, 3, 5, 7, 3, 7, 17, ...}.
  161.  
  162. If sR = {0, 1, 2, 3}, dR = {4, 5}
  163. and CODE[IP] = PHYSICS -1
  164. then sR[0] is updated with sR[0] + -1 = 255
  165. and in the next cycle sR = {1, 2, 3, 4}, dR = {5, 255}.
  166.  
  167. If sR = {0, 1, 2, 3}, dR = {4, 5}
  168. and CODE[IP] = PHYSICS -16
  169. Then sR[0] is updated with sR[0] + (-16) = -16.
  170. The bitmask for rotation is 1 1 0 0 0 0
  171. for the register set {-16, 1, 2, 3} {4, 5},
  172. so in the next cycle sR = {1, -16, 2, 3}, dR = {4, 5}.
  173.  
  174. If sR = {0, 1, 2, 3}, dR = {4, 5}
  175. and CODE[IP] = PHYSICS 15
  176. then sR[0] is updated with sR[0] + 15 = 15.
  177. The bitmask for rotation is 1 0 1 1 1 1
  178. for the register set {15, 1, 2, 3} {4, 5}
  179. so in the next cycle sR = {2, 1, 3, 4}, dR = {5, 15}.
  180.  
  181.  
  182. IV. Syntax
  183.  
  184. The Balance language concrete syntax is simply a single line of bytes,
  185. each written as two hexadecimal digits, with no whitespace or other
  186. characters.
  187.  
  188.  
  189. V. Balance Certified Professional Program (BCPP)
  190.  
  191. As a professional programmer, you are invited to join one of the
  192. industry's leading programmer certification programs. BCPP
  193. certification can be obtained automatically by solving a series of
  194. challenge problems and verifying the results using the supplied
  195. program "certify".
  196.  
  197. Please see the file PUZZLES for the list of challenge problems. To
  198. certify a solution, stored in a file called "solve.bal", to the
  199. problem called "prop", run the command
  200.  
  201. certify prop solve.bal
  202.  
  203. Because a professional programmer knows that shorter code is better
  204. code, your certified skill level depends on the length of the program
  205. that you submit to the certifier.
  206.  
  207. A challenge problem consists of a description of the initial register
  208. and memory values and the desired final configuration. A program
  209. solves the challenge if it achieves the final configuration and halts
  210. gracefully (SCIENCE 0 with M[sR[0]] <> 0).
  211.  
  212. Accepted applicants will receive an engraved sandstone diploma and
  213. will be added to the 19101 electronic edition of "Who's Who in
  214. Computerology." Please allow 4-6 weeks for delivery.
  215.  

Hell is other programming languages. -- Sartran
Hell is that programming language! -- Dan
Ordinarily, one would enrich this language with more powerful means of computation. Instead I take a different tack... -- Harmonious Monk