import Base import ./world.bend as W import ./column.bend as Col import ./system.bend as Sys import ./transaction.bend as T import ./ordinary_declaration.bend as Declaration import ./owner_product.bend as Product import ./capabilities.bend as Cap import ./ordinary_recoverable_array_resource.bend as O import ./recoverable_array.bend as A import ./recoverable_array_owner.bend as AO import ./recoverable_owner.bend as RO import ./recoverable_component.bend as RC import ./recovery_journal.bend as Journal # Explicit opt-in recovery for captures. The normal #50 entry is unchanged. type ComponentAttempt is Type: ComponentApplied{owner:H} ComponentRefused{owner:H,incoming:Maybe

,error:W.WorldError} type ComponentWrite<-S:Data,-P:Type,-identity:Declaration.Declaration,-H:Type> is Type: ComponentWrite{replace:H -> W.Handle -> Maybe

-> ComponentAttempt} type CaptureWrite<-Q:Type,-H:Type> is Type: CaptureWrite{replace:H -> Array -> H,swap:H -> U32 -> Q -> O.Attempt} def component_function(~S:Data,~P:Type,~identity:Declaration.Declaration,~H:Type,value:ComponentWrite) -> H -> W.Handle -> Maybe

-> ComponentAttempt: match value: case ComponentWrite{f}:f def component_replace(~S:Data,~P:Type,~identity:Declaration.Declaration,~H:Type,~cap:ComponentWrite,owner:H,handle:W.Handle,incoming:Maybe

) -> ComponentAttempt: component_function(~S,~P,~identity,~H,cap)(owner,handle,incoming) def capture_replace_function(~Q:Type,~H:Type,value:CaptureWrite) -> H -> Array -> H: match value: case CaptureWrite{f,_}:f def capture_swap_function(~Q:Type,~H:Type,value:CaptureWrite) -> H -> U32 -> Q -> O.Attempt: match value: case CaptureWrite{_,f}:f def capture_replace(~Q:Type,~H:Type,~cap:CaptureWrite,owner:H,incoming:Array) -> H: capture_replace_function(~Q,~H,cap)(owner,incoming) def capture_swap(~Q:Type,~H:Type,~cap:CaptureWrite,owner:H,index:U32,incoming:Q) -> O.Attempt: capture_swap_function(~Q,~H,cap)(owner,index,incoming) def Recovery(~S:Data,~P:Type,~Q:Type) -> Type:Product.Owner>,List>> def Ops(~S:Data,~P:Type,~Q:Type,~identity:Declaration.Declaration,H:Type) -> Type: Cap.Bundle,CaptureWrite> def component_released(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,capture:Array,recovery:Product.Owner>,List>>,result:W.World,R,E> & RC.Record) -> Product.Owner,R,E>,Array> & Product.Owner>,List>>: match recovery result: case Product.Owner{components,captures},(world,record):(Product.Owner{world,capture},Product.Owner{record <> components,captures}) def component_inverse(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,old:RC.Record,success:Bool,state:Product.Owner,R,E>,Array>,recovery:Product.Owner>,List>>) -> Product.Owner,R,E>,Array> & Product.Owner>,List>>: match success state recovery: case True{} state Product.Owner{components,captures}:(state,Product.Owner{old <> components,captures}) case False{} Product.Owner{world,capture} recovery:component_released(~S,~P,~Q,~R,~E,capture,recovery,RC.undo(~S,~P,~R,~E,old,world)) def component_changed(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,capture:Array,journal:List,R,E>,Array>,~Product.Owner>,List>>)>,result:RC.Replacement,R,E>,S,P>) -> ComponentAttempt,R,E>,Array>,Product.Owner>,List>>>,P>: match result: case RC.Applied{world,old}:ComponentApplied{O.Frame{Product.Owner{world,capture},(success => s => r => component_inverse(~S,~P,~Q,~R,~E,old,success,s,r)) <> journal}} case RC.Refused{world,incoming,error}:ComponentRefused{O.Frame{Product.Owner{world,capture},journal},incoming,error} def frame_component(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,owner:O.Frame,R,E>,Array>,Product.Owner>,List>>>,handle:W.Handle,incoming:Maybe

) -> ComponentAttempt,R,E>,Array>,Product.Owner>,List>>>,P>: match owner: case O.Frame{Product.Owner{world,capture},journal}:component_changed(~S,~P,~Q,~R,~E,capture,journal,RC.replace(~S,~P,~R,~E,Declaration.key(~S,~P,identity),world,handle,incoming)) def frame_replaced(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,result:Product.Owner,R,E>,Array> & List,R,E>,Array>,~Product.Owner>,List>>)>) -> O.Frame,R,E>,Array>,Product.Owner>,List>>>: match result: case (state,journal):O.Frame{state,journal} def frame_capture_replace(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,owner:O.Frame,R,E>,Array>,Product.Owner>,List>>>,incoming:Array) -> O.Frame,R,E>,Array>,Product.Owner>,List>>>: match owner: case O.Frame{state,journal}:frame_replaced(~S,~P,~Q,~R,~E,AO.replace(~Product.Owner,R,E>,Array>,~Q,~W.World,R,E>,~Product.lens(~W.World,R,E>,~Array,~Product.Right{}),~Product.Owner>,List>>,~List>,~Product.lens(~List>,~List>,~Product.Right{}),state,journal,incoming)) def frame_swapped(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,result:AO.Attempt,R,E>,Array>,Product.Owner>,List>>,Q>) -> O.Attempt,R,E>,Array>,Product.Owner>,List>>>,Q>: match result: case AO.Applied{state,journal}:O.Applied{O.Frame{state,journal}} case AO.Refused{state,journal,incoming}:O.Refused{O.Frame{state,journal},incoming} def frame_capture_swap(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,owner:O.Frame,R,E>,Array>,Product.Owner>,List>>>,index:U32,incoming:Q) -> O.Attempt,R,E>,Array>,Product.Owner>,List>>>,Q>: match owner: case O.Frame{state,journal}:frame_swapped(~S,~P,~Q,~R,~E,AO.swap(~Product.Owner,R,E>,Array>,~Q,~W.World,R,E>,~Product.lens(~W.World,R,E>,~Array,~Product.Right{}),~Product.Owner>,List>>,~List>,~Product.lens(~List>,~List>,~Product.Right{}),state,journal,index,incoming)) def grant(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration) -> Ops(~S,~P,~Q,~identity,O.Frame,R,E>,Array>,Product.Owner>,List>>>): Cap.Bundle{ComponentWrite{frame_component(~S,~P,~Q,~R,~E,~identity)},CaptureWrite{frame_capture_replace(~S,~P,~Q,~R,~E),frame_capture_swap(~S,~P,~Q,~R,~E)}} type Request is Type:Request{args:Args,capture:Array} type Reply is Type: Reply{outcome:T.Outcome,output:Out,capture:Array,recovery:Product.Owner>,List>>} def finished(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~Error:Data,~Out:Type,outcome:T.Outcome,output:Out,result:Product.Owner,R,E>,Array> & Product.Owner>,List>>) -> Sys.Outcome,R,E>,Unit,Reply>: match result: case (Product.Owner{world,capture},recovery):Sys.Succeeded{world,Reply{outcome,output,capture,recovery}} def completing(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~Error:Data,~Out:Type,state:Product.Owner,R,E>,Array>,journal:List,R,E>,Array>,~Product.Owner>,List>>)>,result:T.Outcome & Out) -> Sys.Outcome,R,E>,Unit,Reply>: match result: case (+outcome,output):finished(~S,~P,~Q,~R,~E,~Error,~Out,outcome,output,Journal.finish(~Product.Owner,R,E>,Array>,~Product.Owner>,List>>,journal,O.succeeded(~Error,outcome),state,Product.Owner{[],[]})) def returned(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~Error:Data,~Out:Type,result:O.Frame,R,E>,Array>,Product.Owner>,List>>> & (T.Outcome & Out)) -> Sys.Outcome,R,E>,Unit,Reply>: match result: case (O.Frame{state,journal},output):completing(~S,~P,~Q,~R,~E,~Error,~Out,state,journal,output) def runner(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),world:W.World,R,E>,request:Request) -> Sys.Outcome,R,E>,Unit,Reply>: match request: case Request{args,capture}:returned(~S,~P,~Q,~R,~E,~Error,~Out,body(O.Frame,R,E>,Array>,Product.Owner>,List>>>,grant(~S,~P,~Q,~R,~E,~identity),args,O.Frame{Product.Owner{world,capture},[]})) type Owner is Type:Owner{registry:Registry,capture:Array} def Owned(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out)) -> Type:Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q> type Defined is Type: Defined{world:World,owner:Owned} DefinitionRejected{world:World,capture:Array} def registered(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),capture:Array,result:Sys.RegistrationResult,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>) -> Defined,R,E>,Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,Q>: match result: case Sys.Registered{world,registry}:Defined{world,Owner{registry,capture}} case Sys.RegistrationFailed{world}:DefinitionRejected{world,capture} def define(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),world:W.World,R,E>,name:String,capture:Array) -> Defined,R,E>,Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,Q>: registered(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body,capture,Sys.register(~S,~Col.Column,~R,~E,~Request,~Unit,~Reply,~runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body),world,name,[Declaration.key(~S,~P,identity)])) type Run is Type: Completed{world:World,owner:Owned,outcome:T.Outcome,output:Out,recovery:Recovery} RegistrationRejected{world:World,owner:Owned,args:Args} TransportFailed{world:World} def done(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),outcome:T.Outcome,output:Out,capture:Array,recovery:Product.Owner>,List>>,result:W.World,R,E> & Sys.Registry,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>) -> Run,R,E>,Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,Args,Error,Out,Product.Owner>,List>>>: match result: case (world,registry):Completed{world,Owner{registry,capture},outcome,output,recovery} def completed(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),result:Sys.RunResult,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>) -> Run,R,E>,Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,Args,Error,Out,Product.Owner>,List>>>: match result: case Sys.RegistrationRejected{registry,world,Request{args,capture}}:RegistrationRejected{world,Owner{registry,capture},args} case Sys.Completed{registry,Sys.Succeeded{world,Reply{+outcome,output,capture,recovery}}}:done(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body,outcome,output,capture,recovery,(world,registry)) case Sys.Completed{_,Sys.Failed{world,_}}:TransportFailed{world} def run(~S:Data,~P:Type,~Q:Type,~R:Type,~E:Data,~identity:Declaration.Declaration,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-caps:Ops(~S,~P,~Q,~identity,H) -> Args -> H -> H & (T.Outcome & Out),owner:Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,world:W.World,R,E>,args:Args) -> Run,R,E>,Owner,R,E,Request,Unit,Reply,runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body)>,Q>,Args,Error,Out,Product.Owner>,List>>>: match owner: case Owner{registry,capture}:completed(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body,Sys.run(~S,~Col.Column,~R,~E,~Request,~Unit,~Reply,~runner(~S,~P,~Q,~R,~E,~identity,~Args,~Error,~Out,~body),registry,world,Request{args,capture}))