summaryrefslogtreecommitdiff
path: root/ocaml
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 21:27:31 +0700
committerpolwex <polwex@sortug.com>2025-10-06 21:27:31 +0700
commit6ce2c5919f36776fe8aea711b94bbd2d64c8207a (patch)
treef79796c7ea188ed25a2bb0642bebe6dc0d05409c /ocaml
parentbcbd110b17b3c9bcb0e28e28fd33388f1c954a27 (diff)
going in circles man
Diffstat (limited to 'ocaml')
-rw-r--r--ocaml/lib/boot.ml101
-rw-r--r--ocaml/lib/nock.ml90
-rw-r--r--ocaml/lib/noun.ml29
-rw-r--r--ocaml/test/examine_ivory.ml2
4 files changed, 169 insertions, 53 deletions
diff --git a/ocaml/lib/boot.ml b/ocaml/lib/boot.ml
index 61bb202..f8289ec 100644
--- a/ocaml/lib/boot.ml
+++ b/ocaml/lib/boot.ml
@@ -192,39 +192,21 @@ let life eve =
| _ -> ())
| _ -> ());
- (* First, manually compute the two parts to see where it fails *)
+ (* Call lifecycle formula directly like C does *)
let gat =
try
Printf.printf "[Boot] calling Nock.nock_on(eve, [2 [0 3] [0 2]])...\n%!";
- (* Step 1: Compute *[eve [0 3]] = slot 3 of eve *)
- Printf.printf "[Boot] Step 1: Computing *[eve [0 3]] (slot 3 of subject)...\n%!";
- let slot3_result = Nock.nock_on eve (Noun.cell (Noun.atom 0) (Noun.atom 3)) in
- Printf.printf "[Boot] ✓ Slot 3 computed: %s\n%!"
- (if Noun.is_cell slot3_result then "cell" else "atom");
-
- (* Step 2: Compute *[eve [0 2]] = slot 2 of eve *)
- Printf.printf "[Boot] Step 2: Computing *[eve [0 2]] (slot 2 of subject)...\n%!";
- let slot2_result = Nock.nock_on eve (Noun.cell (Noun.atom 0) (Noun.atom 2)) in
- Printf.printf "[Boot] ✓ Slot 2 computed: %s\n%!"
- (if Noun.is_cell slot2_result then "cell" else "atom");
-
- (* Debug: check if slot2 looks like a valid Nock formula *)
- Printf.printf "[Boot] Checking slot2 structure (should be a formula):\n%!";
- (match slot2_result with
- | Noun.Cell (h, t) ->
- Printf.printf "[Boot] slot2 = [%s %s]\n%!"
- (if Noun.is_atom h then
- let z = match h with Noun.Atom z -> z | _ -> Z.zero in
- "atom(" ^ Z.to_string z ^ ")"
- else "cell")
- (if Noun.is_atom t then "atom" else "cell")
- | Noun.Atom z ->
- Printf.printf "[Boot] slot2 = atom(%s)\n%!" (Z.to_string z));
-
- (* Step 3: Compute *[slot3_result slot2_result] *)
- Printf.printf "[Boot] Step 3: Computing *[slot3 slot2] (nock slot-2 formula on slot-3 subject)...\n%!";
- let result = Nock.nock_on slot3_result slot2_result in
+ (* Build the lifecycle formula: [2 [0 3] [0 2]] *)
+ let lifecycle_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 result = Nock.nock_on eve lifecycle_formula in
Printf.printf "[Boot] ✓ Nock.nock_on returned successfully\n%!";
result
with e ->
@@ -559,17 +541,68 @@ let boot_lite ~fs state ivory_path =
Printf.printf "[Lite Boot] Cuing ivory pill...\n%!";
let pil = Serial.cue pill_bytes in
- (* Ivory pill is [%ivory core], extract the core *)
+ (* Ivory pill is [%ivory event_list] - run lifecycle like C does *)
if not (Noun.is_cell pil) then begin
Printf.printf "[Lite Boot] ✗ Pill is not a cell\n%!";
Error "Ivory pill must be a cell"
end else begin
- let _tag = Noun.head pil in
- let core = Noun.tail pil in
+ let tag = Noun.head pil in
+ let eve = Noun.tail pil in
+
+ (* Log tag info *)
+ Printf.printf "[Lite Boot] Tag is %s\n%!"
+ (if Noun.is_atom tag then "atom" else "cell");
+ Printf.printf "[Lite Boot] Event list is %s\n%!"
+ (if Noun.is_atom eve then "atom" else "cell (list)");
+
+ (* Run u3v_life: execute lifecycle formula on event list *)
+ Printf.printf "[Lite Boot] Running lifecycle formula on ivory events...\n%!";
+
+ (* Enable slot debugging *)
+ Noun.slot_debug := true;
+
+ (* Debug: Check pill structure *)
+ let rec lent n =
+ match n with
+ | Noun.Atom _ when Noun.equal n (Noun.atom 0) -> 0
+ | Noun.Cell (_, t) -> 1 + lent t
+ | _ -> 0
+ in
+ let pil_len = lent pil in
+ let eve_len = lent eve in
+ Printf.printf "[Lite Boot] DEBUG: pil has %d elements, eve has %d elements\n%!" pil_len eve_len;
+
+ (* Debug: Dump head(eve) structure in detail *)
+ let rec dump_noun depth max_depth n =
+ if depth >= max_depth then "..."
+ else match n with
+ | Noun.Atom z -> Z.to_string z
+ | Noun.Cell (h, t) ->
+ "[" ^ dump_noun (depth+1) max_depth h ^ " " ^
+ dump_noun (depth+1) max_depth t ^ "]"
+ in
+ let eve_head = Noun.head eve in
+ Printf.printf "[Lite Boot] DEBUG: head(eve) structure (depth 5):\n%s\n%!"
+ (dump_noun 0 5 eve_head);
+
+ (* Build the lifecycle formula: [2 [0 3] [0 2]] *)
+ let lifecycle_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
+
+ (* Execute the lifecycle formula to produce kernel *)
+ let gat = Nock.nock_on eve lifecycle_formula in
+
+ (* Extract slot 7 (the kernel) from resulting gate *)
+ let core = Noun.slot (Z.of_int 7) gat in
- Printf.printf "[Lite Boot] Extracted ivory core, setting as kernel\n%!";
+ Printf.printf "[Lite Boot] ✓ Lifecycle completed\n%!";
- (* Set the core directly as the kernel - no lifecycle formula needed *)
+ (* Set the resulting core as the kernel *)
State.boot state core;
Printf.printf "[Lite Boot] ✓ Ivory kernel booted!\n\n%!";
diff --git a/ocaml/lib/nock.ml b/ocaml/lib/nock.ml
index 670b12b..28cecf9 100644
--- a/ocaml/lib/nock.ml
+++ b/ocaml/lib/nock.ml
@@ -64,14 +64,65 @@ let rec nock_on bus fol =
| 2 ->
(* *[subject formula new_subject] - evaluate with new subject *)
+ (* C evaluates tail first, then head - must match this order! *)
if not (is_cell gal) then (
if should_trace then Printf.eprintf "[Nock:%d] Op2: gal not cell\n%!" !trace_depth;
raise Exit
);
- let b_gal = head gal in
let c_gal = tail gal in
- let seb = nock_on bus b_gal in
- let nex = nock_on bus c_gal in
+ let b_gal = head gal in
+
+ (* Debug: log what formulas we're evaluating *)
+ if should_trace && !trace_depth <= 2 then begin
+ Printf.eprintf "[Nock:%d-DEBUG] Op2 about to evaluate:\n%!" !trace_depth;
+ Printf.eprintf " b_gal (for subject): %s\n%!"
+ (if is_cell b_gal then
+ let h = head b_gal in
+ if is_atom h then
+ (match h with Atom n -> "[" ^ Z.to_string n ^ " ...]" | _ -> "[?]")
+ else "[cell ...]"
+ else "not-cell");
+ Printf.eprintf " c_gal (for formula): %s\n%!"
+ (if is_cell c_gal then
+ let h = head c_gal in
+ if is_atom h then
+ (match h with Atom n -> "[" ^ Z.to_string n ^ " ...]" | _ -> "[?]")
+ else "[cell ...]"
+ else "not-cell")
+ end;
+
+ let nex = nock_on bus c_gal in (* Tail first like C *)
+
+ if should_trace && !trace_depth <= 2 then begin
+ Printf.eprintf "[Nock:%d-DEBUG] Op2 computed formula:\n%!" !trace_depth;
+ Printf.eprintf " nex opcode: %s\n%!"
+ (if is_cell nex then
+ let h = head nex in
+ if is_atom h then
+ (match h with Atom n -> Z.to_string n | _ -> "?")
+ else "cell"
+ else "atom")
+ end;
+
+ let seb = nock_on bus b_gal in (* Head second like C *)
+
+ if should_trace && !trace_depth <= 2 then begin
+ Printf.eprintf "[Nock:%d-DEBUG] Op2 FINAL CHECK:\n%!" !trace_depth;
+ Printf.eprintf " seb (subject) from b_gal=[0 %s]\n%!"
+ (match b_gal with
+ | Cell (Atom _, Atom n) -> Z.to_string n
+ | _ -> "?");
+ Printf.eprintf " nex (formula) from c_gal=[0 %s], nex opcode = %s\n%!"
+ (match c_gal with
+ | Cell (Atom _, Atom n) -> Z.to_string n
+ | _ -> "?")
+ (if is_cell nex then
+ let h = head nex in
+ if is_atom h then (match h with Atom n -> Z.to_string n | _ -> "?")
+ else "cell"
+ else "NOT-A-CELL!")
+ end;
+
nock_on seb nex
| 3 ->
@@ -165,21 +216,28 @@ let rec nock_on bus fol =
| 10 ->
(* hint - in reference implementation, hints are mostly ignored *)
- if should_trace then Printf.eprintf "[Nock:%d] Op10: hint (gal is %s)\n%!" !trace_depth
- (if is_cell gal then "cell" else "atom");
+ (* Two forms: [10 [[b c] d]] and [10 [b c]] *)
+ if not (is_cell gal) then (
+ if should_trace then Printf.eprintf "[Nock:%d] Op10: gal not cell\n%!" !trace_depth;
+ raise Exit
+ );
+ let p_gal = head gal in (* First part: hint tag or [tag value] *)
+ let q_gal = tail gal in (* Second part: formula *)
+
+ (* Determine which form we have *)
let nex =
- if is_cell gal then begin
- (* [[hint-tag hint-value] formula] *)
- let hint_part = head gal in
- let formula = tail gal in
- if should_trace then Printf.eprintf "[Nock:%d] Op10: hint_part is %s, formula is %s\n%!" !trace_depth
- (if is_cell hint_part then "cell" else "atom")
- (if is_cell formula then "cell" else "atom");
- formula
+ if is_cell p_gal then begin
+ (* Form 1: [10 [[b c] d]] - full hint with value *)
+ (* p_gal = [b c], q_gal = d (formula) *)
+ if should_trace then Printf.eprintf "[Nock:%d] Op10: full hint, formula is %s\n%!" !trace_depth
+ (if is_cell q_gal then "cell" else "atom");
+ q_gal (* Execute the formula, ignore hint *)
end else begin
- (* [hint-tag formula] where hint-value is implicit *)
- if should_trace then Printf.eprintf "[Nock:%d] Op10: implicit hint\n%!" !trace_depth;
- gal
+ (* Form 2: [10 [b c]] - hint with implicit value *)
+ (* p_gal = b (hint tag), q_gal = c (formula) *)
+ if should_trace then Printf.eprintf "[Nock:%d] Op10: simple hint, formula is %s\n%!" !trace_depth
+ (if is_cell q_gal then "cell" else "atom");
+ q_gal (* Execute the formula, ignore hint *)
end
in
nock_on bus nex
diff --git a/ocaml/lib/noun.ml b/ocaml/lib/noun.ml
index c59ec80..9be65b7 100644
--- a/ocaml/lib/noun.ml
+++ b/ocaml/lib/noun.ml
@@ -40,16 +40,39 @@ let tail = function
- 2 is head, 3 is tail
- For n > 1: if even, go left; if odd, go right
*)
+let slot_debug = ref false
+
let rec slot n noun =
- if Z.equal n Z.one then
+ let debug = !slot_debug && (Z.equal n (Z.of_int 2) || Z.equal n (Z.of_int 3)) in
+ if debug then
+ Printf.eprintf "[SLOT-DEBUG] slot(%s, noun)\n%!" (Z.to_string n);
+
+ if Z.equal n Z.one then begin
+ if debug then Printf.eprintf "[SLOT-DEBUG] slot(%s) = identity\n%!" (Z.to_string n);
noun
+ end
else if Z.equal n Z.zero then
raise Exit
- else
+ else begin
let bit = Z.testbit n 0 in (* Check if odd *)
let parent = Z.shift_right n 1 in
+ if debug then
+ Printf.eprintf "[SLOT-DEBUG] slot(%s): bit=%b parent=%s\n%!"
+ (Z.to_string n) bit (Z.to_string parent);
let sub = slot parent noun in
- if bit then tail sub else head sub
+ let result = if bit then tail sub else head sub in
+ if debug then begin
+ Printf.eprintf "[SLOT-DEBUG] slot(%s): taking %s, result is %s\n%!"
+ (Z.to_string n) (if bit then "tail" else "head")
+ (if is_cell result then
+ let h = head result in
+ if is_atom h then
+ (match h with Atom z -> "cell[" ^ Z.to_string z ^ " ...]" | _ -> "cell[? ...]")
+ else "cell[cell ...]"
+ else "atom")
+ end;
+ result
+ end
(** Equality test for nouns *)
let rec equal a b =
diff --git a/ocaml/test/examine_ivory.ml b/ocaml/test/examine_ivory.ml
index 34b5fed..490f735 100644
--- a/ocaml/test/examine_ivory.ml
+++ b/ocaml/test/examine_ivory.ml
@@ -68,6 +68,8 @@ let main env =
Printf.printf " Slot 2 starts with opcode 2 - could be lifecycle formula!\n";
Printf.printf "\n ✓ Tail appears to BE the Arvo core itself!\n";
Printf.printf " This means the lifecycle formula operates on the CORE, not null!\n"
+ | Noun.Cell (Noun.Atom op, _rest) ->
+ Printf.printf " Slot 2 starts with opcode: %s\n" (Z.to_string op)
| _ ->
Printf.printf " Slot 2 doesn't match expected lifecycle formula pattern\n"
with _ ->