1 import Base 2 import ./owner_product.bend as Product 3 import ./world.bend as W 4 # Canonical product lenses preserve affine siblings during selected-field operations. 5 def viewed(~R:Type,~P:Type,~Rest:Type,~V:Data,~lens:Product.Lens,rest:Rest,result:P & V) -> R & V: 6 match result: 7 case (part,value):(Product.put_field(~R,~P,~Rest,lens)(rest,part),value) 8 def taken(~R:Type,~P:Type,~Rest:Type,~V:Data,~lens:Product.Lens,~view:P -> P & V,result:Rest & P) -> R & V: 9 match result: 10 case (rest,part):viewed(~R,~P,~Rest,~V,~lens,rest,view(part)) 11 def project(~R:Type,~P:Type,~Rest:Type,~V:Data,~lens:Product.Lens,~view:P -> P & V,resource:R) -> R & V: 12 taken(~R,~P,~Rest,~V,~lens,~view,Product.get_field(~R,~P,~Rest,lens)(resource)) 13 def replacing(~R:Type,~P:Type,~Rest:Type,~lens:Product.Lens,replacement:P,result:Rest & P) -> R & P: 14 match result: 15 case (rest,old):(Product.put_field(~R,~P,~Rest,lens)(rest,replacement),old) 16 def replace(~R:Type,~P:Type,~Rest:Type,~lens:Product.Lens,replacement:P,resource:R) -> R & P: 17 replacing(~R,~P,~Rest,~lens,replacement,Product.get_field(~R,~P,~Rest,lens)(resource)) 18 # Runtime replacement owners cannot be captured in an erased projector template. 19 # Trusted engine boundary threads the same World fields through the core join. 20 def replace_world(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~lens:Product.Lens,replacement:P,world:W.World) -> W.World & P: 21 match world: 22 case W.World{+namespace,allocator,store,resource,+events,pending,+registrations,+nextSystemId,+clock,lifecycle}:W.observe_resource_finish(~S,~C,~R,~E,~P,replace(~R,~P,~Rest,~lens,replacement,resource),namespace,allocator,store,events,pending,registrations,nextSystemId,clock,lifecycle) 23 def restored(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,result:W.World & P) -> W.World: 24 match result: 25 case (world,_):world 26 def restore(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~lens:Product.Lens,old:P,world:W.World) -> W.World: 27 restored(~S,~C,~R,~E,~P,replace_world(~S,~C,~R,~E,~P,~Rest,~lens,old,world))