-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.m
310 lines (166 loc) · 7.28 KB
/
context.m
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
305
306
307
(* Copyright E.M.Clarke and Xudong Zhao, Jan 22, 1991 *)
(* ORGANIZATION OF PROOF CONTEXT *)
(* GivenIdentitiesAt is the conjunction of identities given in a proof
section *)
GivenIdentitiesAt[_] := {};
(* RulesGivenIn is the simplify rules comes from the given formulas in a
proof section *)
RulesGivenIn[_] := {};
GivenFormulasIn[_] := {};
(* the current context path and the current proof section *)
CurrentPath = {};
CurrentSection := CurrentPath[[1]];
(* change context *)
(* start a new proof section *)
StartSection[] := StartSection[Unique["Temporal"]];
StartSection[a_] := (
(* reset the known upper and lower bounds *)
ResetKnownBounds[];
(* set the current proof context *)
PrependTo[CurrentPath, a];);
(* end the current proof section *)
EndSection[] := (
(* clear the given upper or lower bounds, which are no longer
useful *)
clear[GivenUpperAt[CurrentSection], GivenLowerAt[CurrentSection]];
GivenUpperAt[CurrentSection] =.;
GivenLowerAt[CurrentSection] =.;
(* clear the set of given properties *)
RulesGivenIn[CurrentSection] = {};
GivenIdentitiesAt[CurrentSection] = {};
(* reset the current proof context *)
CurrentPath = Drop[CurrentPath, 1] ;
RulesGivenIn[a] = {};
(* reset the known upper or lower bound *)
ResetKnownBounds[]; );
ResetKnownBounds[] := (
KnownUpper = Table[Unknown, {NumberOfTerms}];
KnownLower = Table[Unknown, {NumberOfTerms}]; );
(* clear the rewrite rules about some functions *)
clear[a___] := Clear[a];
(* EvaluateAssuming tries to calculate "result" assuming "cond" is true *)
SetAttributes[ EvaluateAssuming, HoldRest ];
EvaluateAssuming[True, result_] := result;
EvaluateAssuming[cond_, result_] := Block[{tempresult},
(* otherwise, start a temporal proof context for it *)
StartSection[];
(* make "cond" as given *)
AddKnowledge[cond, CurrentSection];
(* calculate "result" under the new context *)
tempresult = result;
(* then end the temporal context *)
EndSection[];
(* and return with the result *)
tempresult];
(* save the theorems so that they can be used later *)
(* save to the current section *)
SetAttributes[ProveAndSave, {HoldAll}];
ProveAndSave[form_] := (Prove[form]; Given[form]);
ProveAndSave[form__] := (Prove[and[form]]; Given[form]);
(* save to another section *)
ProveAndSaveTo[form__, a_] := (Prove[and[form]]; GivenTo[and[form], a]);
(* save given formulas in current section *)
Given[a__] := GivenTo[and[a], CurrentSection];
(* save given formulas to some directed section *)
GivenTo[form_, section_] :=
AddKnowledge[WeakSimplify[form], section];
AddKnowledge[and[a_, b__], section_] :=
( AddKnowledge[a, section]; AddKnowledge[and[b], section] );
AddKnowledge[form_, section_] := (
AppendTo[GivenFormulasIn[section], form];
(* if given a inequality, add some knowledge about the upper or
lower bounds *)
If[Head[form] == LessEqual,
AddUpperBound[form[[1]] - form[[2]], 0, section]];
If[Head[form] == Less,
AddUpperBound[form[[1]] - form[[2]], Strict[0], section]];
(* if given a identity, add it to the set of given identities, so
it can be used for substitution *)
If[Head[form]==Equal,
AddIdentity[form, section]];
(* use the given formula as simplify rules *)
AddSimplifyRules[form, section];);
(* GivenIdentities is the conjunctions of given identities *)
GivenIdentities := Apply[Union, Map[GivenIdentitiesAt, CurrentPath]];
(* RulesFormGiven specifies the simplify rules come from the given
knowledge in current proof context *)
RulesFromGiven := Apply[Union, Map[RulesGivenIn, CurrentPath]];
GivenFormulas[] := Apply[Union, Map[GivenFormulasIn, CurrentPath]];
(* the given upper bound for the "n"th basic term *)
GivenUpper[n_] := Apply[Union, Map[(GivenUpperAt[#][n])&, CurrentPath]];
(* the given lower bound for the "n"th basic term *)
GivenLower[n_] := Apply[Union, Map[(GivenLowerAt[#][n])&, CurrentPath]];
(* install the Upper or Lower bounds given in a section *)
GivenUpperAt[section_] := (
GivenUpperAt[section] = Unique["Upper"];
GivenUpperAt[section][_] := {};
GivenUpperAt[section]);
GivenLowerAt[section_] := (
GivenLowerAt[section] = Unique["Lower"];
GivenLowerAt[section][_] := {};
GivenLowerAt[section]);
(* if "upper" is an upper bound for "f1 + f2", "upper - f2" is an upper
bound for "f1" and "upper - f1" is an upper bound for "f2" *)
AddUpperBound[f1_ + f2_, upper_, section_] := (
AddUpperBound[f1, upper - f2, section];
AddUpperBound[f2, upper - f1, section]);
(* if "upper" is an upper bound for "n f", if "n > 0", "upper/n" is an
upper bound for "f", otherwise "upper/n" is a lower bound for "f" *)
AddUpperBound[n_?NumberQ f_, upper_, section_] :=
If[n > 0, AddUpperBound[f, upper/n, section],
AddLowerBound[f, upper/n, section]];
AddUpperBound[f_ f1_^(n_Integer?Negative e_.), upper_, section_] :=
AddUpperBound[f, upper f1^(-n e), section] /;
EvenQ[n] || WeakSimplify[f1>=0];
AddUpperBound[f_ f1_^(n_Integer?Negative e_.), upper_, section_] :=
AddLowerBound[f, upper f1^(-n e), section] /;
OddQ[n] && WeakSimplify[f1<=0];
(* add "upper" as an extra upper bound for "f" in context "section" *)
AddUpperBound[f_, upper_, section_] := (
If[NumberQ[f] || !FreeQ[upper, f], Return[]];
ResetKnownBounds[];
PrependTo[GivenUpperAt[section][TermNumber[f]], upper]);
(* if "lower" is a lower bound for "f1 + f2", "lower - f2" is a lower bound
for "f1" and "lower - f1" is a lower bound for "f2" *)
AddLowerBound[f1_ + f2_, lower_, section_] := (
AddLowerBound[f1, lower - f2, section];
AddLowerBound[f2, lower - f1, section]);
(* if "lower" is a lower bound for "n f", if "n > 0", "lower/n" is a lower
bound for "f", otherwise "lower/n" is an upper bound for "f" *)
AddLowerBound[n_?NumberQ f_, lower_, section_] :=
If[n > 0, AddLowerBound[f, lower/n, section],
AddUpperBound[f, lower/n, section]];
AddLowerBound[f_ f1_^(n_Integer?Negative e_.), upper_, section_] :=
AddLowerBound[f, upper f1^(-n e), section] /;
EvenQ[n] || WeakSimplify[f1>=0];
AddLowerBound[f_ f1_^(n_Integer?Negative e_.), upper_, section_] :=
AddUpperBound[f, upper f1^(-n e), section] /;
OddQ[n] && WeakSimplify[f1<=0];
(* add "lower" as an extra lower bound for "f" in context "section" *)
AddLowerBound[f_, lower_, section_] := (
If[NumberQ[f] || !FreeQ[lower, f], Return[]];
ResetKnownBounds[];
PrependTo[GivenLowerAt[section][TermNumber[f]], lower]);
(* add a identity to the tontext "section" *)
AddIdentity[a_ == b_, section_] :=
AppendTo[GivenIdentitiesAt[section], a->b];
(* add simplify rules which comes from "form", to the context "section" *)
AddSimplifyRules[form_, section_] :=
(RulesGivenIn[section] =
Union[RulesFrom[form], RulesGivenIn[section]]);
(* initialize the current proof context *)
Initialize[] := (
(* if the prover is at a temporal context , end it *)
While[!FreeQ[CurrentPath, Temp], EndSection[]];
(* clear the set of given properties *)
RulesGivenIn[CurrentSection] = {};
GivenIdentitiesAt[CurrentSection] = {};
(* clear the set of given upper or lower bounds of expressions *)
clear[GivenUpperAt[CurrentSection], GivenLowerAt[CurrentSection]];
GivenUpperAt[CurrentSection][_] := {};
GivenLowerAt[CurrentSection][_] := {};
(* reset the known upper or lower bound *)
ResetKnownBounds[];
(* reset the branch stack *)
BranchStack = {};
);