-
Notifications
You must be signed in to change notification settings - Fork 1
/
001-separate-paren-groups.dfy
151 lines (137 loc) · 4.7 KB
/
001-separate-paren-groups.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
function ParenthesesDepth(s: string, i: int, j: int): int
decreases j - i
requires 0 <= i <= j <= |s|
{
if i == j then
0
else if s[i] == '(' then
ParenthesesDepth(s, i+1, j) + 1
else if s[i] == ')' then
ParenthesesDepth(s, i+1, j) - 1
else
ParenthesesDepth(s, i+1, j)
}
predicate InnerDepthsPositive(s: string)
{
forall i :: 0 < i < |s| ==> ParenthesesDepth(s, 0, i) > 0
}
predicate InnerDepthsNonnegative(s: string)
{
forall i :: 0 < i < |s| ==> ParenthesesDepth(s, 0, i) >= 0
}
lemma ParenthesesDepthSum(s: string, i: int, j: int, k: int)
decreases j - i
requires 0 <= i <= j <= k <= |s|
ensures ParenthesesDepth(s, i, k) == ParenthesesDepth(s, i, j) + ParenthesesDepth(s, j, k)
{
if i != j {
ParenthesesDepthSum(s, i+1, j, k);
}
}
lemma ParenthesesSuffixEq(s: string, i: int, j: int)
decreases j -i
requires 0 <= i <= j <= |s|
ensures ParenthesesDepth(s, i, j) == ParenthesesDepth(s[..j], i, j)
{
if i != j {
ParenthesesSuffixEq(s, i+1, j);
}
}
lemma ParenthesesPrefixEq(s: string, i: int, j: int)
decreases j -i
requires 0 <= i <= j <= |s|
ensures ParenthesesDepth(s, i, j) == ParenthesesDepth(s[i..], 0, j-i)
{ }
lemma ParenthesesSubstring(s: string, i: int, j: int)
decreases j - i
requires 0 <= i <= j <= |s|
ensures ParenthesesDepth(s, i, j) == ParenthesesDepth(s[i..j], 0, j-i)
{
assert ParenthesesDepth(s, i, j) == ParenthesesDepth(s[..j], i, j)
by { ParenthesesSuffixEq(s, i, j); }
assert ParenthesesDepth(s[..j], i, j) == ParenthesesDepth(s[i..j], 0, j-i)
by { ParenthesesPrefixEq(s[..j], i, j); }
}
lemma ParenthesesCommonSegment(s: string, t: string, i: int, j: int)
requires 0 <= i <= j <= |s|
requires 0 <= i <= j <= |t|
requires s[i..j] == t[i..j]
ensures ParenthesesDepth(s, i, j) == ParenthesesDepth(t, i, j)
{
ParenthesesSubstring(s, i, j);
ParenthesesSubstring(t, i, j);
}
lemma ParenthesesDepthAppend(s: string, c: char)
ensures ParenthesesDepth(s + [c], 0, |s|+1) == ParenthesesDepth(s, 0, |s|) + ParenthesesDepth([c], 0, 1)
{
ParenthesesSubstring(s + [c], 0, |s|);
ParenthesesSubstring(s + [c], |s|, |s| + 1);
ParenthesesDepthSum(s + [c], 0, |s|, |s| + 1);
}
lemma InnerDepthsPositiveAppendDecompose(s: string, c: char)
requires InnerDepthsPositive(s)
requires ParenthesesDepth(s, 0, |s|) > 0
ensures InnerDepthsPositive(s + [c])
{
forall i: int | 0 < i < |s| + 1
ensures ParenthesesDepth(s + [c], 0, i) > 0
{
if (i <= |s|) {
ParenthesesCommonSegment(s, s + [c], 0, i);
}
}
}
method separate_paren_groups(paren_string: string) returns (res : seq<string>)
// pre-conditions-start
requires ParenthesesDepth(paren_string, 0, |paren_string|) == 0
requires InnerDepthsNonnegative(paren_string)
// pre-conditions-end
// post-conditions-start
ensures forall k :: 0 <= k < |res| ==> ParenthesesDepth(res[k], 0, |res[k]|) == 0
ensures forall k :: 0 <= k < |res| ==> InnerDepthsPositive(res[k])
// post-conditions-end
{
// impl-start
res := [];
var current_string: string := "";
var current_depth: int := 0;
for i := 0 to |paren_string|
// invariants-start
invariant forall k :: 0 <= k < |res| ==> ParenthesesDepth(res[k], 0, |res[k]|) == 0
invariant forall k :: 0 <= k < |res| ==> InnerDepthsPositive(res[k])
invariant ParenthesesDepth(paren_string, 0, |paren_string|) == 0
invariant InnerDepthsNonnegative(paren_string)
invariant ParenthesesDepth(paren_string, i, |paren_string|) + current_depth == 0
invariant ParenthesesDepth(paren_string, 0, i) == current_depth
invariant ParenthesesDepth(current_string, 0, |current_string|) == current_depth
invariant InnerDepthsPositive(current_string)
invariant current_string == "" || current_depth > 0
invariant current_depth >= 0
// invariants-end
{
var c: char := paren_string[i];
// lemma-use-start
ParenthesesDepthAppend(current_string, c);
ParenthesesDepthSum(paren_string, 0, i, i+1);
if (current_string != "") {
InnerDepthsPositiveAppendDecompose(current_string, c);
}
// lemma-use-end
if (c == '(')
{
current_depth := current_depth + 1;
current_string := current_string + [c];
}
else if (c == ')')
{
current_depth := current_depth - 1;
current_string := current_string + [c];
if (current_depth == 0)
{
res := res + [current_string];
current_string := "";
}
}
}
// impl-end
}