ordinary_resource_field_system.bend
bendvy-ecs-core@0.1.0.4 · 111 lines · 22 definitions
1import Base
2import bendvy-ecs-core@0.1.0.4/trace_facts.bend as TraceFacts
3import bendvy-ecs-core@0.1.0.4/resource_field_binding.bend as B
4import bendvy-ecs-core@0.1.0.4/resource_field.bend as Field
5import bendvy-ecs-core@0.1.0.4/ordinary_schema.bend as Schema
6import bendvy-ecs-core@0.1.0.4/ordinary_query_declaration.bend as Q
7import bendvy-ecs-core@0.1.0.4/ordinary_resource_system.bend as Whole
8import bendvy-ecs-core@0.1.0.4/owner_product.bend as Product
9import bendvy-ecs-core@0.1.0.4/world.bend as W
10import bendvy-ecs-core@0.1.0.4/system.bend as Sys
11import bendvy-ecs-core@0.1.0.4/transaction.bend as T
12import bendvy-ecs-core@0.1.0.4/capabilities.bend as Cap
13
14
15
16type Read<-S:Data,-P:Type,-identity:Schema.Resource<S,P>,-H:Type,-V:Data> is Type:
17 Read{operation:Cap.ValueRead<H,V>}
18
19
20type Write<-S:Data,-P:Type,-identity:Schema.Resource<S,P>,-H:Type,-V:Data> is Type:
21 Write{operation:Cap.ValueWrite<H,P,V>}
22
23def read_field(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,cap:Read<S,P,identity,H,V>) -> H -> H & V:
24 match cap:
25 case Read{operation}:Cap.value_read_field(~H,~V,operation)
26
27def read_value(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,~cap:Read<S,P,identity,H,V>,owner:H) -> H & V:
28 read_field(~S,~P,~identity,~H,~V,cap)(owner)
29
30def get_field(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,cap:Write<S,P,identity,H,V>) -> H -> H & V:
31 match cap:
32 case Write{operation}:Cap.value_write_read_field(~H,~P,~V,operation)
33
34def get(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,~cap:Write<S,P,identity,H,V>,owner:H) -> H & V:
35 get_field(~S,~P,~identity,~H,~V,cap)(owner)
36
37def set_field(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,cap:Write<S,P,identity,H,V>) -> H -> P -> H:
38 match cap:
39 case Write{operation}:Cap.value_write_field(~H,~P,~V,operation)
40
41
42def set(~S:Data,~P:Type,~identity:Schema.Resource<S,P>,~H:Type,~V:Data,~cap:Write<S,P,identity,H,V>,owner:H,replacement:P) -> H:
43 set_field(~S,~P,~identity,~H,~V,cap)(owner,replacement)
44
45
46type Declaration<-S:Data,-H:Type,-Ops:Type> is Type:
47 Declaration{grant:Ops,clauses:List<&2,Q.Clause<S>>}
48
49
50def read(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~lens:Product.Lens<R,P,Rest>,~identity:Schema.Resource<S,P>,~binding:B.Binding<S,R,P,Rest,lens,identity>,~view:P -> P & V) -> Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Read<S,P,identity,T.Tx<W.World<S,C,R,E>,E>,V>>:
51 Declaration{Read{Field.read(~S,~C,~R,~E,~P,~Rest,~V,~lens,~view)},[Q.Clause{Schema.resource_key(~S,~P,identity),Q.Read{}}]}
52
53
54
55def write(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~Rest:Type,~V:Data,~lens:Product.Lens<R,P,Rest>,~identity:Schema.Resource<S,P>,~binding:B.Binding<S,R,P,Rest,lens,identity>,~view:P -> P & V) -> Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Write<S,P,identity,T.Tx<W.World<S,C,R,E>,E>,V>>:
56 Declaration{Write{Field.write(~S,~C,~R,~E,~P,~Rest,~V,~lens,~view)},[Q.Clause{Schema.resource_key(~S,~P,identity),Q.Write{}}]}
57
58
59def pair(~S:Data,~H:Type,~L:Type,~R:Type,left:Declaration<S,H,L>,right:Declaration<S,H,R>) -> Declaration<S,H,Cap.Bundle<L,R>>:
60 match left right:
61 case Declaration{l,lc} Declaration{r,rc}:Declaration{Cap.Bundle{l,r},List.append(&2,Q.Clause<S>,lc,rc)}
62
63def grant(~S:Data,~H:Type,~Ops:Type,value:Declaration<S,H,Ops>) -> Ops:
64 match value:
65 case Declaration{grant,_}:grant
66
67def clauses(~S:Data,~H:Type,~Ops:Type,value:Declaration<S,H,Ops>) -> List<&2,Q.Clause<S>>:
68 match value:
69 case Declaration{_,clauses}:clauses
70
71
72def access(~S:Data,clauses:List<&2,Q.Clause<S>>) -> List<&2,String>:
73 match clauses:
74 case []:[]
75 case Q.Clause{name,_} <> rest:name <> access(~S,rest)
76
77
78
79def runner(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~declaration:Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Ops(T.Tx<W.World<S,C,R,E>,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome<Error> & Out),world:W.World<S,C,R,E>,args:Args) -> Sys.Outcome<W.World<S,C,R,E>,Error,Out>:
80 Whole.finished(~S,~C,~R,~E,~Error,~Out,Cap.invoke_owned(~Ops,~Args,~(T.Outcome<Error> & Out),~body,~T.Tx<W.World<S,C,R,E>,E>,~grant(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),declaration),args,T.begin(~W.World<S,C,R,E>,~E,world)))
81
82
83
84type Owner<-S:Data,-Registry:Type,-clauses:List<&2,Q.Clause<S>>> is Type:
85 Owner{registry:Registry}
86
87
88type Defined<-S:Data,-World:Type,-Registry:Type,-clauses:List<&2,Q.Clause<S>>> is Type:
89 Defined{world:World,owner:Owner<S,Registry,clauses>}
90 Rejected{world:World}
91def registered(~S:Data,~C:Type,~R:Type,~E:Data,~Args:Type,~Error:Data,~Out:Type,~run:W.World<S,C,R,E> -> Args -> Sys.Outcome<W.World<S,C,R,E>,Error,Out>,~clauses:List<&2,Q.Clause<S>>,result:Sys.RegistrationResult<S,C,R,E,Args,Error,Out,run>) -> Defined<S,W.World<S,C,R,E>,Sys.Registry<S,C,R,E,Args,Error,Out,run>,clauses>:
92 match result:
93 case Sys.RegistrationFailed{world}:Rejected{world}
94 case Sys.Registered{world,registry}:Defined{world,Owner{registry}}
95
96
97
98def define(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~declaration:Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Ops(T.Tx<W.World<S,C,R,E>,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome<Error> & Out),world:W.World<S,C,R,E>,name:String) -> Defined<S,W.World<S,C,R,E>,Sys.Registry<S,C,R,E,Args,Error,Out,runner(~S,~C,~R,~E,~Ops,~Args,~Error,~Out,~declaration,~body)>,clauses(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),declaration)>:
99 registered(~S,~C,~R,~E,~Args,~Error,~Out,~runner(~S,~C,~R,~E,~Ops,~Args,~Error,~Out,~declaration,~body),~clauses(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),declaration),Sys.register(~S,~C,~R,~E,~Args,~Error,~Out,~runner(~S,~C,~R,~E,~Ops,~Args,~Error,~Out,~declaration,~body),world,name,access(~S,clauses(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),declaration))))
100
101
102
103def run(~S:Data,~C:Type,~R:Type,~E:Data,~Args:Type,~Error:Data,~Out:Type,~run:W.World<S,C,R,E> -> Args -> Sys.Outcome<W.World<S,C,R,E>,Error,Out>,~clauses:List<&2,Q.Clause<S>>,owner:Owner<S,Sys.Registry<S,C,R,E,Args,Error,Out,run>,clauses>,world:W.World<S,C,R,E>,args:Args) -> Sys.RunResult<S,C,R,E,Args,Error,Out,run>:
104 match owner:
105 case Owner{registry}:Sys.run(~S,~C,~R,~E,~Args,~Error,~Out,~run,registry,world,args)
106
107
108
109
110def runner_observed(~S:Data,~C:Type,~R:Type,~E:Data,~Ops:Type -> Type,~Args:Type,~Error:Data,~Out:Type,~declaration:Declaration<S,T.Tx<W.World<S,C,R,E>,E>,Ops(T.Tx<W.World<S,C,R,E>,E>)>,~body:@-H:Type -> @-caps:Ops(H) -> Args -> H -> H & (T.Outcome<Error> & Out),trace:TraceFacts.State,world:W.World<S,C,R,E>,args:Args) -> Sys.Outcome<W.World<S,C,R,E>,Error,Out> & TraceFacts.State:
111 Whole.finished_observed(~S,~C,~R,~E,~Error,~Out,Cap.invoke_owned(~Ops,~Args,~(T.Outcome<Error> & Out),~body,~T.Tx<W.World<S,C,R,E>,E>,~grant(~S,~T.Tx<W.World<S,C,R,E>,E>,~Ops(T.Tx<W.World<S,C,R,E>,E>),declaration),args,T.begin_with_trace(~W.World<S,C,R,E>,~E,world,trace)))