import Base import ./execution_command_buffer.bend as Buffer import ./execution_command_removed.bend as Removed import ./world.bend as W import ./execution_composer_layout.bend as Layout import ./execution_resource_policy.bend as Policy import ./execution_relation_store.bend as RelationStore import ./relation_types.bend as Relation import ./execution_relation_command.bend as RelationCommands import ./execution_relation_operation.bend as Operation import ./string_equality.bend as StringEquality import ./execution_relation_resource_dispatch.bend as Dispatch import ./execution_relation_resource_snapshot.bend as Snapshot import ./execution_lifetime_scope_resource_dispatch.bend as ScopeDispatch import ./execution_lifetime_scope_store.bend as ScopeStore import ./execution_lifetime_scope_command.bend as ScopeCommands # This closed algebra retains schema evidence supplied by the same declaration. # A route selects a grant; it never creates any of these type equalities. type Relations<-S:Data,-resources:Layout.Layout> is Data: NoRelations{} RelationAllowed{route:Layout.Route,valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},descriptor:Relation.Descriptor,payload:{Layout.payload(resources,route) == RelationStore.Store : Type},view:{Layout.view(resources,route) == RelationStore.View : Data},protected:{Layout.policy(resources,route) == Policy.ProtectedRelation{S,descriptor} : Policy.Policy},rest:Relations} ScopeAllowed{route:Layout.Route,valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},payload:{Layout.payload(resources,route) == ScopeStore.Store : Type},view:{Layout.view(resources,route) == ScopeStore.View : Data},protected:{Layout.policy(resources,route) == Policy.ProtectedLifetimeScope{S} : Policy.Policy},rest:Relations} type DeferredRelation<-S:Data> is Data: DeferredRelation{route:Layout.Route,key:String,descriptor:Relation.Descriptor} DeferredScope{route:Layout.Route,key:String} type Selection<-S:Data,-resources:Layout.Layout> is Type: Selection{program:Dispatch.Program(~S,~resources),snapshot:Snapshot.Program(~S,~resources),scopes:ScopeDispatch.Program(~S,~resources),cleanup:CleanupSelection} type CleanupSelection<-S:Data,-resources:Layout.Layout> is Type: Unprovisioned{} CleanupActive{graphs:List<&2,Layout.Route>,scopeRoutes:List<&2,Layout.Route>} Provisioned{program:Dispatch.Program(~S,~resources),snapshot:Snapshot.Program(~S,~resources),scopes:ScopeDispatch.Program(~S,~resources),graphs:List<&2,Layout.Route>,scopeRoutes:List<&2,Layout.Route>} def RelationSelection(~S:Data,~resources:Layout.Layout) -> Type:Selection def selection_field(-S:Data,-resources:Layout.Layout,value:Selection) -> Dispatch.Program(~S,~resources): match value: case Selection{program,_,_,_}:program def snapshot_field(-S:Data,-resources:Layout.Layout,value:Selection) -> Snapshot.Program(~S,~resources): match value: case Selection{_,snapshot,_,_}:snapshot def scopes_field(-S:Data,-resources:Layout.Layout,value:Selection) -> ScopeDispatch.Program(~S,~resources): match value: case Selection{_,_,scopes,_}:scopes def no_selection(~S:Data,~resources:Layout.Layout) -> Selection: Selection{Dispatch.empty(~S,~resources),Snapshot.empty(~S,~resources),ScopeDispatch.empty(~S,~resources),Unprovisioned{}} def extend_selection(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},~descriptor:Relation.Descriptor,~payload:{Layout.payload(resources,route) == RelationStore.Store : Type},~view:{Layout.view(resources,route) == RelationStore.View : Data},~protected:{Layout.policy(resources,route) == Policy.ProtectedRelation{S,descriptor} : Policy.Policy},selection:Selection) -> Selection: match selection: case Selection{program,snapshot,scopes,cleanup}:Selection{Dispatch.extend(~S,~resources,~route,~valid,~descriptor,~payload,~view,~protected,program),Snapshot.extend(~S,~resources,~route,~descriptor,~payload,~view,~protected,snapshot),scopes,cleanup} def extend_scope_selection(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},~payload:{Layout.payload(resources,route) == ScopeStore.Store : Type},~view:{Layout.view(resources,route) == ScopeStore.View : Data},~protected:{Layout.policy(resources,route) == Policy.ProtectedLifetimeScope{S} : Policy.Policy},selection:Selection) -> Selection: match selection: case Selection{program,snapshot,scopes,cleanup}:Selection{program,snapshot,ScopeDispatch.extend(~S,~resources,~route,~valid,~payload,~view,~protected,scopes),cleanup} def attach_cleanup(-S:Data,-resources:Layout.Layout,gameplay:Selection,cleanup:CleanupSelection) -> Selection: match gameplay: case Selection{program,snapshot,scopes,_}:Selection{program,snapshot,scopes,cleanup} def cleanup_selection(-S:Data,-resources:Layout.Layout,value:Selection) -> Selection: match value: case Selection{program,snapshot,scopes,Unprovisioned{}}:Selection{program,snapshot,scopes,Unprovisioned{}} case Selection{program,snapshot,scopes,CleanupActive{graphs,routes}}:Selection{program,snapshot,scopes,CleanupActive{graphs,routes}} case Selection{_,_,_,Provisioned{program,snapshot,scopes,graphs,routes}}:Selection{program,snapshot,scopes,CleanupActive{graphs,routes}} type CleanupRoutes is Data: CleanupRoutes{graphs:List<&2,Layout.Route>,scopes:List<&2,Layout.Route>} def cleanup_routes(-S:Data,-resources:Layout.Layout,value:Selection) -> Maybe<&2,CleanupRoutes>: match value: case Selection{_,_,_,Unprovisioned{}}:None{} case Selection{_,_,_,CleanupActive{graphs,scopes}}:Some{CleanupRoutes{graphs,scopes}} case Selection{_,_,_,Provisioned{_,_,_,graphs,scopes}}:Some{CleanupRoutes{graphs,scopes}} # Deferred graph writes are descriptions of barrier effects, not immediate body # resource-write grants or resource extraction recipes. def deferred_descriptions(~S:Data,~resources:Layout.Layout,relations:Relations) -> List<&2,DeferredRelation>: match relations: case NoRelations{}:[] case RelationAllowed{+route,_,descriptor,_,_,_,rest}:DeferredRelation{route,Layout.key(resources,route),descriptor} <> deferred_descriptions(~S,~resources,rest) case ScopeAllowed{+route,_,_,_,_,rest}:DeferredScope{route,Layout.key(resources,route)} <> deferred_descriptions(~S,~resources,rest) def same_route(left:Layout.Route,right:Layout.Route) -> Bool: match left right: case Layout.AtLeaf{} Layout.AtLeaf{}:True{} case Layout.InLeft{left} Layout.InLeft{right}:same_route(left,right) case Layout.InRight{left} Layout.InRight{right}:same_route(left,right) case _ _:False{} def same_kind(left:Relation.RelationKind,right:Relation.RelationKind) -> Bool: match left right: case Relation.Ordinary{} Relation.Ordinary{}:True{} case Relation.Hierarchy{} Relation.Hierarchy{}:True{} case _ _:False{} def same_descriptor(-S:Data,left:Relation.Descriptor,right:Relation.Descriptor) -> Bool: match left right: case Relation.Descriptor{lk,ln,li,lt} Relation.Descriptor{rk,rn,ri,rt}:Bool.and(U32.is_eq(lk,rk),Bool.and(StringEquality.equal(ln,rn),Bool.and(StringEquality.equal(li,ri),same_kind(lt,rt)))) def relations_agree(~S:Data,~resources:Layout.Layout,left:Relations,right:Relations) -> Bool: match left right: case NoRelations{} NoRelations{}:True{} case RelationAllowed{lr,_,ld,_,_,_,left} RelationAllowed{rr,_,rd,_,_,_,right}:Bool.and(same_route(lr,rr),Bool.and(same_descriptor(S,ld,rd),relations_agree(~S,~resources,left,right))) case ScopeAllowed{lr,_,_,_,_,left} ScopeAllowed{rr,_,_,_,_,right}:Bool.and(same_route(lr,rr),relations_agree(~S,~resources,left,right)) case _ _:False{} # Registration supplies an abstract affine K. A declaration can obtain entity # commands only by returning the book advanced by this explicit grant. Physical # scope access remains in the existing independent Builder book. type Book<-S:Data,-layout:Layout.Layout,-resources:Layout.Layout> is Type: Book{permission:Permission,relations:Relations,selection:RelationSelection(~S,~resources)} type Permission is Data: FamilyOnly{} EntityStructural{} type EntityCommands<-Q:Type,-S:Data,-layout:Layout.Layout,-Permit:Data,-F:Type -> Type> is Type: EntityCommands{despawn:Q -> W.Handle -> Q,spawn:Q -> Removed.Removed(layout) -> F(Q & Buffer.SpawnAdmission)} def EntitySelection(~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,~Q:Type,~Ops:Type) -> Type: @-commands:EntityCommands -> Ops def Grant(~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,~Q:Type,~K:Type) -> Type: @-Ops:Type -> @-selection:EntitySelection(~S,~layout,~Permit,~F,~Q,~Ops) -> K -> K & Ops def entity_identity(~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,~Q:Type,~commands:EntityCommands) -> EntityCommands:commands def entity(~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,~Q:Type,~K:Type,~grant:Grant(~S,~layout,~Permit,~F,~Q,~K),book:K) -> K & EntityCommands: grant(EntityCommands,entity_identity(~S,~layout,~Permit,~F,~Q),book) def fresh(~S:Data,~layout:Layout.Layout,~resources:Layout.Layout) -> Book:Book{FamilyOnly{},NoRelations{},no_selection(~S,~resources)} def grant(~S:Data,~layout:Layout.Layout,~resources:Layout.Layout,~Permit:Data,~F:Type -> Type,~Q:Type,~despawn:Q -> W.Handle -> Q,~spawn:Q -> Removed.Removed(layout) -> F(Q & Buffer.SpawnAdmission),~Ops:Type,~choose:EntitySelection(~S,~layout,~Permit,~F,~Q,~Ops),book:Book) -> Book & Ops: match book: case Book{_,relations,selection}:(Book{EntityStructural{},relations,selection},choose(EntityCommands{despawn,spawn})) def RelationGrant(~S:Data,~resources:Layout.Layout,~Q:Type,~K:Type) -> Type: @-route:Layout.Route -> @-valid:{Layout.grant_valid(~resources,~route) == True{} : Bool} -> @-descriptor:Relation.Descriptor -> @-payload:{Layout.payload(resources,route) == RelationStore.Store : Type} -> @-view:{Layout.view(resources,route) == RelationStore.View : Data} -> @-protected:{Layout.policy(resources,route) == Policy.ProtectedRelation{S,descriptor} : Policy.Policy} -> K -> K & RelationCommands.HierarchyCommands def relation_grant(~S:Data,~layout:Layout.Layout,~resources:Layout.Layout,~Q:Type,~enqueue:Q -> Layout.Route -> Operation.Operation -> Q,~route:Layout.Route,~valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},~descriptor:Relation.Descriptor,~payload:{Layout.payload(resources,route) == RelationStore.Store : Type},~view:{Layout.view(resources,route) == RelationStore.View : Data},~protected:{Layout.policy(resources,route) == Policy.ProtectedRelation{S,descriptor} : Policy.Policy},book:Book) -> Book & RelationCommands.HierarchyCommands: match book: case Book{permission,relations,selection}:(Book{permission,RelationAllowed{route,valid,descriptor,payload,view,protected,relations},extend_selection(~S,~resources,~route,~valid,~descriptor,~payload,~view,~protected,selection)},RelationCommands.hierarchy(~S,~Q,~route,~enqueue)) def ScopeGrant(~S:Data,~resources:Layout.Layout,~Q:Type,~K:Type) -> Type: @-route:Layout.Route -> @-valid:{Layout.grant_valid(~resources,~route) == True{} : Bool} -> @-payload:{Layout.payload(resources,route) == ScopeStore.Store : Type} -> @-view:{Layout.view(resources,route) == ScopeStore.View : Data} -> @-protected:{Layout.policy(resources,route) == Policy.ProtectedLifetimeScope{S} : Policy.Policy} -> K -> K & ScopeCommands.Commands def scope_grant(~S:Data,~layout:Layout.Layout,~resources:Layout.Layout,~Q:Type,~enqueue:Q -> Layout.Route -> ScopeCommands.Operation -> Q,~route:Layout.Route,~valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},~payload:{Layout.payload(resources,route) == ScopeStore.Store : Type},~view:{Layout.view(resources,route) == ScopeStore.View : Data},~protected:{Layout.policy(resources,route) == Policy.ProtectedLifetimeScope{S} : Policy.Policy},book:Book) -> Book & ScopeCommands.Commands: match book: case Book{_,relations,selection}:(Book{EntityStructural{},ScopeAllowed{route,valid,payload,view,protected,relations},extend_scope_selection(~S,~resources,~route,~valid,~payload,~view,~protected,selection)},ScopeCommands.provide(~S,~Q,~route,~enqueue)) def despawn_field(~Q:Type,~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,commands:EntityCommands) -> Q -> W.Handle -> Q: match commands: case EntityCommands{despawn,_}:despawn type Prepared<-Ops:Type,-S:Data,-resources:Layout.Layout> is Type: Prepared{permission:Permission,relations:Relations,selection:RelationSelection(~S,~resources),ops:Ops} def prepared(~S:Data,~layout:Layout.Layout,~resources:Layout.Layout,-B:Type,-Ops:Type,result:B & (Book & Ops)) -> B & Prepared: match result: case (book,(Book{permission,relations,selection},ops)):(book,Prepared{permission,relations,selection,ops}) def agrees(left:Permission,right:Permission) -> Bool: match left right: case FamilyOnly{} FamilyOnly{}:True{} case EntityStructural{} EntityStructural{}:True{} case _ _:False{} def ops_field(-Ops:Type,-S:Data,-resources:Layout.Layout,value:Prepared) -> Ops: match value: case Prepared{_,_,_,ops}:ops # Only a declaration that consumed the structural grant can obtain this field. def spawn_field(~Q:Type,~S:Data,~layout:Layout.Layout,~Permit:Data,~F:Type -> Type,commands:EntityCommands) -> Q -> Removed.Removed(layout) -> F(Q & Buffer.SpawnAdmission): match commands: case EntityCommands{_,spawn}:spawn