9 val string_with_color : string -> color -> string
11 val clear : unit -> unit
13 val reset : unit -> unit
19 let ansi_code_clear = "\027[2J" (* Clear screen *)
20 let ansi_code_reset = "\027[1;1H" (* Reset cursor position *)
22 let string_of_color = function
23 | `green -> "\027[0;32m"
24 | `red -> "\027[1;31m"
26 let string_with_color s c =
27 sprintf "%s%s\027[0m" (string_of_color c) s
30 print_string ansi_code_clear
33 print_string ansi_code_reset
37 module type MATRIX = sig
39 type t = {r : int; k : int}
44 val create : rs:int -> ks:int -> 'a -> 'a t
46 val get_neighbors : 'a t -> Point.t -> 'a list
48 val map : 'a t -> f:('a -> 'b) -> 'b t
50 val mapi : 'a t -> f:(Point.t -> 'a -> 'b) -> 'b t
52 val iter : 'a t -> f:(Point.t -> 'a -> unit) -> unit
54 val print : 'a t -> to_string:('a -> string) -> unit
57 module Matrix : MATRIX = struct
59 type t = {r : int; k : int}
67 module Direction = struct
72 let all = [ NW ; N ; NE
80 | NW -> {r = -1; k = -1}
81 | N -> {r = -1; k = 0}
82 | NE -> {r = -1; k = 1}
83 | W -> {r = 0; k = -1}
85 | SW -> {r = 1; k = -1}
87 | SE -> {r = 1; k = 1}
90 type 'a t = 'a array array
92 let create ~rs ~ks x =
93 Array.make_matrix ~dimx:rs ~dimy:ks x
100 f {Point.r; Point.k} x
104 let print t ~to_string =
107 Array.iter r ~f:(fun x -> printf "%s" (to_string x));
112 Array.map t ~f:(Array.map ~f:(fun x -> f x))
119 f {Point.r; Point.k} x
123 let get t {Point.r; Point.k} =
126 let is_within_bounds t {Point.r; Point.k} =
128 | [||] -> assert false
130 r >= 0 && r < Array.length t &&
131 k >= 0 && k < Array.length t.(0)
133 let neighborhood t point =
134 List.map Direction.all ~f:Direction.to_offset
135 |> List.map ~f:(fun offset_point -> Point.(point + offset_point))
136 |> List.filter ~f:(is_within_bounds t)
138 let get_neighbors t point =
139 List.map (neighborhood t point) ~f:(get t)
148 module State = struct
153 module PhenoType = struct
159 type t = { msg : Msg.t
160 ; pheno : PhenoType.t
166 module type RULE = sig
167 val create : unit -> Cell.t
169 val transition : state:State.t -> inputs:Msg.t list -> Cell.t
173 module Conway : RULE = struct
176 let state_of_string : (string -> state) = function
181 let state_of_int : (int -> state) = function
186 let int_of_state : (state -> int) = function
190 let string_of_state : (state -> string) = function
194 let msg_of_state : (state -> Msg.t) =
197 let pheno_of_state : (state -> PhenoType.t) = function
202 msg |> state_of_string |> int_of_state
204 let next state ~live_neighbors =
206 | A when live_neighbors < 2 -> D
207 | A when live_neighbors < 4 -> A
208 | A when live_neighbors > 3 -> D
209 | D when live_neighbors = 3 -> A
213 let cell_of_state s =
214 { Cell.msg = s |> msg_of_state
215 ; Cell.pheno = s |> pheno_of_state
216 ; Cell.state = s |> string_of_state
220 Random.int 2 |> state_of_int |> cell_of_state
222 let live_neighbors inputs =
223 inputs |> List.map ~f:int_of_msg |> List.fold_left ~init:0 ~f:(+)
225 let transition ~state ~inputs =
228 |> next ~live_neighbors:(live_neighbors inputs)
233 module Automaton : sig
236 val create : rows:int
239 -> rules: (module RULE) list
244 type cell = { data : Cell.t
245 ; rule : (module RULE)
248 type t = { grid : cell Matrix.t
249 ; interval : Time.Span.t
253 let create ~rows:rs ~columns:ks ~interval ~rules =
254 let n = List.length rules in
255 let i = Random.int n in
257 let rule = List.nth_exn rules i in
258 let module Rule = (val rule : RULE) in
260 ; data = Rule.create ()
264 { grid = Matrix.map ~f:init (Matrix.create ~rs ~ks ())
265 ; interval = Time.Span.of_float interval
266 ; bar = String.make ks '-'
269 let cell_to_string cell =
275 Matrix.print t.grid ~to_string:cell_to_string;
280 Matrix.mapi t.grid ~f:(
281 fun point {rule; data} ->
282 let module Rule = (val rule : RULE) in
283 let neighbors = Matrix.get_neighbors t.grid point in
286 ~state:data.Cell.state
287 ~inputs:(List.map neighbors ~f:(fun cell -> cell.data.Cell.msg))
296 Time.pause t.interval;
303 let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
304 let interval = 0.1 in
306 [ (module Conway : RULE)
309 Automaton.loop (Automaton.create ~rows:(rows - 3) ~columns ~interval ~rules)
313 let summary = "Polymorphic Cellular Automata" in
314 let spec = Command.Spec.empty in
315 Command.basic ~summary spec main
318 let () = Command.run spec