A more obvious order in this context.
[cellular-automata.git] / polymorphism / 001 / src / polymorphism.ml
CommitLineData
8c93b722
SK
1open Core.Std
2
3
4d49c95e 4module type MATRIX = sig
7c363dd8
SK
5 module Point : sig
6 type t = {r : int; k : int}
7 end
8
4d49c95e
SK
9 type 'a t
10
a1665c92 11 val create : rs:int -> ks:int -> 'a -> 'a t
4d49c95e 12
7c363dd8 13 val get_neighbors : 'a t -> Point.t -> 'a list
4d49c95e 14
65dbef41
SK
15 val map : 'a t -> f:('a -> 'b) -> 'b t
16
a1665c92 17 val mapi : 'a t -> f:(Point.t -> 'a -> 'b) -> 'b t
4d49c95e 18
a1665c92 19 val iter : 'a t -> f:(Point.t -> 'a -> unit) -> unit
0ce0e798
SK
20
21 val print : 'a t -> to_string:('a -> string) -> unit
4d49c95e
SK
22end
23
24module Matrix : MATRIX = struct
7c363dd8
SK
25 module Point = struct
26 type t = {r : int; k : int}
27
28 let (+) p p' =
29 { r = p.r + p'.r
30 ; k = p.k + p'.k
31 }
32 end
33
394125ca
SK
34 module Direction = struct
35 type t = NW | N | NE
36 | W | E
37 | SW | S | SE
38
39 let all = [ NW ; N ; NE
40 ; W ; E
41 ; SW ; S ; SE
42 ]
43
7c363dd8
SK
44 let to_offset =
45 let open Point in
46 function
47 | NW -> {r = -1; k = -1}
48 | N -> {r = -1; k = 0}
49 | NE -> {r = -1; k = 1}
50 | W -> {r = 0; k = -1}
51 | E -> {r = 0; k = 1}
52 | SW -> {r = 1; k = -1}
53 | S -> {r = 1; k = 0}
54 | SE -> {r = 1; k = 1}
394125ca
SK
55 end
56
4d49c95e
SK
57 type 'a t = 'a array array
58
a1665c92
SK
59 let create ~rs ~ks x =
60 Array.make_matrix ~dimx:rs ~dimy:ks x
4d49c95e
SK
61
62 let iter t ~f =
63 Array.iteri t ~f:(
63fe855d
SK
64 fun r ks ->
65 Array.iteri ks ~f:(
a1665c92
SK
66 fun k x ->
67 f {Point.r; Point.k} x
4d49c95e
SK
68 )
69 )
70
0ce0e798
SK
71 let print t ~to_string =
72 Array.iter t ~f:(
63fe855d
SK
73 fun r ->
74 Array.iter r ~f:(fun x -> printf "%s" (to_string x));
0ce0e798
SK
75 print_newline ()
76 )
77
4d49c95e 78 let map t ~f =
65dbef41
SK
79 Array.map t ~f:(Array.map ~f:(fun x -> f x))
80
81 let mapi t ~f =
4d49c95e 82 Array.mapi t ~f:(
63fe855d
SK
83 fun r ks ->
84 Array.mapi ks ~f:(
a1665c92
SK
85 fun k x ->
86 f {Point.r; Point.k} x
4d49c95e
SK
87 )
88 )
89
7c363dd8 90 let get t {Point.r; Point.k} =
63fe855d 91 t.(r).(k)
394125ca 92
7c363dd8 93 let is_within_bounds t {Point.r; Point.k} =
394125ca
SK
94 match t with
95 | [||] -> assert false
96 | t ->
97 r >= 0 && r < Array.length t &&
98 k >= 0 && k < Array.length t.(0)
99
7c363dd8 100 let neighborhood t point =
394125ca 101 List.map Direction.all ~f:Direction.to_offset
7c363dd8
SK
102 |> List.map ~f:(fun offset_point -> Point.(point + offset_point))
103 |> List.filter ~f:(is_within_bounds t)
394125ca 104
7c363dd8
SK
105 let get_neighbors t point =
106 List.map (neighborhood t point) ~f:(get t)
4d49c95e
SK
107end
108
109
a96702d3
SK
110module Msg = struct
111 type t = string
112end
113
114
115module State = struct
116 type t = string
117end
118
119
120module PhenoType = struct
121 type t = string
122end
123
da8f1674 124
a96702d3
SK
125module Cell = struct
126 type t = { msg : Msg.t
127 ; pheno : PhenoType.t
128 ; state : State.t
129 }
130end
0ce0e798 131
0ce0e798 132
a96702d3
SK
133module type RULE = sig
134 val create : unit -> Cell.t
da8f1674 135
a96702d3 136 val transition : state:State.t -> inputs:Msg.t list -> Cell.t
da8f1674
SK
137end
138
139
a96702d3
SK
140module Conway : RULE = struct
141 type state = D | A
0d6f7833 142
a96702d3
SK
143 let state_of_string : (string -> state) = function
144 | "D" -> D
145 | "A" -> A
146 | _ -> assert false
147
148 let state_of_int : (int -> state) = function
0ce0e798
SK
149 | 0 -> D
150 | 1 -> A
151 | _ -> assert false
152
a96702d3 153 let int_of_state : (state -> int) = function
0d6f7833
SK
154 | D -> 0
155 | A -> 1
156
a96702d3
SK
157 let string_of_state : (state -> string) = function
158 | D -> "D"
159 | A -> "A"
160
161 let msg_of_state : (state -> Msg.t) =
162 string_of_state
163
164 let pheno_of_state : (state -> PhenoType.t) = function
0ce0e798
SK
165 | D -> " "
166 | A -> "o"
167
a96702d3
SK
168 let int_of_msg msg =
169 msg |> state_of_string |> int_of_state
0ce0e798 170
a96702d3
SK
171 let next state ~live_neighbors =
172 match state with
0d6f7833
SK
173 | A when live_neighbors < 2 -> D
174 | A when live_neighbors < 4 -> A
175 | A when live_neighbors > 3 -> D
176 | D when live_neighbors = 3 -> A
b36c0aef
SK
177 | A -> A
178 | D -> D
a96702d3
SK
179
180 let cell_of_state s =
181 { Cell.msg = s |> msg_of_state
182 ; Cell.pheno = s |> pheno_of_state
183 ; Cell.state = s |> string_of_state
184 }
185
186 let create () =
187 Random.int 2 |> state_of_int |> cell_of_state
188
189 let live_neighbors inputs =
190 inputs |> List.map ~f:int_of_msg |> List.fold_left ~init:0 ~f:(+)
191
192 let transition ~state ~inputs =
193 state
194 |> state_of_string
195 |> next ~live_neighbors:(live_neighbors inputs)
196 |> cell_of_state
0d6f7833
SK
197end
198
199
d9fa5d46
SK
200module Automaton : sig
201 type t
aed335e3 202
a96702d3
SK
203 val create : rows:int
204 -> columns:int
205 -> interval:float
206 -> rules: (module RULE) list
207 -> t
aed335e3 208
d9fa5d46
SK
209 val loop : t -> unit
210end = struct
a96702d3
SK
211 type cell = { data : Cell.t
212 ; rule : (module RULE)
213 }
214
215 type t = { grid : cell Matrix.t
d9fa5d46
SK
216 ; interval : Time.Span.t
217 ; bar : string
218 }
aed335e3 219
a96702d3
SK
220 let create ~rows:rs ~columns:ks ~interval ~rules =
221 let n = List.length rules in
222 let i = Random.int n in
223 let init () =
224 let rule = List.nth_exn rules i in
225 let module Rule = (val rule : RULE) in
226 { rule
227 ; data = Rule.create ()
228 }
229 in
230 { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ())
d9fa5d46
SK
231 ; interval = Time.Span.of_float interval
232 ; bar = String.make ks '-'
233 }
234
a96702d3
SK
235 let cell_to_string cell =
236 cell.data.Cell.pheno
237
d9fa5d46
SK
238 let print t =
239 print_endline t.bar;
a96702d3 240 Matrix.print t.grid ~to_string:cell_to_string;
d9fa5d46
SK
241 print_endline t.bar
242
243 let next t =
244 let grid =
245 Matrix.mapi t.grid ~f:(
a96702d3
SK
246 fun point {rule; data} ->
247 let module Rule = (val rule : RULE) in
d9fa5d46 248 let neighbors = Matrix.get_neighbors t.grid point in
a96702d3
SK
249 let data =
250 Rule.transition
251 ~state:data.Cell.state
252 ~inputs:(List.map neighbors ~f:(fun cell -> cell.data.Cell.msg))
253 in
254 {rule; data}
d9fa5d46
SK
255 )
256 in
257 {t with grid}
258
259 let rec loop t =
260 print t;
261 Time.pause t.interval;
262 loop (next t)
263end
7707ff62
SK
264
265
266let main () =
267 Random.self_init ();
d9fa5d46 268 let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
a96702d3
SK
269 let interval = 0.1 in
270 let rules =
271 [ (module Conway : RULE)
272 ]
273 in
8526e3e1 274 Automaton.loop (Automaton.create ~rows:(rows - 3) ~columns ~interval ~rules)
8c93b722
SK
275
276
7d89c037
SK
277let spec =
278 let summary = "Polymorphic Cellular Automata" in
7707ff62 279 let spec = Command.Spec.empty in
7d89c037
SK
280 Command.basic ~summary spec main
281
282
283let () = Command.run spec
This page took 0.042543 seconds and 4 git commands to generate.