ordinary_recovery_component.bend

bendvy-ecs-core@0.1.0.1 · 49 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.1/recovery_use.bend as Use
3import bendvy-ecs-core@0.1.0.1/world.bend as W
4import bendvy-ecs-core@0.1.0.1/column.bend as Col
5import bendvy-ecs-core@0.1.0.1/owner_product.bend as Product
6import bendvy-ecs-core@0.1.0.1/ordinary_component_binding.bend as B
7import bendvy-ecs-core@0.1.0.1/ordinary_declaration.bend as Identity
8import bendvy-ecs-core@0.1.0.1/ordinary_recovery_declaration.bend as D
9import bendvy-ecs-core@0.1.0.1/ordinary_recoverable_array_resource.bend as F
10import bendvy-ecs-core@0.1.0.1/ordinary_recoverable_component_capture.bend as Existing
11import bendvy-ecs-core@0.1.0.1/recoverable_component.bend as RC
12import bendvy-ecs-core@0.1.0.1/recoverable_component_family.bend as Family
13import bendvy-ecs-core@0.1.0.1/recoverable_owner.bend as RO
14import bendvy-ecs-core@0.1.0.1/recovery_journal.bend as Journal
15# Existing schema take/put/view are trusted provisioning, never gameplay inverses.
16# Nominal recoverable component replacement capability family. P may be
17# affine Type; the grant is bound to its ordinary component identity, not a
18# printable access name or a caller-provided inverse.
19def Ops(~S:Data,~P:Type,~identity:Identity.Declaration<S,P>,H:Type) -> Type:Existing.ComponentWrite<S,P,identity,H>
20def empty(~S:Data,~P:Type,unit:Unit) -> List<RC.Record<S,P>>:[]
21def released(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>,~All:Type,~ReleasedRest:Type,~path:Product.Lens<All,List<RC.Record<S,P>>,ReleasedRest>,capture:Capture,recovery:All,result:W.World<S,C,R,E> & RC.Record<S,P>) -> Product.Owner<W.World<S,C,R,E>,Capture> & All:
22  match result:
23    case (world,record):(Product.Owner{world,capture},RO.append(~All,~RC.Record<S,P>,~ReleasedRest,~path,recovery,record))
24def inverse(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>,~All:Type,~ReleasedRest:Type,~path:Product.Lens<All,List<RC.Record<S,P>>,ReleasedRest>,old:RC.Record<S,P>,success:Bool,state:Product.Owner<W.World<S,C,R,E>,Capture>,recovery:All) -> Product.Owner<W.World<S,C,R,E>,Capture> & All:
25  match success state:
26    case True{} state:(state,RO.append(~All,~RC.Record<S,P>,~ReleasedRest,~path,recovery,old))
27    case False{} Product.Owner{world,capture}:released(~S,~C,~R,~E,~Capture,~Rest,~P,~V,~Token,~take,~put,~view,~binding,~All,~ReleasedRest,~path,capture,recovery,Family.undo(~S,~C,~R,~E,~Rest,~P,~V,~Token,~take,~put,~view,~B.family(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding),old,world))
28def replaced(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>,~All:Type,~ReleasedRest:Type,~path:Product.Lens<All,List<RC.Record<S,P>>,ReleasedRest>,capture:Capture,journal:List<Journal.Action(~Product.Owner<W.World<S,C,R,E>,Capture>,~All)>,result:RC.Replacement<W.World<S,C,R,E>,S,P>) -> Existing.ComponentAttempt<F.Frame<Product.Owner<W.World<S,C,R,E>,Capture>,All>,P>:
29  match result:
30    case RC.Applied{world,old}:Existing.ComponentApplied{F.Frame{Product.Owner{world,capture},(success => state => recovery => inverse(~S,~C,~R,~E,~Capture,~Rest,~P,~V,~Token,~take,~put,~view,~binding,~All,~ReleasedRest,~path,old,success,state,recovery)) <> journal}}
31    case RC.Refused{world,incoming,error}:Existing.ComponentRefused{F.Frame{Product.Owner{world,capture},journal},incoming,error}
32# Replace an optional component owner through the canonical binding.
33# Immediate refusal returns frame, incoming owner and error without adding
34# an inverse. Accepted records the displaced component receipt; commit returns
35# displaced owners, while rollback restores recorded state and recovers
36# replacements. Other World/capture owners are retained through the frame.
37def replace(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>,~All:Type,~ReleasedRest:Type,~path:Product.Lens<All,List<RC.Record<S,P>>,ReleasedRest>,frame:F.Frame<Product.Owner<W.World<S,C,R,E>,Capture>,All>,handle:W.Handle<S>,incoming:Maybe<P>) -> Existing.ComponentAttempt<F.Frame<Product.Owner<W.World<S,C,R,E>,Capture>,All>,P>:
38  match frame:
39    case F.Frame{Product.Owner{world,capture},journal}:replaced(~S,~C,~R,~E,~Capture,~Rest,~P,~V,~Token,~take,~put,~view,~binding,~All,~ReleasedRest,~path,capture,journal,Family.replace(~S,~C,~R,~E,~Rest,~P,~V,~Token,~take,~put,~view,B.family(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding),world,handle,incoming))
40# Advanced canonical grant builder specialized at the complete recovery
41# product/path. Raw erased lenses must match the declaration; gameplay
42# receives the resulting nominal component operation, not journal callbacks.
43def grant(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>,~All:Type,~ReleasedRest:Type,~path:Product.Lens<All,List<RC.Record<S,P>>,ReleasedRest>,unit:Unit) -> Existing.ComponentWrite<S,P,B.identity(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding),F.Frame<Product.Owner<W.World<S,C,R,E>,Capture>,All>>:Existing.ComponentWrite{replace(~S,~C,~R,~E,~Capture,~Rest,~P,~V,~Token,~take,~put,~view,~binding,~All,~ReleasedRest,~path)}
44# Build a complete typed component-recovery declaration from its ordinary
45# binding. Derives access/use metadata and the canonical recovery accumulator
46# without executing an operation. Compose with other leaves using
47# ordinary-recovery-declaration.both; semantics: docs/parity/recovery.md.
48def declaration(~S:Data,~C:Type,~R:Type,~E:Data,~Capture:Type,~Rest:Type,~P:Type,~V:Data,~Token:Data,~take:C -> Rest & Col.Column<S,P>,~put:Rest -> Col.Column<S,P> -> C,~view:P -> P & V,~binding:B.Binding<S,C,Rest,P,V,Token,take,put,view>) -> D.Declaration<S,Product.Owner<W.World<S,C,R,E>,Capture>,List<RC.Record<S,P>>,Ops(~S,~P,~B.identity(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding)),grant(~S,~C,~R,~E,~Capture,~Rest,~P,~V,~Token,~take,~put,~view,~binding)>:
49  D.Described{empty(~S,~P),[Identity.key(~S,~P,B.identity(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding))],Use.known([Use.Use{Use.Component{Identity.key(~S,~P,B.identity(~S,~C,~Rest,~P,~V,~Token,~take,~put,~view,binding))},Use.Write{}}])}