import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts 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_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 ./relation_cleanup.bend as Cleanup import ./world.bend as W import ./transaction.bend as T import ./commands.bend as Cmd # Trusted setup captures component-binding-derived clear and declaration installs. # This wrapper cannot infer declarations from an arbitrary existing App recipe. type Installed Relation.World(~S,~C,~R,~E) & DTO.Observation,-fold:Owners -> Owners & List<&2,Fields.Fields>,-declarations:List<&2,Ord.Declaration>,-clear:C -> W.Handle -> C & List<&2,A.Notice>,-record:W.Handle -> A.Notice> is Type: Installed{app:Build.Registered} def descriptors(~S:Data,declarations:List<&2,Ord.Declaration>) -> List<&2,G.Descriptor>: match declarations: case []:[] case Ord.Declaration{descriptor} <> rest:descriptor <> descriptors(~S,rest) def empty(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,app:Build.Registered) -> Installed: Installed{app} def install_relation(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~declaration:Ord.Declaration,installed:Installed) -> Installed,~observe,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S))),fold,List.append(&2,Ord.Declaration,declarations,[declaration]),clear,record>: match installed: case Installed{app}:Installed{Relation.install(~S,~C,~R,~E,~declaration,~Owners,~Rows,~observe,~fold,app)} def result(~S:Data,~C:Type,~R:Type,~E:Data,result:Cleanup.CleanupResult) -> Relation.World(~S,~C,~R,~E): match result: case Cleanup.Complete{world}:world case Cleanup.Incomplete{world,frames}:W.event_publish(~S,~A.Store,~R,~A.Notice,world,[A.CleanupPaused{frames}]) def apply(~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,world:Relation.World(~S,~C,~R,~E),handle:W.Handle) -> Relation.World(~S,~C,~R,~E): result(~S,~C,~R,~E,Cleanup.automatic(~S,~C,~R,~E,~clear,~record,world,handle,descriptors(~S,declarations))) def checked(~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,allowed:Bool,world:Relation.World(~S,~C,~R,~E),undo:List<(Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E))>,commands:List>,events:List<&2,A.Notice>,trace:TraceFacts.State,handle:W.Handle) -> T.Tx> & A.QueueStatus: match allowed: case False{}:(T.Tx{world,undo,commands,events,trace},A.MissingEntity{}) case True{}:(T.Tx{world,undo,Deferred.Raw{(world => apply(~S,~C,~R,~E,~declarations,~clear,~record,world,handle))} <> commands,events,trace},A.Queued{}) def validated(~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,handle:W.Handle,undo:List<(Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E))>,commands:List>,events:List<&2,A.Notice>,trace:TraceFacts.State,seen:Relation.World(~S,~C,~R,~E) & Bool) -> T.Tx> & A.QueueStatus: match seen: case (world,allowed):checked(~S,~C,~R,~E,~declarations,~clear,~record,allowed,world,undo,commands,events,trace,handle) def queue(~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: match tx: case T.Tx{world,undo,commands,events,trace}:validated(~S,~C,~R,~E,~declarations,~clear,~record,handle,undo,commands,events,trace,W.valid(~S,~A.Store,~R,~A.Notice,world,handle)) def returned(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Error:Data,owners:Owners,recipe:Tree.Recipe,debug:Bool,status:A.QueueStatus,seen:T.Returned) -> Installed & DTO.Product>: match seen: case T.Returned{world,outcome}:(Installed{Build.Registered{world,owners,recipe,debug}},DTO.Product{status,outcome}) def queued(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Error:Data,owners:Owners,recipe:Tree.Recipe,debug:Bool,outcome:T.Outcome,seen:T.Tx> & A.QueueStatus) -> Installed & DTO.Product>: match seen: case (tx,status):returned(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,~Error,owners,recipe,debug,status,Cmd.tx_finish(~S,~A.Store,~R,~A.Notice,~Error,tx,outcome)) def cleanup_app(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~Error:Data,installed:Installed,handle:W.Handle,outcome:T.Outcome) -> Installed & DTO.Product>: match installed: case Installed{Build.Registered{world,owners,recipe,debug}}:queued(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,~Error,owners,recipe,debug,outcome,queue(~S,~C,~R,~E,~declarations,~clear,~record,T.begin(~Relation.World(~S,~C,~R,~E),~A.Notice,world),handle)) def barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,installed:Installed) -> Installed: match installed: case Installed{Build.Registered{world,owners,recipe,debug}}:Installed{Build.Registered{W.barrier(~S,~A.Store,~R,~A.Notice,world),owners,recipe,debug}} def snapshot_returned(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,seen:Build.Registered & Maybe<&2,DTO.Product>,DTO.Observation>>) -> Installed & Maybe<&2,DTO.Product>,DTO.Observation>>: match seen: case (app,view):(Installed{app},view) def snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~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>,~declarations:List<&2,Ord.Declaration>,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,installed:Installed,flag:Bool) -> Installed & Maybe<&2,DTO.Product>,DTO.Observation>>: match installed: case Installed{app}:snapshot_returned(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,Build.snapshot(~S,~Relation.World(~S,~C,~R,~E),~Owners,~Rows,~observe,~fold,Build.enabled(~S,~Relation.World(~S,~C,~R,~E),~Owners,~Rows,~observe,~fold,app,flag)))