summaryrefslogtreecommitdiff
path: root/ocaml/lib/state.ml
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-10-06 05:34:08 +0700
committerpolwex <polwex@sortug.com>2025-10-06 05:34:08 +0700
commitc3545b7ba9e8448226417fab6edaa2d039c9babe (patch)
tree798fd2bf87a67ac91e31b2fb7f0cd6590064b2c1 /ocaml/lib/state.ml
parent9fd3f41bf9a3326c5f0866f39f2ed151adc21565 (diff)
wait what? runtime working?HEADmaster
Diffstat (limited to 'ocaml/lib/state.ml')
-rw-r--r--ocaml/lib/state.ml127
1 files changed, 104 insertions, 23 deletions
diff --git a/ocaml/lib/state.ml b/ocaml/lib/state.ml
index f1acefe..6fdf725 100644
--- a/ocaml/lib/state.ml
+++ b/ocaml/lib/state.ml
@@ -73,38 +73,119 @@ let boot state kernel_noun =
Hashtbl.clear state.yot;
Mutex.unlock state.lock
-(* Poke: apply an event to the kernel
+(* Poke Formula - Gate call formula
+ *
+ * This is the Nock formula to call the Arvo kernel gate with an event.
*
- * In real Arvo:
- * - Runs Nock with the poke formula
- * - Updates kernel state
- * - Increments event number
- * - Returns effects
+ * Formula: [9 2 [0 3] [0 2]]
+ * - Opcode 9: Call gate at slot 2
+ * - Argument construction from slots 2 and 3
*
- * For now: simplified version that just stores the event
+ * Subject structure: [event kernel]
+ * - Slot 2 = event (the ovum)
+ * - Slot 3 = kernel (Arvo core)
+ *
+ * For simplicity, we'll use formula 7 composition for now:
+ * [7 [event kernel] kernel] - simplified, just returns kernel
*)
-let poke state _event_noun =
- Mutex.lock state.lock;
- (* In a real implementation, this would run Nock:
- * let effects = Nock.nock_on state.roc poke_formula in
- * state.roc <- new_kernel_state;
- *
- * For now, we just update event count
+let poke_formula =
+ (* Simplified formula: [0 3] - just return the kernel for now
+ * TODO: Use real gate call formula: [9 2 [0 3] [0 2]]
*)
- state.eve <- Int64.succ state.eve;
- Mutex.unlock state.lock;
+ Noun.cell
+ (Noun.atom 0) (* Opcode 0: slot *)
+ (Noun.atom 3) (* Slot 3: the kernel *)
+
+(* Parse poke result
+ *
+ * Arvo poke result structure: [effects new-kernel]
+ * Or sometimes: [[moves new-kernel] effects]
+ *
+ * For now, simplified: assume result is the new kernel
+ *)
+let parse_poke_result result =
+ (* TODO: Parse real Arvo result structure
+ * For now: treat whole result as new kernel *)
+ let new_kernel = result in
+ let effects = [] in (* No effects parsed yet *)
+ (new_kernel, effects)
+
+(* Poke: apply an event to the kernel
+ *
+ * Real Arvo poke sequence:
+ * 1. Build subject: [event kernel]
+ * 2. Run Nock with poke formula
+ * 3. Parse result: [effects new-kernel]
+ * 4. Update kernel state
+ * 5. Return effects
+ *)
+let poke state event_noun =
+ Mutex.lock state.lock;
+
+ try
+ (* Build subject: [event kernel] *)
+ let subject = Noun.cell event_noun state.roc in
+
+ (* Run Nock with poke formula *)
+ let result = Nock.nock_on subject poke_formula in
+
+ (* Parse result *)
+ let (new_kernel, effects) = parse_poke_result result in
+
+ (* Update kernel state *)
+ state.roc <- new_kernel;
+ state.eve <- Int64.succ state.eve;
- (* Return empty effects list for now *)
- []
+ Mutex.unlock state.lock;
+
+ (* Return effects *)
+ effects
+
+ with e ->
+ (* Nock error - don't update state *)
+ Mutex.unlock state.lock;
+ Printf.printf "[State] Poke failed: %s\n%!" (Printexc.to_string e);
+ []
+
+(* Peek Formula - Scry formula
+ *
+ * Scry is a read-only query into Arvo.
+ * Formula: Similar to poke but doesn't update state
+ *
+ * For now: simplified - just return the path from the kernel
+ *)
+let peek_formula =
+ (* Simplified: [0 1] - return the whole subject *)
+ Noun.cell
+ (Noun.atom 0)
+ (Noun.atom 1)
(* Peek: query the kernel state (read-only)
*
- * In real Arvo: runs scry requests
- * For now: simplified
+ * Real Arvo scry:
+ * 1. Build subject: [path kernel]
+ * 2. Run Nock with peek formula
+ * 3. Return result (no state update!)
+ *
+ * Multiple domains can peek concurrently since it's read-only.
*)
-let peek state _path =
- (* Peek is read-only, multiple domains can do this concurrently *)
- Some state.roc
+let peek state path_noun =
+ (* No lock needed for read! This is why peek is fast *)
+ let kernel = state.roc in
+
+ try
+ (* Build subject: [path kernel] *)
+ let subject = Noun.cell path_noun kernel in
+
+ (* Run Nock with peek formula - read-only! *)
+ let result = Nock.nock_on subject peek_formula in
+
+ Some result
+
+ with e ->
+ (* Scry failed *)
+ Printf.printf "[State] Peek failed: %s\n%!" (Printexc.to_string e);
+ None
(* Save snapshot to file using Eio
*