import Base import ./ordinary_machine_initialization.bend as Init import ./ordinary_machine_declaration.bend as OD import ./machine.bend as M import ./world.bend as W import ./system.bend as Sys # Each ordinary condition is an actual registered system plus its own revision. # The revision is separate from the Registry's real World-clock cursor. def present(~S:Data,~K:Data,~V:Data,slot:M.Slot) -> Bool: match slot: case M.Missing{}:False{} case _:True{} def revision_taken(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,pair:R & Init.State) -> R & (Nat & Bool): match pair: case (resource,Init.State{+slot,stream,+revision}):(put(resource,Init.State{slot,stream,revision}),(revision,present(~S,~K,~V,slot))) def observed(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,prior:Nat,pair:W.World & (Nat & Bool)) -> Sys.Outcome,Unit,Nat & Bool>: match pair: case (world,(+current,present)):Sys.Succeeded{world,(current,Bool.and(present,Nat.is_lt(prior,current)))} def runner(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,world:W.World,prior:Nat) -> Sys.Outcome,Unit,Nat & Bool>: observed(~S,~K,~V,~R,~take,~put,~C,~E,prior,W.observe_resource(~S,~C,~R,~E,~(Nat & Bool),~(resource => revision_taken(~S,~K,~V,~R,~take,~put,~C,~E,take(resource))),world)) type Instance R & Init.State,-put:R -> Init.State -> R> is Type: Instance{registry:Sys.Registry,lastRevision:Nat} type Definition R & Init.State,-put:R -> Init.State -> R> is Type: Defined{world:W.World,instance:Instance} DefinitionRejected{world:W.World} def registered(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,result:Sys.RegistrationResult) -> Definition: match result: case Sys.Registered{world,registry}:Defined{world,Instance{registry,0n}} case Sys.RegistrationFailed{world}:DefinitionRejected{world} def define(~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,world:W.World,name:String) -> Definition: registered(~S,~K,~V,~R,~take,~put,~C,~E,Sys.register(~S,~C,~R,~E,~Nat,~Unit,~(Nat & Bool),~runner(~S,~K,~V,~R,~take,~put,~C,~E),world,name,["machine." ++ OD.name(~S,~K,~V,~R,~Init.take_slot(~S,~K,~V,~R,~take,~put),declaration) ++ ".changed"])) type Observed R & Init.State,-put:R -> Init.State -> R> is Type: Observed{world:W.World,instance:Instance,value:Maybe<&2,Bool>} RegistrationRejected{world:W.World,instance:Instance} def completed(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,prior:Nat,result:Sys.RunResult) -> Observed: match result: case Sys.Completed{registry,Sys.Succeeded{world,(revision,value)}}:Observed{world,Instance{registry,revision},Some{value}} case Sys.Completed{registry,Sys.Failed{world,_}}:Observed{world,Instance{registry,prior},None{}} case Sys.RegistrationRejected{registry,world,_}:RegistrationRejected{world,Instance{registry,prior}} def run(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,instance:Instance,world:W.World) -> Observed: match instance: case Instance{registry,+prior}:completed(~S,~K,~V,~R,~take,~put,~C,~E,prior,Sys.run(~S,~C,~R,~E,~Nat,~Unit,~(Nat & Bool),~runner(~S,~K,~V,~R,~take,~put,~C,~E),registry,world,prior)) def run_if(~S:Data,~K:Data,~V:Data,~R:Type,~take:R -> R & Init.State,~put:R -> Init.State -> R,~C:Type,~E:Data,enabled:Bool,instance:Instance,world:W.World) -> Observed: match enabled: case False{}:Observed{world,instance,None{}} case True{}:run(~S,~K,~V,~R,~take,~put,~C,~E,instance,world)