import Base import ./owned_event_log.bend as Log import ./event_stream.bend as Stream import ./event_publication.bend as Publication import ./owner_product.bend as Product # Trusted raw descriptor. Ordinary leaf/both derive complete matching log/stream trees. type Catalog List<&2,E> -> Logs & Receipt,-drain:Logs -> Logs & Receipt,-clear:F -> F> is Data:Catalog{} def leaf_clear(~S:Data,stream:Stream.Stream) -> Stream.Stream: match stream: case Stream.Stream{domain,batches,positions,nextReader,frameStart,boundary,capacity,dropped}:Stream.Stream{domain,[],positions,nextReader,frameStart,boundary,capacity,dropped} def leaf(~S:Data,~P:Type) -> Catalog,Stream.Stream,List>,Log.retain(~P),Log.drain(~P),leaf_clear(~S)>:Catalog{} def joined(~LA:Type,~LB:Type,~RA:Type,~RB:Type,first:LA & RA,second:LB & RB) -> Product.Owner & Product.Owner: (first,receiptA)=first (second,receiptB)=second (Product.Owner{first,second},Product.Owner{receiptA,receiptB}) def pair_retain(~EA:Data,~EB:Data,~LA:Type,~LB:Type,~RA:Type,~RB:Type,~retainA:LA -> List<&2,EA> -> LA & RA,~retainB:LB -> List<&2,EB> -> LB & RB,logs:Product.Owner,records:List<&2,Publication.Publication>) -> Product.Owner & Product.Owner: match logs: case Product.Owner{first,second}: +shared=records joined(~LA,~LB,~RA,~RB,retainA(first,Publication.first_values(~EA,~EB,shared)),retainB(second,Publication.second_values(~EA,~EB,shared))) def pair_drain(~LA:Type,~LB:Type,~RA:Type,~RB:Type,~drainA:LA -> LA & RA,~drainB:LB -> LB & RB,logs:Product.Owner) -> Product.Owner & Product.Owner: match logs: case Product.Owner{first,second}:joined(~LA,~LB,~RA,~RB,drainA(first),drainB(second)) def pair_clear(~FA:Type,~FB:Type,~clearA:FA -> FA,~clearB:FB -> FB,family:Product.Owner) -> Product.Owner: match family: case Product.Owner{first,second}:Product.Owner{clearA(first),clearB(second)} def both(~EA:Data,~LA:Type,~FA:Type,~RA:Type,~retainA:LA -> List<&2,EA> -> LA & RA,~drainA:LA -> LA & RA,~clearA:FA -> FA,~EB:Data,~LB:Type,~FB:Type,~RB:Type,~retainB:LB -> List<&2,EB> -> LB & RB,~drainB:LB -> LB & RB,~clearB:FB -> FB,~leftCatalog:Catalog,~rightCatalog:Catalog) -> Catalog,Product.Owner,Product.Owner,Product.Owner,pair_retain(~EA,~EB,~LA,~LB,~RA,~RB,~retainA,~retainB),pair_drain(~LA,~LB,~RA,~RB,~drainA,~drainB),pair_clear(~FA,~FB,~clearA,~clearB)>:Catalog{}