import Base import ./change_tick.bend as Tick import world.bend as W import transaction.bend as T import commands.bend as Cmd import compose.bend as Q import capabilities.bend as Cap # Trusted declaration; gameplay can only thread universally quantified H. type Plan<-S:Data,-Store:Type,-R:Type,-E:Data,-Ops:Type -> Type,-H:Type> is Type: Plan{caps:Ops(H),select:Q.Frame -> Q.Frame & Bool,enter:Q.Frame -> H,leave:H -> Q.Frame} def plan_caps(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~H:Type,plan:Plan) -> Ops(H): match plan: case Plan{caps,_,_,_}: caps def plan_select(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~H:Type,plan:Plan) -> Q.Frame -> Q.Frame & Bool: match plan: case Plan{_,select,_,_}: select def plan_enter(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~H:Type,plan:Plan) -> Q.Frame -> H: match plan: case Plan{_,_,enter,_}: enter def plan_leave(~S:Data,~Store:Type,~R:Type,~E:Data,~Ops:Type -> Type,~H:Type,plan:Plan) -> H -> Q.Frame: match plan: case Plan{_,_,_,leave}: leave def left(~S:Data,~Store:Type,~R:Type,~E:Data,~H:Type,~U:Data,~Out:Data,~leave:H -> Q.Frame,observed:H & (T.Outcome & Out)) -> Q.Frame & (T.Outcome & Out): match observed: case (owner,result): (leave(owner),result) def prepend(~S: Data,~Store: Type,~R: Type,~E: Data,~U: Data,~Out: Data,value: Out,observed: T.Tx,E> & (T.Outcome> & List<&2,Out>)) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): (tx,result) = observed (outcome,values) = result (tx,(outcome,value <> values)) def completed(~S: Data,~Store: Type,~R: Type,~E: Data,~U: Data,~Out: Data,next: T.Tx,E> -> T.Tx,E> & (T.Outcome> & List<&2,Out>),tx: T.Tx,E>,value: Out,result: T.Outcome>) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match result: case T.Success{}: prepend(~S,~Store,~R,~E,~U,~Out,value,next(tx)) case T.Failure{error}: (tx,(T.Failure{error},[])) def body_returned(~S: Data,~Store: Type,~R: Type,~E: Data,~U: Data,~Out: Data,next: T.Tx,E> -> T.Tx,E> & (T.Outcome> & List<&2,Out>),observed: Q.Frame & (T.Outcome & Out)) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match observed: case (Q.Frame{tx,_,internal,_},(user,value)): completed(~S,~Store,~R,~E,~U,~Out,next,tx,value,Q.outcome(~U,internal,user)) def selected(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,next: T.Tx,E> -> T.Tx,E> & (T.Outcome> & List<&2,Out>),observed: Q.Frame & Bool) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match observed: case (Q.Frame{tx,_,_,_},False{}): next(tx) case (frame,True{}): body_returned(~S,~Store,~R,~E,~U,~Out,next,left(~S,~Store,~R,~E,~H,~U,~Out,~plan_leave(~S,~Store,~R,~E,~Ops,~H,plan),Cap.invoke(~Ops,~A,~(T.Outcome & Out),~body,~H,~plan_caps(~S,~Store,~R,~E,~Ops,~H,plan),args,plan_enter(~S,~Store,~R,~E,~Ops,~H,plan)(frame)))) def selected_row(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,observed: Q.Frame & Bool) -> T.Tx,E> & Q.RowResult: match observed: case (Q.Frame{tx,_,_,_},False{}): (tx,Q.Skipped{}) case (frame,True{}): Q.row_body_returned(~S,~Store,~R,~E,~U,~Out,left(~S,~Store,~R,~E,~H,~U,~Out,~plan_leave(~S,~Store,~R,~E,~Ops,~H,plan),Cap.invoke(~Ops,~A,~(T.Outcome & Out),~body,~H,~plan_caps(~S,~Store,~R,~E,~Ops,~H,plan),args,plan_enter(~S,~Store,~R,~E,~Ops,~H,plan)(frame)))) # Each row returns before the next; roster length does not grow the JS stack. def run_handles_acc(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),handles: List<&2,W.Handle>,+args: A,+cursor: Maybe<&2,Tick.Tick>,values: List<&2,Out>,observed: T.Tx,E> & Q.RowResult) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match handles observed: case _ Tuple{tx,Q.Failed{error}}: (tx,(T.Failure{error},List.reverse(&2,Out,values))) case Nil{} Tuple{tx,Q.Skipped{}}: (tx,(T.Success{},List.reverse(&2,Out,values))) case Nil{} Tuple{tx,Q.Passed{value}}: (tx,(T.Success{},List.reverse(&2,Out,value <> values))) case Con{handle,rest} Tuple{tx,Q.Skipped{}}: run_handles_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,rest,args,cursor,values,selected_row(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,plan_select(~S,~Store,~R,~E,~Ops,~H,plan)(Q.Frame{tx,handle,None{},cursor}))) case Con{handle,rest} Tuple{tx,Q.Passed{value}}: run_handles_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,rest,args,cursor,value <> values,selected_row(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,plan_select(~S,~Store,~R,~E,~Ops,~H,plan)(Q.Frame{tx,handle,None{},cursor}))) def run_handles(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),handles: List<&2,W.Handle>,+args: A,+cursor:Maybe<&2,Tick.Tick>,tx: T.Tx,E>) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): run_handles_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,handles,args,cursor,[],(tx,Q.Skipped{})) def finish_returned(~S: Data,~Store: Type,~R: Type,~E: Data,~U: Data,~Out: Data,values: List<&2,Out>,returned: T.Returned,Q.Error>) -> Q.Executed,Q.Error,Out>: match returned: case T.Returned{world,T.Success{}}: Q.Executed{world,T.Success{},values} case T.Returned{world,T.Failure{error}}: Q.Executed{world,T.Failure{error},[]} def finished(~S: Data,~Store: Type,~R: Type,~E: Data,~U: Data,~Out: Data,observed: T.Tx,E> & (T.Outcome> & List<&2,Out>)) -> Q.Executed,Q.Error,Out>: match observed: case (tx,(result,values)): finish_returned(~S,~Store,~R,~E,~U,~Out,values,Cmd.tx_finish(~S,~Store,~R,~E,~Q.Error,tx,result)) def scope_finished(~S:Data,~Store:Type,~R:Type,~E:Data,~U:Data,~Out:Data,owned:Bool,result:Q.Executed,Q.Error,Out>) -> Q.Executed,Q.Error,Out>: match result: case Q.Executed{world,outcome,values}:Q.Executed{W.finish_run(~S,~Store,~R,~E,owned,world),outcome,values} def reserved_inner(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,observed: W.World & List<&2,W.Handle>) -> Q.Executed,Q.Error,Out>: (world,handles) = observed finished(~S,~Store,~R,~E,~U,~Out,run_handles(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,handles,args,cursor,T.begin(~W.World,~E,world))) # Snapshot live handles in ascending order; deferred commands stay invisible. def reserved_scoped(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,handles:List<&2,W.Handle>,admitted:W.World & Bool) -> Q.Executed,Q.Error,Out>: (world,owned)=admitted scope_finished(~S,~Store,~R,~E,~U,~Out,owned,reserved_inner(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,cursor,(world,handles))) def reserved(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,observed: W.World & List<&2,W.Handle>) -> Q.Executed,Q.Error,Out>: (world,handles)=observed reserved_scoped(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,cursor,handles,W.ensure_run(~S,~Store,~R,~E,world)) def each(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,world: W.World) -> Q.Executed,Q.Error,Out>: reserved(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,None{},W.handles(~S,~Store,~R,~E,world)) # Preserve explicit reader cursor in the accepted row frame. def each_since(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~plan: Plan,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,world: W.World,cursor:Maybe<&2,Tick.Tick>) -> Q.Executed,Q.Error,Out>: reserved(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~plan,~body,args,cursor,W.handles(~S,~Store,~R,~E,world)) # Separate closed template fields avoid constructing runtime Plan for projections. def selected_fields(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,next: T.Tx,E> -> T.Tx,E> & (T.Outcome> & List<&2,Out>),observed: Q.Frame & Bool) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match observed: case (Q.Frame{tx,_,_,_},False{}): next(tx) case (frame,True{}): body_returned(~S,~Store,~R,~E,~U,~Out,next,left(~S,~Store,~R,~E,~H,~U,~Out,~leave,Cap.invoke(~Ops,~A,~(T.Outcome & Out),~body,~H,~caps,args,enter(frame)))) def selected_fields_row(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,observed: Q.Frame & Bool) -> T.Tx,E> & Q.RowResult: match observed: case (Q.Frame{tx,_,_,_},False{}): (tx,Q.Skipped{}) case (frame,True{}): Q.row_body_returned(~S,~Store,~R,~E,~U,~Out,left(~S,~Store,~R,~E,~H,~U,~Out,~leave,Cap.invoke(~Ops,~A,~(T.Outcome & Out),~body,~H,~caps,args,enter(frame)))) def run_handles_fields_acc(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),handles: List<&2,W.Handle>,+args: A,+cursor: Maybe<&2,Tick.Tick>,values: List<&2,Out>,observed: T.Tx,E> & Q.RowResult) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): match handles observed: case _ Tuple{tx,Q.Failed{error}}: (tx,(T.Failure{error},List.reverse(&2,Out,values))) case Nil{} Tuple{tx,Q.Skipped{}}: (tx,(T.Success{},List.reverse(&2,Out,values))) case Nil{} Tuple{tx,Q.Passed{value}}: (tx,(T.Success{},List.reverse(&2,Out,value <> values))) case Con{handle,rest} Tuple{tx,Q.Skipped{}}: run_handles_fields_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,rest,args,cursor,values,selected_fields_row(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,select(Q.Frame{tx,handle,None{},cursor}))) case Con{handle,rest} Tuple{tx,Q.Passed{value}}: run_handles_fields_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,rest,args,cursor,value <> values,selected_fields_row(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,select(Q.Frame{tx,handle,None{},cursor}))) def run_handles_fields(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),handles: List<&2,W.Handle>,+args: A,+cursor:Maybe<&2,Tick.Tick>,tx: T.Tx,E>) -> T.Tx,E> & (T.Outcome> & List<&2,Out>): run_handles_fields_acc(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,handles,args,cursor,[],(tx,Q.Skipped{})) def reserved_fields_inner(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,observed: W.World & List<&2,W.Handle>) -> Q.Executed,Q.Error,Out>: (world,handles) = observed finished(~S,~Store,~R,~E,~U,~Out,run_handles_fields(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,handles,args,cursor,T.begin(~W.World,~E,world))) # Snapshot live handles in ascending order; deferred commands stay invisible. def reserved_fields_scoped(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,handles:List<&2,W.Handle>,admitted:W.World & Bool) -> Q.Executed,Q.Error,Out>: (world,owned)=admitted scope_finished(~S,~Store,~R,~E,~U,~Out,owned,reserved_fields_inner(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,cursor,(world,handles))) def reserved_fields(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,cursor:Maybe<&2,Tick.Tick>,observed: W.World & List<&2,W.Handle>) -> Q.Executed,Q.Error,Out>: (world,handles)=observed reserved_fields_scoped(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,cursor,handles,W.ensure_run(~S,~Store,~R,~E,world)) def each_fields(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,world: W.World) -> Q.Executed,Q.Error,Out>: reserved_fields(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,None{},W.handles(~S,~Store,~R,~E,world)) # Preserve explicit reader cursor in the accepted row frame. def each_fields_since(~S: Data,~Store: Type,~R: Type,~E: Data,~Ops: Type -> Type,~H: Type,~A: Data,~U: Data,~Out: Data,~caps:Ops(H),~select:Q.Frame -> Q.Frame & Bool,~enter:Q.Frame -> H,~leave:H -> Q.Frame,~body: @-H: Type -> @-caps: Ops(H) -> A -> H -> H & (T.Outcome & Out),args: A,world: W.World,cursor:Maybe<&2,Tick.Tick>) -> Q.Executed,Q.Error,Out>: reserved_fields(~S,~Store,~R,~E,~Ops,~H,~A,~U,~Out,~caps,~select,~enter,~leave,~body,args,cursor,W.handles(~S,~Store,~R,~E,world))