lemma/evaluation/
branch_semantics.rs1use crate::computation::OperationResult;
10use crate::planning::semantics::ValueKind;
11
12#[derive(Debug, Clone)]
14pub enum BranchOutcome {
15 Taken,
17 NotTaken,
19 Propagate(OperationResult),
22}
23
24fn boolean_value(result: &OperationResult, context: &str) -> bool {
27 match result {
28 OperationResult::Value(literal) => match &literal.value {
29 ValueKind::Boolean(boolean) => *boolean,
30 other => panic!("BUG: {context} expected a boolean, got {other:?}"),
31 },
32 OperationResult::Veto(veto) => {
33 panic!("BUG: {context} inspected for a boolean while vetoed: {veto}")
34 }
35 }
36}
37
38pub fn condition_outcome(condition: &OperationResult) -> BranchOutcome {
44 if condition.vetoed() {
45 return BranchOutcome::Propagate(condition.clone());
46 }
47 if boolean_value(condition, "branch condition") {
48 BranchOutcome::Taken
49 } else {
50 BranchOutcome::NotTaken
51 }
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum PiecewiseDecision {
58 Taken { arm: usize },
60 Default,
62 Undecided { arm: usize },
65}
66
67pub fn piecewise_decision(
70 arm_count: usize,
71 mut outcome: impl FnMut(usize) -> Option<BranchOutcome>,
72) -> PiecewiseDecision {
73 assert!(arm_count > 0, "BUG: empty piecewise");
74 for arm in (1..arm_count).rev() {
75 match outcome(arm) {
76 Some(BranchOutcome::Taken) => return PiecewiseDecision::Taken { arm },
77 Some(BranchOutcome::NotTaken) => continue,
78 Some(BranchOutcome::Propagate(_)) | None => {
79 return PiecewiseDecision::Undecided { arm };
80 }
81 }
82 }
83 PiecewiseDecision::Default
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub struct OpenArm {
89 pub arm: usize,
90 pub body: bool,
91}
92
93pub fn open_arms(
98 arm_count: usize,
99 mut outcome: impl FnMut(usize) -> Option<BranchOutcome>,
100) -> Vec<OpenArm> {
101 assert!(arm_count > 0, "BUG: empty piecewise");
102 let mut open = Vec::new();
103 for arm in (1..arm_count).rev() {
104 let (body, last) = match outcome(arm) {
105 Some(BranchOutcome::Taken) => (true, true),
106 Some(BranchOutcome::NotTaken) => (false, false),
107 Some(BranchOutcome::Propagate(veto)) if !veto.is_missing_data() => (false, true),
108 Some(BranchOutcome::Propagate(_)) | None => (true, false),
109 };
110 open.push(OpenArm { arm, body });
111 if last {
112 return open;
113 }
114 }
115 open.push(OpenArm { arm: 0, body: true });
116 open
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122 use crate::computation::VetoType;
123
124 fn outcome_from(
125 list: &[Option<BranchOutcome>],
126 ) -> impl FnMut(usize) -> Option<BranchOutcome> + '_ {
127 move |arm| list[arm].clone()
128 }
129
130 #[test]
131 fn piecewise_decision_default_when_all_not_taken() {
132 let outcomes = [
133 None,
134 Some(BranchOutcome::NotTaken),
135 Some(BranchOutcome::NotTaken),
136 ];
137 assert_eq!(
138 piecewise_decision(3, outcome_from(&outcomes)),
139 PiecewiseDecision::Default
140 );
141 }
142
143 #[test]
144 fn piecewise_decision_highest_taken_wins() {
145 let outcomes = [None, Some(BranchOutcome::Taken), Some(BranchOutcome::Taken)];
146 assert_eq!(
147 piecewise_decision(3, outcome_from(&outcomes)),
148 PiecewiseDecision::Taken { arm: 2 }
149 );
150 }
151
152 #[test]
153 fn piecewise_decision_stops_at_unfilled_condition() {
154 let outcomes = [
155 None,
156 Some(BranchOutcome::Taken),
157 None,
158 Some(BranchOutcome::NotTaken),
159 ];
160 assert_eq!(
161 piecewise_decision(4, outcome_from(&outcomes)),
162 PiecewiseDecision::Undecided { arm: 2 }
163 );
164 }
165
166 #[test]
167 fn piecewise_decision_stops_at_vetoed_condition() {
168 let veto = OperationResult::Veto(VetoType::computation("no"));
169 let outcomes = [
170 None,
171 Some(BranchOutcome::Taken),
172 Some(BranchOutcome::Propagate(veto)),
173 ];
174 assert_eq!(
175 piecewise_decision(3, outcome_from(&outcomes)),
176 PiecewiseDecision::Undecided { arm: 2 }
177 );
178 }
179 use crate::planning::semantics::{DataPath, LiteralValue};
180
181 fn boolean(value: bool) -> OperationResult {
182 OperationResult::from_literal(LiteralValue::from_bool(value))
183 }
184
185 fn user_veto() -> OperationResult {
186 OperationResult::Veto(VetoType::UserDefined {
187 message: Some("blocked".to_string()),
188 })
189 }
190
191 fn missing_data_veto() -> OperationResult {
192 OperationResult::Veto(VetoType::missing_data(
193 DataPath::new(vec![], "x".to_string()),
194 None,
195 ))
196 }
197
198 #[test]
199 fn propagates_any_veto() {
200 assert!(matches!(
201 condition_outcome(&user_veto()),
202 BranchOutcome::Propagate(_)
203 ));
204 assert!(matches!(
205 condition_outcome(&missing_data_veto()),
206 BranchOutcome::Propagate(_)
207 ));
208 }
209
210 #[test]
211 fn boolean_decides_when_not_vetoed() {
212 assert!(matches!(
213 condition_outcome(&boolean(true)),
214 BranchOutcome::Taken
215 ));
216 assert!(matches!(
217 condition_outcome(&boolean(false)),
218 BranchOutcome::NotTaken
219 ));
220 }
221}