machine_conditions.bend
bendvy-ecs-core@0.1.0.0 · 36 lines · 0 definitions
1import Base
2
3# Atom is the caller's schema-bound heterogeneous condition shape. Env is only
4# captured machine metadata; the world and its component owners remain Type.
5type Tree<Atom: Data> is Data:
6 Leaf{atom: Atom}
7 Constant{value: Bool}
8 Negate{inner: Tree<Atom>}
9 Both{left: Tree<Atom>,right: Tree<Atom>}
10 Either{left: Tree<Atom>,right: Tree<Atom>}
11
12def all(~Atom: Data,items: List<&2,Tree<Atom>>) -> Tree<Atom>:
13 match items:
14 case Nil{}: Constant{True{}}
15 case Con{head,tail}: Both{head,all(~Atom,tail)}
16def any(~Atom: Data,items: List<&2,Tree<Atom>>) -> Tree<Atom>:
17 match items:
18 case Nil{}: Constant{False{}}
19 case Con{head,tail}: Either{head,any(~Atom,tail)}
20
21def both_next(~Env: Data,first: Bool,env: Env,next: Env -> Bool) -> Bool:
22 match first:
23 case False{}: False{}
24 case True{}: next(env)
25def either_next(~Env: Data,first: Bool,env: Env,next: Env -> Bool) -> Bool:
26 match first:
27 case False{}: next(env)
28 case True{}: True{}
29
30def evaluate(~Atom: Data,~Env: Data,~leaf: Atom -> Env -> Bool,tree: Tree<Atom>,+env: Env) -> Bool:
31 match tree:
32 case Leaf{atom}: leaf(atom,env)
33 case Constant{value}: value
34 case Negate{inner}: Bool.not(evaluate(~Atom,~Env,~leaf,inner,env))
35 case Both{left,right}: both_next(~Env,evaluate(~Atom,~Env,~leaf,left,env),env,env => evaluate(~Atom,~Env,~leaf,right,env))
36 case Either{left,right}: either_next(~Env,evaluate(~Atom,~Env,~leaf,left,env),env,env => evaluate(~Atom,~Env,~leaf,right,env))