1 import Base 2 import ./ordinary_schema.bend as Schema 3 import ./schema_fragments.bend as Fragment 4 import ./owner_product.bend as Product 5 # Ordinary declaration tree. Physical ownership and index shape are both typed. 6 type Branch<-L:Data,-R:Data> is Data: 7 Branch{left:L,right:R} 8 type Built<-S:Data,-R:Type,-Index:Data> is Data: 9 Built{entries:List<&2,Fragment.Entry>,index:Index} 10 def leaf(~S:Data,~P:Type,+identity:Schema.Resource) -> Built>: 11 Built{Schema.entries(~S,~Unit,~P,Schema.resource_schema(~S,~P,identity)),identity} 12 def joined(~S:Data,~L:Type,~R:Type,~LI:Data,~RI:Data,left:Built,right:Built) -> Built,Branch,Built>>: 13 match left right: 14 case Built{+le,+li} Built{+re,+ri}:Built{List.append(&2,Fragment.Entry,le,re),Branch{Built{le,li},Built{re,ri}}} 15 def entries(~S:Data,~R:Type,~I:Data,value:Built) -> List<&2,Fragment.Entry>: 16 match value: 17 case Built{entries,_}:entries 18 def validate(~S:Data,~R:Type,~I:Data,value:Built) -> Fragment.Validation: 19 Fragment.validate(~S,entries(~S,~R,~I,value)) 20 def whole_take(~P:Type,value:P) -> Unit & P: 21 (Unit{},value) 22 def whole_put(~P:Type,rest:Unit,value:P) -> P: 23 value 24 def whole(~P:Type) -> Product.Lens: 25 Product.Lens{whole_take(~P),whole_put(~P)} 26 # Erased canonical lens; no closure/payload is retained in the runtime cursor. 27 type Cursor<-S:Data,-W:Type,-P:Type,-Rest:Type,-I:Data,-lens:Product.Lens> is Data: 28 Cursor{index:I} 29 def root(~S:Data,~R:Type,~I:Data,value:Built) -> Cursor: 30 match value: 31 case Built{_,index}:Cursor{index} 32 def left_lens(~W:Type,~L:Type,~R:Type,~Rest:Type,~outer:Product.Lens,Rest>) -> Product.Lens: 33 Product.compose(~W,~Product.Owner,~Rest,~L,~R,~outer,~Product.lens(~L,~R,~Product.Left{})) 34 def right_lens(~W:Type,~L:Type,~R:Type,~Rest:Type,~outer:Product.Lens,Rest>) -> Product.Lens: 35 Product.compose(~W,~Product.Owner,~Rest,~R,~L,~outer,~Product.lens(~L,~R,~Product.Right{})) 36 def left(~S:Data,~W:Type,~L:Type,~R:Type,~Rest:Type,~LI:Data,~RI:Data,~outer:Product.Lens,Rest>,value:Cursor,Rest,Branch,Built>,outer>) -> Cursor: 37 match value: 38 case Cursor{Branch{Built{_,index},_}}:Cursor{index} 39 def right(~S:Data,~W:Type,~L:Type,~R:Type,~Rest:Type,~LI:Data,~RI:Data,~outer:Product.Lens,Rest>,value:Cursor,Rest,Branch,Built>,outer>) -> Cursor: 40 match value: 41 case Cursor{Branch{_,Built{_,index}}}:Cursor{index} 42 def identity(~S:Data,~W:Type,~P:Type,~Rest:Type,~lens:Product.Lens,value:Cursor,lens>) -> Schema.Resource: 43 match value: 44 case Cursor{identity}:identity 45 # Identity and canonical physical path are compile-time indices of the binding. 46 # Raw constructors remain trusted; application systems receive opaque grants. 47 type Binding<-S:Data,-W:Type,-P:Type,-Rest:Type,-lens:Product.Lens,-identity:Schema.Resource> is Data: 48 Binding{} 49 def bind(~S:Data,~W:Type,~P:Type,~Rest:Type,~lens:Product.Lens,~cursor:Cursor,lens>) -> Binding: 50 Binding{}