import Base import ./execution_resource_policy.bend as Policy import ./clock_state.bend as Clock import ./ordinary_declaration.bend as Identity import ./ordinary_schema.bend as Resource import ./schema_fragments.bend as Fragment import ./execution_composer_layout.bend as Layout import ./persistence_declaration.bend as Persist import ./decode_data.bend as Decode import ./world.bend as W type FragmentSchema<-S:Data,-C:Type,-R:Type> is Data: FragmentSchema{entries:List<&2,Fragment.Entry>} def component(~S:Data,~P:Type,~V:Data,~refresh:P -> P & V,identity:Identity.Declaration) -> Layout.Layout: Layout.LayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Policy.Ordinary{}} def resource(~S:Data,~P:Type,~V:Data,~refresh:P -> P & V,identity:Resource.Resource) -> Layout.Layout: Layout.LayoutLeaf{P,V,refresh,Resource.resource_key(~S,~P,identity),Policy.Ordinary{}} def plain(~S:Data,~P:Type,~identity:Identity.Declaration,~V:Data,~refresh:P -> P & V,extension:Persist.Plain) -> Layout.Layout: Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Plain{},Policy.Ordinary{}} def transient(~S:Data,~P:Type,~identity:Identity.Declaration,~V:Data,~refresh:P -> P & V,extension:Persist.Transient) -> Layout.Layout: Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Transient{},Policy.Ordinary{}} def constructed(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & Decode.Raw,~V:Data,~refresh:P -> P & V,extension:Persist.Constructed) -> Layout.Layout: Layout.ClassifiedLayoutLeaf{P,V,refresh,Identity.key(~S,~P,identity),Fragment.Constructed{},Policy.Ordinary{}} def layout_entries(~S:Data,+kind:Fragment.RegistryKind,layout:Layout.Layout) -> List<&2,Fragment.Entry>: match layout: case Layout.EmptyLayout{}:[] case Layout.LayoutLeaf{_,_,_,+key,_}:[Fragment.Entry{kind,key,key}] case Layout.ClassifiedLayoutLeaf{_,_,_,+key,persistence,_}:[Fragment.Classified{kind,key,key,persistence}] case Layout.LayoutPair{left,right}:List.append(&2,Fragment.Entry,layout_entries(~S,kind,left),layout_entries(~S,kind,right)) def inventory(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> List<&2,Fragment.Entry>: List.append(&2,Fragment.Entry,layout_entries(~S,Fragment.Component{},components),layout_entries(~S,Fragment.Resource{},resources)) def declared(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> FragmentSchema: FragmentSchema{inventory(~S,~components,~resources)} def entries(~S:Data,~C:Type,~R:Type,value:FragmentSchema) -> List<&2,Fragment.Entry>: match value: case FragmentSchema{entries}:entries def fragment(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~Provisions:Type,provisions:Provisions) -> Fragment.Fragment: Fragment.Fragment{inventory(~S,~components,~resources),provisions} def validate(~S:Data,~components:Layout.Layout,~resources:Layout.Layout) -> Fragment.Validation: Fragment.validate(~S,inventory(~S,~components,~resources)) def empty_columns(~S:Data,~components:Layout.Layout,_:Unit) -> Layout.columns(~S,components): Layout.empty_columns(~S,components) type Initialized<-S:Data,-Components:Layout.Layout,-Resources:Layout.Layout,-E:Data> is Type: SchemaCreated{factory:W.Factory,world:W.World} SchemaRefused{factory:W.Factory,resources:Layout.resources(~S,Resources),error:Fragment.Error} NamespaceRefused{factory:W.Factory,resources:Layout.resources(~S,Resources)} def initialized_world(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,world:W.World) -> W.World: match world: case W.World{namespace,allocator,store,owners,events,pending,registrations,next,+clock,lifecycle}:W.World{namespace,allocator,store,Layout.initialize_resources(~S,resources,Clock.current(clock),owners),events,pending,registrations,next,clock,lifecycle} def created(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,result:W.Creation) -> Initialized: match result: case W.Created{factory,world}:SchemaCreated{factory,initialized_world(~S,~components,~resources,~E,world)} case W.CreateRejected{factory,resource}:NamespaceRefused{factory,resource} def create_checked(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,result:Fragment.Validation,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized: match result: case Fragment.Invalid{error}:SchemaRefused{factory,owners,error} case Fragment.Valid{}:created(~S,~components,~resources,~E,W.create(~S,~Layout.columns(~S,components),~Layout.resources(~S,resources),~E,~empty_columns(~S,~components),factory,owners)) def create(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized: create_checked(~S,~components,~resources,~E,validate(~S,~components,~resources),factory,owners) # The trusted closed seed must initialize every occupied row through Layout.initialize. # It constructs initial storage and never receives runtime execution authority. def trusted_create_seeded_checked(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,~seed:Unit -> Layout.columns(~S,components),result:Fragment.Validation,factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized: match result: case Fragment.Invalid{error}:SchemaRefused{factory,owners,error} case Fragment.Valid{}:created(~S,~components,~resources,~E,W.create(~S,~Layout.columns(~S,components),~Layout.resources(~S,resources),~E,~seed,factory,owners)) def trusted_create_seeded(~S:Data,~components:Layout.Layout,~resources:Layout.Layout,~E:Data,~seed:Unit -> Layout.columns(~S,components),factory:W.Factory,owners:Layout.resources(~S,resources)) -> Initialized: trusted_create_seeded_checked(~S,~components,~resources,~E,~seed,validate(~S,~components,~resources),factory,owners)