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 ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output) -> Declaration -> R,rest:Rest,value:P & Output) -> R & RequestResult:
match value:
case (owner,output):(put(rest,Service.Ready{owner}),Called{output})
def slot_called(~S:Data,~R:Type,~P:Type,~Rest:Type,~Input:Type,~Output:Type,~identity:Identity ,~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.Ready{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 ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,world:W.World ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,transaction:T.Tx ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,~declaration:Declaration ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,_: Declaration ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,_: Declaration ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,_: Declaration ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,value:Rest & Service.Slot ) -> R & List<&2,Service.Status ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,~declaration:Declaration ,~put:Rest -> Service.Slot -> R,~call:P -> Input -> P & Output,~declaration:Declaration,-H:Type> is Type:
Grant{operation:Cap.OwnedRequest,~take:R -> Rest & Service.Slot:
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,~take:R -> Rest & Service.Slot & 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,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 & RequestResult) -> T.Tx,~take:R -> Rest & Service.Slot,~take:R -> Rest & Service.Slot) -> 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) -> 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) -> 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) -> 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>:
match value:
case (rest,Service.Missing{}):(put(rest,Service.Missing{}),[status(~S,~Input,~Output,identity,False{})])
case (rest,Service.Ready{owner}):(put(rest,Service.Ready{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>) -> 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,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) -> Service.Setup:
Service.Setup{[declaration_entry(~S,~R,~P,~Rest,~Input,~Output,~identity,~take,~put,~call,declaration)]}