summaryrefslogtreecommitdiff
path: root/ocaml/test/test_nock_iter.ml
blob: 05a03ce6495e6b76769e04a64af52d565f926090 (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
(** Test the fully iterative Nock interpreter - no stack overflow! *)

open Nock_lib

let () =
  Printf.printf "Testing iterative Nock interpreter (no stack limit needed)...\n\n";

  (* Test 1: Simple increment *)
  Printf.printf "[1] Testing simple increment: *[42 [4 0 1]]\n";
  let result = Nock_iter.nock_on (Noun.atom 42) (Noun.cell (Noun.atom 4) (Noun.cell (Noun.atom 0) (Noun.atom 1))) in
  Printf.printf "    Result: %s\n" (Z.to_string (match result with Noun.Atom { z; _ } -> z | _ -> Z.zero));
  Printf.printf "    Expected: 43\n\n";

  (* Test 2: Distribution *)
  Printf.printf "[2] Testing distribution: *[42 [[4 0 1] [4 0 1]]]\n";
  let inc_fol = Noun.cell (Noun.atom 4) (Noun.cell (Noun.atom 0) (Noun.atom 1)) in
  let dist = Noun.cell inc_fol inc_fol in
  let result = Nock_iter.nock_on (Noun.atom 42) dist in
  Printf.printf "    Result: [%s %s]\n"
    (Z.to_string (match Noun.head result with Noun.Atom { z; _ } -> z | _ -> Z.zero))
    (Z.to_string (match Noun.tail result with Noun.Atom { z; _ } -> z | _ -> Z.zero));
  Printf.printf "    Expected: [43 43]\n\n";

  (* Test 3: Small pill to verify no recursion issues *)
  Printf.printf "[3] Loading ivory pill (this would stack overflow with recursive version)...\n";
  Eio_main.run (fun env ->
    let fs = Eio.Stdenv.fs env in
    let bytes = Eio.Path.(load (fs / "ivory.pill")) |> Bytes.of_string in
    Printf.printf "    Pill size: %d bytes\n" (Bytes.length bytes);

    let pill = Serial.cue bytes in
    Printf.printf "    Cued successfully\n";

    match pill with
    | Noun.Cell { h = _tag; t = core; _ } ->
        Printf.printf "    Structure: [tag core]\n";
        Printf.printf "    Core mug: 0x%08lx\n" (Noun.mug core);

        Printf.printf "\n    Running lifecycle formula with ITERATIVE interpreter...\n";
        Printf.printf "    (No OCAMLRUNPARAM needed!)\n";

        let formula = Noun.cell
          (Noun.atom 2)
          (Noun.cell
            (Noun.cell (Noun.atom 0) (Noun.atom 3))
            (Noun.cell (Noun.atom 0) (Noun.atom 2))) in

        let start = Unix.gettimeofday () in
        let kernel = Nock_iter.nock_on core formula in
        let elapsed = Unix.gettimeofday () -. start in

        Printf.printf "    ✓ SUCCESS in %.2fs\n" elapsed;
        Printf.printf "    Kernel mug: 0x%08lx\n" (Noun.mug kernel);
        Printf.printf "\n✅ Iterative Nock interpreter works perfectly!\n";
        Printf.printf "   No stack overflow, no OCAMLRUNPARAM needed.\n";

    | _ ->
        Printf.printf "    Unexpected pill structure\n"
  )