import Base import ./recovery_use.bend as Use import ./resource_field_binding.bend as Binding import ./world.bend as W import ./owner_product.bend as Product import ./ordinary_recovery_declaration.bend as D import ./ordinary_recoverable_array_resource.bend as F import ./recoverable_array.bend as A import ./recoverable_array_owner.bend as AO import ./recovery_journal.bend as Journal import ./world_resource_owner.bend as WR import ./ordinary_schema.bend as Schema # Canonical selected resource field with generated recovery path; no caller inverse. # Binding supplies the same identity/path pair as ordinary resource-field grants. # Advanced capability-shape projection for identity-indexed recoverable Array

# replacement and checked element swap, with opaque execution owner H. def Ops(~S:Data,~P:Type,~identity:Schema.Resource>,H:Type) -> Type:F.Write # Trusted recovery lens into World resources while threading actual capture and # all other World fields. This is setup plumbing, not a gameplay World accessor. def resource_target(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type) -> Product.Lens,Capture>,R,Capture & WR.Rest>: Product.compose(~Product.Owner,Capture>,~W.World,~Capture,~R,~WR.Rest,~Product.lens(~W.World,~Capture,~Product.Left{}),~WR.lens(~S,~C,~R,~E)) # Trusted composition of World-resource and selected Array-field lenses. # Canonical binding supplies the identity/path pair; siblings/capture stay owned. def target(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>) -> Product.Lens,Capture>,Array

,(Capture & WR.Rest) & FieldRest>: Product.compose(~Product.Owner,Capture>,~R,~(Capture & WR.Rest),~Array

,~FieldRest,~resource_target(~S,~C,~R,~E,~Capture),~fieldLens) def empty(~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,unit:Unit) -> List>:[] def replaced(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,result:Product.Owner,Capture> & List,Capture>,~All)>) -> F.Frame,Capture>,All>: match result: case (state,journal):F.Frame{state,journal} # Advanced recoverable replacement: consume incoming Array

, install it in # the selected field and journal the displaced array in the shared recovery path. # Commit returns displaced owners; rollback restores originals and returns displaced # replacements through the declaration recovery result, not a duplicate live owner. def replace(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,frame:F.Frame,Capture>,All>,incoming:Array

) -> F.Frame,Capture>,All>: match frame: case F.Frame{state,journal}:replaced(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens,~All,~Rest,~path,AO.replace(~Product.Owner,Capture>,~P,~((Capture & WR.Rest) & FieldRest),~target(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens),~All,~Rest,~path,state,journal,incoming)) def swapped(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,result:AO.Attempt,Capture>,All,P>) -> F.Attempt,Capture>,All>,P>: match result: case AO.Applied{state,journal}:F.Applied{F.Frame{state,journal}} case AO.Refused{state,journal,incoming}:F.Refused{F.Frame{state,journal},incoming} # Advanced bounds-checked element replacement consuming P. Applied journals old P; # Refused returns actual incoming P with state/journal preserved when index is out # of bounds. Recovery follows shared journal order; unlike raw Array access this # does not wrap an invalid index into another slot. def swap(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,frame:F.Frame,Capture>,All>,index:U32,incoming:P) -> F.Attempt,Capture>,All>,P>: match frame: case F.Frame{state,journal}:swapped(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens,~All,~Rest,~path,AO.swap(~Product.Owner,Capture>,~P,~((Capture & WR.Rest) & FieldRest),~target(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens),~All,~Rest,~path,state,journal,index,incoming)) # Advanced canonical grant builder hiding resource lens and journal plumbing # behind the identity-indexed recoverable write operations. Does not execute them. def grant(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~identity:Schema.Resource>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,unit:Unit) -> F.Write,Capture>,All>>: F.Write{replace(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens,~All,~Rest,~path),swap(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens,~All,~Rest,~path)} # Derive the recoverable selected Array resource declaration from its canonical # Binding. Initializes empty recovery records and publishes known Write-use metadata # for this resource key. Shared recovery composition controls finish/undo order; # there is no caller-authored inverse or automatic external-effect rollback. # See docs/parity/recovery.md for explicit owned recovery boundaries. def declaration(~S:Data,~C:Type,~E:Data,~Capture:Type,~R:Type,~P:Type,~FieldRest:Type,~fieldLens:Product.Lens,FieldRest>,~identity:Schema.Resource>,~binding:Binding.Binding,FieldRest,fieldLens,identity>) -> D.Declaration,Capture>,List>,Ops(~S,~P,~identity),grant(~S,~C,~E,~Capture,~R,~P,~FieldRest,~fieldLens,~identity)>: D.Described{empty(~Capture,~R,~P,~FieldRest,~fieldLens),[Schema.resource_key(~S,~Array

,identity)],Use.known([Use.Use{Use.Resource{Schema.resource_key(~S,~Array

,identity)},Use.Write{}}])}