import Base import ./world.bend as W import ./local.bend as Local import ./transaction.bend as T import ./compose.bend as Q import ./ordinary_query_declaration.bend as Decl import ./ordinary_capture_query.bend as O import ./captured_schedule_composition.bend as Comp type Leaf<-S:Data,-C:Type,-R:Type,-E:Data,-L:Type,-Ops:Type -> Type,-A:Data,-U:Data,-Out:Data,-query:Decl.Declaration,Ops(Q.Frame)>,-body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out))> is Type: Leaf{owner:O.Owner,args:A,enabled:Bool} def bind(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),owner:O.Owner,args:A,enabled:Bool) -> Leaf: Leaf{owner,args,enabled} def catalog(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),leaf:Leaf) -> Leaf & List<&2,Comp.Identity>: match leaf: case Leaf{O.Owner{Local.Instance{+ns,+id,+name,+access,capture},cursor},args,enabled}:(Leaf{O.Owner{Local.Instance{ns,id,name,access,capture},cursor},args,enabled},[Comp.Identity{ns,id,name,access}]) def returned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),args:A,enabled:Bool,result:O.Run,O.Owner,A,Q.Error,Out>) -> Comp.Outcome,Leaf,Q.Error>: match result: case O.Completed{world,owner,Done{_}}:Comp.Completed{world,Leaf{owner,args,enabled}} case O.Completed{world,owner,Fail{error}}:Comp.Failed{world,Leaf{owner,args,enabled},error} case O.Refused{world,owner,_}:Comp.Refused{world,Leaf{owner,args,enabled}} def skipped(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),args:A,result:O.Owner & W.World) -> Comp.Outcome,Leaf,Q.Error>: match result: case (owner,world):Comp.Skipped{world,Leaf{owner,args,False{}}} def selected(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),allowed:Bool,leaf:Leaf,world:W.World) -> Comp.Outcome,Leaf,Q.Error>: match allowed leaf: case False{}, leaf:Comp.Refused{world,leaf} case True{}, Leaf{owner,args,False{}}:skipped(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,args,O.skip(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,owner,world)) case True{}, Leaf{owner,+args,True{}}:returned(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,args,True{},O.run(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,owner,world,args)) def dispatch(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~Ops:Type -> Type,~A:Data,~U:Data,~Out:Data,~query:Decl.Declaration,Ops(Q.Frame)>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome & Out)),leaf:Leaf,world:W.World,id:U32) -> Comp.Outcome,Leaf,Q.Error>: match leaf: case Leaf{O.Owner{Local.Instance{ns,+actual,name,access,capture},cursor},args,enabled}:selected(~S,~C,~R,~E,~L,~Ops,~A,~U,~Out,~query,~body,U32.is_eq(actual,id),Leaf{O.Owner{Local.Instance{ns,actual,name,access,capture},cursor},args,enabled},world)