summaryrefslogtreecommitdiff
path: root/ocaml/test/test_nock_iter.ml
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-07 02:38:28 +0700
committerpolwex <polwex@sortug.com>2025-10-07 02:38:28 +0700
commit64611d312280dd5d63d498ded09ae4e9a6eaf34c (patch)
tree790dff3049654d3f584b3a17ffb077957f1117ad /ocaml/test/test_nock_iter.ml
parentca8a67a583ad39bdb4cf36d635536e099af21bdf (diff)
refining nock with tail calls and sheet
Diffstat (limited to 'ocaml/test/test_nock_iter.ml')
-rw-r--r--ocaml/test/test_nock_iter.ml59
1 files changed, 59 insertions, 0 deletions
diff --git a/ocaml/test/test_nock_iter.ml b/ocaml/test/test_nock_iter.ml
new file mode 100644
index 0000000..05a03ce
--- /dev/null
+++ b/ocaml/test/test_nock_iter.ml
@@ -0,0 +1,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"
+ )