1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::{
Binder1,
BinderN,
Builder,
Comp,
Gen,
LogOpN,
Op,
Quantifier,
Rebuild,
Sig,
Val,
VName,
FunOp,
PredOp,
};
fn expand_fun(op: FunOp, vs: Vec<Val>, xs: Vec<VName>, m: Comp, sig: &Sig, gen: &mut Gen) -> Comp {
assert!(
!op.output.contains_prop(),
"Can't have bool-output primitive function. Define a predicate instead."
);
let flat_output = op.output.flatten();
assert!(
xs.len() == flat_output.len(),
"expand_fun mismatch between var-count and output-type-count",
);
let output_tuple = Val::tuple(
xs.clone()
.into_iter()
.map(|x| x.val())
.collect()
);
let q_sig = xs.into_iter().zip(flat_output).collect();
let mut disjuncts: Vec<Builder> = op.axioms.into_iter().map(|axiom| {
Builder::new(|gen| axiom.rename(gen))
.flatten()
.apply_v(vs.clone())
.flatten()
.apply_v(vec![output_tuple.clone()])
.not()
}).collect();
disjuncts.push(Builder::lift(m));
let new_body =
Builder::log_op(LogOpN::Or, disjuncts).build(gen);
let x_result = gen.next();
let new_m =
Comp::quant_many(
Quantifier::Forall,
q_sig,
new_body.normal_form_single_case(sig,gen),
x_result.clone(),
Comp::return1(x_result),
);
new_m
}
#[allow(dead_code)]
fn expand_pred(op: PredOp, vs: Vec<Val>, x: VName, m: Comp, sig: &Sig, gen: &mut Gen, is_pos: bool) -> Comp {
let sig_clone1 = sig.clone();
let sig_clone2 = sig.clone();
// Build the POSITIVE branch: one big disjunction. SOME axiom is
// FALSE, OR the condition with 'true' plugged in is TRUE.
//
// !axiom_0 OR ... OR !axiom_n OR condition[true]
let mut t_disjuncts: Vec<Builder> =
op.axioms.clone().into_iter().map(|axiom| {
Builder::new(|gen| axiom.rename(gen))
.flatten()
.apply_v(vs.clone())
.not()
})
.collect();
let t_val = Val::from_bool(is_pos);
t_disjuncts.push(
Builder::lift(m.clone().substitute(&x, &t_val))
);
let pos_branch = Builder::new(move |gen| {
Builder::log_op(LogOpN::Or, t_disjuncts)
.build(gen)
.normal_form_single_case(&sig_clone1, gen)
});
// Build the FALSE branch: disjunction with a conjunction. ALL
// axioms are TRUE, OR the condition with 'false' plugged in is
// TRUE.
//
// (axiom_0 AND ... AND axiom_n) OR condition[false]
//
// Note that if there are no axioms, this collapses to simply:
//
// true
//
// We use if-then-else for this case below, because easy_smt
// panics when you construct an AND_MANY node with no arguments.
let f_conjuncts: Vec<Builder> =
op.axioms.clone().into_iter().map(|axiom| {
Builder::new(|gen| axiom.rename(gen))
.flatten()
.apply_v(vs.clone())
})
.collect();
let f_val = Val::from_bool(!is_pos);
let neg_branch = if f_conjuncts.len() > 0 {
Builder::new(move |gen| {
Builder::log_op(LogOpN::Or, [
Builder::log_op(LogOpN::And, f_conjuncts),
Builder::lift(m.clone().substitute(&x, &f_val))
])
.build(gen)
.normal_form_single_case(&sig_clone2, gen)
})
} else {
Builder::return_(Val::true_())
};
Builder::log_op(LogOpN::And, [
pos_branch,
neg_branch,
])
.build(gen)
.normal_form_single_case(sig,gen)
}
impl Comp {
pub fn expand_funs(mut self, sig: &Sig, gen: &mut Gen, mut anti_stack: Vec<Rebuild>) -> Self {
loop {
match self {
Self::Bind1(b, x, m) => {
match b {
Binder1::LogOpN(LogOpN::Pred(oc,is_pos), vs) => {
// anti_stack.push(Rebuild::LogOpN(LogOpN::Pred(oc, is_pos), vs, x));
// self = *m;
match sig.get_applied_op(&oc).unwrap() {
Op::Const(..) => {
panic!("Got constant op {:?} in Pred", oc)
}
Op::Direct(..) => {
panic!("Got direct fun {:?} in Pred", oc)
}
Op::Pred(op) => {
println!("Expanding pred {}...", &oc.ident);
self = expand_pred(op.clone(), vs, x, *m, sig, gen, is_pos);
break;
}
// Treat Fun like a Symbol, since this
// is the relational abstraction being
// applied.
Op::Fun(..) => {
anti_stack.push(Rebuild::LogOpN(
LogOpN::Pred(oc,is_pos),
vs,
x,
));
self = *m;
}
// Same deal for Rec
Op::Rec(..) => {
anti_stack.push(Rebuild::LogOpN(
LogOpN::Pred(oc,is_pos),
vs,
x,
));
self = *m;
}
Op::Symbol(_op) => {
anti_stack.push(Rebuild::LogOpN(
LogOpN::Pred(oc,is_pos),
vs,
x,
));
self = *m;
}
}
}
Binder1::LogQuantifier(q, xs, body) => {
let body = body.expand_funs(sig,gen,Vec::new());
anti_stack.push(Rebuild::Quantifier(q, xs, body, x));
self = *m;
},
b => {
anti_stack.push(Rebuild::Bind1(b, x));
self = *m;
}
}
}
Self::BindN(BinderN::Call(oc, vs), ps, m) => {
let xs = ps
.into_iter()
.map(|p| p.unwrap_atom().expect("Call should only be bound to flat patterns"))
.collect();
match sig.get_applied_op(&oc).unwrap() {
Op::Fun(op) => {
println!("Expanding call {}...", &oc);
self = expand_fun(op, vs, xs, *m, sig, gen);
break;
}
Op::Pred(_op) => {
panic!("Got pred op {:?} in Fun", oc)
}
Op::Rec(op) => {
println!("Expanding call {}...", &oc);
self = expand_fun(op.as_fun_op(), vs, xs, *m, sig, gen);
break;
}
r => panic!("Can't expand_fun on {:?}", r),
}
}
Self::Ite(cond, then_b, else_b) => {
let then_b = then_b.expand_funs(sig,gen,Vec::new());
let else_b = else_b.expand_funs(sig,gen,Vec::new());
self = Self::ite(cond, then_b, else_b);
break;
}
Self::Return(vs) => {
self = Self::Return(vs);
break;
}
m => panic!("expand_funs: Unexpected Comp {:?}", m),
}
}
self.rebuild_from_stack(anti_stack)
}
}