import Base import ./change_tick.bend as Tick import ./decode_frame.bend as F import ./decode_requests.bend as Request import ./compose.bend as Query import ./world.bend as World import ./transaction.bend as T import ./capabilities.bend as Cap import ./decode_data.bend as D import ./ordinary_declaration.bend as Identity # Trusted traversal kernel. A is an explicit application-owned state threaded # once across selected rows; the Local adapter retains it on system failure. # Data row observations stay commit-only. This is not a new captures policy. def completed(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~A:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,next:F.Frame -> A -> List<&2,Out> -> F.Frame & (A & (T.Outcome> & List<&2,Out>)),frame:F.Frame,state:A,values:List<&2,Out>,result:T.Outcome>) -> F.Frame & (A & (T.Outcome> & List<&2,Out>)): match result: case T.Failure{error}:(frame,(state,(T.Failure{error},[]))) case T.Success{}:next(frame,state,values) def returned(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~A:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,next:F.Frame -> A -> List<&2,Out> -> F.Frame & (A & (T.Outcome> & List<&2,Out>)),values:List<&2,Out>,observed:F.Frame & (A & (T.Outcome & Out))) -> F.Frame & (A & (T.Outcome> & List<&2,Out>)): match observed: case (F.Frame{owner,handle,+error,cursor,receipts,pending},(state,(outcome,value))):completed(~S,~C,~R,~E,~P,~Q,~A,~U,~Out,~Ops,~identity,~project,next,F.Frame{owner,handle,error,cursor,receipts,pending},state,value <> values,Query.outcome(~U,error,outcome)) def selected(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~A:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~grants:Ops(F.Frame),~body:@-H:Type -> @-caps:Ops(H) -> A -> H -> H & (A & (T.Outcome & Out)),next:F.Frame -> A -> List<&2,Out> -> F.Frame & (A & (T.Outcome> & List<&2,Out>)),state:A,values:List<&2,Out>,observed:F.Frame & Bool) -> F.Frame & (A & (T.Outcome> & List<&2,Out>)): match observed: case (frame,False{}):next(frame,state,values) case (frame,True{}):returned(~S,~C,~R,~E,~P,~Q,~A,~U,~Out,~Ops,~identity,~project,next,values,Cap.invoke_owned(~Ops,~A,~(A & (T.Outcome & Out)),~body,~F.Frame,~grants,state,frame)) def at_handle(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,frame:F.Frame,handle:World.Handle,cursor:Maybe<&2,Tick.Tick>) -> F.Frame: match frame: case F.Frame{owner,_,_,_,receipts,pending}:F.Frame{owner,handle,None{},cursor,receipts,pending} def run_rows(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Q:Type,~A:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,~selector:F.Frame -> F.Frame & Bool,~grants:Ops(F.Frame),~body:@-H:Type -> @-caps:Ops(H) -> A -> H -> H & (A & (T.Outcome & Out)),handles:List<&2,World.Handle>,+cursor:Maybe<&2,Tick.Tick>,frame:F.Frame,state:A,values:List<&2,Out>) -> F.Frame & (A & (T.Outcome> & List<&2,Out>)): match handles: case []:(frame,(state,(T.Success{},List.reverse(&2,Out,values)))) case handle <> rest:selected(~S,~C,~R,~E,~P,~Q,~A,~U,~Out,~Ops,~identity,~project,~grants,~body,(frame => state => values => run_rows(~S,~C,~R,~E,~P,~Q,~A,~U,~Out,~Ops,~identity,~project,~selector,~grants,~body,rest,cursor,frame,state,values)),state,values,selector(at_handle(~S,~C,~R,~E,~P,~Q,~identity,~project,frame,handle,cursor)))