import Base import entity_key.bend as Key import entity_allocator.bend as Alloc import relation_types.bend as G import relation_graph.bend as C import relation_commands.bend as A import world.bend as W # Explicit experiment continuation. Exhaustion retains every affine owner/frame. type CleanupResult is Type: Complete{world: W.World,R,A.Notice>} Incomplete{world: W.World,R,A.Notice>,frames: List<&2,G.CleanupFrame>} def snapshot(~S: Data,~C: Type,store: A.Store,descriptor: G.Descriptor,id:Key.Key) -> A.Store & List<&2,Key.Key>: A.store_inverse(~S,~C,store,descriptor,id) def clear_join(~S:Data,~C:Type,~E:Data,cleared:C & List<&2,A.Notice>,graph:G.Graph) -> A.Store & List<&2,A.Notice>: (components,notices) = cleared (A.Store{components,graph},notices) def clear_store(~S: Data,~C: Type,~E: Data,~clear: C -> W.Handle -> C & List<&2,A.Notice>,store: A.Store,handle: W.Handle) -> A.Store & List<&2,A.Notice>: match store: case A.Store{components,graph}: clear_join(~S,~C,~E,clear(components,handle),graph) def publish_final(~S:Data,~C:Type,~R:Type,~E:Data,~record:W.Handle -> A.Notice,deactivated:W.World,R,A.Notice> & W.Status,notices:List<&2,A.Notice>,+handle:W.Handle) -> W.World,R,A.Notice>: (world,_) = deactivated W.release_cleared(~S,~A.Store,~R,~A.Notice,W.event_publish(~S,~A.Store,~R,~A.Notice,world,List.append(&2,A.Notice,notices,[record(handle)])),handle) def finalized(~S: Data,~C: Type,~R: Type,~E: Data,~record: W.Handle -> A.Notice,cleared: W.World,R,A.Notice> & List<&2,A.Notice>,+handle: W.Handle) -> W.World,R,A.Notice>: (world,notices) = cleared publish_final(~S,~C,~R,~E,~record,W.deactivate(~S,~A.Store,~R,~A.Notice,world,handle),notices,handle) def finish_live(~S: Data,~C: Type,~R: Type,~E: Data,~clear: C -> W.Handle -> C & List<&2,A.Notice>,~record: W.Handle -> A.Notice,world: W.World,R,A.Notice>,+handle: W.Handle) -> W.World,R,A.Notice>: finalized(~S,~C,~R,~E,~record,W.with_store(~S,~A.Store,~R,~A.Notice,~List<&2,A.Notice>,store => clear_store(~S,~C,~E,~clear,store,handle),world),handle) # A paused continuation may outlive its full entity key. Validate before clear. def finish_checked(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,checked:W.World,R,A.Notice> & Bool,handle:W.Handle) -> W.World,R,A.Notice>: match checked: case (world,False{}):world case (world,True{}):finish_live(~S,~C,~R,~E,~clear,~record,world,handle) def finish(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,world:W.World,R,A.Notice>,+handle:W.Handle) -> W.World,R,A.Notice>: finish_checked(~S,~C,~R,~E,~clear,~record,W.valid(~S,~A.Store,~R,~A.Notice,world,handle),handle) def entered(~S: Data,~C: Type,~R: Type,~E: Data,checked: W.World,R,A.Notice> & Bool,id:Key.Key,descriptors: List<&2,G.Descriptor>,frames: List<&2,G.CleanupFrame>) -> W.World,R,A.Notice> & List<&2,G.CleanupFrame>: match checked: case (world,False{}): (world,frames) case (world,True{}): (world,G.CleanupDescriptors{id,descriptors} <> frames) def children_start(~S: Data,~C: Type,~R: Type,~E: Data,observed: W.World,R,A.Notice> & List<&2,Key.Key>,id:Key.Key,descriptor: G.Descriptor,remaining: List<&2,G.Descriptor>,frames: List<&2,G.CleanupFrame>) -> W.World,R,A.Notice> & List<&2,G.CleanupFrame>: (world,children) = observed (world,G.CleanupChildren{id,descriptor,remaining,children} <> frames) def child_step(~S: Data,~C: Type,~R: Type,~E: Data,kind: G.RelationKind,world: W.World,R,A.Notice>,namespace: U32,id:Key.Key,+descriptor: G.Descriptor,remaining: List<&2,G.Descriptor>,child:Key.Key,children: List<&2,Key.Key>,frames: List<&2,G.CleanupFrame>) -> W.World,R,A.Notice> & List<&2,G.CleanupFrame>: match kind: case G.Hierarchy{}: (world,G.CleanupEnter{child} <> G.CleanupChildren{id,descriptor,remaining,children} <> frames) case G.Ordinary{}: (A.apply_unrelate(~S,~C,~R,~E,world,descriptor,W.Handle{namespace,child}),G.CleanupChildren{id,descriptor,remaining,children} <> frames) def kind(~S: Data,d: G.Descriptor) -> G.RelationKind: match d: case G.Descriptor{_,_,_,kind}: kind def step(~S: Data,~C: Type,~R: Type,~E: Data,~clear: C -> W.Handle -> C & List<&2,A.Notice>,~record: W.Handle -> A.Notice,frame: G.CleanupFrame,world: W.World,R,A.Notice>,+namespace: U32,+descriptors: List<&2,G.Descriptor>,frames: List<&2,G.CleanupFrame>) -> W.World,R,A.Notice> & List<&2,G.CleanupFrame>: match frame: case G.CleanupEnter{+id}: entered(~S,~C,~R,~E,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{namespace,id}),id,descriptors,frames) case G.CleanupDescriptors{id,[]}: (world,G.CleanupFinish{id} <> frames) case G.CleanupDescriptors{+id,+d <> remaining}: children_start(~S,~C,~R,~E,W.with_store(~S,~A.Store,~R,~A.Notice,~List<&2,Key.Key>,store => snapshot(~S,~C,store,d,id),world),id,d,remaining,frames) case G.CleanupChildren{+id,d,remaining,[]}: (A.apply_unrelate(~S,~C,~R,~E,world,d,W.Handle{namespace,id}),G.CleanupDescriptors{id,remaining} <> frames) case G.CleanupChildren{id,+d,remaining,child <> children}: child_step(~S,~C,~R,~E,kind(~S,d),world,namespace,id,d,remaining,child,children,frames) case G.CleanupFinish{id}: (finish(~S,~C,~R,~E,~clear,~record,world,W.Handle{namespace,id}),frames) def resumed(~S:Data,~C:Type,~R:Type,~E:Data,observed:W.World,R,A.Notice> & List<&2,G.CleanupFrame>,next:W.World,R,A.Notice> -> List<&2,G.CleanupFrame> -> CleanupResult) -> CleanupResult: (world,frames) = observed next(world,frames) def drive(~S: Data,~C: Type,~R: Type,~E: Data,~clear: C -> W.Handle -> C & List<&2,A.Notice>,~record: W.Handle -> A.Notice,fuel: Nat,frames: List<&2,G.CleanupFrame>,world: W.World,R,A.Notice>,+namespace: U32,+descriptors: List<&2,G.Descriptor>) -> CleanupResult: match fuel frames: case _ Nil{}: Complete{world} case 0n frames: Incomplete{world,frames} case 1n+rest Con{frame,frames}: resumed(~S,~C,~R,~E,step(~S,~C,~R,~E,~clear,~record,frame,world,namespace,descriptors,frames),world => frames => drive(~S,~C,~R,~E,~clear,~record,rest,frames,world,namespace,descriptors)) # Structural product budget. No scalar Nat aggregate/product is computed. # Small units retain one affine World+continuation and short-circuit Complete. def units(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,fuel:Nat,result:CleanupResult,namespace:U32,descriptors:List<&2,G.Descriptor>) -> CleanupResult: match result: case Complete{world}: Complete{world} case Incomplete{world,frames}: drive(~S,~C,~R,~E,~clear,~record,fuel,frames,world,namespace,descriptors) def edge_units(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,remaining:List<&2,G.Edge>,result:CleanupResult,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: match remaining result: case Nil{} result: result case Con{_,rest} Complete{world}: Complete{world} case Con{_,rest} result: edge_units(~S,~C,~R,~E,~clear,~record,rest,units(~S,~C,~R,~E,~clear,~record,2n,result,namespace,descriptors),namespace,descriptors) def descriptor_units(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,remaining:List<&2,G.Descriptor>,result:CleanupResult,edges:List<&2,G.Edge>,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: match remaining result: case Nil{} result: edge_units(~S,~C,~R,~E,~clear,~record,edges,result,namespace,descriptors) case Con{_,rest} Complete{world}: Complete{world} case Con{_,rest} result: descriptor_units(~S,~C,~R,~E,~clear,~record,rest,units(~S,~C,~R,~E,~clear,~record,2n,result,namespace,descriptors),edges,namespace,descriptors) def block(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,result:CleanupResult,edges:List<&2,G.Edge>,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: descriptor_units(~S,~C,~R,~E,~clear,~record,descriptors,units(~S,~C,~R,~E,~clear,~record,3n,result,namespace,descriptors),edges,namespace,descriptors) def node_blocks(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,remaining:Nat,result:CleanupResult,+edges:List<&2,G.Edge>,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: match remaining result: case _ Complete{world}: Complete{world} case 0n result: block(~S,~C,~R,~E,~clear,~record,result,edges,namespace,descriptors) case 1n+rest result: node_blocks(~S,~C,~R,~E,~clear,~record,rest,block(~S,~C,~R,~E,~clear,~record,result,edges,namespace,descriptors),edges,namespace,descriptors) def phase_blocks(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,remaining:List<&2,G.Edge>,result:CleanupResult,nodes:Nat,+edges:List<&2,G.Edge>,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: match remaining result: case _ Complete{world}: Complete{world} case Nil{} result: node_blocks(~S,~C,~R,~E,~clear,~record,nodes,result,edges,namespace,descriptors) case Con{_,rest} result: phase_blocks(~S,~C,~R,~E,~clear,~record,rest,block(~S,~C,~R,~E,~clear,~record,result,edges,namespace,descriptors),nodes,edges,namespace,descriptors) def owner_blocks(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,remaining:Nat,result:CleanupResult,+nodes:Nat,+edges:List<&2,G.Edge>,+namespace:U32,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: match remaining result: case _ Complete{world}: Complete{world} case 0n result: result case 1n+rest result: owner_blocks(~S,~C,~R,~E,~clear,~record,rest,phase_blocks(~S,~C,~R,~E,~clear,~record,edges,result,nodes,edges,namespace,descriptors),nodes,edges,namespace,descriptors) def original_edges(~S:Data,~C:Type,store:A.Store) -> A.Store & List<&2,G.Edge>: match store: case A.Store{components,G.Graph{+edges,inverses}}: (A.Store{components,G.Graph{edges,inverses}},edges) def automatic_start(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,world:W.World,R,A.Notice>,+nodes:Nat,+edges:List<&2,G.Edge>,+namespace:U32,id:Key.Key,+descriptors:List<&2,G.Descriptor>) -> CleanupResult: owner_blocks(~S,~C,~R,~E,~clear,~record,nodes,drive(~S,~C,~R,~E,~clear,~record,1n,[G.CleanupEnter{id}],world,namespace,descriptors),nodes,edges,namespace,descriptors) def automatic_inventory(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,observed:W.World,R,A.Notice> & List<&2,G.Edge>,namespace:U32,id:Key.Key,descriptors:List<&2,G.Descriptor>) -> CleanupResult: match observed: case (W.World{+ns,Alloc.Allocator{+next,+highWater,slots,+capacity,+depth,+free,+order},store,resource,+events,pending,+registrations,+nextSystemId,+clock},edges): automatic_start(~S,~C,~R,~E,~clear,~record,W.World{ns,Alloc.Allocator{next,highWater,slots,capacity,depth,free,order},store,resource,events,pending,registrations,nextSystemId,clock},U32.to_nat(highWater),edges,namespace,id,descriptors) def automatic(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,world:W.World,R,A.Notice>,handle:W.Handle,descriptors:List<&2,G.Descriptor>) -> CleanupResult: match handle: case W.Handle{namespace,id}: automatic_inventory(~S,~C,~R,~E,~clear,~record,W.with_store(~S,~A.Store,~R,~A.Notice,~List<&2,G.Edge>,store => original_edges(~S,~C,store),world),namespace,id,descriptors)