(* Time-stamp: <modified the 17/06/2019 (at 14:54) by Erwan Jahier> *)

(* This is algo 5.3 in the book *)

open Algo

let vars = ["d","int"]
let d=10

let (init_vars: 'v neighbor list -> 'v local_env) =
  fun _nl ->
    set empty_env "d" (Random.int d)

let (enable_f:'v neighbor list -> 'v local_env -> action list) =
  fun nl e ->
    if (get e "d") <> 0 then ["CD"] else []
  
let (step_f : 'v neighbor list -> 'v local_env -> action -> 'v local_env) =
  fun nl e ->
    function  | _ -> set e "d" 0


let (value_to_string: ('v -> string)) = string_of_int
let (value_to_data : ('v -> Data.v)) = fun i -> Data.I i
let (copy_value : ('v -> 'v)) = fun x -> x


let () =
  let algo_root = "root" in
  let algo_p = "p" in

  Algo.register
    ([(algo_root, vars, (Some init_vars), enable_f, step_f);
      (algo_p, P.vars, (Some P.init_vars), P.enable_f, P.step_f)
     ],
     value_to_string,
     value_to_data,
     copy_value) ;

  Algo.reg_actions   algo_root ["CD"];
  Algo.reg_actions   algo_p P.actions;


  ()