import Base import ./world.bend as W import ./system.bend as Sys import ./schedule.bend as Sch 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: match checked: case (world,Fail{error}): Sch.Failed{Sch.Schedule{namespace,name,all,owners},world,error,observations} case (world,Done{allowed}): Sch.conditioned_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,next,allowed,owners,world,namespace,name,all,observations,id) 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: match steps: case []: Sch.Finished{Sch.Schedule{namespace,name,all,owners},world,observations} 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}])) 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{}])) 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) 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: match allowed schedule: case False{} schedule: Sch.Rejected{schedule,world} 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,[]) 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: match schedule: 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) 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: (world,+actual) = named run_actual_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,schedule,world,actual) # Run ordered steps with fallible conditions and an explicit false-condition # skip callback. Foreign namespace rejects before callbacks; condition failure # stops with returned World/owners and prior observations. Explicit Barrier steps # apply World.barrier; completion adds no final flush or schedule-wide rollback. # Dispatch, condition and skip are trusted adapters with their own effect contracts. 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: run_namespace_skip(~S,~C,~R,~E,~H,~Error,~skip,~dispatch,~condition,schedule,W.namespace(~S,~C,~R,~E,world))