import Base import ./trace_facts.bend as Facts import ./world.bend as W import ./owner_product.bend as Product import ./owned_event_log.bend as Log import ./owned_event_family_storage.bend as Storage import ./recovery_journal.bend as Journal # Trusted transport. Ordinary declaration builders derive both paths together; # authored gameplay receives only the abstract emission capability. type Stage is Type: Stage{newest:List,trace:Facts.State} def empty(~State:Type,~Recovery:Type) -> Stage:Stage{[],Facts.Off{}} def recovered(~P:Type,~All:Type,~Rest:Type,~path:Product.Lens,Rest>,payload:P,parts:Rest & List

) -> All: match parts: case (rest,values):Product.put_field(~All,~List

,~Rest,path)(rest,List.append(&1,P,values,[payload])) def recover(~P:Type,~All:Type,~Rest:Type,~path:Product.Lens,Rest>,payload:P,all:All) -> All: recovered(~P,~All,~Rest,~path,payload,Product.get_field(~All,~List

,~Rest,path)(all)) def action(~S:Data,~C:Type,~R:Type,~P:Type,~Rest:Type,~E:Data,~path:Product.Lens,Rest>,~inject:Nat -> E,~All:Type,~RecoveryRest:Type,~recovery:Product.Lens,RecoveryRest>,payload:P,success:Bool,world:W.World,all:All) -> W.World & All: match success: case True{}:(Storage.commit_owned(~S,~C,~R,~P,~Rest,~E,~path,~inject,world,[payload]),all) case False{}:(world,recover(~P,~All,~RecoveryRest,~recovery,payload,all)) def emit(~S:Data,~C:Type,~R:Type,~P:Type,~Rest:Type,~E:Data,~path:Product.Lens,Rest>,~inject:Nat -> E,~All:Type,~RecoveryRest:Type,~recovery:Product.Lens,RecoveryRest>,stage:Stage,All>,payload:P) -> Stage,All>: match stage: case Stage{newest,trace}:Stage{(success => world => all => action(~S,~C,~R,~P,~Rest,~E,~path,~inject,~All,~RecoveryRest,~recovery,payload,success,world,all)) <> newest,trace} # Reverse the action spine once: commits retain the global inter-family order, # and abort receipts retain each family's order. Every closure executes once. def finish(~State:Type,~Recovery:Type,stage:Stage,success:Bool,state:State,recovery:Recovery) -> State & Recovery: match stage: case Stage{newest,_}:Journal.finish(~State,~Recovery,List.reverse(&1,Journal.Action(~State,~Recovery),newest),success,state,recovery) def with_trace(~State:Type,~Recovery:Type,trace:Facts.State) -> Stage:Stage{[],trace} def record_named(~State:Type,~Recovery:Type,name:String,stage:Stage) -> Stage: match stage: case Stage{newest,Facts.Off{}}:Stage{newest,Facts.Off{}} case Stage{newest,Facts.On{facts}}:Stage{newest,Facts.On{Facts.Fact{Facts.EventValue{},name,Facts.SendOperation{},None{},Facts.UnavailableValue{},Facts.OpaqueValue{},Facts.Attempted{}} <> facts}} def finish_observed(~State:Type,~Recovery:Type,stage:Stage,+success:Bool,state:State,recovery:Recovery) -> (State & Recovery) & Facts.State: match stage: case Stage{newest,trace}:(Journal.finish(~State,~Recovery,List.reverse(&1,Journal.Action(~State,~Recovery),newest),success,state,recovery),Facts.settle(trace,success))