import Base import ./change_tick.bend as Tick import internal/relation_failures/domains.bend as D import internal/relation_failures/readings.bend as Read import internal/relation_failures/routing.bend as Route import internal/relation_failures/barrier.bend as B import world.bend as W import system.bend as Sy import event_runtime.bend as Ev type Runtime is Type: Runtime{world: W.World,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,nextReader: U32} type Input is Type: Input{readings: List<&2,Read.KeyReading>,args: A} type Reader -> Input -> Sy.Outcome,Error,O>> is Type: Reader{registry: Sy.Registry,Error,O,runner>,keys: List<&2,D.DescriptorKey>,id: U32} type Registered -> Input -> Sy.Outcome,Error,O>> is Type: Registered{runtime: Runtime,reader: Reader} RegistrationFailed{runtime: Runtime} type Completion is Type: Succeeded{output: O} Failed{error: Error} Rejected{args: A} type RunResult -> Input -> Sy.Outcome,Error,O>> is Type: Ran{runtime: Runtime,reader: Reader,completion: Completion} def started_clocked(~S: Data,~C: Type,~R: Type,~E: Data,clocked: W.World & Tick.Tick) -> Runtime: (world,clock) = clocked Runtime{world,[],[],Tick.total(clock),0n,0n,65536n,1} def start(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World) -> Runtime: started_clocked(~S,~C,~R,~E,W.clock(~S,~C,~R,~E,world)) def frame(~S: Data,~C: Type,~R: Type,~E: Data,runtime: Runtime) -> Runtime: match runtime: case Runtime{world,domains,+positions,+tick,+frameStart,_,+capacity,+nextReader}: Runtime{world,D.trim(~S,~E,domains,positions,frameStart,capacity),positions,tick,tick,frameStart,capacity,nextReader} def registered_result(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,result: Sy.RegistrationResult,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Registered: match result: case Sy.RegistrationFailed{world}: RegistrationFailed{Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}} case Sy.Registered{world,registry}: Registered{Runtime{world,domains,positions,tick,frameStart,boundary,capacity,(nextReader+1 : U32)},Reader{registry,keys,nextReader}} def register_available(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,+keys: List<&2,D.DescriptorKey>,+name: String,+access: List<&2,String>) -> Registered: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: registered_result(~S,~C,~R,~E,~A,~Error,~O,~runner,Sy.register(~S,~C,~R,~E,~Input,~Error,~O,~runner,world,name,access),keys,domains,positions,tick,frameStart,boundary,capacity,nextReader) # Shared by standalone readers and composed parameters; no system registration. def reserve_reader_checked(~S:Data,~C:Type,~R:Type,~E:Data,available:Bool,runtime:Runtime) -> Runtime & Maybe<&2,U32>: match available runtime: case False{} runtime:(runtime,None{}) case True{} Runtime{world,domains,positions,tick,frameStart,boundary,capacity,+nextReader}: (Runtime{world,domains,positions,tick,frameStart,boundary,capacity,(nextReader+1 : U32)},Some{nextReader}) def reserve_reader(~S:Data,~C:Type,~R:Type,~E:Data,runtime:Runtime) -> Runtime & Maybe<&2,U32>: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,+nextReader}: reserve_reader_checked(~S,~C,~R,~E,U32.is_lt(nextReader,4294967295),Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}) def register_checked(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Input -> Sy.Outcome,Error,O>,available:Bool,runtime:Runtime,keys:List<&2,D.DescriptorKey>,name:String,access:List<&2,String>) -> Registered: match available: case False{}:RegistrationFailed{runtime} case True{}:register_available(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,keys,name,access) def register(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Input -> Sy.Outcome,Error,O>,runtime:Runtime,keys:List<&2,D.DescriptorKey>,name:String,access:List<&2,String>) -> Registered: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,+nextReader}: register_checked(~S,~C,~R,~E,~A,~Error,~O,~runner,U32.is_lt(nextReader,4294967295),Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},keys,name,access) def position_or(position: Maybe,id: U32,tick: Nat) -> Ev.Position: match position: case None{}: Ev.Position{id,0n,tick} case Some{found}: found def succeeded_collected(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,id:U32,joined: W.World & List<&2,D.Domain>,registry: Sy.Registry,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32,output: O) -> RunResult: (world,domains) = joined Ran{Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},Reader{registry,keys,id},Succeeded{output}} def failed_clocked(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,clocked: W.World & Tick.Tick,registry: Sy.Registry,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,+domains: List<&2,D.Domain>,+positions: List<&2,Ev.Position>,+id: U32,+previous: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32,error: Error) -> RunResult: (world,clock) = clocked Ran{Runtime{world,Read.activate(~S,~E,keys,domains,id),Ev.activate(positions,id,previous),Tick.total(clock),frameStart,boundary,capacity,nextReader},Reader{registry,keys,id},Failed{error}} def succeeded_clocked(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,clocked: W.World & Tick.Tick,registry: Sy.Registry,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,+domains: List<&2,D.Domain>,+positions: List<&2,Ev.Position>,+id: U32,+previous: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32,output: O) -> RunResult: (world,+clock) = clocked succeeded_collected(~S,~C,~R,~E,~A,~Error,~O,~runner,id,Route.collect(~S,~C,~R,~E,~classify,world,Tick.total(clock),Read.activate(~S,~E,keys,domains,id)),registry,keys,Ev.update(Ev.activate(positions,id,previous),id,Tick.total(clock),False{}),Tick.total(clock),frameStart,boundary,capacity,nextReader,output) def outcome(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,outcome: Sy.Outcome,Error,O>,registry: Sy.Registry,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,+domains: List<&2,D.Domain>,+positions: List<&2,Ev.Position>,+id: U32,+tick: Nat,+frameStart: Nat,+boundary: Nat,+capacity: Nat,+nextReader: U32) -> RunResult: match outcome: case Sy.Failed{world,error}: failed_clocked(~S,~C,~R,~E,~A,~Error,~O,~runner,~classify,W.clock(~S,~C,~R,~E,world),registry,keys,domains,positions,id,tick,frameStart,boundary,capacity,nextReader,error) case Sy.Succeeded{world,output}: succeeded_clocked(~S,~C,~R,~E,~A,~Error,~O,~runner,~classify,W.clock(~S,~C,~R,~E,world),registry,keys,domains,positions,id,tick,frameStart,boundary,capacity,nextReader,output) def run_result(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,result: Sy.RunResult,Error,O,runner>,+keys: List<&2,D.DescriptorKey>,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,id: U32,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> RunResult: match result: case Sy.RegistrationRejected{registry,world,Input{_,args}}: Ran{Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},Reader{registry,keys,id},Rejected{args}} case Sy.Completed{registry,completed}: outcome(~S,~C,~R,~E,~A,~Error,~O,~runner,~classify,completed,registry,keys,domains,positions,id,tick,frameStart,boundary,capacity,nextReader) def effect_clocked(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,effect: W.World -> W.World & O,advanced: W.World & Tick.Tick) -> W.World & O: (world,_) = advanced effect(world) def run_identified(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,runtime: Runtime,identified: Sy.Registry,Error,O,runner> & U32,+keys: List<&2,D.DescriptorKey>,args: A) -> RunResult: match runtime: case Runtime{world,+domains,+positions,+tick,+frameStart,+boundary,+capacity,+nextReader}: (registry,+id) = identified run_result(~S,~C,~R,~E,~A,~Error,~O,~runner,~classify,Sy.run(~S,~C,~R,~E,~Input,~Error,~O,~runner,registry,world,Input{Read.all(~S,~E,keys,domains,position_or(Ev.lookup(positions,id),id,tick)),args}),keys,domains,positions,id,tick,frameStart,boundary,capacity,nextReader) def run(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,~classify: E -> Maybe>,runtime: Runtime,reader: Reader,args: A) -> RunResult: match reader: case Reader{registry,keys,id}: run_identified(~S,~C,~R,~E,~A,~Error,~O,~runner,~classify,runtime,(registry,id),keys,args) def skipped_checked(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,allowed: Bool,runtime: Runtime,reader: Reader,+id: U32) -> Runtime & Reader: match allowed runtime: case False{} runtime: (runtime,reader) case True{} Runtime{world,domains,positions,+tick,frameStart,boundary,capacity,nextReader}: (Runtime{world,domains,Ev.update(positions,id,tick,False{}),tick,frameStart,boundary,capacity,nextReader},reader) def skip_meta(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,checked: W.World & Bool,registry: Sy.Registry,Error,O,runner>,keys: List<&2,D.DescriptorKey>,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32,+id: U32) -> Runtime & Reader: (world,allowed) = checked skipped_checked(~S,~C,~R,~E,~A,~Error,~O,~runner,allowed,Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},Reader{registry,keys,id},id) def skip_namespace(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,same: Bool,world: W.World,registry: Sy.Registry,Error,O,runner>,keys: List<&2,D.DescriptorKey>,readerId:U32,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime & Reader: match same registry: case False{} registry: (Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},Reader{registry,keys,readerId}) case True{} Sy.Registry{+namespace,+id,+name,+access,+cursor}: skip_meta(~S,~C,~R,~E,~A,~Error,~O,~runner,W.registration_matches(~S,~C,~R,~E,world,id,name,access),Sy.Registry{namespace,id,name,access,cursor},keys,domains,positions,tick,frameStart,boundary,capacity,nextReader,readerId) def skip_named(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,reader: Reader,named: W.World & U32,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime & Reader: match reader: case Reader{Sy.Registry{+namespace,+id,+name,+access,+cursor},keys,readerId}: (world,actual) = named skip_namespace(~S,~C,~R,~E,~A,~Error,~O,~runner,U32.is_eq(namespace,actual),world,Sy.Registry{namespace,id,name,access,cursor},keys,readerId,domains,positions,tick,frameStart,boundary,capacity,nextReader) def skip(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> Input -> Sy.Outcome,Error,O>,runtime: Runtime,reader: Reader) -> Runtime & Reader: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: skip_named(~S,~C,~R,~E,~A,~Error,~O,~runner,reader,W.namespace(~S,~C,~R,~E,world),domains,positions,tick,frameStart,boundary,capacity,nextReader) def barrier_join(~S: Data,~C: Type,~R: Type,~E: Data,state: B.BarrierState,positions: List<&2,Ev.Position>,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime: match state: case B.BarrierState{world,domains,tick}: Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader} def barrier(~S: Data,~C: Type,~R: Type,~E: Data,~classify: E -> Maybe>,runtime: Runtime) -> Runtime: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: barrier_join(~S,~C,~R,~E,B.barrier(~S,~C,~R,~E,~classify,world,domains),positions,frameStart,boundary,capacity,nextReader) def with_world_collected(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,collected: W.World & List<&2,D.Domain>,output: O,positions: List<&2,Ev.Position>,tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime & O: (world,domains) = collected (Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},output) def with_world_clocked(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,~classify: E -> Maybe>,clocked: W.World & Tick.Tick,output: O,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime & O: (world,+clock) = clocked with_world_collected(~S,~C,~R,~E,~O,Route.collect(~S,~C,~R,~E,~classify,world,Tick.total(clock),domains),output,positions,Tick.total(clock),frameStart,boundary,capacity,nextReader) def with_world_result(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,~classify: E -> Maybe>,result: W.World & O,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,_: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Runtime & O: (world,output) = result with_world_clocked(~S,~C,~R,~E,~O,~classify,W.clock(~S,~C,~R,~E,world),output,domains,positions,frameStart,boundary,capacity,nextReader) def with_world(~S: Data,~C: Type,~R: Type,~E: Data,~O: Type,~classify: E -> Maybe>,effect: W.World -> W.World & O,runtime: Runtime) -> Runtime & O: match runtime: case Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: with_world_result(~S,~C,~R,~E,~O,~classify,effect_clocked(~S,~C,~R,~E,~O,effect,W.write_clock(~S,~C,~R,~E,world)),domains,positions,tick,frameStart,boundary,capacity,nextReader)