1 import Base 2 import ./world.bend as W 3 import ./system.bend as Sys 4 import ./schedule.bend as Sch 5 6 def after_condition(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,next: H -> W.World -> List<&2,Sch.Observation> -> Sch.RunResult,checked: W.World & Result,owners: H,+namespace: U32,+name: String,+all: List<&2,Sch.Step>,+observations: List<&2,Sch.Observation>,+id: U32) -> Sch.RunResult: 7 match checked: 8 case (world,Fail{error}): Sch.Failed{Sch.Schedule{namespace,name,all,owners},world,error,observations} 9 case (world,Done{allowed}): Sch.conditioned_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,next,allowed,owners,world,namespace,name,all,observations,id) 10 11 def execute_skip(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,~condition: W.World -> U32 -> W.World & Result,steps: List<&2,Sch.Step>,owners: H,world: W.World,+namespace: U32,+name: String,+all: List<&2,Sch.Step>,+observations: List<&2,Sch.Observation>) -> Sch.RunResult: 12 match steps: 13 case []: Sch.Finished{Sch.Schedule{namespace,name,all,owners},world,observations} 14 case Sch.Phase{phase} <> rest: execute_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,rest,owners,world,namespace,name,all,List.append(&2,Sch.Observation,observations,[Sch.Entered{phase}])) 15 case Sch.Barrier{} <> rest: execute_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,rest,owners,W.barrier(~S,~C,~R,~E,world),namespace,name,all,List.append(&2,Sch.Observation,observations,[Sch.Applied{}])) 16 case Sch.System{+id,slot} <> rest: after_condition(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,h => w => obs => execute_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,rest,h,w,namespace,name,all,obs),condition(world,slot),owners,namespace,name,all,observations,id) 17 18 def run_checked_skip(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,~condition: W.World -> U32 -> W.World & Result,allowed: Bool,schedule: Sch.Schedule,world: W.World) -> Sch.RunResult: 19 match allowed schedule: 20 case False{} schedule: Sch.Rejected{schedule,world} 21 case True{} Sch.Schedule{+namespace,+name,+steps,owners}: execute_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,steps,owners,world,namespace,name,steps,[]) 22 23 24 def run_actual_skip(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,~condition: W.World -> U32 -> W.World & Result,schedule: Sch.Schedule,world: W.World,+actual: U32) -> Sch.RunResult: 25 match schedule: 26 case Sch.Schedule{+namespace,+name,+steps,owners}: run_checked_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,U32.is_eq(namespace,actual),Sch.Schedule{namespace,name,steps,owners},world) 27 28 def run_namespace_skip(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,~condition: W.World -> U32 -> W.World & Result,schedule: Sch.Schedule,named: W.World & U32) -> Sch.RunResult: 29 (world,+actual) = named 30 run_actual_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,schedule,world,actual) 31 32 # Run ordered steps with fallible conditions and an explicit false-condition 33 # skip callback. Foreign namespace rejects before callbacks; condition failure 34 # stops with returned World/owners and prior observations. Explicit Barrier steps 35 # apply World.barrier; completion adds no final flush or schedule-wide rollback. 36 # Dispatch, condition and skip are trusted adapters with their own effect contracts. 37 def run_with_skip(~S: Data,~C: Type,~R: Type,~E: Data,~H: Type,~Error: Data,~skip: H -> W.World -> U32 -> H & W.World,~dispatch: H -> W.World -> U32 -> H & Sys.Outcome,Error,Unit>,~condition: W.World -> U32 -> W.World & Result,schedule: Sch.Schedule,world: W.World) -> Sch.RunResult: 38 run_namespace_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,schedule,W.namespace(~S,~C,~R,~E,world))