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
use crate::{
Binder1,
BType,
Comp,
Quantifier,
Sig,
};
use graph_cycles::Cycles;
use petgraph::graph::Graph;
use std::collections::HashSet;
pub struct SortIndex(pub Vec<BType>);
impl SortIndex {
fn to_index(&self, s: &BType) -> usize {
for (i,s1) in self.0.iter().enumerate() {
if s1 == s {
return i
}
}
panic!("No index found for base type {}", s)
}
fn from_index(&self, i: usize) -> BType {
self.0[i].clone()
}
}
#[derive(Clone)]
pub struct SortGraph(HashSet<(BType,BType)>);
impl SortGraph {
pub fn new() -> Self { SortGraph(HashSet::new()) }
fn add_edge(&mut self, s1: BType, s2: BType) {
assert!(
s1 != BType::prop() && s2 != BType::prop(),
"sort graph edges should not be on 'bool'",
);
self.0.insert((s1,s2));
}
pub fn get_cycles(&self) -> Vec<Vec<BType>> {
let index = self.get_index();
let mut i_graph = Vec::new();
for (s1,s2) in self.0.iter() {
let i1 = index.to_index(s1);
let i2 = index.to_index(s2);
i_graph.push((i1 as u32,i2 as u32));
}
let g = Graph::<(), ()>::from_edges(i_graph);
let mut cycles = Vec::new();
g.visit_all_cycles(|_g, c| {
let mut c_s = Vec::new();
for i in c.iter() {
c_s.push(index.from_index(i.index()));
}
cycles.push(c_s);
});
cycles
}
fn get_index(&self) -> SortIndex {
let mut sorts = HashSet::new();
for (s1,s2) in self.0.iter() {
sorts.insert(s1.clone());
sorts.insert(s2.clone());
}
SortIndex(sorts.into_iter().collect())
}
pub fn append(&mut self, other: Self) {
self.0.extend(other.0);
}
}
pub fn test() -> Graph<(), ()> {
let g = Graph::<(), ()>::from_edges([
(1, 2),
(2, 3),
(3, 4),
(3, 2),
]);
g.visit_all_cycles(|_g, c| {
println!("Cycle: {c:?}");
});
g
}
impl Comp {
pub fn sort_graph(&self) -> SortGraph {
self.sort_graph_r(HashSet::new())
}
fn sort_graph_r(&self, foralls: HashSet<BType>) -> SortGraph {
match self {
Comp::Bind1(Binder1::LogQuantifier(q, xs, body), _x, m) => {
let mut q_sorts = Vec::new();
for (_x,t) in xs {
for s in t.clone().flatten() {
q_sorts.push(s.unwrap_base().unwrap())
}
}
match q {
Quantifier::Exists => {
// If we encounter an Exists quantifier, then
// edges to the newly-quantified sorts are
// recorded from every Forall-quantified sort that
// we are under.
let mut graph = body.sort_graph_r(foralls.clone());
for f_s in foralls.iter() {
for e_s in q_sorts.iter() {
graph.add_edge(f_s.clone(), e_s.clone());
}
}
graph.append(m.sort_graph_r(foralls));
graph
}
Quantifier::Forall => {
// If we encounter an Forall quantifier, we record
// its sorts before descending into the body.
//
// We *don't* use those sorts when descending on
// the continuation, which is outside the
// quantifier scope.
let mut body_foralls = foralls.clone();
for s in q_sorts {
body_foralls.insert(s);
}
let mut graph = body.sort_graph_r(body_foralls);
graph.append(m.sort_graph_r(foralls));
graph
}
}
}
Comp::Bind1(b, _x, m) => match b {
Binder1::Eq(..) |
Binder1::LogNot(..) |
Binder1::LogOpN(..) => {
m.sort_graph_r(foralls)
}
b => panic!(
"Unexpected {:?} encounterd during sort_graph",
b
),
}
Comp::Ite(_v, m1, m2) => {
let mut graph = m1.sort_graph_r(foralls.clone());
graph.append(m2.sort_graph_r(foralls));
graph
}
Comp::Return(_vs) => SortGraph::new(),
m => todo!("sort_graph_r for {:?}", m),
}
}
}
impl Sig {
pub fn sort_graph_combined(&self, term: &Comp) -> SortGraph {
let (_, axioms) = self.relevant_with_axioms(term);
let mut graph = SortGraph::new();
for a in &axioms {
graph.append(a.sort_graph());
}
graph.append(term.sort_graph());
graph
}
// pub fn sort_graph(&self) -> SortGraph {
// // Operator axioms are counted when they are spliced into the
// // main assertion, so we only look at the axioms here.
// let mut graph = SortGraph::new();
// for a in self.axioms.iter() {
// graph.append(a.sort_graph());
// }
// graph
// }
}