import Base import entity_key.bend as Key import ./relation_types.bend as G import ./relation_commands.bend as A import ./relation_reorder.bend as Reorder import ./world.bend as W import ./capabilities.bend as Cap import ./hierarchy_traversal.bend as T import ./hierarchy_provider.bend as P # Separate traversal requests avoid computing unrelated snapshot fields. # Descriptor and World remain hidden by the same rank-two authority boundary. type Descendants is Data: Descendants{entity:W.Handle,order:T.Order} type Read is Type: Read{root:Cap.Request,Maybe<&2,W.Handle>>,ancestors:Cap.Request,Maybe<&2,P.Path>>,descendants:Cap.Request,Maybe<&2,P.Path>>} type Selection is Data: Ancestors{} DescendantOrder{order:T.Order} def selected(~S:Data,graph:G.Graph,descriptor:G.Descriptor,id:Key.Key,selection:Selection) -> T.Walk: match selection: case Ancestors{}: T.ancestors(~S,graph,descriptor,id) case DescendantOrder{order}: T.descendants(~S,graph,descriptor,id,order) def store_path(~S:Data,~C:Type,store:A.Store,descriptor:G.Descriptor,target:W.Handle,selection:Selection) -> A.Store & P.Path: match store target: case A.Store{components,+graph} W.Handle{+namespace,id}: (A.Store{components,graph},P.path(~S,selected(~S,graph,descriptor,id,selection),namespace)) def present_path(~S:Data,~C:Type,~R:Type,~E:Data,result:W.World,R,A.Notice> & P.Path) -> W.World,R,A.Notice> & Maybe<&2,P.Path>: (world,value) = result (world,Some{value}) def checked_path(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,observed:W.World,R,A.Notice> & Bool,target:W.Handle,selection:Selection) -> W.World,R,A.Notice> & Maybe<&2,P.Path>: match observed: case (world,False{}): (world,None{}) case (world,True{}): present_path(~S,~C,~R,~E,W.with_store(~S,~A.Store,~R,~A.Notice,~P.Path,store => store_path(~S,~C,store,Reorder.descriptor(~S,descriptor),target,selection),world)) def lookup_path(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,world:W.World,R,A.Notice>,target:W.Handle,selection:Selection) -> W.World,R,A.Notice> & Maybe<&2,P.Path>: match target: case W.Handle{+namespace,+id}: checked_path(~S,~C,~R,~E,~descriptor,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{namespace,id}),W.Handle{namespace,id},selection) def lookup_ancestors(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,world:W.World,R,A.Notice>,target:W.Handle) -> W.World,R,A.Notice> & Maybe<&2,P.Path>: lookup_path(~S,~C,~R,~E,~descriptor,world,target,Ancestors{}) def lookup_descendants(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,world:W.World,R,A.Notice>,request:Descendants) -> W.World,R,A.Notice> & Maybe<&2,P.Path>: match request: case Descendants{target,order}: lookup_path(~S,~C,~R,~E,~descriptor,world,target,DescendantOrder{order}) def store_root(~S:Data,~C:Type,store:A.Store,descriptor:G.Descriptor,target:W.Handle) -> A.Store & Maybe<&2,W.Handle>: match store target: case A.Store{components,+graph} W.Handle{namespace,+id}: (A.Store{components,graph},P.handle(~S,T.root(T.ancestors(~S,graph,descriptor,id),id),namespace)) def checked_root(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,observed:W.World,R,A.Notice> & Bool,target:W.Handle) -> W.World,R,A.Notice> & Maybe<&2,W.Handle>: match observed: case (world,False{}): (world,None{}) case (world,True{}): W.with_store(~S,~A.Store,~R,~A.Notice,~Maybe<&2,W.Handle>,store => store_root(~S,~C,store,Reorder.descriptor(~S,descriptor),target),world) def lookup_root(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,world:W.World,R,A.Notice>,target:W.Handle) -> W.World,R,A.Notice> & Maybe<&2,W.Handle>: match target: case W.Handle{+namespace,+id}: checked_root(~S,~C,~R,~E,~descriptor,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{namespace,id}),W.Handle{namespace,id}) def root(-H:Type,-S:Data,caps:Read,owner:H,target:W.Handle) -> H & Maybe<&2,W.Handle>: match caps: case Read{Cap.Request{get},_,_}: get(owner,target) def ancestors(-H:Type,-S:Data,caps:Read,owner:H,target:W.Handle) -> H & Maybe<&2,P.Path>: match caps: case Read{_,Cap.Request{get},_}: get(owner,target) def descendants(-H:Type,-S:Data,caps:Read,owner:H,target:W.Handle,order:T.Order) -> H & Maybe<&2,P.Path>: match caps: case Read{_,_,Cap.Request{get}}: get(owner,Descendants{target,order}) def grant(~S:Data,~C:Type,~R:Type,~E:Data,~descriptor:G.HierarchyDescriptor,~Out:Type,~body:@-H:Type -> Read -> W.Handle -> H -> H & Out,target:W.Handle,world:W.World,R,A.Notice>) -> W.World,R,A.Notice> & Out: body(W.World,R,A.Notice>,Read{Cap.Request{lookup_root(~S,~C,~R,~E,~descriptor)},Cap.Request{lookup_ancestors(~S,~C,~R,~E,~descriptor)},Cap.Request{lookup_descendants(~S,~C,~R,~E,~descriptor)}},target,world)