~/bend-docscommunity

ordinary_capture_query.bend source

ordinary_capture_query.bend on the hub · documented module

import Baseimport ./change_tick.bend as Tickimport ./world.bend as Wimport ./compose.bend as Qimport ./capture_query.bend as Foldimport ./ordinary_query_declaration.bend as Declimport ./ordinary_system.bend as Ordinaryimport ./local.bend as Localimport ./captures.bend as Captureimport ./transaction.bend as Timport ./commands.bend as Cmd# Capture lifecycle reuses Local; each completed run consumes its captured tick.# Advanced lowering of the closed query declaration into its typed grant# and membership predicate; it performs no traversal or capture mutation.def plan(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out))) -> Q.Plan<S,C,R,E,Ops>:  Q.Plan{Decl.grant(~S,~Q.Frame<S,C,R,E>,~Ops(Q.Frame<S,C,R,E>),query),Decl.selector(~S,~Q.Frame<S,C,R,E>,~Ops(Q.Frame<S,C,R,E>),query)}def finished(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,result:Fold.Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>) -> Local.Outcome<W.World<S,C,R,E>,L,Q.Error<U>,List<&2,Out>>:  match result:    case Fold.Executed{world,capture,T.Success{},values}:Local.Succeeded{world,capture,values}    case Fold.Executed{world,capture,T.Failure{error},_}:Local.Failed{world,capture,error}# Advanced captured-query runner with explicit (args,cursor). Threads L# through selected rows while retaining performed World changes.# Failure retains returned capture but discards row outputs. Bypasses runtime# registration checks; ordinary callers use define/run or App capture add.def runner(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),world:W.World<S,C,R,E>,capture:L,args:A & Maybe<&2,Tick.Tick>) -> Local.Outcome<W.World<S,C,R,E>,L,Q.Error<U>,List<&2,Out>>:  match args:    case (args,cursor):finished(~S,~C,~R,~E,~L,~U,~Out,Fold.each_since_with(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body),~body,~Cmd.tx_finish_preserving(~S,~C,~R,~E,~Q.Error<U>),args,cursor,capture,world))# Runtime-bound captured-query owner: a Local instance owns affine capture L# and a separate cursor tracks query lifecycle visibility. Keep both returned# parts together across runs; raw constructors are trusted administration.type Owner<-S:Data,-C:Type,-R:Type,-E:Data,-L:Type,-Ops:Type -> Type,-A:Data,-U:Data,-Out:Data,-query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,-body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out))> is Type:  Owner{instance:Local.Instance<S,C,R,E,L,A & Maybe<&2,Tick.Tick>,Q.Error<U>,List<&2,Out>,runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body)>,cursor:Maybe<&2,Tick.Tick>}# Registered returns World and captured-query owner; Rejected returns# World and initial capture L without invoking the query body.type Registration<World:Type,Owned:Type,L:Type> is Type:  Registered{world:World,owner:Owned}  Rejected{world:World,capture:L}# Completed returns World, retained capture owner and ordered row outputs# or query error. Refused returns World/owner and original Data args before# body execution. Capture state survives body failure; failed row outputs do not.type Run<World:Type,Owned:Type,A:Data,Error:Data,Out:Data> is Type:  Completed{world:World,owner:Owned,result:Result<Error,List<&2,Out>>}  Refused{world:World,owner:Owned,args:A}# Removed returns World and detached affine capture after deregistration.# RemovalRejected returns World and original owner. No finalizer is invoked# here: caller decides how to reuse or dispose the returned capture.type Removal<World:Type,Owned:Type,L:Type> is Type:  Removed{world:World,capture:L}  RemovalRejected{world:World,owner:Owned}def registered(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),result:Local.Registered<S,C,R,E,L,A & Maybe<&2,Tick.Tick>,Q.Error<U>,List<&2,Out>,runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body)>) -> Registration<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,L>:  match result:    case Local.Registered{world,instance}:Registered{world,Owner{instance,None{}}}    case Local.RegistrationRejected{world,capture}:Rejected{world,capture}# Register a captured query with access derived from the same declaration.# Consumes capture; refusal returns it with World. Success owns a real Local# registration and initializes an absent query cursor. No body runs at setup.# Example: examples/captured-query/app.bend.def define(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),world:W.World<S,C,R,E>,name:String,capture:L) -> Registration<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,L>:  registered(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,Capture.register(~S,~C,~R,~E,~L,~(A & Maybe<&2,Tick.Tick>),~Q.Error<U>,~List<&2,Out>,~runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body),world,name,Ordinary.access(~S,Decl.clauses(~S,~Q.Frame<S,C,R,E>,~Ops(Q.Frame<S,C,R,E>),query)),capture))def returned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),cursor:Maybe<&2,Tick.Tick>,result:Local.Run<S,C,R,E,L,A & Maybe<&2,Tick.Tick>,Q.Error<U>,List<&2,Out>,runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body)>) -> Run<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,A,Q.Error<U>,Out>:  match result:    case Local.Completed{world,instance,Done{values},tick}:Completed{world,Owner{instance,Some{tick}},Done{values}}    case Local.Completed{world,instance,Fail{error},tick}:Completed{world,Owner{instance,Some{tick}},Fail{error}}    case Local.Refused{world,instance,(args,_)}:Refused{world,Owner{instance,cursor},args}# Execute the checked captured-query registration at its retained cursor.# Completed success/error consume the captured execution tick; refusal preserves it.# Query failure retains performed World changes and returned capture changes;# registration refusal returns original args without running body. Deferred# commands remain invisible until explicit barriers.def run(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),owner:Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,world:W.World<S,C,R,E>,args:A) -> Run<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,A,Q.Error<U>,Out>:  match owner:    case Owner{instance,+cursor}:returned(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,cursor,Capture.run(~S,~C,~R,~E,~L,~(A & Maybe<&2,Tick.Tick>),~Q.Error<U>,~List<&2,Out>,~runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body),instance,world,(args,cursor)))# No callback entry: preserve capture and the existing query reader cursor.# Return the same captured owner and World without invoking the body.# Both capture state and query cursor remain unchanged.def skip(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),owner:Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,world:W.World<S,C,R,E>) -> Owner<S,C,R,E,L,Ops,A,U,Out,query,body> & W.World<S,C,R,E>:  (owner,world)def removed(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),cursor:Maybe<&2,Tick.Tick>,result:Local.Disposal<S,C,R,E,L,A & Maybe<&2,Tick.Tick>,Q.Error<U>,List<&2,Out>,runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body),L>) -> Removal<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,L>:  match result:    case Local.Disposed{world,capture}:Removed{world,capture}    case Local.DisposalRejected{world,instance}:RemovalRejected{world,Owner{instance,cursor}}# Remove the matching registration and return its actual affine capture.# Refusal returns the original owner for retry; success returns Removed with# World/capture. Does not invoke a user finalizer or reset unrelated systems.def unregister(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration<S,Q.Frame<S,C,R,E>,Ops(Q.Frame<S,C,R,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),owner:Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,world:W.World<S,C,R,E>) -> Removal<W.World<S,C,R,E>,Owner<S,C,R,E,L,Ops,A,U,Out,query,body>,L>:  match owner:    case Owner{instance,cursor}:removed(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,cursor,Capture.unregister(~S,~C,~R,~E,~L,~(A & Maybe<&2,Tick.Tick>),~Q.Error<U>,~List<&2,Out>,~runner(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body),instance,world))