1 import Base 2 import ./lifecycle.bend as Life 3 import ./execution_query_domain.bend as D 4 5 type Recipe is Data: 6 Read{domain:D.Domain} 7 Write{domain:D.Domain} 8 Both{left:Recipe,right:Recipe} 9 type Presence is Data: 10 Optional{} 11 Required{} 12 With{} 13 Without{} 14 type Row<-V:Data> is Data: 15 Row{offset:U32,view:Maybe<&2,V>} 16 StampedRow{offset:U32,view:Maybe<&2,V>,stamp:Life.Stamp} 17 # Raw Row carries unknown lifecycle metadata; canonical caches use StampedRow. 18 def row_offset(-V:Data,row:Row) -> U32: 19 match row: 20 case Row{offset,_}:offset 21 case StampedRow{offset,_,_}:offset 22 def row_view(-V:Data,row:Row) -> Maybe<&2,V>: 23 match row: 24 case Row{_,view}:view 25 case StampedRow{_,view,_}:view 26 def row_stamp(-V:Data,row:Row) -> Life.Stamp: 27 match row: 28 case Row{_,_}:Life.Stamp{None{},None{}} 29 case StampedRow{_,None{},_}:Life.Stamp{None{},None{}} 30 case StampedRow{_,Some{_},stamp}:stamp 31 type Single<-V:Data> is Data: 32 NoMatch{} 33 One{row:Row} 34 Multiple{} 35 def present(-V:Data,value:Maybe<&2,V>) -> Bool: 36 match value: 37 case None{}:False{} 38 case Some{_}:True{} 39 def membership(-V:Data,presence:Presence,value:Maybe<&2,V>) -> Bool: 40 match presence: 41 case Optional{}:True{} 42 case Required{}:present(V,value) 43 case With{}:present(V,value) 44 case Without{}:Bool.not(present(V,value)) 45 def selected_row(-V:Data,offset:U32,view:Maybe<&2,V>,rows:List<&2,Row>,matched:Bool) -> List<&2,Row>: 46 match matched: 47 case False{}:rows 48 case True{}:Row{offset,view} <> rows 49 def single(-V:Data,rows:List<&2,Row>) -> Single: 50 match rows: 51 case []:NoMatch{} 52 case row <> []:One{row} 53 case _ <> _ <> _:Multiple{} 54 def count(-V:Data,rows:List<&2,Row>) -> U32: 55 match rows: 56 case []:0 57 case _ <> rest:U32.add(1,count(V,rest)) 58 59 def inventory_valid(result:D.Validation) -> Bool: 60 match result: 61 case D.Valid{}:True{} 62 case _:False{} 63 def validate_recipe(+capacity:U32,recipe:Recipe) -> Bool: 64 match recipe: 65 case Read{domain}:inventory_valid(D.validate(capacity,domain)) 66 case Write{domain}:inventory_valid(D.validate(capacity,domain)) 67 case Both{left,right}:Bool.and(validate_recipe(capacity,left),validate_recipe(capacity,right))