src/Tools/qed.cc
changeset 902 cc80f53b28c6
parent 813 4a266c3d4cc0
equal deleted inserted replaced
901:77795af99315 902:cc80f53b28c6
     1 // Little utility to convert result() -> qed ... in Isabelle's files
     1 // Little utility to convert result() -> qed ... in Isabelle's files
     2 // Written in 1994 by Carsten Clasohm (clasohm@informatik.tu-muenchen.de)
     2 // Written in 1995 by Carsten Clasohm (clasohm@informatik.tu-muenchen.de)
     3 
     3 
     4 #define LIN_LEN   1000		// maximal length of lines in sourcefile
     4 #define LIN_LEN   1000		// maximal length of lines in sourcefile
     5 
     5 
     6 #include <iostream.h>
     6 #include <iostream.h>
     7 #include <fstream.h>
     7 #include <fstream.h>
     8 #include <string.h>
     8 #include <string.h>
     9 #include <assert.h>
     9 #include <assert.h>
    10 #include <stdio.h>
    10 #include <stdio.h>
    11 #include <unistd.h>
    11 #include <unistd.h>
    12 #include <ctype.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 }
    13 
    25 
    14 main(int argc, char *argv[])
    26 main(int argc, char *argv[])
    15 {
    27 {
    16     char l[LIN_LEN];
    28     char l[LIN_LEN];
    17     int lines = 0;
    29     int lines = 0;
    39 	char *rPos;
    51 	char *rPos;
    40 	char *valPos;
    52 	char *valPos;
    41 	char *eqPos;
    53 	char *eqPos;
    42 	char *tmp;
    54 	char *tmp;
    43 
    55 
    44 	if ((rPos = strstr(l, "result()")) && (!isalpha(*(rPos-1)))
    56 	if ((rPos = strstr(l, "result()")) && (!isalnum(*(rPos-1)))
    45 	    && (valPos = strstr(l, "val ")) && (eqPos = strstr(l, "=")))
    57 	    && (valPos = strstr(l, "val ")) && (eqPos = strstr(l, "=")))
    46 	{                            // does line contain "result()" and "val"?
    58 	{                            // does line contain "result()" and "val"?
    47 	    char name[LIN_LEN];
    59 	    char name[LIN_LEN];
    48 
    60 
    49 	    assert(eqPos-(valPos+4) > 0);
    61 	    assert(eqPos-(valPos+4) > 0);
    50 	    strncpy(name, valPos+4, eqPos-(valPos+4));
    62 	    strncpy(name, valPos+4, eqPos-(valPos+4));
    51 	    name[eqPos-(valPos+4)] = 0;
    63 	    name[eqPos-(valPos+4)] = 0;
    52 	    if (!isalpha(name[eqPos-(valPos+4)-1]))
    64 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
    53 	        name[eqPos-(valPos+4)-1] = 0;
    65 	        name[eqPos-(valPos+4)-1] = 0;
    54 #ifdef DEBUG
    66 #ifdef DEBUG
    55 	    cerr << "Found result: \"" << name << "\"\n";
    67 	    cerr << "Found result: \"" << name << "\"\n";
    56 #endif
    68 #endif
    57 	    char prefix[LIN_LEN];
    69 	    char prefix[LIN_LEN];
    58 	    char arg[LIN_LEN];
    70 	    char arg[LIN_LEN];
    59 
    71 
    60 	    if ((rPos - eqPos < 5) && (rPos == strstr(l, "result();")))
    72 	    if (ExcludeThm(name))
       
    73 		out << l << '\n';
       
    74 	    else if ((rPos - eqPos < 5) && (rPos == strstr(l, "result();")))
    61 	    {                                              // replace by "qed"?
    75 	    {                                              // replace by "qed"?
    62 		strncpy(prefix, l, valPos-l);
    76 		strncpy(prefix, l, valPos-l);
    63 		prefix[valPos-l] = 0;
    77 		prefix[valPos-l] = 0;
    64 		out << prefix << "qed \"" << name << "\";" << '\n';
    78 		out << prefix << "qed \"" << name << "\";" << '\n';
    65 	    }
    79 	    }
    73 		out << prefix << "bind_thm(\"" << name << "\", "
    87 		out << prefix << "bind_thm(\"" << name << "\", "
    74 		    << arg << ");\n";
    88 		    << arg << ");\n";
    75 	    }
    89 	    }
    76 	}
    90 	}
    77 	else if ((rPos = strstr(l, "prove_goal")) 
    91 	else if ((rPos = strstr(l, "prove_goal")) 
    78 		 && (!isalpha(*(rPos-1))) 
    92 		 && (!isalnum(*(rPos-1))) 
    79 		 && (!isalpha(*(rPos+10)) || (*(rPos+10) == 'w'))
    93 		 && (!isalnum(*(rPos+10)) || (*(rPos+10) == 'w'))
    80 		 && (valPos = strstr(l, "val ")) 
    94 		 && (valPos = strstr(l, "val ")) 
    81 		 && (eqPos = strstr(l, "="))
    95 		 && (eqPos = strstr(l, "="))
    82                  && (rPos - eqPos < 5))
    96                  && (rPos - eqPos < 5))
    83 	{                                    // replace prove_goal by qed_goal?
    97 	{                                    // replace prove_goal by qed_goal?
    84 	    char name[LIN_LEN];
    98 	    char name[LIN_LEN];
    85 
    99 
    86 	    assert(eqPos-(valPos+4) > 0);
   100 	    assert(eqPos-(valPos+4) > 0);
    87 	    strncpy(name, valPos+4, eqPos-(valPos+4));
   101 	    strncpy(name, valPos+4, eqPos-(valPos+4));
    88 	    name[eqPos-(valPos+4)] = 0;
   102 	    name[eqPos-(valPos+4)] = 0;
    89 	    if (!isalpha(name[eqPos-(valPos+4)-1]))
   103 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
    90 	        name[eqPos-(valPos+4)-1] = 0;
   104 	        name[eqPos-(valPos+4)-1] = 0;
    91 #ifdef DEBUG
   105 #ifdef DEBUG
    92 	    cerr << "Found prove_goal: \"" << name << "\"\n";
   106 	    cerr << "Found prove_goal: \"" << name << "\"\n";
    93 #endif
   107 #endif
    94 	    char prefix[LIN_LEN];
   108 	    if (ExcludeThm(name))
    95 	    char arg[LIN_LEN];
   109 		out << l << '\n';
       
   110 	    else
       
   111 	    {
       
   112 	        char prefix[LIN_LEN];
       
   113 	        char arg[LIN_LEN];
    96 
   114 
    97 	    strncpy(prefix, l, valPos-l);
   115 	        strncpy(prefix, l, valPos-l);
    98 	    prefix[valPos-l] = 0;
   116 	        prefix[valPos-l] = 0;
    99 	    out << prefix << "qed_goal" << ((*(rPos+10) == 'w') ? "w " : " ")
   117 	        out << prefix << "qed_goal"
   100 		<< '\"' << name << '\"' << strchr(rPos, ' ') << '\n';
   118                     << ((*(rPos+10) == 'w') ? "w " : " ")
       
   119 		    << '\"' << name << '\"' << strchr(rPos, ' ') << '\n';
       
   120 	    }
   101 	}
   121 	}
   102 	else if ((rPos = strstr(l, "standard"))
   122 	else if ((rPos = strstr(l, "standard"))
   103 		 && (!isalpha(*(rPos-1))) 
   123 		 && (!isalnum(*(rPos-1))) 
   104 		 && (!isalpha(*(rPos+8))) 
   124 		 && (!isalnum(*(rPos+8))) 
   105 		 && (valPos = strstr(l, "val ")) 
   125 		 && (valPos = strstr(l, "val ")) 
   106 		 && (eqPos = strstr(l, "="))
   126 		 && (eqPos = strstr(l, "="))
   107                  && (rPos - eqPos < 5)
   127                  && (rPos - eqPos < 5)
   108                  && (l[strlen(l)-1] == ';'))
   128                  && (l[strlen(l)-1] == ';'))
   109 	{                                                   // insert bind_thm?
   129 	{                                                   // insert bind_thm?
   110 	    char name[LIN_LEN];
   130 	    char name[LIN_LEN];
   111 
   131 
   112 	    assert(eqPos-(valPos+4) > 0);
   132 	    assert(eqPos-(valPos+4) > 0);
   113 	    strncpy(name, valPos+4, eqPos-(valPos+4));
   133 	    strncpy(name, valPos+4, eqPos-(valPos+4));
   114 	    name[eqPos-(valPos+4)] = 0;
   134 	    name[eqPos-(valPos+4)] = 0;
   115 	    if (!isalpha(name[eqPos-(valPos+4)-1]))
   135 	    if (!isalnum(name[eqPos-(valPos+4)-1]))
   116 	        name[eqPos-(valPos+4)-1] = 0;
   136 	        name[eqPos-(valPos+4)-1] = 0;
   117 #ifdef DEBUG
   137 #ifdef DEBUG
   118 	    cerr << "Found standard: \"" << name << "\"\n";
   138 	    cerr << "Found standard: \"" << name << "\"\n";
   119 #endif
   139 #endif
   120 	    char prefix[LIN_LEN];
   140 	    if (ExcludeThm(name))
   121 	    char arg[LIN_LEN];
   141 		out << l << '\n';
       
   142 	    else
       
   143 	    {
       
   144 	        char prefix[LIN_LEN];
       
   145 	        char arg[LIN_LEN];
   122 
   146 
   123 	    strncpy(prefix, l, valPos-l);
   147 	        strncpy(prefix, l, valPos-l);
   124 	    prefix[valPos-l] = 0;
   148 	        prefix[valPos-l] = 0;
   125 	    strcpy(l+strlen(l)-1, ");");        // insert ")" before line's ';'
   149 	        strcpy(l+strlen(l)-1, ");");    // insert ")" before line's ';'
   126 	    out << prefix << "bind_thm (\"" << name << "\","
   150 	        out << prefix << "bind_thm (\"" << name << "\","
   127 	        << strchr(rPos, ' ') << '\n';
   151 	            << strchr(rPos, ' ') << '\n';
       
   152             }
   128 	}
   153 	}
   129 	else                                           // output line unchanged
   154 	else                                           // output line unchanged
   130 	    out << l << '\n';
   155 	    out << l << '\n';
   131 	in.getline(l, LIN_LEN);
   156 	in.getline(l, LIN_LEN);
   132     }
   157     }