import Base import ./column.bend as Col import ./world.bend as W import ./ordinary_declaration.bend as Identity import ./ordinary_component_binding.bend as Binding import ./commands.bend as Cmd import ./transaction.bend as Tx def handle_id(~S:Data,handle:W.Handle) -> U32: match handle: case W.Handle{_,key}:W.handle_index(~S,W.Handle{0,key}) def accepted(~S:Data,~Store:Type,~Rest:Type,~P:Type,~E:Data,~put:Rest -> Col.Column -> Store,~emit:W.Handle -> E,rest:Rest,+handle:W.Handle,result:Col.SwapOutcome) -> Store & List<&2,E>: match result: case Col.Accepted{column,Some{_}}:(put(rest,column),[emit(handle)]) case Col.Accepted{column,None{}}:(put(rest,column),[]) case Col.Rejected{column,_}:(put(rest,column),[]) def leaf_taken(~S:Data,~Store:Type,~Rest:Type,~P:Type,~E:Data,~put:Rest -> Col.Column -> Store,~emit:W.Handle -> E,+handle:W.Handle,taken:Rest & Col.Column) -> Store & List<&2,E>: (rest,column)=taken accepted(~S,~Store,~Rest,~P,~E,~put,~emit,rest,handle,Col.clear(~S,~P,column,handle_id(~S,handle))) def leaf_record(~S:Data,~Store:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:Store -> Rest & Col.Column,~put:Rest -> Col.Column -> Store,~view:P -> P & V,~E:Data,~inject:Identity.Declaration -> W.Handle -> E,~binding:Binding.Binding,handle:W.Handle) -> E: inject(Binding.identity(~S,~Store,~Rest,~P,~V,~Token,~take,~put,~view,binding),handle) def leaf(~S:Data,~Store:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:Store -> Rest & Col.Column,~put:Rest -> Col.Column -> Store,~view:P -> P & V,~E:Data,~inject:Identity.Declaration -> W.Handle -> E,~binding:Binding.Binding,store:Store,handle:W.Handle) -> Store & List<&2,E>: leaf_taken(~S,~Store,~Rest,~P,~E,~put,~leaf_record(~S,~Store,~Rest,~P,~V,~Token,~take,~put,~view,~E,~inject,~binding),handle,take(store)) def append(~Store:Type,~E:Data,first:List<&2,E>,second:Store & List<&2,E>) -> Store & List<&2,E>: (store,records)=second (store,List.append(&2,E,first,records)) def product_next(~S:Data,~Store:Type,~E:Data,~right:Store -> W.Handle -> Store & List<&2,E>,handle:W.Handle,first:Store & List<&2,E>) -> Store & List<&2,E>: (store,records)=first append(~Store,~E,records,right(store,handle)) def product(~S:Data,~Store:Type,~E:Data,~left:Store -> W.Handle -> Store & List<&2,E>,~right:Store -> W.Handle -> Store & List<&2,E>,store:Store,+handle:W.Handle) -> Store & List<&2,E>: product_next(~S,~Store,~E,~right,handle,left(store,handle)) def despawn(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,E>,~record:W.Handle -> E,tx:Tx.Tx,E>,handle:W.Handle) -> Tx.Tx,E> & W.Status: Cmd.tx_despawn_observed(~S,~C,~R,~E,~clear,~record,tx,handle)