import Base import entity_key.bend as Key import ./persistence_declaration.bend as Persist import ./decode_data.bend as D import ./world.bend as W import ./column.bend as Col import ./ordinary_schema.bend as Schema import ./ordinary_declaration.bend as Identity import ./owner_product.bend as Product # The single ordinary Schema owns all keys. The erased traversal holds no keys, # presenters or payloads. Ordinary builders generate matching leaf slots/order. # Trusted ordinary schema setup indexed by matching storage construction # and population counting. Contains one schema, not live World/resource owners. # Raw construction must preserve entry/slot order; no lens laws are checked. type Provision<-S:Data,-C:Type,-R:Type,-empty:Unit -> C,-count:C -> List<&2,W.Handle> -> C & List<&2,Maybe<&2,U32>>> is Data: Provision{schema:Schema.Schema} def counted(~S:Data,~P:Type,total:U32,result:Col.Column & Bool,ih:Col.Column -> U32 -> Col.Column & U32) -> Col.Column & U32: match result: case (column,True{}):ih(column,(total + 1 : U32)) case (column,False{}):ih(column,total) def column_count(~S:Data,~P:Type,handles:List<&2,W.Handle>,column:Col.Column,total:U32) -> Col.Column & U32: match handles: case []:(column,total) case W.Handle{_,Key.EntityKey{id,_}} <> rest:counted(~S,~P,total,Col.has(~S,~P,column,id),column => total => column_count(~S,~P,rest,column,total)) def component_counted(~S:Data,~P:Type,result:Col.Column & U32) -> Col.Column & List<&2,Maybe<&2,U32>>: match result: case (column,count):(column,[Some{count}]) def component_count(~S:Data,~P:Type,column:Col.Column,handles:List<&2,W.Handle>) -> Col.Column & List<&2,Maybe<&2,U32>>: component_counted(~S,~P,column_count(~S,~P,handles,column,0)) def component_empty(~S:Data,~P:Type,unit:Unit) -> Col.Column:Col.empty(~S,~P) def resource_count(~S:Data,value:Unit,handles:List<&2,W.Handle>) -> Unit & List<&2,Maybe<&2,U32>>:(value,[None{}]) def resource_empty(unit:Unit) -> Unit:unit # Declare a component with matching empty-column/counting operations. # Does not allocate a World or consume a component payload; combines with # product for heterogeneous affine schema storage. def component(~S:Data,~P:Type,declaration:Identity.Declaration) -> Provision,Unit,component_empty(~S,~P),component_count(~S,~P)>: Provision{Schema.component(~S,~P,declaration)} # Declare a resource schema leaf. Its component-storage contribution is # Unit and population slot is None; the caller supplies actual R on creation. def resource(~S:Data,~P:Type,declaration:Schema.Resource) -> Provision: Provision{Schema.resource_schema(~S,~P,declaration)} def product_counted(~L:Type,~R:Type,left:L,before:List<&2,Maybe<&2,U32>>,right:R & List<&2,Maybe<&2,U32>>) -> Product.Owner & List<&2,Maybe<&2,U32>>: match right: case (right,after):(Product.Owner{left,right},List.append(&2,Maybe<&2,U32>,before,after)) def product_left(~S:Data,~L:Type,~R:Type,~rightCount:R -> List<&2,W.Handle> -> R & List<&2,Maybe<&2,U32>>,right:R,handles:List<&2,W.Handle>,left:L & List<&2,Maybe<&2,U32>>) -> Product.Owner & List<&2,Maybe<&2,U32>>: match left: case (left,before):product_counted(~L,~R,left,before,rightCount(right,handles)) def product_count(~S:Data,~L:Type,~R:Type,~leftCount:L -> List<&2,W.Handle> -> L & List<&2,Maybe<&2,U32>>,~rightCount:R -> List<&2,W.Handle> -> R & List<&2,Maybe<&2,U32>>,store:Product.Owner,handles:List<&2,W.Handle>) -> Product.Owner & List<&2,Maybe<&2,U32>>: match store: case Product.Owner{left,right}: +handles=handles product_left(~S,~L,~R,~rightCount,right,handles,leftCount(left,handles)) def product_empty(~L:Type,~R:Type,~leftEmpty:Unit -> L,~rightEmpty:Unit -> R,unit:Unit) -> Product.Owner: Product.Owner{leftEmpty(Unit{}),rightEmpty(Unit{})} # Compose trusted provisions in left/right schema/storage/count-slot order. # Payload types may be affine; this setup value contains no live payloads. # Does not validate duplicate keys or build a World. def product(~S:Data,~LC:Type,~LR:Type,~RC:Type,~RR:Type,~leftEmpty:Unit -> LC,~leftCount:LC -> List<&2,W.Handle> -> LC & List<&2,Maybe<&2,U32>>,~rightEmpty:Unit -> RC,~rightCount:RC -> List<&2,W.Handle> -> RC & List<&2,Maybe<&2,U32>>,left:Provision,right:Provision) -> Provision,Product.Owner,product_empty(~LC,~RC,~leftEmpty,~rightEmpty),product_count(~S,~LC,~RC,~leftCount,~rightCount)>: match left right: case Provision{left} Provision{right}:Provision{Schema.product(~S,~LC,~LR,~RC,~RR,left,right)} # Trusted factory-based World creation returning provision and Creation. # Refusal preserves caller resources through the Creation result. This uses # the supplied pure Factory and does not allocate a process-unique namespace; # prefer checked IO creation for independently created ordinary Worlds. def create(~S:Data,~C:Type,~R:Type,~E:Data,~empty:Unit -> C,~count:C -> List<&2,W.Handle> -> C & List<&2,Maybe<&2,U32>>,provision:Provision,factory:W.Factory,resource:R) -> Provision & W.Creation: (provision,W.create(~S,~C,~R,~E,~empty,factory,resource)) # Declare a component provision with plain persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def component_plain(~S:Data,~P:Type,~identity:Identity.Declaration,extension:Persist.Plain) -> Provision,Unit,component_empty(~S,~P),component_count(~S,~P)>:Provision{Schema.component_plain(~S,~P,~identity,extension)} # Declare a component provision with transient persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def component_transient(~S:Data,~P:Type,~identity:Identity.Declaration,extension:Persist.Transient) -> Provision,Unit,component_empty(~S,~P),component_count(~S,~P)>:Provision{Schema.component_transient(~S,~P,~identity,extension)} # Declare a component provision with constructed persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def component_constructed(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,extension:Persist.Constructed) -> Provision,Unit,component_empty(~S,~P),component_count(~S,~P)>:Provision{Schema.component_constructed(~S,~P,~identity,~project,extension)} # Declare a resource provision with plain persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def resource_plain(~S:Data,~P:Type,~identity:Identity.Declaration,extension:Persist.Plain) -> Provision:Provision{Schema.resource_plain(~S,~P,~identity,extension)} # Declare a resource provision with transient persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def resource_transient(~S:Data,~P:Type,~identity:Identity.Declaration,extension:Persist.Transient) -> Provision:Provision{Schema.resource_transient(~S,~P,~identity,extension)} # Declare a resource provision with constructed persistence classification # from its identity-bound extension. Derives matching empty/count operations; # does not execute codecs, validate saved input or construct payload values. def resource_constructed(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,extension:Persist.Constructed) -> Provision:Provision{Schema.resource_constructed(~S,~P,~identity,~project,extension)}