1 import Base 2 import ./ordinary_machine_initialization.bend as Init 3 import ./ordinary_app_composer.bend as Build 4 import ./ordinary_app_recipe.bend as Tree 5 import ./ordinary_app_observation_data.bend as DTO 6 import ./ordinary_app_registration_fields.bend as Fields 7 import ./world.bend as W 8 import ./inspector.bend as I 9 import ./ordinary_machine_declaration.bend as OD 10 import ./ordinary_machine_operations.bend as Ops 11 import ./machine.bend as M 12 import ./capabilities.bend as Cap 13 import ./transaction.bend as T 14 import ./commands.bend as Cmd 15 16 # A declaration-derived value observer, not a fabricated resource/system record. 17 def current_text(~S:Data,~K:Data,~V:Data,~render:V -> String,view:M.CurrentView) -> String: 18 match view: 19 case M.Unavailable{}:"Unavailable" 20 case M.Current{value}:"Current:" ++ render(value) 21 # The ordered vocabulary comes from the same operational declaration. The 22 # registered observer is invoked only on the enabled snapshot branch. 23 def vocabulary_text(~V:Data,~render:V -> String,values:List<&2,V>) -> List<&2,String>: 24 match values: 25 case []:[] 26 case value <> rest:render(value) <> vocabulary_text(~V,~render,rest) 27 def vocabulary_description(~V:Data,~render:V -> String,name:String,value:Maybe<&2,List<&2,V>>) -> List<&2,String>: 28 match value: 29 case None{}:[] 30 case Some{values}:[name ++ ":Vocabulary=" ++ List.show(&2,String,x=>x,vocabulary_text(~V,~render,values))] 31 def presented(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~declaration:OD.Declaration,~render:V -> String,result:I.Frame & M.CurrentView) -> W.World & DTO.Observation>: 32 match result: 33 case (I.Frame{world,_},+view):(world,DTO.Observation{view,List.append(&2,String,[OD.name(~S,~K,~V,~R,~take,declaration) ++ ":" ++ current_text(~S,~K,~V,~render,view)],vocabulary_description(~V,~render,OD.name(~S,~K,~V,~R,~take,declaration),OD.values(~S,~K,~V,~R,~take,declaration)))}) 34 # Advanced declaration-derived current-value observer with detached rendering and 35 # optional vocabulary. Returns World; the retained slot projector/render function 36 # are trusted setup, not a newly validated arbitrary resource adapter. 37 def observe(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~declaration:OD.Declaration,~render:V -> String,world:W.World) -> W.World & DTO.Observation>: 38 presented(~S,~K,~V,~C,~R,~E,~take,~declaration,~render,OD.get(~I.Frame,~S,~K,~V,~OD.inspect(~S,~K,~V,~C,~R,~E,~take,~declaration),I.Frame{world,None{}})) 39 # Advanced observer attachment pairing prior rows with declared machine current 40 # view. Retains World, registration owners and debug; does not create machine state. 41 def attach(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered,Owners,Rows,prior,fold>) -> Build.Registered,Owners,DTO.Product>,Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render)),fold>: 42 match app: 43 case Build.Registered{world,owners,_,debug}:Build.Registered{world,owners,Tree.Recipe{},debug} 44 # Operational queue and debug current grant are generated from the SAME declaration. 45 # Queue a machine request through declaration-derived write caps in one Tx, then 46 # finish with the supplied outcome. Success retains queued state; failure restores 47 # recorded changes. This does not commit a transition or execute handler phases; 48 # vocabulary metadata is not a value-membership validation gate. 49 def queue(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~Error:Data,world:W.World,request:M.QueueInput,outcome:T.Outcome) -> T.Returned,Error>: 50 Cmd.tx_finish(~S,~C,~R,~E,~Error,Cap.value_set(~T.Tx,E>,~M.QueueInput,~M.CurrentView,~Ops.write(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration),T.begin(~W.World,~E,world),request),outcome) 51 52 # Ordinary operational binding: setup derives the observer from the same declaration. 53 # Composer App type adding this declaration's detached current-value observation. 54 # Machine storage remains in World resources; no fabricated system owner is added. 55 def Application(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: 56 Build.Registered,Owners,DTO.Product>,Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render)),fold> 57 # Install the declaration-derived machine observer, preserving actual App owners. 58 # Does not initialize state, register handlers or apply a queued transition. 59 def install(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered,Owners,Rows,prior,fold>) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold): 60 attach(~S,~K,~V,~C,~R,~E,~take,~declaration,~render,~Owners,~Rows,~prior,~fold,app) 61 def returned(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,owners:Owners,recipe:Tree.Recipe,DTO.Product>,Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render))>,debug:Bool,result:T.Returned,Error>) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome: 62 match result: 63 case T.Returned{world,status}:(Build.Registered{world,owners,recipe,debug},status) 64 # App-level queue adapter returning actual App and transaction outcome. Retains 65 # observer/debug/registration owners; transition application remains explicit. 66 def queue_app(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,~Error:Data,app:Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold),request:M.QueueInput,outcome:T.Outcome) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome: 67 match app: 68 case Build.Registered{world,owners,recipe,debug}:returned(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold,~Error,owners,recipe,debug,queue(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~Error,world,request,outcome)) 69 def read_finished(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,view:M.CurrentView,result:Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & T.Outcome) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & M.CurrentView: 70 match result: 71 case (app,_):(app,view) 72 def read_taken(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owners:Owners,recipe:Tree.Recipe,DTO.Product>,Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render))>,debug:Bool,result:T.Tx,E> & M.CurrentView) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & M.CurrentView: 73 match result: 74 case (frame,view):read_finished(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold,view,returned(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold,~Unit,owners,recipe,debug,Cmd.tx_finish(~S,~C,~R,~E,~Unit,frame,T.Success{}))) 75 # Read declared current value while returning App, without depending on debug. 76 # Unavailable remains an explicit view. Does not apply queue or run handlers. 77 def read_app(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold)) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & M.CurrentView: 78 match app: 79 case Build.Registered{world,owners,recipe,debug}:read_taken(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold,owners,recipe,debug,Cap.value_read(~T.Tx,E>,~M.CurrentView,~Ops.read(~S,~K,~V,~C,~R,~E,~take,~declaration),T.begin(~W.World,~E,world))) 80 # Type spelling and snapshot assembly are generated by the operational library. 81 # Advanced detached row type: prior observation paired with current machine view. 82 def ObservedRows(~S:Data,~K:Data,~V:Data,~Rows:Data) -> Data: 83 DTO.Product> 84 # Advanced closed observer composition threading World through prior then machine. 85 def observation(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> W.World -> W.World & DTO.Observation>>: 86 Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render)) 87 # Advanced typed observation recipe projection, with no separate machine owner. 88 def Recipe(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>) -> Type: 89 Tree.Recipe,DTO.Product>,Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render))> 90 # Set observation flag then return optional combined snapshot with App. Disabled 91 # bypasses current-value projection/rendering; no queue application or barrier. 92 def snapshot_app(~S:Data,~K:Data,~V:Data,~C:Type,~R:Type,~E:Data,~take:R -> R & M.Slot,~put:R -> M.Slot -> R,~same:V -> V -> Bool,~declaration:OD.Declaration,~render:V -> String,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold),flag:Bool) -> Application(~S,~K,~V,~C,~R,~E,~take,~put,~same,~declaration,~render,~Owners,~Rows,~prior,~fold) & Maybe<&2,DTO.Product>,DTO.Observation>>>>: 93 Build.snapshot(~S,~W.World,~Owners,~DTO.Product>,~Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render)),~fold,Build.enabled(~S,~W.World,~Owners,~DTO.Product>,~Tree.observe_pair(~W.World,~Rows,~M.CurrentView,~prior,~observe(~S,~K,~V,~C,~R,~E,~take,~declaration,~render)),~fold,app,flag)) 94 95 # Ordinary initialization preserves the existing app's sole owners and debug recipe. 96 def initialized_app(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,owners:Owners,recipe:Tree.Recipe,Rows,prior>,debug:Bool,world:W.World) -> Build.Registered,Owners,Rows,prior,fold>: 97 Build.Registered{world,owners,recipe,debug} 98 # Initialize missing state from make; already-present state skips make. Preserve 99 # actual App owners/recipe/debug. Uses ordinary initialization and its initial 100 # publication/revision semantics, not handler execution or generic undo recovery. 101 def init_app(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,~declaration:OD.Declaration,~make:W.World -> W.World & V,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered,Owners,Rows,prior,fold>) -> Build.Registered,Owners,Rows,prior,fold>: 102 match app: 103 case Build.Registered{world,owners,recipe,debug}:initialized_app(~S,~K,~V,~R,~take,~put,~C,~E,~Owners,~Rows,~prior,~fold,owners,recipe,debug,Init.init_state(~S,~K,~V,~R,~take,~put,~C,~E,~declaration,~make,world)) 104 # Explicitly insert/replace state value through ordinary initialization storage. 105 # Preserve App owners; occupied insertion resets transition messages under the 106 # existing insertion contract. This is distinct from a queued transition request. 107 def insert_app(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,~declaration:OD.Declaration,~Owners:Type,~Rows:Data,~prior:W.World -> W.World & DTO.Observation,~fold:Owners -> Owners & List<&2,Fields.Fields>,app:Build.Registered,Owners,Rows,prior,fold>,value:V) -> Build.Registered,Owners,Rows,prior,fold>: 108 match app: 109 case Build.Registered{world,owners,recipe,debug}:initialized_app(~S,~K,~V,~R,~take,~put,~C,~E,~Owners,~Rows,~prior,~fold,owners,recipe,debug,Init.insert_state(~S,~K,~V,~R,~take,~put,~C,~E,~declaration,world,value))