import Base import ./recovery_use.bend as Use 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 ./ordinary_recoverable_component_capture.bend as Existing 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 World resource target and generated recovery path; no caller inverse. # Nominal recoverable Array

resource capability family with whole-array # replace and bounded element swap. P may be arbitrary affine Type; this does # not provide automatic inverses for unrelated resource mutations. def Ops(~S:Data,~P:Type,~identity:Schema.Resource>,H:Type) -> Type:F.Write # Advanced trusted lens selecting the Array

World resource while # retaining capture and the rest of World. No live owner is copied or exposed. def target(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type) -> Product.Lens,E>,Capture>,Array

,Capture & WR.Rest,E>>: Product.compose(~Product.Owner,E>,Capture>,~W.World,E>,~Capture,~Array

,~WR.Rest,E>,~Product.lens(~W.World,E>,~Capture,~Product.Left{}),~WR.lens(~S,~C,~Array

,~E)) def empty(~Capture:Type,~P:Type,unit:Unit) -> List>:[] def replaced(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,result:Product.Owner,E>,Capture> & List,E>,Capture>,~All)>) -> F.Frame,E>,Capture>,All>: match result: case (state,journal):F.Frame{state,journal} # Replace the whole Array

resource in the shared recovery journal. # Consumes incoming array and returns frame; commit recovers the displaced # array, rollback restores it and recovers the replacement. Capture and other # World owners remain siblings, not independent copies. def replace(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,frame:F.Frame,E>,Capture>,All>,incoming:Array

) -> F.Frame,E>,Capture>,All>: match frame: case F.Frame{state,journal}:replaced(~S,~C,~E,~Capture,~P,~All,~Rest,~path,AO.replace(~Product.Owner,E>,Capture>,~P,~(Capture & WR.Rest,E>),~target(~S,~C,~E,~Capture,~P),~All,~Rest,~path,state,journal,incoming)) def swapped(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,result:AO.Attempt,E>,Capture>,All,P>) -> F.Attempt,E>,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} # Attempt one Array

resource element swap. Out-of-bounds returns Refused # with incoming P and unchanged journal; success records the displaced owner. # Receipt recovery follows the enclosing commit/rollback, not immediate copying. def swap(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,frame:F.Frame,E>,Capture>,All>,index:U32,incoming:P) -> F.Attempt,E>,Capture>,All>,P>: match frame: case F.Frame{state,journal}:swapped(~S,~C,~E,~Capture,~P,~All,~Rest,~path,AO.swap(~Product.Owner,E>,Capture>,~P,~(Capture & WR.Rest,E>),~target(~S,~C,~E,~Capture,~P),~All,~Rest,~path,state,journal,index,incoming)) # Advanced closed grant builder for canonical resource and recovery paths. # Gameplay receives typed replace/swap; it does not author inverses or journals. def grant(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~identity:Schema.Resource>,~All:Type,~Rest:Type,~path:Product.Lens>,Rest>,unit:Unit) -> F.Write,E>,Capture>,All>>: F.Write{replace(~S,~C,~E,~Capture,~P,~All,~Rest,~path),swap(~S,~C,~E,~Capture,~P,~All,~Rest,~path)} # Declare recoverable Array

resource operations using its ordinary # resource identity. Supplies complete access/use metadata and generated # recovery path; performs no resource mutation or allocation of payload values. # Supported domain and receipts: docs/parity/recovery.md. def declaration(~S:Data,~C:Type,~E:Data,~Capture:Type,~P:Type,~identity:Schema.Resource>) -> D.Declaration,E>,Capture>,List>,Ops(~S,~P,~identity),grant(~S,~C,~E,~Capture,~P,~identity)>: D.Described{empty(~Capture,~P),[Schema.resource_key(~S,~Array

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

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