00001 package edu.ksu.cis.bandera.abstraction.pvs;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.io.*;
00036 import java.util.*;
00037
00038 public class PVS {
00039 public static Vector proofs;
00040
00041
00042 public static void begin_float(PrintWriter f_pvs) {
00043 f_pvs.print("abstraction: THEORY\n\nBEGIN\n\n");
00044 f_pvs.print(" b: VAR boolean\n");
00045 f_pvs.print(" TRUE?(b): boolean = (b=TRUE)\n");
00046 f_pvs.print(" FALSE?(b): boolean = (b=FALSE)\n\n");
00047 f_pvs.print(" \n");
00048 f_pvs.print(" e,e1,e2: VAR real\n");
00049 f_pvs.print(" nz_e: VAR {e | e/=0}\n");
00050 }
00051
00052 public static void begin_int(PrintWriter f_pvs) {
00053 f_pvs.print("basic1: THEORY\n\nBEGIN\n\n");
00054 f_pvs.print(" e1: VAR int\n e2: VAR {e:int|e/=0}\n");
00055 f_pvs.print(" /(e1,e2) : int = floor(e1/e2)\n END basic1\n\n");
00056 f_pvs.print("basic2: THEORY\n\nBEGIN\n\n");
00057 f_pvs.print(" e1: VAR int\n e2: VAR {e:int|e/=0}\n");
00058 f_pvs.print(" //(e1,e2) : int = e1 - e2*floor(e1/e2)\n");
00059 f_pvs.print(" END basic2\n\n");
00060
00061 f_pvs.print(" abstraction: THEORY\n\nBEGIN\n\n");
00062 f_pvs.print("IMPORTING basic1\n");
00063 f_pvs.print("IMPORTING basic2\n");
00064 f_pvs.print(" b: VAR boolean\n");
00065 f_pvs.print(" TRUE?(b): boolean = (b=TRUE)\n");
00066 f_pvs.print(" FALSE?(b): boolean = (b=FALSE)\n\n");
00067 f_pvs.print(" \n");
00068 f_pvs.print(" e,e1,e2: VAR int\n");
00069 f_pvs.print(" nz_e: VAR {e | e/=0}\n");
00070
00071 }
00072
00073 public static void check(PrintWriter f_pvs, int npredicates) {
00074 int i,j;
00075 for(i=0;i<npredicates;i++)
00076 f_pvs.print(" default"+i+": THEOREM P"+i+"(0)\n");
00077
00078 f_pvs.print(" domain: THEOREM ");
00079 for(i=0;i<npredicates-1;i++)
00080 f_pvs.print(" P"+i+"(e) OR ");
00081 f_pvs.print(" P"+i+"(e)\n");
00082
00083 f_pvs.print(" overlap: THEOREM ");
00084 for(i=0;i<npredicates;i++)
00085 for(j=i+1;j<npredicates;j++)
00086 f_pvs.print(" NOT(P"+i+"(e) AND P"+j+"(e)) AND ");
00087 f_pvs.print(" TRUE\n\n");
00088 }
00089
00090 public static void check_BASL(PrintWriter f_spec, int npredicates,
00091 Vector tokens) {
00092 int lookahead;
00093
00094 for(int i=0;i<npredicates;i++) {
00095 lookahead=((Integer)proofs.elementAt(i)).intValue();
00096 if(lookahead==1) {
00097 f_spec.print("\n DEFAULT = "+tokens.elementAt(i)+";\n");
00098 i=npredicates;
00099 }
00100 }
00101 lookahead=((Integer)proofs.elementAt(npredicates)).intValue();
00102 if(lookahead==0)
00103 f_spec.print("/* Warning: abstraction does not cover the concrete domain!!! */\n");
00104 lookahead=((Integer)proofs.elementAt(npredicates+1)).intValue();
00105 if(lookahead==0)
00106 f_spec.print("/* Warning: overlapping !!! */\n");
00107
00108
00109 }
00110
00111 public static void execAndWait(String command) {
00112
00113 try {
00114 System.out.println(command);
00115 Runtime runtime = Runtime.getRuntime();
00116 Process p = runtime.exec(command);
00117 InputStream commandErr = p.getErrorStream();
00118 InputStream commandOut = p.getInputStream();
00119
00120 byte [] buffer = new byte[4000];
00121 int count = 0;
00122 int charsAvail = commandErr.available();
00123 if (charsAvail > 0)
00124 count = commandErr.read(buffer,count,charsAvail);
00125
00126 proofs = read(commandOut);
00127
00128 String output = new String(buffer,0,count);
00129 System.err.println(output);
00130 p.waitFor();
00131 return;
00132 }
00133 catch (Exception e) {
00134 throw new RuntimeException("exec of command '" + command
00135 + "' was aborted: \n" + e);
00136 }
00137
00138 }
00139
00140 public static void prf(PrintWriter f_prf, int npredicates, int basic_type)
00141 {
00142 int n;
00143 int i,j,k;
00144
00145
00146
00147
00148 f_prf.print("(|abstraction|\n");
00149
00150 for(i=0;i<npredicates;i++)
00151 f_prf.print(" (|default"+i+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00152 f_prf.print(" (|domain| \"\" (GRIND) (SKIP) (SKIP))\n");
00153 f_prf.print(" (|overlap| \"\" (GRIND) (SKIP) (SKIP))\n");
00154
00155 n=1;
00156 for(i=0;i<npredicates;i++)
00157 for(j=0;j<npredicates;j++)
00158 for(k=0;k<npredicates;k++) {
00159 f_prf.print(" (|add"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00160 n++;
00161 }
00162
00163 n=1;
00164 for(i=0;i<npredicates;i++)
00165 for(j=0;j<npredicates;j++)
00166 for(k=0;k<npredicates;k++) {
00167 f_prf.print(" (|sub"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00168 n++;
00169 }
00170
00171 n=1;
00172 for(i=0;i<npredicates;i++)
00173 for(j=0;j<npredicates;j++)
00174 for(k=0;k<npredicates;k++) {
00175 f_prf.print(" (|mul"+n+"| \"\" (GRIND :THEORIES (\"real_props\")) (SKIP) (SKIP))\n");
00176 n++;
00177 }
00178
00179 n=1;
00180 for(i=0;i<npredicates;i++)
00181 for(j=0;j<npredicates;j++)
00182 for(k=0;k<npredicates;k++) {
00183 f_prf.print(" (|div"+n+"| \"\" (GRIND :THEORIES (\"real_props\")) (SKIP) (SKIP))\n");
00184 n++;
00185 }
00186
00187 if(basic_type==sym.INT) {
00188 n=1;
00189 for(i=0;i<npredicates;i++)
00190 for(j=0;j<npredicates;j++)
00191 for(k=0;k<npredicates;k++) {
00192 f_prf.print(" (|rem"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00193 n++;
00194 }
00195 }
00196
00197 n=1;
00198 for(i=0;i<npredicates;i++)
00199 for(j=0;j<npredicates;j++) {
00200 f_prf.print(" (|eq"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00201 n++;
00202 f_prf.print(" (|eq"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00203 n++;
00204 }
00205
00206
00207
00208 n=1;
00209 for(i=0;i<npredicates;i++)
00210 for(j=0;j<npredicates;j++) {
00211 f_prf.print(" (|lt"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00212 n++;
00213 f_prf.print(" (|lt"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00214 n++;
00215 }
00216
00217
00218 n=1;
00219 for(i=0;i<npredicates;i++)
00220 for(j=0;j<npredicates;j++) {
00221 f_prf.print(" (|gt"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00222 n++;
00223 f_prf.print(" (|gt"+n+"| \"\" (GRIND) (SKIP) (SKIP))\n");
00224 n++;
00225 }
00226
00227
00228
00229 f_prf.print(")");
00230
00231 }
00232
00233 public static Vector read(InputStream is) {
00234 try {
00235 BufferedReader r = new BufferedReader( new InputStreamReader(is));
00236 String line = r.readLine();
00237 Vector v = new Vector();
00238
00239 while(line != null) {
00240 StringTokenizer st = new StringTokenizer(line);
00241 while (st.hasMoreTokens()) {
00242 String s = st.nextToken();
00243 if(s.compareTo("proved")==0)
00244 v.add( new Integer(1));
00245 if(s.compareTo("unproved")==0)
00246 v.add( new Integer(0));
00247 }
00248 line = r.readLine();
00249 }
00250 r.close();
00251 return v;
00252 } catch (Exception e) {
00253 e.printStackTrace();
00254 throw new RuntimeException("could not read output from pvs\n");
00255 }
00256 }
00257
00258 public static void rest(PrintWriter f_pvs, int npredicates, int basic_type) {
00259
00260 int n;
00261 int i,j,k;
00262
00263
00264
00265
00266
00267 f_pvs.println("\n");
00268
00269 n=1;
00270 for(i=0;i<npredicates;i++)
00271 for(j=0;j<npredicates;j++)
00272 for(k=0;k<npredicates;k++) {
00273 f_pvs.print(" add"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT P"+k+"(e1+e2))\n");
00274 n++;
00275 }
00276
00277 f_pvs.print("\n");
00278 n=1;
00279 for(i=0;i<npredicates;i++)
00280 for(j=0;j<npredicates;j++)
00281 for(k=0;k<npredicates;k++) {
00282 f_pvs.print(" sub"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT P"+k+"(e1-e2))\n");
00283 n++;
00284 }
00285
00286 f_pvs.print("\n");
00287 n=1;
00288 for(i=0;i<npredicates;i++)
00289 for(j=0;j<npredicates;j++)
00290 for(k=0;k<npredicates;k++) {
00291 f_pvs.print(" mul"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT P"+k+"(e1*e2))\n");
00292 n++;
00293 }
00294
00295 f_pvs.print("\n");
00296 n=1;
00297 for(i=0;i<npredicates;i++)
00298 for(j=0;j<npredicates;j++)
00299 for(k=0;k<npredicates;k++) {
00300 f_pvs.print(" div"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(nz_e)) IMPLIES NOT P"+k+"(e1/nz_e))\n");
00301 n++;
00302 }
00303
00304 if(basic_type==sym.INT) {
00305 f_pvs.print("\n");
00306 n=1;
00307 for(i=0;i<npredicates;i++)
00308 for(j=0;j<npredicates;j++)
00309 for(k=0;k<npredicates;k++) {
00310 f_pvs.print(" rem"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(nz_e)) IMPLIES NOT P"+k+"(e1//nz_e))\n");
00311 n++;
00312 }
00313 }
00314 f_pvs.print("\n");
00315 n=1;
00316 for(i=0;i<npredicates;i++)
00317 for(j=0;j<npredicates;j++) {
00318 f_pvs.print(" eq"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT TRUE?(e1=e2))\n"); n++;
00319 f_pvs.print(" eq"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT FALSE?(e1=e2))\n");
00320 n++;
00321 }
00322
00323
00324
00325 f_pvs.print("\n");
00326 n=1;
00327 for(i=0;i<npredicates;i++)
00328 for(j=0;j<npredicates;j++) {
00329 f_pvs.print(" lt"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT TRUE?(e1<e2))\n"); n++;
00330 f_pvs.print(" lt"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT FALSE?(e1<e2))\n");
00331 n++;
00332 }
00333
00334
00335
00336 f_pvs.print("\n");
00337 n=1;
00338 for(i=0;i<npredicates;i++)
00339 for(j=0;j<npredicates;j++) {
00340 f_pvs.print(" gt"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT TRUE?(e1>e2))\n"); n++;
00341 f_pvs.print(" gt"+n+": THEOREM ((P"+i+"(e1) AND P"+j+"(e2)) IMPLIES NOT FALSE?(e1>e2))\n");
00342 n++;
00343 }
00344
00345
00346 f_pvs.println("END abstraction");
00347
00348 }
00349
00350 public static void rest_BASL(PrintWriter f_spec, int npredicates,
00351 int basic_type, Vector tokens) {
00352 int n,i,j,k,flag,m;
00353
00354 Vector v;
00355 int value;
00356
00357 try {
00358
00359 n=npredicates+2;
00360
00361 int lookahead=((Integer)proofs.elementAt(n)).intValue();
00362
00363
00364 f_spec.print(" operator + add\n begin\n");
00365 for(i=0;i<npredicates;i++)
00366 for(j=0;j<npredicates;j++) {
00367 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00368 ") -> ");flag=0;
00369 for(k=0;k<npredicates;k++) {
00370 if(lookahead==0) {
00371 if(flag==0) {f_spec.print("{");flag=1;}
00372 else f_spec.print(",");
00373 f_spec.print(" "+tokens.elementAt(k)+" ");
00374 }
00375 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00376 }
00377 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00378 else f_spec.print("};\n");
00379 }
00380 f_spec.print(" end\n\n");
00381
00382
00383 f_spec.print(" operator - sub\n begin\n");
00384 for(i=0;i<npredicates;i++)
00385 for(j=0;j<npredicates;j++) {
00386 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00387 ") -> ");flag=0;
00388 for(k=0;k<npredicates;k++) {
00389 if(lookahead==0) {
00390 if(flag==0) {f_spec.print("{");flag=1;}
00391 else f_spec.print(",");
00392 f_spec.print(" "+tokens.elementAt(k)+" ");
00393 }
00394 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00395 }
00396 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00397 else f_spec.print("};\n");
00398 }
00399 f_spec.print(" end\n\n");
00400
00401
00402 f_spec.print(" operator * mul\n begin\n");
00403 for(i=0;i<npredicates;i++)
00404 for(j=0;j<npredicates;j++) {
00405 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00406 ") -> ");flag=0;
00407 for(k=0;k<npredicates;k++) {
00408 if(lookahead==0) {
00409 if(flag==0) {f_spec.print("{");flag=1;}
00410 else f_spec.print(",");
00411 f_spec.print(" "+tokens.elementAt(k)+" ");
00412 }
00413 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00414 }
00415 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00416 else f_spec.print("};\n");
00417 }
00418 f_spec.print(" end\n\n");
00419
00420
00421
00422 f_spec.print(" operator / div\n begin\n");
00423 for(i=0;i<npredicates;i++)
00424 for(j=0;j<npredicates;j++) {
00425 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00426 ") -> ");flag=0;
00427 for(k=0;k<npredicates;k++) {
00428 if(lookahead==0) {
00429 if(flag==0) {f_spec.print("{");flag=1;}
00430 else f_spec.print(",");
00431 f_spec.print(" "+tokens.elementAt(k)+" ");
00432 }
00433 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00434 }
00435 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00436 else f_spec.print("};\n");
00437 }
00438 f_spec.print(" end\n\n");
00439
00440 if(basic_type == sym.INT) {
00441
00442 f_spec.print(" operator % rem\n begin\n");
00443 for(i=0;i<npredicates;i++)
00444 for(j=0;j<npredicates;j++) {
00445 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00446 ") -> ");flag=0;
00447 for(k=0;k<npredicates;k++) {
00448 if(lookahead==0) {
00449 if(flag==0) {f_spec.print("{");flag=1;}
00450 else f_spec.print(",");
00451 f_spec.print(" "+tokens.elementAt(k)+" ");
00452 }
00453 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00454 }
00455 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00456 else f_spec.print("};\n");
00457 }
00458 f_spec.print(" end\n\n");
00459 }
00460
00461
00462
00463 v = new Vector();
00464
00465 f_spec.print(" test == eq\n begin\n");
00466 for(i=0;i<npredicates;i++)
00467 for(j=0;j<npredicates;j++) {
00468
00469 value = 0;
00470
00471 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00472 ") -> ");flag=0;
00473 if(lookahead==0) {
00474 if(flag==0) {f_spec.print("{");flag=1;}
00475 else f_spec.print(",");
00476 f_spec.print(" TRUE ");
00477
00478 value = 1;
00479
00480 }
00481 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00482 if(lookahead==0) {
00483 if(flag==0) {f_spec.print("{");flag=1;}
00484 else f_spec.print(",");
00485 f_spec.print(" FALSE ");
00486
00487 if(value==1) value=0; else value=2;
00488
00489 }
00490 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00491
00492 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00493 else f_spec.print("};\n");
00494
00495 v.add( new Integer(value));
00496
00497 }
00498 f_spec.print(" end\n\n");
00499
00500
00501 m =0;
00502 f_spec.print(" test != neq\n begin\n");
00503 for(i=0;i<npredicates;i++)
00504 for(j=0;j<npredicates;j++) {
00505 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00506 ") -> ");
00507
00508 value = ((Integer)v.elementAt(m)).intValue();m++;
00509 if(value==0)
00510 f_spec.print("{ TRUE , FALSE };\n");
00511 if(value==1)
00512 f_spec.print("{ FALSE };\n");
00513 if(value==2)
00514 f_spec.print("{ TRUE };\n");
00515 }
00516 f_spec.print(" end\n\n");
00517
00518 v = new Vector();
00519
00520 f_spec.print(" test < lt\n begin\n");
00521 for(i=0;i<npredicates;i++)
00522 for(j=0;j<npredicates;j++) {
00523
00524 value = 0;
00525
00526 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00527 ") -> ");flag=0;
00528 if(lookahead==0) {
00529 if(flag==0) {f_spec.print("{");flag=1;}
00530 else f_spec.print(",");
00531 f_spec.print(" TRUE ");
00532
00533 value = 1;
00534
00535 }
00536 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00537 if(lookahead==0) {
00538 if(flag==0) {f_spec.print("{");flag=1;}
00539 else f_spec.print(",");
00540 f_spec.print(" FALSE ");
00541
00542 if(value==1) value=0; else value=2;
00543
00544 }
00545 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00546
00547 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00548 else f_spec.print("};\n");
00549
00550 v.add( new Integer(value));
00551 }
00552 f_spec.print(" end\n\n");
00553
00554
00555 m =0;
00556 f_spec.print(" test >= ge\n begin\n");
00557 for(i=0;i<npredicates;i++)
00558 for(j=0;j<npredicates;j++) {
00559 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00560 ") -> ");
00561
00562 value = ((Integer)v.elementAt(m)).intValue();m++;
00563 if(value==0)
00564 f_spec.print("{ TRUE , FALSE };\n");
00565 if(value==1)
00566 f_spec.print("{ FALSE };\n");
00567 if(value==2)
00568 f_spec.print("{ TRUE };\n");
00569 }
00570 f_spec.print(" end\n\n");
00571
00572 v = new Vector();
00573
00574 f_spec.print(" test > gt\n begin\n");
00575 for(i=0;i<npredicates;i++)
00576 for(j=0;j<npredicates;j++) {
00577
00578 value = 0;
00579
00580 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00581 ") -> ");flag=0;
00582 if(lookahead==0) {
00583 if(flag==0) {f_spec.print("{");flag=1;}
00584 else f_spec.print(",");
00585 f_spec.print(" TRUE ");
00586
00587 value = 1;
00588
00589 }
00590 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00591 if(lookahead==0) {
00592 if(flag==0) {f_spec.print("{");flag=1;}
00593 else f_spec.print(",");
00594 f_spec.print(" FALSE ");
00595
00596 if(value==1) value=0; else value=2;
00597
00598 }
00599 n++; lookahead=((Integer)proofs.elementAt(n)).intValue();
00600
00601 if (flag==0) f_spec.print(" T; /* ERROR */\n");
00602 else f_spec.print("};\n");
00603
00604 v.add( new Integer(value));
00605 }
00606 f_spec.print(" end\n\n");
00607
00608
00609 m =0;
00610 f_spec.print(" test <= le\n begin\n");
00611 for(i=0;i<npredicates;i++)
00612 for(j=0;j<npredicates;j++) {
00613 f_spec.print(" ("+tokens.elementAt(i)+" , "+tokens.elementAt(j)+
00614 ") -> ");
00615
00616 value = ((Integer)v.elementAt(m)).intValue();m++;
00617 if(value==0)
00618 f_spec.print("{ TRUE , FALSE };\n");
00619 if(value==1)
00620 f_spec.print("{ FALSE };\n");
00621 if(value==2)
00622 f_spec.print("{ TRUE };\n");
00623 }
00624 f_spec.print(" end\n\n");
00625
00626 f_spec.print(" end\n");
00627
00628 } catch (Exception e) {
00629 e.printStackTrace();
00630 throw new
00631 RuntimeException("could not generate spec file from pvs output");
00632 }
00633
00634 }
00635 }