1 import Base 2 import world.bend as W 3 import system.bend as Sys 4 import system_cleanup.bend as Cleanup 5 6 # Closed schema factory indices are erased; registry and ports remain affine. 7 type Instance -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: 8 Instance{owned: Cleanup.Instance} 9 type Disposal -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: 10 Disposed{world: W.World} 11 Rejected{world: W.World,instance: Instance} 12 13 # Schema authors bind these indices once in their ordinary registration factory. 14 def bind(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,registry: Sys.Registry,ports: Ports) -> Instance: 15 Instance{Cleanup.Instance{registry,ports}} 16 17 def disposed(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,result: Cleanup.Cleanup) -> Disposal: 18 match result: 19 case Cleanup.Cleaned{world}: Disposed{world} 20 case Cleanup.CleanupRejected{world,instance}: Rejected{world,Instance{instance}} 21 22 # Callers supply only their actual instance and world; no runtime metadata callbacks. 23 def dispose(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,instance: Instance,world: W.World) -> Disposal: 24 match instance: 25 case Instance{owned}: disposed(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,Cleanup.cleanup(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,owned,world)) 26 27 type Registration -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: 28 Registered{world: W.World,instance: Instance} 29 RegistrationFailed{world: W.World} 30 31 def created_ports(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,registry: Sys.Registry,observed: W.World & Ports) -> Registration: 32 match observed: 33 case (world,ports): Registered{world,bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports)} 34 35 def created(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,~create: U32 -> U32 -> W.World -> W.World & Ports,world: W.World,namespace: U32,identified: Sys.Registry & U32) -> Registration: 36 match identified: 37 case (registry,id): created_ports(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,create(namespace,id,world)) 38 39 def namespaced(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,~create: U32 -> U32 -> W.World -> W.World & Ports,registry: Sys.Registry,observed: W.World & U32) -> Registration: 40 match observed: 41 case (world,namespace): created(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,~create,world,namespace,Sys.identity(~S,~C,~R,~E,~Args,~Error,~Output,~run,registry)) 42 43 def registered(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,~create: U32 -> U32 -> W.World -> W.World & Ports,result: Sys.RegistrationResult) -> Registration: 44 match result: 45 case Sys.RegistrationFailed{world}: RegistrationFailed{world} 46 case Sys.Registered{world,registry}: namespaced(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,~create,registry,W.namespace(~S,~C,~R,~E,world)) 47 48 # The schema factory specializes create/validate/remove once. Failed registration 49 # constructs no ports; successful ports use the actual namespace and registry id. 50 def register(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,~create: U32 -> U32 -> W.World -> W.World & Ports,world: W.World,+name: String,+access: List<&2,String>) -> Registration: 51 registered(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,~create,Sys.register(~S,~C,~R,~E,~Args,~Error,~Output,~run,world,name,access)) 52 53 type RunResult -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: 54 Completed{instance: Instance,outcome: Sys.Outcome,Error,Output>} 55 RegistrationRejected{instance: Instance,world: W.World,args: Args} 56 57 def ran(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,ports: Ports,result: Sys.RunResult) -> RunResult: 58 match result: 59 case Sys.Completed{registry,outcome}: Completed{bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports),outcome} 60 case Sys.RegistrationRejected{registry,world,args}: RegistrationRejected{bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports),world,args} 61 62 # Managed execution returns its advanced registry into the same instance. 63 def run(~S: Data,~C: Type,~R: Type,~E: Data,~Args: Type,~Error: Data,~Output: Type,~run: W.World -> Args -> Sys.Outcome,Error,Output>,~Ports: Type,~validate: Ports -> U32 -> U32 -> Ports & Bool,~remove: W.World -> Ports -> W.World,instance: Instance,world: W.World,args: Args) -> RunResult: 64 match instance: 65 case Instance{Cleanup.Instance{registry,ports}}: ran(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,ports,Sys.run(~S,~C,~R,~E,~Args,~Error,~Output,~run,registry,world,args))