import Base import ./world.bend as W import ./entity_key.bend as K import ./relation_types.bend as T import ./relation_graph.bend as Graph import ./execution_relation_store.bend as Store import ./execution_state.bend as State import ./execution_lifecycle_notices.bend as Notice type Lookup is Data: Value{value:V} MissingEntity{} NamespaceRefused{} type Reader<-S:Data,-H:Type> is Type: Reader{target:H -> W.Handle -> H & Lookup>>,inverse:H -> W.Handle -> H & Lookup>>} def Reads(~S:Data,H:Type) -> Type:Reader def namespace(~S:Data,view:Notice.View) -> U32: match view: case Notice.View{namespace,_,_}:namespace def handle(~S:Data,namespace:U32,value:Maybe<&2,K.Key>) -> Maybe<&2,W.Handle>: match value: case None{}:None{} case Some{key}:Some{W.Handle{namespace,key}} def handles(~S:Data,+namespace:U32,keys:List<&2,K.Key>) -> List<&2,W.Handle>: match keys: case []:[] case key <> rest:W.Handle{namespace,key} <> handles(~S,namespace,rest) def target_namespace(~S:Data,same:Bool,namespace:U32,value:Maybe<&2,K.Key>) -> Lookup>>: match same: case False{}:NamespaceRefused{} case True{}:Value{handle(~S,namespace,value)} def target_view(~S:Data,~descriptor:T.Descriptor,+expected:U32,source:W.Handle,view:Store.View) -> Lookup>>: match view: case Store.View{+actual,graph,_,_,_}:target_namespace(~S,U32.is_eq(expected,actual),actual,Graph.target(~S,graph,descriptor,W.handle_key(~S,source))) def inverse_namespace(~S:Data,same:Bool,namespace:U32,keys:List<&2,K.Key>) -> Lookup>>: match same: case False{}:NamespaceRefused{} case True{}:Value{handles(~S,namespace,keys)} def inverse_view(~S:Data,~descriptor:T.Descriptor,+expected:U32,target:W.Handle,view:Store.View) -> Lookup>>: match view: case Store.View{+actual,graph,_,_,_}:inverse_namespace(~S,U32.is_eq(expected,actual),actual,Graph.inverse(~S,graph,descriptor,W.handle_key(~S,target))) def target_returned(~S:Data,~descriptor:T.Descriptor,~H:Type,expected:U32,source:W.Handle,result:H & Store.View) -> H & Lookup>>: match result: case (owner,view):(owner,target_view(~S,~descriptor,expected,source,view)) def inverse_returned(~S:Data,~descriptor:T.Descriptor,~H:Type,expected:U32,target:W.Handle,result:H & Store.View) -> H & Lookup>>: match result: case (owner,view):(owner,inverse_view(~S,~descriptor,expected,target,view)) def target_checked(~S:Data,~descriptor:T.Descriptor,~C:Type,~R:Type,~view:State.Scope -> State.Scope & Store.View,valid:Bool,expected:U32,scope:State.Scope,source:W.Handle) -> State.Scope & Lookup>>: match valid: case False{}:(scope,MissingEntity{}) case True{}:target_returned(~S,~descriptor,~State.Scope,expected,source,view(scope)) def inverse_checked(~S:Data,~descriptor:T.Descriptor,~C:Type,~R:Type,~view:State.Scope -> State.Scope & Store.View,valid:Bool,expected:U32,scope:State.Scope,target:W.Handle) -> State.Scope & Lookup>>: match valid: case False{}:(scope,MissingEntity{}) case True{}:inverse_returned(~S,~descriptor,~State.Scope,expected,target,view(scope)) def target(~S:Data,~descriptor:T.Descriptor,~C:Type,~R:Type,~view:State.Scope -> State.Scope & Store.View,scope:State.Scope,+source:W.Handle) -> State.Scope & Lookup>>: match scope: case State.Scope{columns,resources,+live,tick,prior,+notice}:target_checked(~S,~descriptor,~C,~R,~view,State.member(~S,source,live),namespace(~S,notice),State.Scope{columns,resources,live,tick,prior,notice},source) def inverse(~S:Data,~descriptor:T.Descriptor,~C:Type,~R:Type,~view:State.Scope -> State.Scope & Store.View,scope:State.Scope,+target:W.Handle) -> State.Scope & Lookup>>: match scope: case State.Scope{columns,resources,+live,tick,prior,+notice}:inverse_checked(~S,~descriptor,~C,~R,~view,State.member(~S,target,live),namespace(~S,notice),State.Scope{columns,resources,live,tick,prior,notice},target) # Only the closed provider supplies view. Gameplay receives no store owner, # graph setter, payload transform, inverse mutation, or concrete Scope type. def reader(~S:Data,~descriptor:T.Descriptor,~C:Type,~R:Type,~view:State.Scope -> State.Scope & Store.View) -> Reader>: Reader{scope => source => target(~S,~descriptor,~C,~R,~view,scope,source),scope => target => inverse(~S,~descriptor,~C,~R,~view,scope,target)} def target_field(~S:Data,~H:Type,cap:Reader) -> H -> W.Handle -> H & Lookup>>: match cap: case Reader{target,_}:target def inverse_field(~S:Data,~H:Type,cap:Reader) -> H -> W.Handle -> H & Lookup>>: match cap: case Reader{_,inverse}:inverse