1 import Base 2 import ./world.bend as W 3 import ./column.bend as Col 4 import ./system.bend as Sys 5 import ./transaction.bend as T 6 import ./ordinary_declaration.bend as Declaration 7 import ./owner_product.bend as Product 8 import ./capabilities.bend as Cap 9 import ./ordinary_recoverable_array_resource.bend as O 10 import ./recoverable_array.bend as A 11 import ./recoverable_array_owner.bend as AO 12 import ./recoverable_owner.bend as RO 13 import ./recoverable_component.bend as RC 14 import ./recovery_journal.bend as Journal 15 # Explicit opt-in recovery for captures. The normal #50 entry is unchanged. 16 type ComponentAttempt is Type: 17 ComponentApplied{owner:H} 18 ComponentRefused{owner:H,incoming:Maybe

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

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

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

) -> ComponentAttempt: 27 component_function(~S,~P,~identity,~H,cap)(owner,handle,incoming) 28 def capture_replace_function(~Q:Type,~H:Type,value:CaptureWrite) -> H -> Array -> H: 29 match value: 30 case CaptureWrite{f,_}:f 31 def capture_swap_function(~Q:Type,~H:Type,value:CaptureWrite) -> H -> U32 -> Q -> O.Attempt: 32 match value: 33 case CaptureWrite{_,f}:f 34 def capture_replace(~Q:Type,~H:Type,~cap:CaptureWrite,owner:H,incoming:Array) -> H: 35 capture_replace_function(~Q,~H,cap)(owner,incoming) 36 def capture_swap(~Q:Type,~H:Type,~cap:CaptureWrite,owner:H,index:U32,incoming:Q) -> O.Attempt: 37 capture_swap_function(~Q,~H,cap)(owner,index,incoming) 38 def Recovery(~S:Data,~P:Type,~Q:Type) -> Type:Product.Owner>,List>> 39 def Ops(~S:Data,~P:Type,~Q:Type,~identity:Declaration.Declaration,H:Type) -> Type: 40 Cap.Bundle,CaptureWrite> 41 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>>: 42 match recovery result: 43 case Product.Owner{components,captures},(world,record):(Product.Owner{world,capture},Product.Owner{record <> components,captures}) 44 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>>: 45 match success state recovery: 46 case True{} state Product.Owner{components,captures}:(state,Product.Owner{old <> components,captures}) 47 case False{} Product.Owner{world,capture} recovery:component_released(~S,~P,~Q,~R,~E,capture,recovery,RC.undo(~S,~P,~R,~E,old,world)) 48 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>: 49 match result: 50 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}} 51 case RC.Refused{world,incoming,error}:ComponentRefused{O.Frame{Product.Owner{world,capture},journal},incoming,error} 52 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>: 53 match owner: 54 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)) 55 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>>>: 56 match result: 57 case (state,journal):O.Frame{state,journal} 58 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>>>: 59 match owner: 60 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)) 61 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>: 62 match result: 63 case AO.Applied{state,journal}:O.Applied{O.Frame{state,journal}} 64 case AO.Refused{state,journal,incoming}:O.Refused{O.Frame{state,journal},incoming} 65 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>: 66 match owner: 67 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)) 68 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>>>): 69 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)}} 70 type Request is Type:Request{args:Args,capture:Array} 71 type Reply is Type: 72 Reply{outcome:T.Outcome,output:Out,capture:Array,recovery:Product.Owner>,List>>} 73 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>: 74 match result: 75 case (Product.Owner{world,capture},recovery):Sys.Succeeded{world,Reply{outcome,output,capture,recovery}} 76 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>: 77 match result: 78 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{[],[]})) 79 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>: 80 match result: 81 case (O.Frame{state,journal},output):completing(~S,~P,~Q,~R,~E,~Error,~Out,state,journal,output) 82 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>: 83 match request: 84 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},[]})) 85 type Owner is Type:Owner{registry:Registry,capture:Array} 86 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> 87 type Defined is Type: 88 Defined{world:World,owner:Owned} 89 DefinitionRejected{world:World,capture:Array} 90 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>: 91 match result: 92 case Sys.Registered{world,registry}:Defined{world,Owner{registry,capture}} 93 case Sys.RegistrationFailed{world}:DefinitionRejected{world,capture} 94 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>: 95 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)])) 96 type Run is Type: 97 Completed{world:World,owner:Owned,outcome:T.Outcome,output:Out,recovery:Recovery} 98 RegistrationRejected{world:World,owner:Owned,args:Args} 99 TransportFailed{world:World} 100 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>>>: 101 match result: 102 case (world,registry):Completed{world,Owner{registry,capture},outcome,output,recovery} 103 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>>>: 104 match result: 105 case Sys.RegistrationRejected{registry,world,Request{args,capture}}:RegistrationRejected{world,Owner{registry,capture},args} 106 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)) 107 case Sys.Completed{_,Sys.Failed{world,_}}:TransportFailed{world} 108 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>>>: 109 match owner: 110 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}))