import Base import ./dependency_plan.bend as Plan import ./dependency_executor.bend as Exec import ./execution_owner_continuation.bend as Continuation type Registry<-World:Type,-Owners:Type,-Error:Data,-Out:Data,-dispatch:Owners -> World -> U32 -> Owners & (World & Exec.Invocation)> is Type: Registry{owners:Owners,nodes:List<&2,Plan.Node>} type Product is Type: Product{left:L,right:R,leftIds:List<&2,U32>} def ids(nodes:List<&2,Plan.Node>) -> List<&2,U32>: match nodes: case []:[] case Plan.Node{id,_} <> rest:id <> ids(rest) def contains(ids:List<&2,U32>,+id:U32) -> Bool: match ids: case []:False{} case first <> rest:Bool.or(U32.is_eq(first,id),contains(rest,id)) def left_returned(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),pair:(R & List<&2,U32>) & (L & (World & Exec.Invocation))) -> Product & (World & Exec.Invocation): match pair: case ((right,leftIds),(left,(world,status))):(Product{left,right,leftIds},(world,status)) def right_returned(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),pair:(L & List<&2,U32>) & (R & (World & Exec.Invocation))) -> Product & (World & Exec.Invocation): match pair: case ((left,leftIds),(right,(world,status))):(Product{left,right,leftIds},(world,status)) def paired_selected(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),selected:Bool,left:L,right:R,leftIds:List<&2,U32>,world:World,id:U32) -> Product & (World & Exec.Invocation): match selected: case True{}: continuation:Continuation.PairedHeader<(L & (World & Exec.Invocation)),(Product & (World & Exec.Invocation))> = Continuation.PairedHeader{R & List<&2,U32>,(right,leftIds),left_returned(~World,~L,~R,~Error,~Out,~leftDispatch,~rightDispatch)} Continuation.paired_returned(L & (World & Exec.Invocation),Product & (World & Exec.Invocation),continuation,leftDispatch(left,world,id)) case False{}: continuation:Continuation.PairedHeader<(R & (World & Exec.Invocation)),(Product & (World & Exec.Invocation))> = Continuation.PairedHeader{L & List<&2,U32>,(left,leftIds),right_returned(~World,~L,~R,~Error,~Out,~leftDispatch,~rightDispatch)} Continuation.paired_returned(R & (World & Exec.Invocation),Product & (World & Exec.Invocation),continuation,rightDispatch(right,world,id)) def paired_dispatch_actual(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),owners:Product,world:World,+id:U32) -> Product & (World & Exec.Invocation): match owners: case Product{left,right,+leftIds}:paired_selected(~World,~L,~R,~Error,~Out,~leftDispatch,~rightDispatch,contains(leftIds,id),left,right,leftIds,world,id) def paired_dispatch(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),owners:Product,world:World,id:U32) -> Product & (World & Exec.Invocation): paired_dispatch_actual(~World,~L,~R,~Error,~Out,~leftDispatch,~rightDispatch,owners,world,id) def paired(~World:Type,~L:Type,~R:Type,~Error:Data,~Out:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),left:Registry,right:Registry) -> Registry,Error,Out,paired_dispatch(~World,~L,~R,~Error,~Out,~leftDispatch,~rightDispatch)>: match left right: case Registry{left,+ln} Registry{right,rn}:Registry{Product{left,right,ids(ln)},List.append(&2,Plan.Node,ln,rn)} def empty_dispatch(~World:Type,~Error:Data,~Out:Data,owners:Unit,world:World,_:U32) -> Unit & (World & Exec.Invocation): (owners,(world,Exec.AdmissionRefused{})) def empty(~World:Type,~Error:Data,~Out:Data) -> Registry: Registry{Unit{},[]} type ConditionRegistry<-World:Type,-Owners:Type,-Error:Data,-dispatch:Owners -> World -> U32 -> Owners & (World & Exec.Invocation)> is Type: ConditionRegistry{registry:Registry,invalid:Maybe<&2,Plan.Invalid>} def condition_permission(allowed:Bool,id:U32) -> Maybe<&2,Plan.Invalid>: match allowed: case True{}:None{} case False{}:Some{Plan.InvalidCondition{id}} def empty_conditions(~World:Type,~Error:Data) -> ConditionRegistry: ConditionRegistry{empty(~World,~Error,~Bool),None{}} def paired_conditions(~World:Type,~L:Type,~R:Type,~Error:Data,~leftDispatch:L -> World -> U32 -> L & (World & Exec.Invocation),~rightDispatch:R -> World -> U32 -> R & (World & Exec.Invocation),left:ConditionRegistry,right:ConditionRegistry) -> ConditionRegistry,Error,paired_dispatch(~World,~L,~R,~Error,~Bool,~leftDispatch,~rightDispatch)>: match left right: case ConditionRegistry{left,li} ConditionRegistry{right,ri}:ConditionRegistry{paired(~World,~L,~R,~Error,~Bool,~leftDispatch,~rightDispatch,left,right),Plan.first_error(~String,li,ri)}