summaryrefslogtreecommitdiff
path: root/ocaml/test/test_life_formula.ml
blob: 722154b074689714bfc54b5bd660d9290e4429ff (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
(* Test lifecycle formula on atom 0 *)

open Nock_lib

let () =
  Printf.printf "Testing lifecycle formula [2 [0 3] [0 2]] on atom 0\n\n%!";

  (* Build the lifecycle formula *)
  let lyf = Noun.cell (Noun.atom 2)
    (Noun.cell
      (Noun.cell (Noun.atom 0) (Noun.atom 3))
      (Noun.cell (Noun.atom 0) (Noun.atom 2))) in

  Printf.printf "Formula: [2 [0 3] [0 2]]\n%!";
  Printf.printf "Subject: 0 (null)\n\n%!";

  (* Try running it *)
  begin try
    let result = Nock.nock_on (Noun.atom 0) lyf in
    Printf.printf "✓ SUCCESS! Result: %s\n%!" (match result with
      | Noun.Atom z -> Z.to_string z
      | Noun.Cell _ -> "[cell]")
  with
  | Noun.Exit ->
      Printf.printf "✗ FAILED with Nock Exit\n%!";

      (* Let's trace through the formula step by step *)
      Printf.printf "\nStep-by-step trace:\n%!";
      Printf.printf "Formula: *[0 [2 [0 3] [0 2]]]\n%!";
      Printf.printf "Opcode 2: *[a [2 b c]] = *[*[a b] *[a c]]\n%!";
      Printf.printf "  b = [0 3]\n%!";
      Printf.printf "  c = [0 2]\n%!";
      Printf.printf "\n*[a b] = *[0 [0 3]] = slot 3 of atom 0\n%!";

      (* Try slot 3 on atom 0 *)
      begin try
        let s3 = Noun.slot (Z.of_int 3) (Noun.atom 0) in
        Printf.printf "  slot 3 of 0 = %s (unexpected!)\n%!" (match s3 with
          | Noun.Atom z -> Z.to_string z
          | Noun.Cell _ -> "[cell]")
      with Noun.Exit ->
        Printf.printf "  slot 3 of 0 = ERROR (as expected)\n%!"
      end;

      Printf.printf "\nThis proves the formula CANNOT work on atom 0!\n%!"
  | e ->
      Printf.printf "✗ FAILED with: %s\n%!" (Printexc.to_string e)
  end