summaryrefslogtreecommitdiff
path: root/ocaml/lib/nock.ml
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 22:06:53 +0700
committerpolwex <polwex@sortug.com>2025-10-06 22:06:53 +0700
commit64b132efc5ad870677ac974334b30fdbc4afafd3 (patch)
tree6f3d07fd9ff08d0cfc854d0396fa03c2e3d2622b /ocaml/lib/nock.ml
parent6ce2c5919f36776fe8aea711b94bbd2d64c8207a (diff)
kinda getting there
Diffstat (limited to 'ocaml/lib/nock.ml')
-rw-r--r--ocaml/lib/nock.ml325
1 files changed, 145 insertions, 180 deletions
diff --git a/ocaml/lib/nock.ml b/ocaml/lib/nock.ml
index 28cecf9..97a1d71 100644
--- a/ocaml/lib/nock.ml
+++ b/ocaml/lib/nock.ml
@@ -19,9 +19,43 @@ open Noun
- 11: scry (errors in reference implementation)
*)
-(* Trace depth counter - only log first 20 levels *)
-let trace_depth = ref 0
-let max_trace_depth = 20
+(* Trace tracking - matches C logging style
+ - call_count: global counter for all nock calls
+ - depth: current recursion depth
+ - max_calls: how many calls to log (like C's limit)
+ - max_mug_depth: only compute mugs at shallow depths (expensive!)
+*)
+let call_count = ref 0
+let depth = ref 0
+let max_calls = 100
+let max_mug_depth = 3 (* Only log mugs for depth <= 3 to avoid performance hit *)
+
+(* Helper to generate indentation based on depth *)
+let indent () =
+ match !depth with
+ | 0 -> ""
+ | 1 -> " "
+ | 2 -> " "
+ | 3 -> " "
+ | 4 -> " "
+ | _ -> " "
+
+(* Helper to get opcode name/description *)
+let opcode_name op =
+ match op with
+ | 0 -> "0(slot)"
+ | 1 -> "1(const)"
+ | 2 -> "2(eval)"
+ | 3 -> "3(cell?)"
+ | 4 -> "4(inc)"
+ | 5 -> "5(eq)"
+ | 6 -> "6(if)"
+ | 7 -> "7(compose)"
+ | 8 -> "8(extend)"
+ | 9 -> "9(invoke)"
+ | 10 -> "10(edit)"
+ | 11 -> "11(hint)"
+ | n -> Printf.sprintf "%d(?)" n
(** Main nock evaluation function: nock(subject, formula)
@@ -30,267 +64,198 @@ let max_trace_depth = 20
This is a direct port of _n_nock_on from nock.c:157-396
*)
let rec nock_on bus fol =
- let should_trace = !trace_depth < max_trace_depth in
- if should_trace then incr trace_depth;
+ (* Capture current call number in local variable (like C's my_call)
+ This ensures ENTER and EXIT logs show the same call number even after recursion *)
+ let my_call = !call_count in
+ let should_log = my_call < max_calls in
+
+ (* Log entry - shows opcode, depth, subject type, and mug at shallow depths *)
+ if should_log then begin
+ let opcode_str = match fol with
+ | Cell (Atom op, _) when Z.fits_int op -> opcode_name (Z.to_int op)
+ | Cell (Cell _, _) -> "CELL(dist)"
+ | _ -> "?"
+ in
+ (* Only compute mugs at shallow depths to avoid performance penalty *)
+ if !depth <= max_mug_depth then begin
+ let bus_mug = mug bus in
+ Printf.eprintf "%s>>> ENTER call #%d depth=%d opcode=%s bus=%s[mug=0x%lx]\n%!"
+ (indent ()) my_call !depth opcode_str
+ (if is_cell bus then "cell" else "atom")
+ bus_mug
+ end else begin
+ Printf.eprintf "%s>>> ENTER call #%d depth=%d opcode=%s bus=%s\n%!"
+ (indent ()) my_call !depth opcode_str
+ (if is_cell bus then "cell" else "atom")
+ end;
+ incr call_count
+ end;
+
+ (* Increment depth for recursive calls *)
+ incr depth;
+
try
let result = match fol with
| Cell (hib, gal) when is_cell hib ->
- (* [a b] -> compute both sides and cons *)
- if should_trace then Printf.eprintf "[Nock:%d] Cell-cell formula\n%!" !trace_depth;
+ (* Distribution: [a b] -> compute both sides and cons *)
let poz = nock_on bus hib in
let riv = nock_on bus gal in
cell poz riv
| Cell (Atom op, gal) ->
(* Check if opcode fits in int *)
- if Z.compare op (Z.of_int max_int) > 0 then (
- if should_trace then Printf.eprintf "[Nock:%d] Opcode too large: %s\n%!" !trace_depth (Z.to_string op);
- raise Exit
- );
+ if Z.compare op (Z.of_int max_int) > 0 then raise Exit;
let opcode = Z.to_int op in
- if should_trace then Printf.eprintf "[Nock:%d] Opcode %d\n%!" !trace_depth opcode;
+
(match opcode with
| 0 ->
- (* /[axis subject] - slot/fragment lookup *)
- if not (is_atom gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op0: gal not atom\n%!" !trace_depth;
- raise Exit
- )
+ (* Nock 0: /[axis subject] - slot/fragment lookup *)
+ if not (is_atom gal) then raise Exit
else slot (match gal with Atom n -> n | _ -> raise Exit) bus
| 1 ->
- (* =[constant subject] - return constant *)
+ (* Nock 1: constant - return gal as-is *)
gal
| 2 ->
- (* *[subject formula new_subject] - evaluate with new subject *)
+ (* Nock 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
- );
+ if not (is_cell gal) then raise Exit;
let c_gal = tail 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 ->
- (* ?[subject formula] - is-cell test *)
+ (* Nock 3: ?[subject formula] - is-cell test *)
let gof = nock_on bus gal in
if is_cell gof then atom 0 else atom 1
| 4 ->
- (* +[subject formula] - increment *)
+ (* Nock 4: +[subject formula] - increment *)
let gof = nock_on bus gal in
inc gof
| 5 ->
- (* =[subject formula] - equality test *)
+ (* Nock 5: =[subject formula] - equality test *)
let wim = nock_on bus gal in
- if not (is_cell wim) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op5: wim not cell\n%!" !trace_depth;
- raise Exit
- );
+ if not (is_cell wim) then raise Exit;
let a = head wim in
let b = tail wim in
if equal a b then atom 0 else atom 1
| 6 ->
- (* if-then-else *)
- if not (is_cell gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op6: gal not cell\n%!" !trace_depth;
- raise Exit
- );
+ (* Nock 6: if-then-else *)
+ if not (is_cell gal) then raise Exit;
let b_gal = head gal in
let cd_gal = tail gal in
- if not (is_cell cd_gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op6: cd_gal not cell\n%!" !trace_depth;
- raise Exit
- );
+ if not (is_cell cd_gal) then raise Exit;
let c_gal = head cd_gal in
let d_gal = tail cd_gal in
-
let tys = nock_on bus b_gal in
let nex = match tys with
| Atom n when Z.equal n Z.zero -> c_gal
| Atom n when Z.equal n Z.one -> d_gal
- | _ ->
- if should_trace then Printf.eprintf "[Nock:%d] Op6: tys not 0 or 1\n%!" !trace_depth;
- raise Exit
+ | _ -> raise Exit
in
nock_on bus nex
| 7 ->
- (* composition: *[*[subject b] c] *)
- if not (is_cell gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op7: gal not cell\n%!" !trace_depth;
- raise Exit
- );
+ (* Nock 7: composition - *[*[subject b] c] *)
+ if not (is_cell gal) then raise Exit;
let b_gal = head gal in
let c_gal = tail gal in
let bod = nock_on bus b_gal in
nock_on bod c_gal
| 8 ->
- (* push: *[[*[subject b] subject] c] *)
- if not (is_cell gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op8: gal not cell\n%!" !trace_depth;
- raise Exit
- );
+ (* Nock 8: extend - *[[*[subject b] subject] c] *)
+ if not (is_cell gal) then raise Exit;
let b_gal = head gal in
let c_gal = tail gal in
- if should_trace then Printf.eprintf "[Nock:%d] Op8: computing b_gal...\n%!" !trace_depth;
let heb = nock_on bus b_gal in
- if should_trace then Printf.eprintf "[Nock:%d] Op8: creating new subject [heb bus]...\n%!" !trace_depth;
let bod = cell heb bus in
- if should_trace then Printf.eprintf "[Nock:%d] Op8: computing c_gal on new subject...\n%!" !trace_depth;
nock_on bod c_gal
| 9 ->
- (* call: *[*[subject c] axis] *)
- if not (is_cell gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op9: gal not cell\n%!" !trace_depth;
- raise Exit
- );
+ (* Nock 9: invoke - *[*[subject c] 2 [0 1] 0 axis] *)
+ if not (is_cell gal) then raise Exit;
let b_gal = head gal in
let c_gal = tail gal in
- if not (is_atom b_gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op9: b_gal not atom\n%!" !trace_depth;
- raise Exit
- );
-
+ if not (is_atom b_gal) then raise Exit;
let seb = nock_on bus c_gal in
let nex = slot (match b_gal with Atom n -> n | _ -> raise Exit) seb in
nock_on seb nex
| 10 ->
- (* hint - in reference implementation, hints are mostly ignored *)
- (* 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 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
- (* 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
+ (* Nock 10: edit/hint - replace at slot
+ Two forms:
+ - *[a 10 [b c] d]: edit mode, replace slot b in result of d with result of c
+ - *[a 10 b c]: hint mode, just evaluate c (ignore hint b)
+ *)
+ if not (is_cell gal) then raise Exit;
+ let _p_gal = head gal in (* Unused: hint tag/edit slot *)
+ let q_gal = tail gal in
+ (* For now, treat both forms as hints - just evaluate the formula *)
+ nock_on bus q_gal
| 11 ->
- (* scry - static scry *)
- if should_trace then Printf.eprintf "[Nock:%d] Op11: scry (gal is %s)\n%!" !trace_depth
- (if is_cell gal then "cell" else "atom");
- if not (is_cell gal) then (
- if should_trace then Printf.eprintf "[Nock:%d] Op11: gal not cell\n%!" !trace_depth;
- raise Exit
- );
- let ref_formula = head gal in
- let gof_formula = tail gal in
-
- if should_trace then Printf.eprintf "[Nock:%d] Op11: ref_formula is %s, gof_formula is %s\n%!" !trace_depth
- (if is_cell ref_formula then "cell" else "atom")
- (if is_cell gof_formula then "cell" else "atom");
-
- (* Check if ref_formula looks valid *)
- (match ref_formula with
- | Cell (Atom op, _) when Z.to_int op > 11 ->
- if should_trace then Printf.eprintf "[Nock:%d] Op11: WARNING ref_formula has invalid opcode %d\n%!" !trace_depth (Z.to_int op)
- | _ -> ());
-
- (* Evaluate both formulas *)
- if should_trace then Printf.eprintf "[Nock:%d] Op11: evaluating ref...\n%!" !trace_depth;
- let _ref = nock_on bus ref_formula in
- if should_trace then Printf.eprintf "[Nock:%d] Op11: evaluating gof...\n%!" !trace_depth;
- let _gof = nock_on bus gof_formula in
-
- (* For now, scry always fails (returns block)
- * In real Urbit, this would call into the scry handler
- * C Vere calls u3m_soft_esc which can fail
- * We'll return a crash for now *)
- if should_trace then Printf.eprintf "[Nock:%d] Op11: scry not supported, crashing\n%!" !trace_depth;
- raise Exit
-
- | n ->
+ (* Nock 11: hint (two forms)
+ - *[a 11 [b c] d] → *[[*[a c] *[a d]] 0 3] (dynamic hint)
+ - *[a 11 b c] → *[a c] (static hint)
+
+ The key insight: b is HINT DATA, not a formula!
+ Don't try to evaluate it as Nock code.
+ *)
+ if not (is_cell gal) then raise Exit;
+ let p_gal = head gal in (* b or [b c] - hint tag/data *)
+ let q_gal = tail gal in (* c or d - formula to evaluate *)
+
+ if is_cell p_gal then begin
+ (* Dynamic hint: *[a 11 [b c] d]
+ Spec: *[[*[a c] *[a d]] 0 3]
+ This evaluates both c and d, conses them, then returns slot 3 (= d's result).
+ Since we just want d's result, we can skip the hint evaluation. *)
+ nock_on bus q_gal
+ end else begin
+ (* Static hint: *[a 11 b c]
+ Spec: *[a c]
+ Just evaluate c, ignore the hint atom b. *)
+ nock_on bus q_gal
+ end
+
+ | _ ->
(* Invalid opcode *)
- if should_trace then Printf.eprintf "[Nock:%d] Invalid opcode: %d\n%!" !trace_depth n;
raise Exit
)
| _ ->
(* Invalid formula structure *)
- if should_trace then Printf.eprintf "[Nock:%d] Invalid formula (not [atom cell] or [cell cell])\n%!" !trace_depth;
raise Exit
in
- if should_trace then decr trace_depth;
+
+ (* Restore depth and log exit before returning *)
+ decr depth;
+ if should_log then begin
+ (* Only compute mugs at shallow depths to avoid performance penalty *)
+ if !depth <= max_mug_depth then begin
+ let result_mug = mug result in
+ Printf.eprintf "%s<<< EXIT call #%d depth=%d returns=%s[mug=0x%lx]\n%!"
+ (indent ()) my_call !depth
+ (if is_cell result then "cell" else "atom")
+ result_mug
+ end else begin
+ Printf.eprintf "%s<<< EXIT call #%d depth=%d returns=%s\n%!"
+ (indent ()) my_call !depth
+ (if is_cell result then "cell" else "atom")
+ end
+ end;
result
+
with e ->
- if should_trace then decr trace_depth;
+ (* Restore depth even on exception *)
+ decr depth;
raise e
(** Convenience function: nock(subject, formula) *)