summaryrefslogtreecommitdiff
path: root/ocaml/test/test_poke_formulas.ml
blob: 54c08ff5d5812300c4ce0b5c17e0cdc2bb269ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
77
78
79
80
81
82
83
84
85
(* Test Different Poke Formulas
 *
 * Try various Nock formulas to figure out how to call Arvo
 *)

open Nock_lib

let test_formula name formula kernel event =
  Printf.printf "Testing: %s\n" name;
  Printf.printf "  Formula: %s\n" formula;

  let subject = Noun.cell event kernel in

  try
    let start = Unix.gettimeofday () in
    let result = Nock.nock_on subject
      (match name with
       | "Slot 3" -> Noun.cell (Noun.atom 0) (Noun.atom 3)
       | "Call slot 3 arm 2" ->
           Noun.cell (Noun.atom 9)
             (Noun.cell (Noun.atom 2)
               (Noun.cell (Noun.atom 0) (Noun.atom 3)))
       | "Compose [event kernel] then call" ->
           (* [7 formula-a formula-b]: compute a, use as subject for b *)
           Noun.cell (Noun.atom 7)
             (Noun.cell
               (Noun.cell (Noun.atom 0) (Noun.atom 2))  (* formula-a: get event *)
               (Noun.cell (Noun.atom 9)  (* formula-b: call arm 2 *)
                 (Noun.cell (Noun.atom 2)
                   (Noun.cell (Noun.atom 0) (Noun.atom 3)))))  (* of kernel *)
       | _ -> Noun.cell (Noun.atom 0) (Noun.atom 1)) in
    let elapsed = Unix.gettimeofday () -. start in

    Printf.printf "  โœ“ Success in %.4fs\n" elapsed;
    (match result with
     | Noun.Atom z ->
         if Z.numbits z < 20 then
           Printf.printf "  Result: Atom(%s)\n" (Z.to_string z)
         else
           Printf.printf "  Result: Atom(large: %d bits)\n" (Z.numbits z)
     | Noun.Cell _ -> Printf.printf "  Result: Cell(...)\n");
    Printf.printf "\n";
    true

  with e ->
    Printf.printf "  โœ— Failed: %s\n\n" (Printexc.to_string e);
    false

let test_poke_formulas env =
  Printf.printf "๐Ÿงช Testing Different Poke Formulas\n\n";

  Eio.Switch.run @@ fun _sw ->
  let fs = Eio.Stdenv.fs env in

  (* Load ivory pill *)
  let state = State.create () in
  match Boot.boot_from_file ~fs state "ivory.pill" with
  | Error msg ->
      Printf.printf "โœ— Failed to load pill: %s\n%!" msg
  | Ok () ->
      let kernel = State.get_arvo state in
      let event = Noun.cell (Noun.atom 0) (Noun.atom 42) in

      Printf.printf "Kernel loaded, trying different formulas...\n\n";

      let formulas = [
        ("Slot 3", "[0 3] - just return kernel");
        ("Call slot 3 arm 2", "[9 2 0 3] - call arm 2 of kernel");
        ("Compose [event kernel] then call", "[7 [0 2] [0 3] [9 2 0 1]]");
      ] in

      let _ = List.map (fun (name, desc) ->
        test_formula name desc kernel event
      ) formulas in

      ()

let () =
  Printf.printf "\n";
  Printf.printf "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n";
  Printf.printf " Testing Different Poke Formulas on Arvo\n";
  Printf.printf "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•\n";
  Printf.printf "\n";

  Eio_main.run test_poke_formulas