1 import Base 2 import ./column.bend as Col 3 import ./world.bend as W 4 import ./ordinary_declaration.bend as Identity 5 import ./ordinary_component_binding.bend as Binding 6 import ./commands.bend as Cmd 7 import ./transaction.bend as Tx 8 9 def handle_id(~S:Data,handle:W.Handle) -> U32: 10 match handle: 11 case W.Handle{_,key}:W.handle_index(~S,W.Handle{0,key}) 12 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>: 13 match result: 14 case Col.Accepted{column,Some{_}}:(put(rest,column),[emit(handle)]) 15 case Col.Accepted{column,None{}}:(put(rest,column),[]) 16 case Col.Rejected{column,_}:(put(rest,column),[]) 17 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>: 18 (rest,column)=taken 19 accepted(~S,~Store,~Rest,~P,~E,~put,~emit,rest,handle,Col.clear(~S,~P,column,handle_id(~S,handle))) 20 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: 21 inject(Binding.identity(~S,~Store,~Rest,~P,~V,~Token,~take,~put,~view,binding),handle) 22 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>: 23 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)) 24 def append(~Store:Type,~E:Data,first:List<&2,E>,second:Store & List<&2,E>) -> Store & List<&2,E>: 25 (store,records)=second 26 (store,List.append(&2,E,first,records)) 27 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>: 28 (store,records)=first 29 append(~Store,~E,records,right(store,handle)) 30 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>: 31 product_next(~S,~Store,~E,~right,handle,left(store,handle)) 32 33 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: 34 Cmd.tx_despawn_observed(~S,~C,~R,~E,~clear,~record,tx,handle)