import Base import world.bend as W import system.bend as Sys import system_cleanup.bend as Cleanup # Closed schema factory indices are erased; registry and ports remain affine. type Instance -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: Instance{owned: Cleanup.Instance} type Disposal -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: Disposed{world: W.World} Rejected{world: W.World,instance: Instance} # Schema authors bind these indices once in their ordinary registration factory. 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: Instance{Cleanup.Instance{registry,ports}} 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: match result: case Cleanup.Cleaned{world}: Disposed{world} case Cleanup.CleanupRejected{world,instance}: Rejected{world,Instance{instance}} # Callers supply only their actual instance and world; no runtime metadata callbacks. 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: match instance: 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)) type Registration -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: Registered{world: W.World,instance: Instance} RegistrationFailed{world: W.World} 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: match observed: case (world,ports): Registered{world,bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports)} 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: match identified: case (registry,id): created_ports(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,create(namespace,id,world)) 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: match observed: 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)) 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: match result: case Sys.RegistrationFailed{world}: RegistrationFailed{world} 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)) # The schema factory specializes create/validate/remove once. Failed registration # constructs no ports; successful ports use the actual namespace and registry id. 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: 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)) type RunResult -> Args -> Sys.Outcome,Error,Output>,Ports: Type,-validate: Ports -> U32 -> U32 -> Ports & Bool,-remove: W.World -> Ports -> W.World> is Type: Completed{instance: Instance,outcome: Sys.Outcome,Error,Output>} RegistrationRejected{instance: Instance,world: W.World,args: Args} 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: match result: case Sys.Completed{registry,outcome}: Completed{bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports),outcome} case Sys.RegistrationRejected{registry,world,args}: RegistrationRejected{bind(~S,~C,~R,~E,~Args,~Error,~Output,~run,~Ports,~validate,~remove,registry,ports),world,args} # Managed execution returns its advanced registry into the same instance. 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: match instance: 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))