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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::{
Binder1,
BinderN,
Comp,
Literal,
LogOp1,
OpMode,
Val,
Ident,
VType,
IGen,
Sig,
};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DemandVal {
Positive,
Negative(Ident),
Both(Ident),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DemandSet(HashMap<Ident, DemandVal>);
impl DemandSet {
pub fn empty() -> Self { DemandSet(HashMap::new()) }
pub fn get(&self, x: &Ident) -> Option<DemandVal> {
match self.0.get(x) {
Some(y) => Some(y.clone()),
None => None,
}
}
pub fn add_positive(&mut self, x: &Ident) {
match self.0.get(x) {
Some(DemandVal::Both(_y)) => {},
Some(DemandVal::Positive) => {},
Some(DemandVal::Negative(y)) => {
self.0.insert(x.clone(),DemandVal::Both(y.clone()));
}
None => {
self.0.insert(x.clone(),DemandVal::Positive);
}
}
}
/// First Ident arg is the demanded name, second is the what the
/// negation should be named as. The returned value is what the
/// negation has already been named as, if any.
pub fn add_negative(&mut self, x: &Ident, y: &Ident) -> Ident {
match self.0.get(x) {
Some(DemandVal::Both(y2)) => y2.clone(),
Some(DemandVal::Negative(y2)) => y2.clone(),
Some(DemandVal::Positive) => {
self.0.insert(x.clone(),DemandVal::Both(y.clone()));
y.clone()
}
None => {
self.0.insert(x.clone(),DemandVal::Negative(y.clone()));
y.clone()
}
}
}
/// Adds a negative demand for the given Ident. If a negative
/// demand already exists, add return the negative version's
/// name. If not, generate a unique name for the negative version
/// and return it.
pub fn add_negative_igen(&mut self, x: &Ident, igen: &mut IGen) -> Ident {
match self.0.get(x) {
Some(DemandVal::Both(x2)) => x2.clone(),
Some(DemandVal::Negative(x2)) => x2.clone(),
Some(DemandVal::Positive) => {
let x2 = igen.next();
self.0.insert(x.clone(), DemandVal::Both(x2.clone()));
x2
}
None => {
let x2 = igen.next();
self.0.insert(x.clone(), DemandVal::Negative(x2.clone()));
x2
}
}
}
}
impl Binder1 {
fn neg_normal_form_r(&self, sig: &Sig, dem: &mut DemandSet, igen: &mut IGen) -> Self {
match self {
Self::Eq(pos, vs1, vs2) => {
for v in vs1 {
v.demand_positive(dem);
}
for v in vs2 {
v.demand_positive(dem);
}
Self::Eq(*pos, vs1.clone(), vs2.clone())
}
Self::LogQuantifier(q, xs, m) => {
let mut m2 = m.neg_normal_form_r(sig, dem, igen);
for (x,t) in xs {
match dem.get(x) {
Some(DemandVal::Negative(y)) => {
assert!(
t == &VType::prop(),
"Found negative demand for {:?} of type {}",
x,
t.render(),
);
m2 = m2.substitute(
&y,
&Val::var_negative(x.clone())
);
}
Some(DemandVal::Both(y)) => {
assert!(
t == &VType::prop(),
"Found positive + negative demand for {:?} of type {}",
x,
t.render(),
);
m2 = m2.substitute(
&y,
&Val::var_negative(x.clone())
);
}
// We don't need to take any action for
// positive demands.
Some(DemandVal::Positive) => {}
None => {}
}
}
Self::LogQuantifier(*q, xs.clone(), Box::new(m2))
}
Self::QMode(q, m) => {
let m2 = m.neg_normal_form_r(sig, dem, igen);
Self::QMode(*q, Box::new(m2))
}
Self::LogOpN(op, vs) => {
// Demand all of the args
for v in vs {
v.demand_positive(dem);
}
Self::LogOpN(op.clone(), vs.clone())
}
_ => todo!("neg_normal_form_r for {:?}", self),
}
}
}
impl Comp {
/// Convert a computation into normal form. This may require the
/// creation of new variables, so we require a IGen.
pub fn neg_normal_form(self, sig: &Sig, igen: &mut IGen) -> Self {
self.neg_normal_form_r(sig, &mut DemandSet::empty(), igen)
}
fn neg_normal_form_r(&self, sig: &Sig, dem: &mut DemandSet, igen: &mut IGen) -> Self {
match self {
Self::Return(vs) => {
for v in vs {
match v {
Val::Var(x,_,_,_) => { dem.add_positive(x); }
_ => {},
}
}
Self::Return(vs.clone())
}
Self::Bind1(Binder1::LogOp1(LogOp1::Not, v), x, m) => {
let m2 = m.neg_normal_form_r(sig,dem,igen);
match dem.get(x) {
None => m2,
Some(DemandVal::Positive) => {
// Get the negative version of the value,
// recording a negative demand if it's a
// variable.
let v_neg = v.demand_negative(dem,igen);
// Replace x's with the negative value.
m2.substitute(x, &v_neg)
}
Some(DemandVal::Negative(y1)) => {
// Record a positive demand for the value (if
// it's a variable).
v.demand_positive(dem);
// Replace y1's with the (positive) value. The
// positive x (negative value) is not
// demanded, so there are no x's in m2 to
// replace with anything.
m2.substitute(&y1, v)
}
Some(DemandVal::Both(y1)) => {
v.demand_positive(dem);
let v_neg = v.demand_negative(dem,igen);
m2
.substitute(&y1,v)
.substitute(x,&v_neg)
}
}
}
Self::Bind1(b, x, m) => {
let m2 = m.neg_normal_form_r(sig,dem,igen);
match dem.get(x) {
None => {
// println!("{:?} was not demanded by {:?}", x, m2);
m2
}
Some(DemandVal::Positive) => {
let b2p = b.neg_normal_form_r(sig,dem,igen);
Self::Bind1(b2p, x.clone(), Box::new(m2))
}
Some(DemandVal::Negative(y)) => {
let b2n = b.negate(sig,dem,igen).neg_normal_form_r(sig,dem,igen);
Self::Bind1(b2n, y.clone(), Box::new(m2))
}
Some(DemandVal::Both(y)) => {
let b2p = b.neg_normal_form_r(sig,dem,igen);
let b2n = b.negate(sig,dem,igen).neg_normal_form_r(sig,dem,igen);
Self::Bind1(b2p, x.clone(), Box::new(
Self::Bind1(b2n, y.clone(), Box::new(m2))
))
}
}
}
// Todo: do we need to demand the arguments to a
// BinderN::Call that could be inside 'b' here?
Self::BindN(b, ps, m) => {
match b {
BinderN::Call(call) => {
for a in &call.args {
a.demand_positive(dem);
}
}
BinderN::Seq(_) => unreachable!(
"BinderN::Seq should be gone before neg_normal_form"
)
}
Self::BindN(
b.clone(),
ps.clone(),
Box::new(m.neg_normal_form_r(sig,dem,igen)),
)
}
Self::Ite(cond, then_b, else_b) => {
cond.demand_positive(dem);
Self::ite(
cond.clone(),
then_b.neg_normal_form_r(sig,dem,igen),
else_b.neg_normal_form_r(sig,dem,igen),
)
}
Self::Apply(..) => {
unreachable!(
"Apply should be gone before neg_normal_form_r: {:?}",
self,
)
}
_ => todo!("neg_normal_form_r for {:?}", self),
}
}
}
impl Val {
/// Record positive demand if the value is a Var. Note that
/// nothing gets demanded inside of Thunks.
pub fn demand_positive(&self, dem: &mut DemandSet) {
match self {
Self::Var(x, _, _, _) => {
dem.add_positive(x);
}
Self::Tuple(vs) => {
for v in vs {
v.demand_positive(dem);
}
}
_ => {},
}
}
/// IGenerate the negation of the value, recording a negative
/// demand if it's a Var.
pub fn demand_negative(
&self,
dem: &mut DemandSet,
igen: &mut IGen,
) -> Self {
match self {
Self::Var(x, types, path, true) => {
let x2 = dem.add_negative_igen(&x,igen);
Self::Var(x2, types.clone(), path.clone(), true)
}
// Since this is a negative variable, we can just flip the
// sign here and make a positive demand.
Self::Var(x, types, path, false) => {
dem.add_positive(x);
Self::Var(x.clone(), types.clone(), path.clone(), true)
}
Self::Literal(Literal::LogTrue) =>
Self::Literal(Literal::LogFalse),
Self::Literal(Literal::LogFalse) =>
Self::Literal(Literal::LogTrue),
Self::OpCode(OpMode::ZeroArgAsConst(b), oc) =>
Self::OpCode(OpMode::ZeroArgAsConst(!b), oc.clone()),
_ => panic!("Can't demand (negative) non-prop value {:?}", self),
}
}
}