import Base import world.bend as W import system.bend as Sys import machine_handlers.bend as H import machine.bend as M # Selection metadata has nominal schema/machine indexes; actual registry is affine. type Selector is Data: ExitFrom{value:V} TransitionPair{from:V,to:V} EnterTo{value:V} type Scheduled -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Scheduled{selector:Selector,requirements:List<&2,Q>,entry:H.Entry} type Tree -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Empty{} Single{scheduled:Scheduled} Join{left:Tree,right:Tree} type Bundle -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Bundle{namespace:U32,entries:List<&1,Scheduled>} type Choice is Data: Kept{} SelectedExit{} SelectedTransition{} SelectedEnter{} type Position is Data: Position{selector:Selector,requirements:List<&2,Q>,choice:Choice} type Partition -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Partition{positions:List<&2,Position>,kept:List<&1,H.Entry>,owners:H.Owners} def flattened(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,tree:Tree) -> List<&1,Scheduled>: match tree: case Empty{}: [] case Single{scheduled}: [scheduled] case Join{left,right}: List.append(&1,Scheduled,flattened(~S,~K,~V,~Q,~C,~R,~E,~runner,left),flattened(~S,~K,~V,~Q,~C,~R,~E,~runner,right)) def bundle(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,namespace:U32,tree:Tree) -> Bundle: Bundle{namespace,flattened(~S,~K,~V,~Q,~C,~R,~E,~runner,tree)} def contains(~Q:Data,~same:Q -> Q -> Bool,items:List<&2,Q>,+value:Q) -> Bool: match items: case []: False{} case head <> tail: Bool.or(same(head,value),contains(~Q,~same,tail,value)) def collected(~Q:Data,seen:Bool,+prior:List<&2,Q>,value:Q) -> List<&2,Q>: match seen: case True{}: prior case False{}: List.append(&2,Q,prior,[value]) def collect(~Q:Data,~same:Q -> Q -> Bool,items:List<&2,Q>,+prior:List<&2,Q>) -> List<&2,Q>: match items: case []: prior case +head <> tail: collect(~Q,~same,tail,collected(~Q,contains(~Q,~same,prior,head),prior,head)) def requirements_join(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,selector:Selector,+needs:List<&2,Q>,entry:H.Entry,tail:List<&1,Scheduled> & List<&2,Q>) -> List<&1,Scheduled> & List<&2,Q>: (entries,all)=tail (Scheduled{selector,needs,entry} <> entries,List.append(&2,Q,needs,all)) def requirements(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,entries:List<&1,Scheduled>) -> List<&1,Scheduled> & List<&2,Q>: match entries: case []: ([],[]) case Scheduled{selector,+needs,entry} <> tail: requirements_join(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,needs,entry,requirements(~S,~K,~V,~Q,~C,~R,~E,~runner,tail)) def exit_choice(yes:Bool) -> Choice: match yes: case True{}: SelectedExit{} case False{}: Kept{} def transition_choice(yes:Bool) -> Choice: match yes: case True{}: SelectedTransition{} case False{}: Kept{} def enter_choice(yes:Bool) -> Choice: match yes: case True{}: SelectedEnter{} case False{}: Kept{} def choose(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,selector:Selector,from:V,to:V) -> Choice: match selector: case ExitFrom{value}: exit_choice(same(value,from)) case TransitionPair{old,new}: transition_choice(Bool.and(same(old,from),same(new,to))) case EnterTo{value}: enter_choice(same(value,to)) def placed(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,+choice:Choice,selector:Selector,needs:List<&2,Q>,entry:H.Entry,tail:Partition) -> Partition: match choice tail: case Kept{} Partition{positions,kept,owners}: Partition{Position{selector,needs,choice} <> positions,entry <> kept,owners} case SelectedExit{} Partition{positions,kept,H.Owners{exit,transition,enter}}: Partition{Position{selector,needs,choice} <> positions,kept,H.Owners{entry <> exit,transition,enter}} case SelectedTransition{} Partition{positions,kept,H.Owners{exit,transition,enter}}: Partition{Position{selector,needs,choice} <> positions,kept,H.Owners{exit,entry <> transition,enter}} case SelectedEnter{} Partition{positions,kept,H.Owners{exit,transition,enter}}: Partition{Position{selector,needs,choice} <> positions,kept,H.Owners{exit,transition,entry <> enter}} def partition(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~same:V -> V -> Bool,entries:List<&1,Scheduled>,+from:V,+to:V) -> Partition: match entries: case []: Partition{[],[],H.Owners{[],[],[]}} case Scheduled{+selector,needs,entry} <> tail: placed(~S,~K,~V,~Q,~C,~R,~E,~runner,choose(~S,~K,~V,~same,selector,from,to),selector,needs,entry,partition(~S,~K,~V,~Q,~C,~R,~E,~runner,~same,tail,from,to)) def choose_initial(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,selector:Selector,to:V) -> Choice: match selector: case EnterTo{value}:enter_choice(same(value,to)) case _:Kept{} def partition_initial(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~same:V -> V -> Bool,entries:List<&1,Scheduled>,+to:V) -> Partition: match entries: case []: Partition{[],[],H.Owners{[],[],[]}} case Scheduled{+selector,needs,entry} <> tail: placed(~S,~K,~V,~Q,~C,~R,~E,~runner,choose_initial(~S,~K,~V,~same,selector,to),selector,needs,entry,partition_initial(~S,~K,~V,~Q,~C,~R,~E,~runner,~same,tail,to)) # Recovery explicitly retains all owners even for malformed internal zippers. # Normal public execution constructs its own partition and never accepts a zipper. type Recovery -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Recovery{entries:List<&1,Scheduled>,positions:List<&2,Position>,kept:List<&1,H.Entry>,owners:H.Owners} type Status is Data: Completed{} HandlerFailed{phase:H.Phase,error:String} MissingRequirements{missing:List<&2,Q>} ForeignBundle{} MachineUnavailable{} type BundleResult -> H.Invocation -> Sys.Outcome,String,Unit>> is Type: Returned{bundle:Bundle,world:W.World,status:Status} InternalOwnerRemainder{namespace:U32,recovery:Recovery,world:W.World,status:Status} def recovery_join(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,selector:Selector,needs:List<&2,Q>,entry:H.Entry,tail:Recovery) -> Recovery: match tail: case Recovery{entries,positions,kept,owners}: Recovery{Scheduled{selector,needs,entry} <> entries,positions,kept,owners} def recover(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,positions:List<&2,Position>,kept:List<&1,H.Entry>,owners:H.Owners) -> Recovery: match positions kept owners: case [] kept owners: Recovery{[],[],kept,owners} case Position{selector,needs,Kept{}} <> rest head <> tail owners: recovery_join(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,needs,head,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,rest,tail,owners)) case Position{selector,needs,SelectedExit{}} <> rest kept H.Owners{head <> tail,transition,enter}: recovery_join(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,needs,head,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,rest,kept,H.Owners{tail,transition,enter})) case Position{selector,needs,SelectedTransition{}} <> rest kept H.Owners{exit,head <> tail,enter}: recovery_join(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,needs,head,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,rest,kept,H.Owners{exit,tail,enter})) case Position{selector,needs,SelectedEnter{}} <> rest kept H.Owners{exit,transition,head <> tail}: recovery_join(~S,~K,~V,~Q,~C,~R,~E,~runner,selector,needs,head,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,rest,kept,H.Owners{exit,transition,tail})) case positions kept owners: Recovery{[],positions,kept,owners} def recovered(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,namespace:U32,recovery:Recovery,world:W.World,status:Status) -> BundleResult: match recovery: case Recovery{entries,[],[],H.Owners{[],[],[]}}: Returned{Bundle{namespace,entries},world,status} case recovery: InternalOwnerRemainder{namespace,recovery,world,status} def finished(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,namespace:U32,positions:List<&2,Position>,kept:List<&1,H.Entry>,result:H.HandlerResult) -> BundleResult: match result: case H.Completed{owners,world}: recovered(~S,~K,~V,~Q,~C,~R,~E,~runner,namespace,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,positions,kept,owners),world,Completed{}) case H.Stopped{owners,world,phase,error}: recovered(~S,~K,~V,~Q,~C,~R,~E,~runner,namespace,recover(~S,~K,~V,~Q,~C,~R,~E,~runner,positions,kept,owners),world,HandlerFailed{phase,error}) def missing_selected(~Q:Data,allowed:Bool,value:Q,rest:List<&2,Q>) -> List<&2,Q>: match allowed: case True{}: rest case False{}: value <> rest def missing_join_with(~State:Type,~Q:Data,allowed:Bool,value:Q,pair:State & List<&2,Q>) -> State & List<&2,Q>: (world,rest)=pair (world,missing_selected(~Q,allowed,value,rest)) def missing_checked_with(~State:Type,~Q:Data,~has:State -> Q -> State & Bool,pair:State & Bool,+value:Q,next:State -> State & List<&2,Q>) -> State & List<&2,Q>: (world,allowed)=pair missing_join_with(~State,~Q,allowed,value,next(world)) def missing_with(~State:Type,~Q:Data,~has:State -> Q -> State & Bool,needs:List<&2,Q>,world:State) -> State & List<&2,Q>: match needs: case []: (world,[]) case +value <> rest: missing_checked_with(~State,~Q,~has,has(world,value),value,w => missing_with(~State,~Q,~has,rest,w)) def missing_join(~S:Data,~C:Type,~R:Type,~E:Data,~Q:Data,allowed:Bool,value:Q,pair:W.World & List<&2,Q>) -> W.World & List<&2,Q>: (world,rest)=pair (world,missing_selected(~Q,allowed,value,rest)) def missing_checked(~S:Data,~C:Type,~R:Type,~E:Data,~Q:Data,~has:W.World -> Q -> W.World & Bool,pair:W.World & Bool,+value:Q,next:W.World -> W.World & List<&2,Q>) -> W.World & List<&2,Q>: (world,allowed)=pair missing_join(~S,~C,~R,~E,~Q,allowed,value,next(world)) def missing(~S:Data,~C:Type,~R:Type,~E:Data,~Q:Data,~has:W.World -> Q -> W.World & Bool,needs:List<&2,Q>,world:W.World) -> W.World & List<&2,Q>: missing_with(~W.World,~Q,~has,needs,world) def partitioned_run(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,namespace:U32,world:W.World,captured:M.Snapshot,selected:Partition) -> BundleResult: match selected: case Partition{positions,kept,owners}: finished(~S,~K,~V,~Q,~C,~R,~E,~runner,namespace,positions,kept,H.run(~S,~C,~R,~E,~V,~runner,~K,~take,~put,~publish,~same,owners,world,captured)) def selected_target(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,namespace:U32,entries:List<&1,Scheduled>,world:W.World,+slot:M.Slot,+current:V,pending:M.Pending) -> BundleResult: match pending: case M.NoPending{}: Returned{Bundle{namespace,entries},put(world,M.clear_changed(~S,~K,~V,slot)),Completed{}} case M.Queued{+value,+skipSame}: partitioned_run(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,namespace,put(world,slot),M.Scheduled{value,skipSame},partition(~S,~K,~V,~Q,~C,~R,~E,~runner,~same,entries,current,value)) def selected_run(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,namespace:U32,entries:List<&1,Scheduled>,world:W.World,slot:M.Slot) -> BundleResult: match slot: case M.Missing{}: Returned{Bundle{namespace,entries},put(world,M.Missing{}),MachineUnavailable{}} case M.Present{+current,+pending,+previous,+changed}: selected_target(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,namespace,entries,world,M.Present{current,pending,previous,changed},current,pending) def taken_run(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,namespace:U32,entries:List<&1,Scheduled>,pair:W.World & M.Slot) -> BundleResult: (world,slot)=pair selected_run(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,namespace,entries,world,slot) def preflighted(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,namespace:U32,entries:List<&1,Scheduled>,checked:W.World & List<&2,Q>) -> BundleResult: match checked: case (world,[]): taken_run(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,namespace,entries,take(frame(world))) case (world,missing): Returned{Bundle{namespace,entries},world,MissingRequirements{missing}} def gathered(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,namespace:U32,world:W.World,pair:List<&1,Scheduled> & List<&2,Q>) -> BundleResult: (entries,needs)=pair preflighted(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,~requirement_same,~has,~frame,namespace,entries,missing(~S,~C,~R,~E,~Q,~has,collect(~Q,~requirement_same,needs,[]),world)) def namespace_checked(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,allowed:Bool,bundle:Bundle,world:W.World) -> BundleResult: match allowed bundle: case False{} bundle: Returned{bundle,world,ForeignBundle{}} case True{} Bundle{namespace,entries}: gathered(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,~requirement_same,~has,~frame,namespace,world,requirements(~S,~K,~V,~Q,~C,~R,~E,~runner,entries)) def namespace_join(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,+namespace:U32,entries:List<&1,Scheduled>,pair:W.World & U32) -> BundleResult: (world,actual)=pair namespace_checked(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,~requirement_same,~has,~frame,U32.is_eq(namespace,actual),Bundle{namespace,entries},world) def run(~S:Data,~K:Data,~V:Data,~Q:Data,~C:Type,~R:Type,~E:Data,~runner:W.World -> H.Invocation -> Sys.Outcome,String,Unit>,~take:W.World -> W.World & M.Slot,~put:W.World -> M.Slot -> W.World,~publish:W.World -> M.Transition -> W.World,~same:V -> V -> Bool,~requirement_same:Q -> Q -> Bool,~has:W.World -> Q -> W.World & Bool,~frame:W.World -> W.World,bundle:Bundle,world:W.World) -> BundleResult: match bundle: case Bundle{namespace,entries}: namespace_join(~S,~K,~V,~Q,~C,~R,~E,~runner,~take,~put,~publish,~same,~requirement_same,~has,~frame,namespace,entries,W.namespace(~S,~C,~R,~E,world))