import Base import ./world.bend as W import ./ordinary_app_relation.bend as Relation import ./relation_failure_reader.bend as Ord import ./relation_types.bend as G import ./relation_commands.bend as A import ./ordinary_app_linked_cleanup.bend as LC import ./transaction.bend as T import ./commands.bend as Cmd import ./system.bend as Sys import ./capabilities.bend as Cap import ./ordinary_app_composer.bend as Build import ./ordinary_app_recipe.bend as Tree import ./ordinary_app_observation_data.bend as DTO import ./ordinary_app_registration_fields.bend as Fields import ./ordinary_component_clear_declaration.bend as Clear # Gameplay receives an abstract affine owner and this single live operation. type Ops<-H:Type,-S:Data> is Type: Ops{cleanup:Cap.Request,A.QueueStatus>} def cleanup(-H:Type,-S:Data,caps:Ops,owner:H,handle:W.Handle) -> H & A.QueueStatus: match caps: case Ops{Cap.Request{request}}:request(owner,handle) def writes(keys:List<&2,String>) -> List<&2,String>: match keys: case []:[] case key <> rest:String.concat([key,":write"]) <> writes(rest) def relation_accesses(~S:Data,declarations:List<&2,Ord.Declaration>) -> List<&2,String>: match declarations: case []:[] case Ord.Declaration{G.Descriptor{_,name,_,_}} <> rest:String.concat([name,":write"]) <> relation_accesses(~S,rest) def request(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,tx:T.Tx>,handle:W.Handle) -> T.Tx> & A.QueueStatus: LC.queue(~S,~C,~R,~E,~declarations,~clear,~record,tx,handle) def provide(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice) -> Ops>,S>: Ops{Cap.Request{request(~S,~C,~R,~E,~declarations,~clear,~record)}} def returned(~S:Data,~C:Type,~R:Type,~E:Data,~U:Data,~Out:Data,output:Out,result:T.Returned) -> Sys.Outcome: match result: case T.Returned{world,T.Success{}}:Sys.Succeeded{world,output} case T.Returned{world,T.Failure{error}}:Sys.Failed{world,error} def invoked(~S:Data,~C:Type,~R:Type,~E:Data,~U:Data,~Out:Data,result:T.Tx> & (T.Outcome & Out)) -> Sys.Outcome: match result: case (tx,(outcome,output)):returned(~S,~C,~R,~E,~U,~Out,output,Cmd.tx_finish(~S,~A.Store,~R,~A.Notice,~U,tx,outcome)) def runner(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),world:Relation.World(~S,~C,~R,~E),args:Args) -> Sys.Outcome: invoked(~S,~C,~R,~E,~U,~Out,body(T.Tx>,provide(~S,~C,~R,~E,~declarations,~clear,~record),args,T.begin(~Relation.World(~S,~C,~R,~E),~A.Notice,world))) def Owner(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out)) -> Type: Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)> def register(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),~clearDeclaration:Clear.Declaration,clear>,world:Relation.World(~S,~C,~R,~E),name:String) -> Sys.RegistrationResult,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>: Sys.register(~S,~A.Store,~R,~A.Notice,~Args,~U,~Out,~runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body),world,name,List.append(&2,String,writes(Clear.keys(~S,~C,~A.Notice,~clear,clearDeclaration)),relation_accesses(~S,declarations))) def run(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),owner:Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>,world:Relation.World(~S,~C,~R,~E),args:Args) -> Sys.RunResult,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>: Sys.run(~S,~A.Store,~R,~A.Notice,~Args,~U,~Out,~runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body),owner,world,args) type Defined<-App:Type,-Registry:Type> is Type: Defined{app:App,owner:Registry} DefinitionRejected{app:App} type Executed<-App:Type,-Registry:Type,-Args:Data,-U:Data,-Out:Data> is Type: Executed{app:App,owner:Registry,outcome:T.Outcome,output:Maybe<&2,Out>} RegistrationRejected{app:App,owner:Registry,args:Args} def registered_app(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owners:Owners,recipe:Tree.Recipe,debug:Bool,result:Sys.RegistrationResult,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>) -> Defined,Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>>: match result: case Sys.Registered{world,registry}:Defined{LC.Installed{Build.Registered{world,owners,recipe,debug}},registry} case Sys.RegistrationFailed{world}:DefinitionRejected{LC.Installed{Build.Registered{world,owners,recipe,debug}}} def register_app(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~clearDeclaration:Clear.Declaration,clear>,installed:LC.Installed,name:String) -> Defined,Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>>: match installed: case LC.Installed{Build.Registered{world,owners,recipe,debug}}:registered_app(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body,~Owners,~Rows,~observe,~fold,owners,recipe,debug,register(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body,~clearDeclaration,world,name)) def executed_app(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owners:Owners,recipe:Tree.Recipe,debug:Bool,result:Sys.RunResult,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>) -> Executed,Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>,Args,U,Out>: match result: case Sys.Completed{registry,Sys.Succeeded{world,output}}:Executed{LC.Installed{Build.Registered{world,owners,recipe,debug}},registry,T.Success{},Some{output}} case Sys.Completed{registry,Sys.Failed{world,error}}:Executed{LC.Installed{Build.Registered{world,owners,recipe,debug}},registry,T.Failure{error},None{}} case Sys.RegistrationRejected{registry,world,args}:RegistrationRejected{LC.Installed{Build.Registered{world,owners,recipe,debug}},registry,args} def run_app(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Args:Data,~U:Data,~Out:Data,~body:@-H:Type -> Ops -> Args -> H -> H & (T.Outcome & Out),~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owner:Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>,installed:LC.Installed,input:Args) -> Executed,Sys.Registry,R,A.Notice,Args,U,Out,runner(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body)>,Args,U,Out>: match installed: case LC.Installed{Build.Registered{world,owners,recipe,debug}}:executed_app(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body,~Owners,~Rows,~observe,~fold,owners,recipe,debug,run(~S,~C,~R,~E,~declarations,~clear,~record,~Args,~U,~Out,~body,owner,world,input))