import Base import ./ordinary_schema.bend as Schema import ./schema_fragments.bend as Fragment import ./owner_product.bend as Product # Ordinary declaration tree. Physical ownership and index shape are both typed. type Branch<-L:Data,-R:Data> is Data: Branch{left:L,right:R} type Built<-S:Data,-R:Type,-Index:Data> is Data: Built{entries:List<&2,Fragment.Entry>,index:Index} def leaf(~S:Data,~P:Type,+identity:Schema.Resource) -> Built>: Built{Schema.entries(~S,~Unit,~P,Schema.resource_schema(~S,~P,identity)),identity} def joined(~S:Data,~L:Type,~R:Type,~LI:Data,~RI:Data,left:Built,right:Built) -> Built,Branch,Built>>: match left right: case Built{+le,+li} Built{+re,+ri}:Built{List.append(&2,Fragment.Entry,le,re),Branch{Built{le,li},Built{re,ri}}} def entries(~S:Data,~R:Type,~I:Data,value:Built) -> List<&2,Fragment.Entry>: match value: case Built{entries,_}:entries def validate(~S:Data,~R:Type,~I:Data,value:Built) -> Fragment.Validation: Fragment.validate(~S,entries(~S,~R,~I,value)) def whole_take(~P:Type,value:P) -> Unit & P: (Unit{},value) def whole_put(~P:Type,rest:Unit,value:P) -> P: value def whole(~P:Type) -> Product.Lens: Product.Lens{whole_take(~P),whole_put(~P)} # Erased canonical lens; no closure/payload is retained in the runtime cursor. type Cursor<-S:Data,-W:Type,-P:Type,-Rest:Type,-I:Data,-lens:Product.Lens> is Data: Cursor{index:I} def root(~S:Data,~R:Type,~I:Data,value:Built) -> Cursor: match value: case Built{_,index}:Cursor{index} def left_lens(~W:Type,~L:Type,~R:Type,~Rest:Type,~outer:Product.Lens,Rest>) -> Product.Lens: Product.compose(~W,~Product.Owner,~Rest,~L,~R,~outer,~Product.lens(~L,~R,~Product.Left{})) def right_lens(~W:Type,~L:Type,~R:Type,~Rest:Type,~outer:Product.Lens,Rest>) -> Product.Lens: Product.compose(~W,~Product.Owner,~Rest,~R,~L,~outer,~Product.lens(~L,~R,~Product.Right{})) 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: match value: case Cursor{Branch{Built{_,index},_}}:Cursor{index} 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: match value: case Cursor{Branch{_,Built{_,index}}}:Cursor{index} def identity(~S:Data,~W:Type,~P:Type,~Rest:Type,~lens:Product.Lens,value:Cursor,lens>) -> Schema.Resource: match value: case Cursor{identity}:identity # Identity and canonical physical path are compile-time indices of the binding. # Raw constructors remain trusted; application systems receive opaque grants. type Binding<-S:Data,-W:Type,-P:Type,-Rest:Type,-lens:Product.Lens,-identity:Schema.Resource> is Data: Binding{} def bind(~S:Data,~W:Type,~P:Type,~Rest:Type,~lens:Product.Lens,~cursor:Cursor,lens>) -> Binding: Binding{}