Newer
Older
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
classdef PVS_checker < handle
% class contains all the functionality specific to the PVS theorem
% prover
properties
gui = [];
counter_examples = 10000;
counter_size = 100;
end
methods
%% constructor
function obj = PVS_checker(gui_new)
obj.gui = gui_new;
end
%% pvs_ext_call
function [] = pvs_check(obj)
% file name will be expression_name.m
fileid = fopen([get(obj.gui.function_name_control,'String') '.pvs'],'w');
code = [];
code = sprintf('%s:THEORY\nBEGIN\n',get(obj.gui.function_name_control,'String'));
code = [code obj.pvs_check_for_imports];
% declare variables for each input
inputs = get(obj.gui.function_inputs_control,'String');
inputs = regexp(inputs,',','split');
% initialize inputs to zero
% functions are assumed to be total, so 0 is just for
% convienence
inputs_new = [];
for i=1:size(inputs,2)
% set to zero
code = [code sprintf('%s:VAR real\n',char(inputs(i)))];
inputs_new = [inputs_new ',' char(inputs(i))];
end
code = [code sprintf('%s(%s):real = \n',get(obj.gui.function_name_control,'String'),get(obj.gui.function_inputs_control,'String'))];
code = [code obj.generate_pvs(obj.gui.Grid1,obj.gui.Grid2,0)];
code = [code sprintf('\nEND %s',get(obj.gui.function_name_control,'String'))];
fprintf(fileid,code);
fclose(fileid);
% generate the proof script
obj.generate_pvs_script(get(obj.gui.function_name_control,'String'))
% call pvs in batch mode with script
% ADD check that pvs actually exists in system
[status, result] = system(['pvs -batch -v 3 -l ' get(obj.gui.function_name_control,'String') '.el'])
%object.msgbox_scroll(result);
parsed = obj.parse_pvs_result(result);
if (isempty(parsed))
msgbox('table is valid')
else
Valid_Report = ValidationReport(obj.gui);
Valid_Report.set_results(parsed);
Valid_Report.init();
end
end
%% parse_pvs_result
function output = parse_pvs_result(obj,result)
found = regexp(result,'\w+(?=[\.]{4,})|(?<=[\.]{4,})\w+','match')
char(found)
seqs = [];
for i = 1:2:size(found,2)-1
if(~strcmp(char(found(i+1)),'proved'))
found2 = []
found3 = []
found4 = []
% find the TCC that was unprovable
search_str = sprintf('(?<=Proving formula %s[\\n]*)%s.*?(?=[ \\n]*Rerunning step)',char(found(i)),char(found(i)))
found2 = regexp(result,char(search_str),'match','once')
search_str = sprintf('(?<=%s.*?substitutions:).*?(?=This will undo the proof to: \\n%s)|(?<=%s.*?)No counter-examples(?=.*?%s)',char(found(i)),char(found(i)),char(found(i)),char(found(i)))
found3 = regexp(result,char(search_str),'match','once')
if (~strcmp(found3,'No counter-examples'))
found4 = regexp(found3,'[^\n]*(?===>)|(?<===>)[^\n]*','match')
end
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
seqs = [seqs struct('id', '1234a', 'TCC', found(i), 'seq', found2, 'ce', {found4})]
end
end
output = seqs;
end
%% matlab_to_pvs_syntax_translation
function pvs_string = matlab_to_pvs_syntax_translation(obj,matlab_string)
pvs_string = regexprep(matlab_string,'&&',' AND ')
pvs_string = regexprep(pvs_string,'~(?!=)',' NOT ')
pvs_string = regexprep(pvs_string,'~=',' /= ')
pvs_string = regexprep(pvs_string,'==',' = ')
pvs_string = regexprep(pvs_string,'\|\|',' OR ')
pvs_string = regexprep(pvs_string,'ceil','ceiling')
end
%% generate_pvs_horizontal
function code = generate_pvs_horizontal(obj,g1,g2_cell,depth)
code = [];
code = [code sprintf('COND\n')];
elsecell = [];
for i=1:size(g1.cells,2)
g1cond = get(g1.cells(i).cond,'String');
% condition string of otherwise corresponds to an else
% statement, we allow this statement to be in any order
% in the cells of the grid, so we need to find where it
% is, if it exists.
if (strcmp(g1cond,'otherwise'))
elsecell = g1.cells(i);
found = 1;
continue
end
code = [code sprintf('%s->',obj.matlab_to_pvs_syntax_translation(strtrim(char(get(g1.cells(i).cond,'String')))))];
resultcell = obj.gui.Grid0.search_return(g1.cells(i),g2_cell);
if(~isempty(resultcell))
code = [code obj.matlab_to_pvs_syntax_translation(strtrim(char(get(resultcell.result,'String'))))]
if(isempty(elsecell))
if(i~= size(g1.cells,2))
code = [code sprintf(',\n')];
else
code = [code sprintf('\n')];
end
else
code = [code sprintf(',\n')];
end
else
end
end
if(~isempty(elsecell))
resultcell = obj.gui.Grid0.search_return(elsecell,g2_cell);
code = [code sprintf('ELSE->%s\n',obj.matlab_to_pvs_syntax_translation(strtrim(char(get(resultcell.result,'String')))))];
end
code = [code sprintf('ENDCOND')];
end
%% genereate_pvs
function code = generate_pvs(obj,g1,g2,depth)
g1cond1 = get(g1.cells(1).cond,'String');
g2cond1 = get(g2.cells(1).cond,'String');
code = [];
%1D horizontal
if(~isempty(g1cond1) && isempty(g2cond1))
code = [code obj.generate_pvs_horizontal(g1,g2.cells(1),depth)];
% something in vertical column
else
code = [code sprintf('COND\n')];
elsecell = [];
for i=1:size(g2.cells,2)
g2cond = get(g2.cells(i).cond,'String');
% condition string of otherwise corresponds to an else
% statement, we allow this statement to be in any order
% in the cells of the grid, so we need to find where it
% is, if it exists.
if (strcmp(g2cond,'otherwise'))
elsecell = g2.cells(i);
found = 1;
continue
end
code = [code sprintf('%s->',obj.matlab_to_pvs_syntax_translation(strtrim(char(get(g2.cells(i).cond,'String')))))];
if(~isempty(g2.cells(i).subgrid))
code = [code obj.generate_pvs(g1,g2.cells(i).subgrid,depth)];
else
if (~isempty(g1cond1))
code = [code obj.generate_pvs_horizontal(g1,g2.cells(i),depth)];
else
resultcell = obj.gui.Grid0.search_return(g1.cells(1),g2.cells(i));
if(~isempty(resultcell))
code = [code obj.matlab_to_pvs_syntax_translation(strtrim(char(get(resultcell.result,'String'))))]
else
end
end
end
if(isempty(elsecell))
if(i~= size(g2.cells,2))
code = [code sprintf(',\n')];
else
code = [code sprintf('\n')];
end
else
code = [code sprintf(',\n')];
end
end
if(~isempty(elsecell))
%resultcell = obj.Grid0.search_return(elsecell,elsecell);
code = [code sprintf('ELSE->')];
if(~isempty(elsecell.subgrid))
code = [code obj.generate_pvs(g1,elsecell.subgrid,depth) sprintf('\n')];
else
if (~isempty(g1cond1))
code = [code obj.generate_pvs_horizontal(g1,elsecell,depth) sprintf('\n')];
else
resultcell = obj.gui.Grid0.search_return(g1.cells(1),elsecell);
if(~isempty(resultcell))
code = [code obj.matlab_to_pvs_syntax_translation(strtrim(char(get(resultcell.result,'String')))) sprintf('\n')]
end
end
end
end
code = [code sprintf('ENDCOND')];
end
code
end
%% pvs_check_for_imports
function string = pvs_check_for_imports(obj)
[string,found] = obj.pvs_check_for_imports_g(obj.gui.Grid2,{});
[string2,found2] = obj.pvs_check_for_imports_g(obj.gui.Grid1,found)
string = [string string2];
end
%% pvs_check_for_imports
function [string,found] = pvs_check_for_imports_g(obj,grid,found)
string = [];
for i=1:size(grid.cells,2)
% recurse through
if (~isempty(grid.cells(i).subgrid))
string = [string obj.pvs_check_for_imports_g(grid.cells(i).subgrid)];
end
text = char(get(grid.cells(i).cond,'String'));
vars = regexp(text,'([a-zA-Z][a-zA-Z0-9_]*)','match')
for j=1:size(vars,2)
if(exist(vars{j},'file')==2 && ~strcmp(vars{j},'otherwise'))
%check if already imported file
if (~any(ismember(found,vars{j})))
string = [string 'IMPORTING ' vars{j} sprintf('\n')];
found = [found vars{j}]
end
end
end
end
end
%% pvs_check_for_imports
function [] = pvs_check_for_imports_rg(obj)
end
%% generate_pvs_script
function [] = generate_pvs_script(obj,filename)
fileid = fopen([filename '.el'],'w');
code = ['(prove-tccs-pvs-file "' filename '" "(try (try (tcc) (try (assert) (skip) (fail) ) (fail))(skip) (random-test :count ' sprintf('%ld',obj.counter_examples) ' :size ' sprintf('%d',obj.counter_size) '))" )' sprintf('\n')]
fprintf(fileid,code)
fclose(fileid);
end
end
end