-
Notifications
You must be signed in to change notification settings - Fork 2
/
set_of_seq.dfy
231 lines (208 loc) · 4.42 KB
/
set_of_seq.dfy
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
function set_of_seq<T>(s: seq<T>): set<T>
{
set x: T | x in s :: x
}
lemma in_set_of_seq<T>(x: T, s: seq<T>)
ensures x in set_of_seq(s) <==> x in s
{
}
lemma subset_set_of_seq<T>(s1: seq<T>, s2: seq<T>)
requires set_of_seq(s1) <= set_of_seq(s2)
ensures forall x :: x in s1 ==> x in s2
{
forall x | x in s1 {
in_set_of_seq(x, s1);
}
}
lemma set_of_seq_subset<T>(s1: seq<T>, s2: seq<T>)
requires forall x :: x in s1 ==> x in s2
ensures set_of_seq(s1) <= set_of_seq(s2)
{
}
function set_of_seq_ind<T>(s: seq<T>): set<T>
{
if s == [] then {} else {s[0]} + set_of_seq_ind(s[1..])
}
lemma set_of_seq_ind_eq<T>(s: seq<T>)
ensures set_of_seq(s) == set_of_seq_ind(s)
{
}
lemma card_set_of_seq_le<T>(s: seq<T>)
ensures |set_of_seq(s)| <= |s|
{
if s == [] {
}
else {
calc {
|set_of_seq(s)|;
== { set_of_seq_ind_eq(s); }
|set_of_seq_ind(s)|;
== { assert s == [s[0]] + s[1..]; }
|set_of_seq_ind([s[0]] + s[1..])|;
<= 1 + |set_of_seq_ind(s[1..])|;
== { set_of_seq_ind_eq(s[1..]); }
1 + |set_of_seq(s[1..])|;
<= { card_set_of_seq_le(s[1..]); }
1 + |s[1..]|;
}
}
}
lemma set_of_seq_append<T>(s1: seq<T>, s2: seq<T>)
ensures set_of_seq(s1 + s2) == set_of_seq(s1) + set_of_seq(s2)
{
}
function undup<T>(s: seq<T>): seq<T>
{
if s == [] then
[]
else if s[0] in s[1..] then
undup(s[1..])
else
[s[0]] + undup(s[1..])
}
lemma in_undup<T>(x: T, s: seq<T>)
ensures x in undup(s) <==> x in s
{
}
lemma set_of_seq_undup<T>(s: seq<T>)
ensures set_of_seq(undup(s)) == set_of_seq(s)
{
if s == [] {
}
else {
// set_of_seq_ind_eq(undup(s));
// assert s == [s[0]] + s[1..];
// set_of_seq_ind_eq(undup(s[1..]));
calc {
set_of_seq(undup(s));
{ set_of_seq_ind_eq(undup(s)); }
set_of_seq_ind(undup(s));
{ assert s == [s[0]] + s[1..]; set_of_seq_ind_eq(undup(s[1..])); }
if s[0] in s[1..] then set_of_seq(undup(s[1..])) else {s[0]} + set_of_seq(undup(s[1..]));
}
}
}
lemma undup_undup<T>(s: seq<T>)
ensures undup(undup(s)) == undup(s)
{
if s == [] {
}
else if s[0] in s[1..] {
// assert s == [s[0]] + s[1..];
// set_of_seq_ind_eq(s);
// set_of_seq_ind_eq(s[1..]);
}
else {
// assume undup(s[1..]) == undup(s)[1..];
in_undup(s[0], s[1..]);
// assert s[0] !in undup(s[1..]);
}
}
predicate subseq<T>(s1: seq<T>, s2: seq<T>)
{
if s1 == [] then
true
else if s2 == [] then
false
else if s1[0] == s2[0] then
subseq(s1[1..], s2[1..])
else
subseq(s1, s2[1..])
}
lemma subseq_length<T>(s1: seq<T>, s2: seq<T>)
requires subseq(s1, s2)
ensures |s1| <= |s2|
{
}
lemma set_of_subseq<T>(s1: seq<T>, s2: seq<T>)
requires subseq(s1, s2)
ensures set_of_seq(s1) <= set_of_seq(s2)
{
if s1 == [] {
}
else {
set_of_seq_ind_eq(s1);
set_of_seq_ind_eq(s2);
set_of_seq_ind_eq(s1[1..]);
set_of_seq_ind_eq(s2[1..]);
assert s1 == [s1[0]] + s1[1..];
assert s2 == [s2[0]] + s2[1..];
}
}
lemma subseq_refl<T>(s: seq<T>)
ensures subseq(s, s)
{
}
lemma in_subseq<T>(x: T, s1: seq<T>, s2: seq<T>)
requires subseq(s1, s2)
ensures x in s1 ==> x in s2
{
}
predicate uniq_ind<T>(s: seq<T>)
{
if s == [] then
true
else if s[0] in s[1..] then
false
else
uniq_ind(s[1..])
}
lemma undup_uniq<T>(s: seq<T>)
requires uniq_ind(s)
ensures undup(s) == s
{
}
lemma uniq_undup<T>(s: seq<T>)
ensures uniq_ind(undup(s))
{
if s == [] {
}
else {
in_undup(s[0], s[1..]);
}
}
/*
lemma card_set_of_undup<T>(s: seq<T>)
ensures |set_of_seq_ind(undup(s))| == |undup(s)|
{
if s == [] {
}
else if s[0] in s[1..] {
}
else {
// in_undup(s[0], s[1..]);
// assert s[0] !in undup(s[1..]);
calc {
|set_of_seq_ind(undup(s))|;
|set_of_seq_ind([s[0]] + undup(s[1..]))|;
|{s[0]} + set_of_seq_ind(undup(s[1..]))|;
{ set_of_seq_ind_eq(undup(s[1..]));
in_undup(s[0], s[1..]);
assert s[0] !in set_of_seq_ind(undup(s[1..])); }
1 + |set_of_seq_ind(undup(s[1..]))|;
}
}
}
*/
lemma card_set_of_seq_uniq<T>(s: seq<T>)
requires uniq_ind(s)
ensures |set_of_seq(s)| == |s|
{
if s == [] {
}
else if s[0] in s[1..] {
}
else {
set_of_seq_ind_eq(s[1..]);
set_of_seq_ind_eq(s);
}
// undup_uniq(s);
// set_of_seq_ind_eq(s);
// card_set_of_undup(s);
}
lemma card_set_of_undup<T>(s: seq<T>)
ensures |set_of_seq(undup(s))| == |undup(s)|
{
uniq_undup(s);
card_set_of_seq_uniq(undup(s));
}