src/Tools/qed.cc
changeset 3743 fec0f996ae67
parent 3742 6fccb16a7e3a
child 3744 9921561ade57
equal deleted inserted replaced
3742:6fccb16a7e3a 3743:fec0f996ae67
     1 // Little utility to convert result() -> qed ... in Isabelle's files
       
     2 // Written in 1995 by Carsten Clasohm (clasohm@informatik.tu-muenchen.de)
       
     3 
       
     4 #define LIN_LEN   1000		// maximal length of lines in sourcefile
       
     5 
       
     6 #include <iostream.h>
       
     7 #include <fstream.h>
       
     8 #include <string.h>
       
     9 #include <assert.h>
       
    10 #include <stdio.h>
       
    11 #include <unistd.h>
       
    12 #include <ctype.h>
       
    13 
       
    14 // Null terminated list of theorem names that must not be included in the
       
    15 // theorem database
       
    16 char * excludeName[] = {"temp", "tmp", 0};
       
    17 
       
    18 int ExcludeThm(char *name)
       
    19 {
       
    20     for (int i = 0; excludeName[i]; i++)
       
    21 	if (strcmp(name, excludeName[i]) == 0)
       
    22 	    return 1;
       
    23     return 0;
       
    24 }
       
    25 
       
    26 main(int argc, char *argv[])
       
    27 {
       
    28     char l[LIN_LEN];
       
    29     int lines = 0;
       
    30 
       
    31     // Open input and output files
       
    32     ifstream in(argv[1]);
       
    33     ofstream out(argv[2]);
       
    34 
       
    35     if (in.bad() || out.bad())
       
    36     {
       
    37         cerr << "qed version 1.00, Written in 1994 by Carsten Clasohm"
       
    38                 "(clasohm@informatik.tu-muenchen.de)\n\n";
       
    39 	cerr << "Usage: qed <infile> <outfile>\n";
       
    40 	exit(1);
       
    41     }
       
    42 
       
    43 #ifdef DEBUG
       
    44     cerr << "Processing file " << argv[1] << '\n';
       
    45 #endif
       
    46 
       
    47     // Process each line separatly
       
    48     in.getline(l, LIN_LEN);
       
    49     while (!in.eof())
       
    50     {
       
    51 	char *rPos;
       
    52 	char *valPos;
       
    53 	char *eqPos;
       
    54 	char *tmp;
       
    55 
       
    56 	if ((rPos = strstr(l, "result()")) && (!isalnum(*(rPos-1)))
       
    57 	    && (valPos = strstr(l, "val ")) && (eqPos = strstr(l, "=")))
       
    58 	{                            // does line contain "result()" and "val"?
       
    59 	    char name[LIN_LEN];
       
    60 
       
    61 	    assert(eqPos-(valPos+4) > 0);
       
    62 	    strncpy(name, valPos+4, eqPos-(valPos+4));
       
    63 	    name[eqPos-(valPos+4)] = 0;
       
    64 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
       
    65 	        name[eqPos-(valPos+4)-1] = 0;
       
    66 #ifdef DEBUG
       
    67 	    cerr << "Found result: \"" << name << "\"\n";
       
    68 #endif
       
    69 	    char prefix[LIN_LEN];
       
    70 	    char arg[LIN_LEN];
       
    71 
       
    72 	    if (ExcludeThm(name))
       
    73 		out << l << '\n';
       
    74 	    else if ((rPos - eqPos < 5) && (rPos == strstr(l, "result();")))
       
    75 	    {                                              // replace by "qed"?
       
    76 		strncpy(prefix, l, valPos-l);
       
    77 		prefix[valPos-l] = 0;
       
    78 		out << prefix << "qed \"" << name << "\";" << '\n';
       
    79 	    }
       
    80 	    else				         // replace by bind_thm
       
    81 	    {                                           
       
    82 		int d = (*(eqPos+1) == ' ') ? 2 : 1;
       
    83 		strcpy(arg, eqPos+d);
       
    84 		arg[strlen(arg)-1] = 0;
       
    85 		strcpy(prefix, l);
       
    86 		prefix[valPos-l] = 0;
       
    87 		out << prefix << "bind_thm(\"" << name << "\", "
       
    88 		    << arg << ");\n";
       
    89 	    }
       
    90 	}
       
    91 	else if ((rPos = strstr(l, "prove_goal")) 
       
    92 		 && (!isalnum(*(rPos-1))) 
       
    93 		 && (!isalnum(*(rPos+10)) || (*(rPos+10) == 'w'))
       
    94 		 && (valPos = strstr(l, "val ")) 
       
    95 		 && (eqPos = strstr(l, "="))
       
    96                  && (rPos - eqPos < 5))
       
    97 	{                                    // replace prove_goal by qed_goal?
       
    98 	    char name[LIN_LEN];
       
    99 
       
   100 	    assert(eqPos-(valPos+4) > 0);
       
   101 	    strncpy(name, valPos+4, eqPos-(valPos+4));
       
   102 	    name[eqPos-(valPos+4)] = 0;
       
   103 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
       
   104 	        name[eqPos-(valPos+4)-1] = 0;
       
   105 #ifdef DEBUG
       
   106 	    cerr << "Found prove_goal: \"" << name << "\"\n";
       
   107 #endif
       
   108 	    if (ExcludeThm(name))
       
   109 		out << l << '\n';
       
   110 	    else
       
   111 	    {
       
   112 	        char prefix[LIN_LEN];
       
   113 	        char arg[LIN_LEN];
       
   114 
       
   115 	        strncpy(prefix, l, valPos-l);
       
   116 	        prefix[valPos-l] = 0;
       
   117 	        out << prefix << "qed_goal"
       
   118                     << ((*(rPos+10) == 'w') ? "w " : " ")
       
   119 		    << '\"' << name << '\"' << strchr(rPos, ' ') << '\n';
       
   120 	    }
       
   121 	}
       
   122 	else if ((rPos = strstr(l, "standard"))
       
   123 		 && (!isalnum(*(rPos-1))) 
       
   124 		 && (!isalnum(*(rPos+8))) 
       
   125 		 && (valPos = strstr(l, "val ")) 
       
   126 		 && (eqPos = strstr(l, "="))
       
   127                  && (rPos - eqPos < 5)
       
   128                  && (l[strlen(l)-1] == ';'))
       
   129 	{                                                   // insert bind_thm?
       
   130 	    char name[LIN_LEN];
       
   131 
       
   132 	    assert(eqPos-(valPos+4) > 0);
       
   133 	    strncpy(name, valPos+4, eqPos-(valPos+4));
       
   134 	    name[eqPos-(valPos+4)] = 0;
       
   135 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
       
   136 	        name[eqPos-(valPos+4)-1] = 0;
       
   137 #ifdef DEBUG
       
   138 	    cerr << "Found standard: \"" << name << "\"\n";
       
   139 #endif
       
   140 	    if (ExcludeThm(name))
       
   141 		out << l << '\n';
       
   142 	    else
       
   143 	    {
       
   144 	        char prefix[LIN_LEN];
       
   145 	        char arg[LIN_LEN];
       
   146 
       
   147 	        strncpy(prefix, l, valPos-l);
       
   148 	        prefix[valPos-l] = 0;
       
   149 	        strcpy(l+strlen(l)-1, ");");    // insert ")" before line's ';'
       
   150 	        out << prefix << "bind_thm (\"" << name << "\","
       
   151 	            << strchr(rPos, ' ') << '\n';
       
   152             }
       
   153 	}
       
   154 	else                                           // output line unchanged
       
   155 	    out << l << '\n';
       
   156 	in.getline(l, LIN_LEN);
       
   157     }
       
   158     in.close();
       
   159     out.close();
       
   160 #ifdef DEBUG
       
   161     cerr << "Done\n";
       
   162 #endif
       
   163 }