import Base import ./entity_allocator.bend as A import ./relation_types.bend as T import ./execution_relation_operation.bend as O import ./execution_relation_interpreter.bend as I import ./execution_relation_failure_message.bend as Failure import ./execution_relation_failure_channel.bend as Reader import ./execution_stream_channel.bend as Channel import ./execution_composer_layout.bend as Layout import ./change_tick.bend as Tick # Constructors and mutation functions are trusted provisioning/barrier helpers. # Gameplay receives only operations over an abstract H, never this owner. type Store<-S:Data,-descriptor:T.Descriptor> is Type: Store{namespace:U32,graph:T.Graph,failures:Channel.Store>,sequence:Nat,nextReader:U32} type View<-S:Data,-descriptor:T.Descriptor> is Data: View{namespace:U32,graph:T.Graph,failures:Channel.Store>,sequence:Nat,nextReader:U32} type Reason is Data: NamespaceMismatch{} OperationRefused{reason:O.Reason} type Receipt is Data: Applied{operation:O.Operation} Refused{operation:O.Operation,reason:Reason} type Registration<-S:Data,-descriptor:T.Descriptor,-origin:Layout.Layout,-route:Layout.Route> is Type: Registered{store:Store,reader:Reader.Reader} RegistrationRefused{store:Store} def fresh(~S:Data,~descriptor:T.Descriptor,namespace:U32) -> Store: Store{namespace,T.Graph{[],[]},Channel.empty(~Failure.Message),0n,1} def project_generic(-S:Data,-descriptor:T.Descriptor,store:Store) -> Store & View: match store: case Store{+namespace,+graph,+failures,+sequence,+nextReader}:(Store{namespace,graph,failures,sequence,nextReader},View{namespace,graph,failures,sequence,nextReader}) def project(~S:Data,~descriptor:T.Descriptor,store:Store) -> Store & View: project_generic(S,descriptor,store) def published(~S:Data,~descriptor:T.Descriptor,namespace:U32,graph:T.Graph,nextReader:U32,allocator:A.Allocator,operation:O.Operation,reason:O.Reason,result:Nat & Channel.Store>) -> A.Allocator & (Store & Receipt): match result: case (sequence,failures):(allocator,(Store{namespace,graph,failures,sequence,nextReader},Refused{operation,OperationRefused{reason}})) def publish(~S:Data,~descriptor:T.Descriptor,namespace:U32,graph:T.Graph,failures:Channel.Store>,sequence:Nat,nextReader:U32,allocator:A.Allocator,operation:O.Operation,reason:O.Reason,message:Maybe<&2,Failure.Message>) -> A.Allocator & (Store & Receipt): match message: case None{}:(allocator,(Store{namespace,graph,failures,sequence,nextReader},Refused{operation,OperationRefused{reason}})) case Some{message}:published(~S,~descriptor,namespace,graph,nextReader,allocator,operation,reason,Channel.push(~Failure.Message,sequence,failures,message)) def interpreted(~S:Data,~descriptor:T.Descriptor,namespace:U32,failures:Channel.Store>,sequence:Nat,nextReader:U32,result:A.Allocator & O.Outcome) -> A.Allocator & (Store & Receipt): match result: case (allocator,O.Applied{graph,operation}):(allocator,(Store{namespace,graph,failures,sequence,nextReader},Applied{operation})) case (allocator,O.Refused{graph,+operation,+reason}):publish(~S,~descriptor,namespace,graph,failures,sequence,nextReader,allocator,operation,reason,Failure.refused(~S,~descriptor,operation,reason)) def admitted(~S:Data,~descriptor:T.Descriptor,same:Bool,+namespace:U32,allocator:A.Allocator,store:Store,operation:O.Operation) -> A.Allocator & (Store & Receipt): match same store: case False{} store:(allocator,(store,Refused{operation,NamespaceMismatch{}})) case True{} Store{_,graph,failures,sequence,nextReader}:interpreted(~S,~descriptor,namespace,failures,sequence,nextReader,I.apply(~S,~descriptor,namespace,allocator,graph,operation)) def apply(~S:Data,~descriptor:T.Descriptor,+namespace:U32,allocator:A.Allocator,store:Store,operation:O.Operation) -> A.Allocator & (Store & Receipt): match store: case Store{+storedNamespace,graph,failures,sequence,nextReader}:admitted(~S,~descriptor,U32.is_eq(namespace,storedNamespace),namespace,allocator,Store{storedNamespace,graph,failures,sequence,nextReader},operation) def register_checked(~S:Data,~descriptor:T.Descriptor,~origin:Layout.Layout,~route:Layout.Route,available:Bool,store:Store) -> Registration: match available store: case False{} store:RegistrationRefused{store} case True{} Store{+namespace,graph,failures,sequence,+nextReader}:Registered{Store{namespace,graph,failures,sequence,(nextReader + 1 : U32)},Reader.Reader{namespace,nextReader,0n}} def register(~S:Data,~descriptor:T.Descriptor,~origin:Layout.Layout,~route:Layout.Route,store:Store) -> Registration: match store: case Store{namespace,graph,failures,sequence,+nextReader}:register_checked(~S,~descriptor,~origin,~route,U32.is_lt(nextReader,4294967295),Store{namespace,graph,failures,sequence,nextReader}) def maintained(~S:Data,~descriptor:T.Descriptor,tick:Tick.Tick,store:Store) -> Store: match store: case Store{namespace,graph,failures,+sequence,nextReader}:Store{namespace,graph,Channel.maintain(~Failure.Message,sequence,tick,failures),sequence,nextReader}