import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts import transaction.bend as Tx # Experimental machine metadata. World and component owners remain arbitrary Type. # This module follows the pinned reference marker snapshot semantics; that behavior # is not approval of the disputed blanket next-marker retention requirement. type Pending is Data: NoPending{} Queued{value: V, skipSame: Bool} type Transition is Data: Transition{from: V, to: V} Initial{to: V} type Slot is Data: Missing{} Present{current: V, pending: Pending, previous: Maybe<&2,V>, changed: Bool} # Public capability projections intentionally expose less than debug metadata. type CurrentView is Data: Unavailable{} Current{value: V} type NextView is Data: NoNext{} Next{value: V} def current_view(~S: Data,~M: Data,~V: Data,slot: Slot) -> CurrentView: match slot: case Missing{}: Unavailable{} case Present{current,_,_,_}: Current{current} def pending_view(~S: Data,~M: Data,~V: Data,pending: Pending) -> NextView: match pending: case NoPending{}: NoNext{} case Queued{value,_}: Next{value} def next_view(~S: Data,~M: Data,~V: Data,slot: Slot) -> NextView: match slot: case Missing{}: NoNext{} case Present{_,pending,_,_}: pending_view(~S,~M,~V,pending) def is_current(~S: Data,~M: Data,~V: Data,~same: V -> V -> Bool,view: CurrentView,value: V) -> Bool: match view: case Unavailable{}: False{} case Current{current}: same(current,value) type Family W & Slot,-put: W -> Slot -> W> is Data: Family{name: String} type QueueInput is Data: Set{value: V,skipSame: Bool} Reset{} type Snapshot is Data: Unscheduled{} Scheduled{value: V, skipSame: Bool} type Applied is Data: Applied{slot: Slot, event: Maybe<&2,Transition>} def queue(~S: Data,~M: Data,~V: Data,slot: Slot,value: V,skipSame: Bool) -> Slot: match slot: case Missing{}: Missing{} case Present{current,_,previous,changed}: Present{current,Queued{value,skipSame},previous,changed} def reset(~S: Data,~M: Data,~V: Data,slot: Slot) -> Slot: match slot: case Missing{}: Missing{} case Present{current,_,previous,changed}: Present{current,NoPending{},previous,changed} def clear_changed(~S: Data,~M: Data,~V: Data,slot: Slot) -> Slot: match slot: case Missing{}: Missing{} case Present{current,pending,previous,_}: Present{current,pending,previous,False{}} def snapshot_pending(~V: Data,pending: Pending) -> Snapshot: match pending: case NoPending{}: Unscheduled{} case Queued{value,skipSame}: Scheduled{value,skipSame} def snapshot(~S: Data,~M: Data,~V: Data,slot: Slot) -> Snapshot: match slot: case Missing{}: Unscheduled{} case Present{_,pending,_,_}: snapshot_pending(~V,pending) def apply_decision(~S: Data,~M: Data,~V: Data,+current: V,+value: V,previous: Maybe<&2,V>,changed: Bool,skip: Bool) -> Applied: match skip: case True{}: Applied{Present{current,NoPending{},previous,changed},None{}} case False{}: Applied{Present{value,NoPending{},Some{current},True{}},Some{Transition{current,value}}} def apply_present(~S: Data,~M: Data,~V: Data,~same: V -> V -> Bool,+current: V,+value: V,previous: Maybe<&2,V>,changed: Bool,skipSame: Bool) -> Applied: apply_decision(~S,~M,~V,current,value,previous,changed,Bool.and(skipSame,same(current,value))) # Captured initial value is authoritative; current pending is deleted at the # machine's turn, matching Runtime.ts:1534 even when an earlier hook overwrote it. def apply_snapshot(~S: Data,~M: Data,~V: Data,~same: V -> V -> Bool,slot: Slot,captured: Snapshot) -> Applied: match slot captured: case slot Unscheduled{}: Applied{slot,None{}} case Missing{} Scheduled{_,_}: Applied{Missing{},None{}} case Present{current,_,previous,changed} Scheduled{value,skipSame}: apply_present(~S,~M,~V,~same,current,value,previous,changed,skipSame) # Journal the complete old slot before replacing it. Aliases see the current # world through the same affine transaction; no independent machine owner copy. def write_taken(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~put: W -> Slot -> W, pair: W & Slot,undo: List<&1,(W -> W)>,commands:List<&1,Deferred.Command>,events: List<&2,E>,trace:TraceFacts.State,value: V,skipSame: Bool) -> Tx.Tx: match pair: case (world,+old): Tx.Tx{put(world,queue(~S,~M,~V,old,value,skipSame)),(world => put(world,old)) <> undo,commands,events,trace} def write(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,~put: W -> Slot -> W, tx: Tx.Tx,value: V,skipSame: Bool) -> Tx.Tx: match tx: case Tx.Tx{world,undo,commands,events,trace}: write_taken(~S,~M,~V,~W,~E,~put,take(world),undo,commands,events,trace,value,skipSame) def reset_taken(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~put: W -> Slot -> W, pair: W & Slot,undo: List<&1,(W -> W)>,commands:List<&1,Deferred.Command>,events: List<&2,E>,trace:TraceFacts.State) -> Tx.Tx: match pair: case (world,+old): Tx.Tx{put(world,reset(~S,~M,~V,old)),(world => put(world,old)) <> undo,commands,events,trace} def reset_tx(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,~put: W -> Slot -> W, tx: Tx.Tx) -> Tx.Tx: match tx: case Tx.Tx{world,undo,commands,events,trace}: reset_taken(~S,~M,~V,~W,~E,~put,take(world),undo,commands,events,trace) def next(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,~put: W -> Slot -> W, tx: Tx.Tx,request: QueueInput) -> Tx.Tx: match request: case Set{value,skipSame}: write(~S,~M,~V,~W,~E,~take,~put,tx,value,skipSame) case Reset{}: reset_tx(~S,~M,~V,~W,~E,~take,~put,tx) def read_taken(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, pair: W & Slot,undo: List<&1,(W -> W)>,commands:List<&1,Deferred.Command>,events: List<&2,E>,trace:TraceFacts.State) -> Tx.Tx & Slot: match pair: case (world,slot): (Tx.Tx{world,undo,commands,events,trace},slot) def read(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,tx: Tx.Tx) -> Tx.Tx & Slot: match tx: case Tx.Tx{world,undo,commands,events,trace}: read_taken(~S,~M,~V,~W,~E,take(world),undo,commands,events,trace) def read_declared(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,~put: W -> Slot -> W, _: Family,tx: Tx.Tx) -> Tx.Tx & Slot: read(~S,~M,~V,~W,~E,~take,tx) def next_declared(~S: Data,~M: Data,~V: Data,~W: Type,~E: Data, ~take: W -> W & Slot,~put: W -> Slot -> W, _: Family,tx: Tx.Tx,request: QueueInput) -> Tx.Tx: next(~S,~M,~V,~W,~E,~take,~put,tx,request) type Condition is Data: InState{value: V} StateChanged{} Not{inner: Condition} Constant{value: Bool} And{left: Condition,right: Condition} Or{left: Condition,right: Condition} def current_matches(~S: Data,~M: Data,~V: Data,~same: V -> V -> Bool,slot: Slot,value: V) -> Bool: match slot: case Missing{}: False{} case Present{current,_,_,_}: same(current,value) def changed(~S: Data,~M: Data,~V: Data,slot: Slot) -> Bool: match slot: case Missing{}: False{} case Present{_,_,_,flag}: flag # Arbitrary public variadic combinations become a structural Boolean tree. # Empty and/or preserve the primary API identities. def all(~V: Data,items: List<&2,Condition>) -> Condition: match items: case Nil{}: Constant{True{}} case Con{head,tail}: And{head,all(~V,tail)} def any(~V: Data,items: List<&2,Condition>) -> Condition: match items: case Nil{}: Constant{False{}} case Con{head,tail}: Or{head,any(~V,tail)} def evaluate(~S: Data,~M: Data,~V: Data,~same: V -> V -> Bool,condition: Condition,+slot: Slot) -> Bool: match condition: case InState{value}: current_matches(~S,~M,~V,~same,slot,value) case StateChanged{}: changed(~S,~M,~V,slot) case Not{inner}: Bool.not(evaluate(~S,~M,~V,~same,inner,slot)) case Constant{value}: value case And{left,right}: Bool.and(evaluate(~S,~M,~V,~same,left,slot),evaluate(~S,~M,~V,~same,right,slot)) case Or{left,right}: Bool.or(evaluate(~S,~M,~V,~same,left,slot),evaluate(~S,~M,~V,~same,right,slot)) # A heterogeneous marker snapshot owns each callback exactly once. Each callback # captures a Data machine value and closed lenses, never a world/component owner. type Marker is Type: Marker{steps: List<&1,(W -> W)>} def marker_steps(~W: Type,steps: List<&1,(W -> W)>,world: W) -> W: match steps: case Nil{}: world case Con{step,rest}: marker_steps(~W,rest,step(world)) def run_marker(~S: Data,~W: Type,marker: Marker,world: W) -> W: match marker: case Marker{steps}: marker_steps(~W,steps,world) # Registered schedule/transition owners live alongside the world and are also # threaded, rather than copied into one closure per machine. type OwnedMarker is Type: OwnedMarker{steps: List<&1,(H -> W -> H & W)>} def marker_next(~W: Type,~H: Type,next: H -> W -> H & W,pair: H & W) -> H & W: (owners,world)=pair next(owners,world) def owned_marker_steps(~W: Type,~H: Type,steps: List<&1,(H -> W -> H & W)>,owners: H,world: W) -> H & W: match steps: case Nil{}: (owners,world) case Con{step,rest}: marker_next(~W,~H,h => w => owned_marker_steps(~W,~H,rest,h,w),step(owners,world)) def run_owned_marker(~S: Data,~W: Type,~H: Type,marker: OwnedMarker,owners: H,world: W) -> H & W: match marker: case OwnedMarker{steps}: owned_marker_steps(~W,~H,steps,owners,world)