a one dimensional cellular automata, using comonads
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # cellularAutomata
  2. !!! WARNING !!!
  3. this will probably leak memory until i write a clamp function
  4. also this readme is out of date
  5. !!! WARNING !!!
  6. a small application for running a one-dimensional cellular automata from random inputs, using comonads
  7. now also supports 2d automata, check out [here](conwayExample.txt) for an example of the current output of the program
  8. ## usage
  9. the program will default to the size of the window
  10. `-w` and `-g` inputs can be given to determine the width and height, respectively
  11. ## requirements
  12. * `getOpt`
  13. * `ncurses` (for detecting term width/height)
  14. ## building / running
  15. builds using nix
  16. from a local folder:
  17. ```
  18. nix build .
  19. ./result/bin/cellularAutomata
  20. ```
  21. from the repo directly:
  22. ```
  23. nix run github:techieAgnostic/cellularAutomata -- -w 40 -g 25
  24. ```
  25. it may also be included as a flake input, as one normally would, and added to the package list using the included overlay
  26. ## discussion
  27. this program was an exercise in using comonads to simulate cellular automata.
  28. ### cellular automata
  29. a cellular automaton is defined by a calculation on a local space, which is applied to a global space of cells.
  30. in a one dimensional cellular automata, each cell has two immediate neighbors, to the left and right of it.
  31. additionally, each cell may be in one of two state - Alive, or Dead.
  32. this program has a hard-coded (for now) xor-based rule that works as follows:
  33. regardless of the state of the current cell, it will become Alive if one of its neighbors is alive, but not both, otherwise it will Die.
  34. this local behaviour, when viewed on a global scale, can provide emergent behaviour, in this case a nice triangular pattern (when viewed over time).
  35. ### comonads
  36. a comonad is the dual to a monad.
  37. it is defined by two operations, `extract` and `duplicate`.
  38. `extract` takes a comonad (which wraps some base type) and returns a single item of that type, it can be seen in the semantics of a cellular automata space as `extract`ing the focussed cell from the global space.
  39. `duplicate` takes a comonad space, and wraps it in an extra layer of comonad, essentially creating a comonad of comonads. in semantics of a cellular automata, we can `duplicate` a space into that same space, but focussed on every possible cell in the space.
  40. finally, a comonad is a functor, meaning it can be mapped over. we can use this to our advantage in the semantics of a cellular automata, by creating a function from a comonad space, to a cell state.
  41. this function will be our transition function, and determines the new state of the focussed cell, using the non-focussed parts as its inputs.
  42. by `duplicate`ing the comonad space, and mapping the transition function over this new universe of spaces, we get back a single universe, which represents the next step in time.
  43. ### laziness
  44. due to haskell's laziness, the comonad space can (and should) be infinite in both directions, and as long as we remember to bound the space we want to print, our program will work properly.
  45. as such, the functions to change our focus within the space assumes an infinite list, and i have defaulted to "clamping" our focus at the edges if a finite list is given on either direction. im not sure this is semantically the correct choice, but in regular usage (with infinite lists) it should not come up.
  46. ## example
  47. `./cellularAutomata -w 40 -g 25`
  48. ```
  49. ██ ████ █ █████ █ ███ █ ███ ███
  50. ██ █ ███ █ ██ █ ███ ██ █ █ █ █ ██
  51. █ ███ ███ █ ███ █ █ ██ ██ ███
  52. ███ █ █ █ █ ██ █ ███████ ██ ██
  53. ███ █ █ █ ██ █ ██ ██ ███ ██
  54. █ █ █ ██████ █████ ██████ █ █
  55. ██ ██ █ █ █ ██ █ █ ██ ██ █
  56. ██ ██ ██ ███ █ █ ███ ███ █ █ █
  57. █ ███████ ██ ███ █ █ █ █ █ ███ █ ██
  58. █ █ █████ █ █ ██ █ ██ █ █
  59. ███ █ ██ █ █ ████ █ ██ ██
  60. █ █ █ ████ █ █ █ ███ ██ █ █████████
  61. ██ █ █ ██ █ ███████ █ ██
  62. █████ ██ █ █████ █ ███ █ ███
  63. ██ █ ██ ███ ███ █ ██ █ █ ██
  64. ████ █ █████ ██ ██ █ █ ███ ██ █ ████
  65. █ ███ █ ██ ██ ██ █ █████ █ ██
  66. █ █ ███ ██ █ ██ ████████ █ ██ █ ███
  67. █ █ █ ██ ████ █ ███ █ ███ █ █
  68. ██ █ █████ █ █ ██ █ █ ██ █
  69. ██ █ ██ ███ ██ █ ███ █ █ ██ █ █
  70. ████ █████ ██ █ ██ ███ ███ ██████ ███
  71. █ █ █ ██ █████ █ █ ██ ██ █ █
  72. █ █ █ ███ ██ █ ██ ███ █ █
  73. ██ █ █ ███ █ ███ █ █ ███ █ ███ █ █
  74. █ █ ██ █ █ █ █ ██ █ █ █ ██
  75. ```
  76. example using `rule3` and a non-random starting position:
  77. `./cellularAutomata -w 80 -g 80`
  78. ```
  79. ███
  80. █ █ █
  81. ██ █ ██
  82. █ █ █
  83. ███ ███ ███
  84. █ █ █ █ █
  85. ██ ██ ███ ██ ██
  86. █ █ █
  87. ███ ███ ███
  88. █ █ █ █ █ █ █ █ █
  89. ██ █ ██ ██ █ ██ ██ █ ██
  90. █ █ █ █ █
  91. ███ ███ ███ ███ ███
  92. █ █ █ █ █ █ █ █ █ █ █
  93. ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██
  94. █ █ █
  95. ███ ███ ███
  96. █ █ █ █ █ █ █ █ █
  97. ██ █ ██ ██ █ ██ ██ █ ██
  98. █ █ █ █ █ █ █ █ █
  99. ███ ███ ███ ███ ███ ███ ███ ███ ███
  100. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  101. ██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
  102. █ █ █ █ █
  103. ███ ███ ███ ███ ███
  104. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  105. ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██
  106. █ █ █ █ █ █ █ █ █ █ █
  107. ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
  108. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  109. ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
  110. █ █ █
  111. ███ ███ ███
  112. █ █ █ █ █ █ █ █ █
  113. ██ █ ██ ██ █ ██ ██ █ ██
  114. █ █ █ █ █ █ █ █ █
  115. ███ ███ ███ ███ ███ ███ ███ ███ ███
  116. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  117. ██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
  118. █ █ █ █ █ █ █ █
  119. █ ███ ███ ███ ███ ███ ███ ███ ██
  120. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  121. ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █
  122. █ █ █ █ █ █ █ █ █ █ █ █
  123. █ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ██
  124. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  125. ██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ █
  126. █ █ █
  127. ███ ███ ███
  128. █ █ █ █ █ █ █ █ █
  129. ██ █ ██ ██ █ ██ ██ █ ██
  130. █ █ █ █ █ █ █ █ █
  131. ███ ███ ███ ███ ███ ███ ███ ███ ███
  132. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  133. ██ ██ ███ ██ ██ ██ ██ ███ ██ ██ ██ ██ ███ ██ ██
  134. █ █ █ █ █ █ █
  135. ███ ███ ███ ███ ███ ███ ███
  136. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  137. ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██ ██ █ ██
  138. █ █ █ █ █ █ █ █ █ █ █ █ █
  139. ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
  140. █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
  141. ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
  142. ███
  143. █ █ █
  144. ██ █ ██
  145. █ █ █
  146. ███ ███ ███
  147. █ █ █ █ █
  148. ██ ██ ███ ██ ██
  149. █ █ █
  150. ███ ███ ███
  151. █ █ █ █ █ █ █ █ █
  152. ██ █ ██ ██ █ ██ ██ █ ██
  153. █ █ █ █ █
  154. ███ ███ ███ ███ ███
  155. █ █ █ █ █ █ █ █ █ █ █
  156. ██ ██ ██ ██ ██ █ ██ ██ ██ ██ ██
  157. █ █ █
  158. ```