import Base import ./ordinary_schema.bend as Schema import ./owner_product.bend as Product import ./world.bend as W import ./inspector.bend as I import ./check.bend as K import ./capabilities.bend as Cap # Storage absence is distinct from an optional value of a present resource. # Identity still names P; the physical field holds its optional affine owner. type Slot<-S:Data,-P:Type,-identity:Schema.Resource> is Type: Missing{} Present{owner:P} type Declaration<-S:Data,-R:Type,-P:Type,-Rest:Type,-V:Data,-identity:Schema.Resource,-lens:Product.Lens,Rest>,-view:P -> P & V> is Data: Declaration{} def viewed(~S:Data,~R:Type,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,rest:Rest,result:P & V) -> R & Maybe<&2,V>: match result: case (owner,value):(Product.put_field(~R,~Slot,~Rest,lens)(rest,Present{owner}),Some{value}) def selected(~S:Data,~R:Type,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,result:Rest & Slot) -> R & Maybe<&2,V>: match result: case (rest,Missing{}):(Product.put_field(~R,~Slot,~Rest,lens)(rest,Missing{}),None{}) case (rest,Present{owner}):viewed(~S,~R,~P,~Rest,~V,~identity,~lens,rest,view(owner)) def project(~S:Data,~R:Type,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,resource:R) -> R & Maybe<&2,V>: selected(~S,~R,~P,~Rest,~V,~identity,~lens,~view,Product.get_field(~R,~Slot,~Rest,lens)(resource)) def observe(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,~declaration:Declaration,world:W.World) -> W.World & Maybe<&2,V>: W.observe_resource(~S,~C,~R,~E,~Maybe<&2,V>,~project(~S,~R,~P,~Rest,~V,~identity,~lens,~view),world) def inspect(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,~declaration:Declaration) -> Cap.ValueRead,Maybe<&2,V>>: I.read_resource(~S,~C,~R,~E,~Maybe<&2,V>,~project(~S,~R,~P,~Rest,~V,~identity,~lens,~view)) def condition(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,~declaration:Declaration) -> Cap.ValueRead,Maybe<&2,V>>: K.resource_grant(~S,~C,~R,~E,~Maybe<&2,V>,~project(~S,~R,~P,~Rest,~V,~identity,~lens,~view)) # Preflight examines only the slot; it must not invoke the value presenter. def slot_available(~S:Data,~R:Type,~P:Type,~Rest:Type,~identity:Schema.Resource,~lens:Product.Lens,Rest>,result:Rest & Slot) -> R & List<&2,String>: match result: case (rest,Missing{}):(Product.put_field(~R,~Slot,~Rest,lens)(rest,Missing{}),[Schema.resource_key(~S,~P,identity)]) case (rest,Present{owner}):(Product.put_field(~R,~Slot,~Rest,lens)(rest,Present{owner}),[]) def availability(~S:Data,~R:Type,~P:Type,~Rest:Type,~identity:Schema.Resource,~lens:Product.Lens,Rest>,resource:R) -> R & List<&2,String>: slot_available(~S,~R,~P,~Rest,~identity,~lens,Product.get_field(~R,~Slot,~Rest,lens)(resource)) def required(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~identity:Schema.Resource,~lens:Product.Lens,Rest>,~view:P -> P & V,~declaration:Declaration,world:W.World) -> W.World & List<&2,String>: W.observe_resource(~S,~C,~R,~E,~List<&2,String>,~availability(~S,~R,~P,~Rest,~identity,~lens),world)