import Base import ./clock_state.bend as Clock import ./capabilities.bend as Cap import ./ordinary_service_declaration.bend as Service import ./schema_fragments.bend as Fragment import ./schedule_provision.bend as Provision import ./world.bend as W import ./transaction.bend as T import ./trace_facts.bend as Trace import ./deferred_command.bend as Deferred import ./entity_allocator.bend as Alloc type Identity<-S:Data,-Input:Type,-Output:Type> is Data: Identity{id:U32,key:String} type Declaration<-S:Data,-R:Type,-P:Type,-Rest:Type,-Input:Type,-Output:Type,-identity:Identity,-take:R -> Rest & Service.Slot

,-put:Rest -> Service.Slot

-> R,-call:P -> Input -> P & Output> is Data: Declaration{} type RequestResult<-Input:Type,-Output:Type> is Type: Called{output:Output} Unavailable{input:Input} type Grant<-S:Data,-Input:Type,-Output:Type,-identity:Identity,-H:Type> is Type: Grant{operation:Cap.OwnedRequest>} def declare(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output) -> Declaration: Declaration{} def key(~S:Data,~Input:Type,~Output:Type,identity:Identity) -> String: match identity: case Identity{_,key}:key def requirement(~S:Data,~Input:Type,~Output:Type,identity:Identity) -> Provision.Requirement: match identity: case Identity{id,_}:Provision.Service{id} def entry(~S:Data,~Input:Type,~Output:Type,identity:Identity) -> Fragment.Entry: match identity: case Identity{_,+key}:Fragment.Entry{Fragment.Service{},key,key} def called(~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~put:Rest -> Service.Slot

-> R,rest:Rest,value:P & Output) -> R & RequestResult: match value: case (owner,output):(put(rest,Service.BendvyReady{owner}),Called{output}) def slot_called(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,input:Input,value:Rest & Service.Slot

) -> R & RequestResult: match value: case (rest,Service.Missing{}):(put(rest,Service.Missing{}),Unavailable{input}) case (rest,Service.BendvyReady{owner}):called(~R,~P,~Rest,~Input,~Output,~put,rest,call(owner,input)) def world_called(~S:Data,~C:Type,~R:Type,~E:Data,~Input:Type,~Output:Type,+namespace:U32,allocator:Alloc.Allocator,store:C,+events:List<&2,E>,pending:List>>,+registrations:List<&2,W.RegistrationMeta>,+nextSystem:U32,+clock:Clock.State,value:R & RequestResult) -> W.World & RequestResult: match value: case (resource,result):(W.World{namespace,allocator,store,resource,events,pending,registrations,nextSystem,clock},result) def request_world(~C:Type,~E:Data,~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,world:W.World,input:Input) -> W.World & RequestResult: match world: case W.World{+namespace,allocator,store,resource,+events,pending,+registrations,+nextSystem,+clock}:world_called(~S,~C,~R,~E,~Input,~Output,namespace,allocator,store,events,pending,registrations,nextSystem,clock,slot_called(~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call,input,take(resource))) def transaction_called(~S:Data,~C:Type,~R:Type,~E:Data,~Input:Type,~Output:Type,undo:List<(W.World -> W.World)>,commands:List>>,events:List<&2,E>,trace:Trace.State,value:W.World & RequestResult) -> T.Tx,E> & RequestResult: match value: case (world,result):(T.Tx{world,undo,commands,events,trace},result) def request_transaction(~C:Type,~E:Data,~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,transaction:T.Tx,E>,input:Input) -> T.Tx,E> & RequestResult: match transaction: case T.Tx{world,undo,commands,events,trace}:transaction_called(~S,~C,~R,~E,~Input,~Output,undo,commands,events,trace,request_world(~C,~E,~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call,world,input)) def grant(~C:Type,~E:Data,~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,~declaration:Declaration) -> Grant,E>>: Grant{Cap.OwnedRequest{request_transaction(~C,~E,~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call)}} def request_field(~S:Data,~Input:Type,~Output:Type,~identity:Identity,~H:Type,value:Grant) -> H -> Input -> H & RequestResult: match value: case Grant{operation}:Cap.owned_request_field(~H,~Input,~RequestResult,operation) def request(~S:Data,~Input:Type,~Output:Type,~identity:Identity,~H:Type,~operation:Grant,owner:H,input:Input) -> H & RequestResult: request_field(~S,~Input,~Output,~identity,~H,operation)(owner,input) def declaration_key(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,_: Declaration) -> String: key(~S,~Input,~Output,identity) def declaration_requirement(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,_: Declaration) -> Provision.Requirement: requirement(~S,~Input,~Output,identity) def declaration_entry(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,_: Declaration) -> Fragment.Entry: entry(~S,~Input,~Output,identity) def status(~S:Data,~Input:Type,~Output:Type,identity:Identity,provided:Bool) -> Service.Status: match identity: case Identity{id,key}:Service.Status{id,key,provided} def slot_observed(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,value:Rest & Service.Slot

) -> R & List<&2,Service.Status>: match value: case (rest,Service.Missing{}):(put(rest,Service.Missing{}),[status(~S,~Input,~Output,identity,False{})]) case (rest,Service.BendvyReady{owner}):(put(rest,Service.BendvyReady{owner}),[status(~S,~Input,~Output,identity,True{})]) def world_observed_join(~S:Data,~C:Type,~R:Type,~E:Data,+namespace:U32,allocator:Alloc.Allocator,store:C,+events:List<&2,E>,pending:List>>,+registrations:List<&2,W.RegistrationMeta>,+nextSystem:U32,+clock:Clock.State,value:R & List<&2,Service.Status>) -> W.World & List<&2,Service.Status>: match value: case (resource,statuses):(W.World{namespace,allocator,store,resource,events,pending,registrations,nextSystem,clock},statuses) def world_observed(~C:Type,~E:Data,~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,~declaration:Declaration,world:W.World) -> W.World & List<&2,Service.Status>: match world: case W.World{+namespace,allocator,store,resource,+events,pending,+registrations,+nextSystem,+clock}:world_observed_join(~S,~C,~R,~E,namespace,allocator,store,events,pending,registrations,nextSystem,clock,slot_observed(~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call,take(resource))) def setup(~C:Type,~E:Data,~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity,~take:R -> Rest & Service.Slot

,~put:Rest -> Service.Slot

-> R,~call:P -> Input -> P & Output,~declaration:Declaration) -> Service.Setup: Service.Setup{[declaration_entry(~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call,declaration)]}