ICFP Contest 2006
Team: Abstraction Anonymous

2d.txt

Download

  1. % cat README
  2. From: ohmega@cbv.net
  3. Newsgroups: cult.cbv.discuss
  4. Message-ID: <2C9F8CC7.3ED3@cbv.net>
  5. Date: 22 Jun 19106 06:44:29
  6. X-Organization: Cult of the Bound Variable
  7. Subject: Programming in Two Dimensions
  8.  
  9.  
  10. Dear cult.cbv.discuss:
  11.  
  12. I'm pleased to announce a new programming language called 2D. This
  13. language frees the programmer from the shackles of linear programming
  14. by allowing programs to occupy two dimensions. However, unlike 3- and
  15. 4- dimensional languages like CUBOL and Hypercard, it does not
  16. distract the programmer's attention with needless dimensional abandon.
  17.  
  18. I first present an overview of the language and then delve into a more
  19. careful description of its syntax and semantics.
  20.  
  21. == 2D Overview ==
  22.  
  23. 2D programs are built from boxes connected together by wires. A box takes
  24. the following form:
  25.  
  26. *=======*
  27. !command!
  28. *=======*
  29.  
  30. Wires can connect boxes:
  31.  
  32.  
  33. *========* *========*
  34. !command1!------>!command2!
  35. *========* *========*
  36.  
  37. Each box has two input interfaces: its North and West sides. It also
  38. has two output interfaces, its South and East sides. The following box
  39. sends the input that it receives on its North interface to its East
  40. interface:
  41.  
  42. |
  43. v
  44. *============*
  45. !send [(N,E)]!----->
  46. *============*
  47.  
  48. Wires carry values from one box to another. Each wire starts out with
  49. no value. When a value is sent along a wire, the wire keeps that same
  50. value forever. A box will only activate when all of its inputs (zero,
  51. one, or two) have values.
  52.  
  53. The values flowing along wires take on the following forms:
  54.  
  55. val ::= () | (val, val) | Inl val | Inr val
  56.  
  57. The () value is the single base value. Two values can be paired
  58. together. They can also be stamped with the disjoint constructors Inl
  59. and Inr. Commands manipulate the structure of values and the control
  60. flow of the program by selectively sending along their outputs. For
  61. example, the 'case' command distinguishes between values stamped with
  62. Inl and Inr:
  63.  
  64. |
  65. v
  66. *=============*
  67. !case N of E,S!----
  68. *=============*
  69. |
  70. +--------------
  71.  
  72. If this box is sent Inl () to its North interface, then () is sent
  73. along the wire connecting to the east interface. If it is sent
  74. Inr ((), ()) then ((), ()) is sent along the south interface instead.
  75.  
  76.  
  77. 2D programs can be organized into modules. A module encapsulates a
  78. collection of boxes and wires and gives them a name. The following
  79. module, called stamp, encapsulates the operation of applying the Inl
  80. and Inr constructors to the first and second components of a pair:
  81.  
  82. ,........|.......................................,
  83. :stamp | :
  84. : v :
  85. : *=======* :
  86. : !split N!-----+ :
  87. : *=======* v :
  88. : | *=========================* :
  89. : +------>!send [((Inl W, Inr N),E)]!------
  90. : *=========================* :
  91. : :
  92. ,................................................,
  93.  
  94. (The split command splits a pair, sending the first component
  95. south and the second component east.)
  96.  
  97. A module can be used as a box itself. The following circuit sends
  98. (Inl (), Inr Inl ()) along the wire to the east:
  99.  
  100. *========================*
  101. !send [(((), Inl ()), E)]|---+
  102. *========================* |
  103. +--------------------------------+
  104. v
  105. *=========*
  106. !use stamp!-----------------------------------
  107. *=========*
  108.  
  109. Each time a "use" box is executed, a new copy of the referenced module
  110. is made (with wires carrying no values). Recursion is just a
  111. particular use of modules: modules may also "use" themselves. Mutual
  112. recursion between modules is also permitted.
  113.  
  114. A module is limited to at most one input along each of its north and
  115. west faces. It may have multiple outputs, all along its east face.
  116. When a module is executed, exactly one of its output wires must be
  117. sent a value; this is the value that the "use" box sends along its
  118. interface.
  119.  
  120. == 2D Syntax ==
  121.  
  122. === Box syntax ===
  123.  
  124. A box's north and south edges are written with the = symbol. Its west
  125. and east edges, which must be exactly one character long, are written
  126. with the ! symbol. The box's corners are written *. No whitespace is
  127. allowed between the command and the box that surrounds it.
  128.  
  129. The concrete syntax for commands is as follows:
  130.  
  131. inface ::= N | W
  132.  
  133. outface ::= S | E
  134.  
  135. exp ::= () | (exp, exp) | Inl exp | Inr exp | inface
  136.  
  137. command ::= send []
  138. | send [(exp, outface)]
  139. | send [(exp, outface), (exp, outface)]
  140. | case exp of outface, outface
  141. | split exp
  142. | use "name"
  143.  
  144. Note that extra parentheses are neither required nor permitted.
  145. A space character may be omitted when the character to its left or to
  146. its right is one of ,()[] and two consecutive space characters are
  147. never allowed.
  148.  
  149. A name consists of one or more characters from the following set:
  150.  
  151. 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  152.  
  153. If a wire is connected to the north side of a box, the v character
  154. must be used as follows:
  155.  
  156. |
  157. v
  158. *=======*
  159. !command!
  160. *=======*
  161.  
  162. The wire can connect above any = character. If a wire is connected to
  163. the west side of a box, the > character must be used as follows:
  164.  
  165. *=======*
  166. -->!command!
  167. *=======*
  168.  
  169. At most one wire can be connected to each of a box's four faces.
  170.  
  171. === Wire syntax ===
  172.  
  173. Wires are made from the following characters:
  174.  
  175. |-+#
  176.  
  177. Every wire must use at least one of these characters. That is,
  178. > and v alone are not valid wires.
  179.  
  180. Each character is "open" on some of its sides. The | character is
  181. open on its north and south sides. The - character is open on its
  182. west and east sides. The + and # characters are both open on all
  183. four sides.
  184.  
  185. The = character on the south face of a box is open to its south,
  186. and the ! character on the east side of a box is open to its east.
  187. The v character is open to its north, and the > character is open
  188. to its west.
  189.  
  190. All wire characters within a module must obey the following rules of
  191. connectedness:
  192.  
  193. For each - character, its west and east neighbors must both
  194. be open on their east and west sides, respectively.
  195.  
  196. For each | character, its north and south neighbors must
  197. both be open on their south and north sides, respectively.
  198.  
  199. For each # character, its north, south, west, and east neighbors
  200. must each be open on their south, north, east, and west sides,
  201. respectively.
  202.  
  203. For each + character, exactly two of the following conditions must
  204. be met:
  205. a. its north neighbor is open on its south side
  206. b. its south neighbor is open on its north side
  207. c. its west neighbor is open on its east side
  208. d. its east neighbor is open on its west side
  209.  
  210. Only the | and - wire characters are allowed along module boundaries, and
  211. they only require a single open neighbor on the inside of the module.
  212. (They do not syntactically connect to anything on the outside.)
  213.  
  214. === Module syntax ===
  215.  
  216. The input consists of an arrangement of non-overlapping modules. Each
  217. module is bordered by the . character on its north and south face, the
  218. : character on its west and east face, and the , character in each
  219. corner. Additionally, the north face may optionally have one
  220. occurrence of the | character; this is the north input to the module.
  221. Similarly, the west input (if any) is represented by a - character.
  222. The east side of the module may have any number of occurrences of the
  223. - character; these are its outputs. A module's name must appear in the
  224. upper left corner of the module and be followed by a space.
  225.  
  226. == 2D Semantics ==
  227.  
  228. Evaluation of 2D programs revolves around a function for computing the
  229. value of a module instance. A module instance is a collection of
  230. wires, some of which have values, and the boxes that these wires
  231. connect.
  232.  
  233. A module instance evaluates in a series of evaluation steps. In each
  234. step, the "ready" boxes are identified as those boxes for which all of
  235. their inputs wires have values, and which have not yet executed in
  236. this instance. All ready boxes are evaluated (see below) in an
  237. arbitrary order. If no boxes are ready, then the module instance is
  238. finished. Its output is the value of the single output wire that has a
  239. value. If more than one wire has a value, or if no wire has a value,
  240. then evaluation fails.
  241.  
  242. === Box evaluation ===
  243.  
  244. Boxes only execute when all of their input wires have values. This is
  245. true even if the command does not reference all of the wires.
  246.  
  247. Commands are executed as follows. First, all expressions in the
  248. command are evaluated. The expressions N and W are replaced with the
  249. values on the North and West wires, respectively. If a value is needed
  250. but no wire is connected, then evaluation fails. Then, commands are
  251. executed as follows:
  252.  
  253.  
  254. send []
  255. nothing happens.
  256.  
  257. send [(val, outface)]
  258. val is sent along the specified outface.
  259.  
  260. send [(val1, outface1), (val2, outface2)]
  261. val1 is sent to outface1, and val2 is sent to outface2.
  262. The two outfaces may not be equal.
  263.  
  264. split (val1, val2)
  265. val1 is sent south, and val2 is sent east.
  266.  
  267. case Inl val of outface1, outface2
  268. val is sent to outface1.
  269.  
  270. case Inr val of outface1, outface2
  271. val is sent to outface2.
  272.  
  273. use mod
  274. a new instance of the module mod is evaluated. The inputs to
  275. the module must match the inputs to this box, and are instantiated
  276. with the values along those wires. The output along the east
  277. face is the output of the module instance.
  278.  
  279.  
  280. In any other situation (for example, split ()), the machine fails.
  281. If a value is sent along an outface, then there must be a wire
  282. connected, or the machine fails.
  283.  
  284.  
  285.  
  286. I've developed a prototype interpreter for 2D, which runs on Umix.
  287. Please try it out!
  288.  
  289. - Bill
  290.  
  291.  
  292. ---------------------------------------------
  293. Bill Ohmega "Hell is other programming
  294. ohmega@cbv.net languages." -- Sartran
  295. ---------------------------------------------
  296. % cat mult.spec
  297. From: ohmega@cbv.net
  298. Newsgroups: cult.cbv.discuss
  299. Message-ID: <82F68FA4.A4DE@cbv.net>
  300. Date: 19 Jul 19106 13:51:51
  301. X-Organization: Cult of the Bound Variable
  302. Subject: Is this thing on??
  303.  
  304. Hey,
  305.  
  306. I sent out an announcement for 2D almost a month ago. Is anybody in
  307. newsland using it? I've been using it in my undergraduate class
  308. "Introduction to Programming Languages" this semester with great
  309. success. The students love it!
  310.  
  311. Let me make a challenge: I've attached an implementation of addition
  312. for unary numbers (0 is represented as Inr (), 1 as Inl(Inr ()),
  313. 2 as Inl(Inl(Inr ())) and so on) in 2D. See if you can write a module
  314. called "mult" (you can even use my "plus" module) whose output is
  315. the product of its north and west inputs. The first one to send me
  316. a correct solution gets a package of gobstoppers!
  317.  
  318. - Bill
  319.  
  320. ---------------------------------------------
  321. Bill Ohmega "Hell is other programming
  322. ohmega@cbv.net languages." -- Sartran
  323. ---------------------------------------------
  324.  
  325.  
  326. ** Attachment 'plus.2d' converted of type text/2d **
  327. % cat plus.2d
  328.  
  329. ,..............................|....................................,
  330. :plus | :
  331. : *==================* | :
  332. -->!send [(W,S),(W,E)]!--------#--------------------+ :
  333. : *==================* | | :
  334. : | | | :
  335. : | v v :
  336. : | *==============* *============* :
  337. : | !case N of S, E!--->!send [(N,E)]!-------
  338. : | *==============* *============* :
  339. : | | :
  340. : | v :
  341. : | *========* *================* :
  342. : +------------------->!use plus!------>!send [(Inl W,E)]!---
  343. : *========* *================* :
  344. ,...................................................................,
  345.  
  346.  
  347. ,..............................................................,
  348. :main :
  349. : :
  350. : *================================================* :
  351. : !send [(Inl Inl Inl Inr (),E),(Inl Inl Inr (),S)]!--+ :
  352. : *================================================* | :
  353. : | v :
  354. : | *========* :
  355. : +--------------------------->!use plus!-----
  356. : *========* :
  357. ,..............................................................,
  358.  
  359. % cat reverse.spec
  360. From: <Sam Tertbokim> sam@ccc.edu
  361. Newsgroups: comp.lang.functional
  362. Message-ID: <16BC2AD0.55A0@edu.ccc>
  363. Date: 12 Jul 19106 14:11:21
  364. X-Organization: Undergraduate Department, Cult Community College
  365. Subject: Reversing a List, please help!
  366.  
  367.  
  368. Please help!
  369.  
  370. I need to write a program that reverses a list.
  371.  
  372. Can someone please send me the code that reverses a list.
  373.  
  374. I need the program to do this:
  375.  
  376. ---------------------------------------------------------------------
  377. Problem 1 [45pts].
  378. Create a module called "rev" that reverses the list of elements
  379. received along its North input. The list of elements E1, E2, E3, ...
  380. En is represented as follows:
  381.  
  382. Inl(E1, Inl(E2, Inl(E3, ... Inl(En, Inr ()) ...)))
  383.  
  384. The module should send along its east output:
  385.  
  386. Inl(En, ... Inl(E3, Inl(E2, Inl(E1, Inr ()))) ...)
  387. ---------------------------------------------------------------------
  388.  
  389.  
  390.  
  391. Please help I do not know how to do this and I need to do it by tomorrow.
  392.  
  393. HELP THANK YOU!!!!!
  394.  
  395. Sam
  396.  
  397.  
  398. PS what is a monad
  399.  
  400. % cat raytrace.spec
  401. From: ohmega@cbv.net
  402. Newsgroups: cult.cbv.discuss
  403. Message-ID: <67BC2AC4.F4C0@cbv.net>
  404. Date: 19 Jul 19106 17:21:07
  405. X-Organization: Cult of the Bound Variable
  406. Subject: Ray Tracing
  407.  
  408.  
  409. Dearest friends,
  410.  
  411. I have discovered the most amazing application of the Computing
  412. Device! It is a simulator that replicates the functions of the human
  413. eye. I call it a "Ray Tracer" because it operates along a ray.
  414.  
  415. == Ray Tracing ==
  416.  
  417. Suppose the eye is looking along a one-dimensional ray, and can see a
  418. series of n zero-dimensional surfaces. Each surface has four qualities:
  419.  
  420. * D, the direction it faces (either Towards or Away from the eye)
  421. * R, its reflectance
  422. * T, its translucence
  423. * E, its emission
  424.  
  425. We call these surfaces S1, S2, ... Sn, where S1 is closest to the eye.
  426. Adjacent to each surface are two ray segments; the ones pointing
  427. towards the eye are called Li and the ones facing away are called Ri.
  428. The rays adjacent to Sj that are closer to the eye are called L(j-1)
  429. and R(j-1); the two farther away are called Lj and Rj.
  430.  
  431. L0 L1 L2 Ln
  432. eye <=====> * <=====> * <=====> * ... * <=====> (empty space)
  433. R0 S1 R1 S2 R2 S3 Sn Rn
  434.  
  435. Each ray segment has an intensity value determined by a set of
  436. equations. Ray Tracing consists of determining the value of the ray
  437. L0, which is the intensity that the eye sees.
  438.  
  439. Intensity can take on three values: All, Medium, and None. These are
  440. also the values that the R, T, and E components of surfaces can take
  441. on. The following tables define the operations + and * on these
  442. values.
  443.  
  444. + | None Medium All
  445. ------------------------------
  446. None | None Medium All
  447. Medium | Medium All All
  448. All | All All All
  449.  
  450. * | None Medium All
  451. ------------------------------
  452. None | None None None
  453. Medium | None Medium Medium
  454. All | None Medium All
  455.  
  456.  
  457. The rays satisfy the following equations:
  458.  
  459. Ln = None
  460. R0 = None
  461.  
  462. For i such that 0 <= i < n:
  463. Li = if S(i + 1).D = Towards
  464. then (S(i + 1).R * Ri) +
  465. (S(i + 1).T * L(i + 1)) +
  466. S(i + 1).E
  467. else L(i + 1)
  468.  
  469. For i such that 0 < i <= n:
  470. Ri = if Si.D = Away
  471. then (Si.R * Li) +
  472. (Si.T * R(i - 1)) +
  473. Si.E
  474. else R(i - 1)
  475.  
  476. == Representation ==
  477.  
  478. The ray-tracer consists of a module "main" with a single input along
  479. the direction N, which is the sequence of surfaces. Its output is
  480. a single intensity value, the darkest correct value of L0.
  481.  
  482. Intensity values are represented as follows:
  483. None = Inl ()
  484. Medium = Inr (Inl ())
  485. All = Inr (Inr (Inl ()))
  486.  
  487. Surface orientations are represented as follows:
  488. Towards = Inl ()
  489. Away = Inr ()
  490.  
  491. A single surface with components D, R, T and E is represented
  492. as follows:
  493.  
  494. (D, (R, (T, E)))
  495.  
  496. The list of surfaces S1..Sn is represented as follows:
  497.  
  498. Inl(S1, Inl(S2, Inl(S3, ... Inl(Sn, Inr()) ... )))
  499.  
  500. ---------------------------------------------
  501. Bill Ohmega "Hell is other programming
  502. ohmega@cbv.net languages." -- Sartran
  503. ---------------------------------------------
  504. %
  505.  

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