-rw-r--r-- 22650 holtrace-20261122/holtrace-prune.c raw
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "vector.h"
#include "in.h"
#include "out.h"
#include "maybesandbox.h"
typedef uint8_t uint8;
VECTOR_define_inline(uint8)
typedef long long num;
VECTOR_define_inline(num)
// ===== general constraints
#define CUTOFF 288230376151711743LL
// but likely to abort before that from running out of heap space
// also, could abort from running out of stack space for recursion
// want this in production, ignoring NDEBUG, always having side effects
#define know(property) ((property) ? 0 : (fprintf(stderr,"code line %d: internal error: failed %s\n",__LINE__,#property), exit(100)))
static void reject(num reject_pos,const char *err)
{
fprintf(stderr,"line starting at %lld bytes: %s\n",reject_pos,err);
exit(100);
}
// ===== general parsing
static const num offset[128] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,3,4,5,7,0,9,10,8,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
} ;
// some allowed variations:
// will accept leading zeros
// will not require, e.g., id-1 to be !
// will accept space even when not needed
static num subid_core(num reject_pos,const char **s,num id)
{
const char *t = *s;
char c = *t;
if (c == '\n') reject(reject_pos,"missing numeric argument");
num o = offset[127&c];
if (o) {
*s = t+1;
return id-o;
}
int negate = 0;
if (c == '-') { ++t; negate = 1; }
num result = 0;
for (;;) {
c = *t;
if (result >= CUTOFF) {
fprintf(stderr,"line starting at %lld bytes: refusing to handle number %lld\n",reject_pos,result);
exit(100);
}
if (c >= '0' && c <= '9')
result = 32*result+(c-'0');
else if (c >= 'A' && c <= 'V')
result = 32*result+(c-'7');
else break;
++t;
}
if (*t == ' ') ++t;
if (t == *s) reject(reject_pos,"non-numeric argument where numeric is expected");
*s = t;
return negate ? id-result : result;
}
static num subid(num reject_pos,const char **s,num id)
{
num result = subid_core(reject_pos,s,id);
if (result < 0 || (result >= id && id >= 0)) {
fprintf(stderr,"line starting at %lld bytes: result %lld out of range\n",reject_pos,result);
exit(100);
}
return result;
}
static void noteol(num reject_pos,const char *s)
{
if (*s == '\n') reject(reject_pos,"empty name");
}
static void eol(num reject_pos,const char *s)
{
if (*s != '\n') reject(reject_pos,"extra arguments");
}
static const char *parse_str(const char **s)
{
const char *result = *s;
while (**s != '\n')
++*s;
return result;
}
static void skip_bracket(num start,const char **s)
{
if (**s != ']')
reject(start,"expecting bracket");
++*s;
}
static const char *parse_str_bracket(const char **s)
{
const char *result = *s;
while (**s != '\n' && **s != ']')
++*s;
return result;
}
// ===== bit vectors
typedef vector_uint8 vector_bit;
static int vector_bit_read(const vector_bit *v,num pos)
{
return 1&(vector_uint8_read(v,pos/8)>>(pos&7));
}
static void vector_bit_write(vector_bit *v,num pos,int bit)
{
uint8 byte = vector_uint8_read(v,pos/8);
if (1&(bit^(byte>>(pos&7))))
vector_uint8_write(v,pos/8,byte^(1<<(pos&7)));
}
// ===== first pass: find terms etc. and mark what we want to print
static num sorted_vector_num_lt(const vector_num *v,num than)
{
num left = 0;
num right = vector_num_len(v);
while (left < right) {
num mid = left+(right-left)/2;
if (vector_num_read(v,mid) < than)
left = mid+1;
else
right = mid;
}
return left;
}
// during input, type2data[id] is start position of line with type id
// during output, type2data[oldid] is corresponding newid
// similarly for terms etc.
static vector_num type2data;
static vector_num term2data;
static vector_num thm2data;
static vector_num inf2data;
static vector_num typedef2data;
static vector_num typedef2inf; // map typedef ID to inf ID for the A inference
static vector_num name2pos; // start position of theorem name as \n-terminated string inside in_buf
static vector_num thm2name; // map theorem id to an index in name2pos of name for that theorem
static vector_num name2next; // next index of name for same theorem
static vector_num app2pos; // position of app name inside in_buf
static vector_num app2len; // length of app name inside in_buf
static vector_num app2inf; // inf ID of app name
static vector_num constinf; // inference IDs creating constants
static vector_num constterm; // c term IDs
static vector_num constterm2inf; // corresponding inference IDs
static num types_lt(num start) { return sorted_vector_num_lt(&type2data,start); }
static num terms_lt(num start) { return sorted_vector_num_lt(&term2data,start); }
static num thms_lt(num start) { return sorted_vector_num_lt(&thm2data,start); }
static num infs_lt(num start) { return sorted_vector_num_lt(&inf2data,start); }
static num typedefs_lt(num start) { return sorted_vector_num_lt(&typedef2data,start); }
static vector_bit wanttype;
static vector_bit wantterm;
static vector_bit wantthm;
static vector_bit wantinf;
static vector_bit wanttypedef;
static void mark_inf(num);
static void mark_type(num id)
{
know(id >= 0);
if (vector_bit_read(&wanttype,id)) return;
vector_bit_write(&wanttype,id,1);
num start = vector_num_read(&type2data,id);
know(start >= 0);
know(start < in_bytes);
char tag = in_buf[start];
const char *s = in_buf+start+1;
if (tag == 'a') return;
know(tag == 'b');
num arity = subid(start,&s,-1);
for (num i = 0;i < arity;++i)
mark_type(subid(start,&s,id));
noteol(start,s);
const char *appname = parse_str(&s);
num applen = s-appname;
if (applen == 4 && !memcmp(appname,"bool",4)) return;
if (applen == 3 && !memcmp(appname,"fun",3)) return;
if (applen == 3 && !memcmp(appname,"ind",3)) return;
for (num i = vector_num_len(&app2len)-1;i >= 0;--i)
if (applen == vector_num_read(&app2len,i))
if (!memcmp(in_buf+vector_num_read(&app2pos,i),appname,applen)) {
mark_inf(vector_num_read(&app2inf,i));
return;
}
}
static void mark_term(num id)
{
know(id >= 0);
if (vector_bit_read(&wantterm,id)) return;
vector_bit_write(&wantterm,id,1);
num start = vector_num_read(&term2data,id);
know(start >= 0);
know(start < in_bytes);
char tag = in_buf[start];
const char *s = in_buf+start+1;
num prevtypes = types_lt(start);
mark_type(subid(start,&s,prevtypes));
noteol(start,s);
if (tag == 'e' || tag == 'f') {
mark_term(subid(start,&s,id));
mark_term(subid(start,&s,id));
eol(start,s);
return;
}
if (tag == 'c') {
num pos = sorted_vector_num_lt(&constterm,id);
know(pos < vector_num_len(&constterm));
know(vector_num_read(&constterm,pos) == id);
num infid = vector_num_read(&constterm2inf,pos);
if (infid >= 0)
mark_inf(infid);
return;
}
know(tag == 'd'); // nothing to do
}
static void mark_thm(num id)
{
know(id >= 0);
if (vector_bit_read(&wantthm,id)) return;
vector_bit_write(&wantthm,id,1);
num start = vector_num_read(&thm2data,id);
know(start >= 0);
know(start < in_bytes);
char tag = in_buf[start];
know(tag == 't');
const char *s = in_buf+start+1;
if (*s == '\n') reject(start,"theorem with no conclusion");
num prevterms = terms_lt(start);
while (*s != '\n')
mark_term(subid(start,&s,prevterms));
num previnfs = infs_lt(start);
if (previnfs <= 0) reject(start,"theorem without preceding inference");
mark_inf(previnfs-1);
}
static void mark_typedef(num id)
{
know(id >= 0);
if (vector_bit_read(&wanttypedef,id)) return;
vector_bit_write(&wanttypedef,id,1);
num start = vector_num_read(&typedef2data,id);
know(start >= 0);
know(start < in_bytes);
char tag = in_buf[start];
know(tag == 'A');
mark_inf(vector_num_read(&typedef2inf,id));
}
static void mark_inf(num id)
{
know(id >= 0);
if (vector_bit_read(&wantinf,id)) return;
vector_bit_write(&wantinf,id,1);
num start = vector_num_read(&inf2data,id);
know(start >= 0);
know(start < in_bytes);
char tag = in_buf[start];
const char *s = in_buf+start+1;
if (tag == 'X' || tag == 'B' || tag == 'R' || tag == 'U' || tag == 'C') {
num prevterms = terms_lt(start);
mark_term(subid(start,&s,prevterms));
} else if (tag == 'P' || tag == 'V' || tag == 'Z' || tag == 'E') {
num prevthms = thms_lt(start);
mark_thm(subid(start,&s,prevthms));
mark_thm(subid(start,&s,prevthms));
} else if (tag == 'F') {
num prevterms = terms_lt(start);
num prevthms = thms_lt(start);
mark_term(subid(start,&s,prevterms));
mark_thm(subid(start,&s,prevthms));
} else if (tag == 'S') {
num prevterms = terms_lt(start);
num prevthms = thms_lt(start);
mark_thm(subid(start,&s,prevthms));
while (*s != '\n')
mark_term(subid(start,&s,prevterms));
} else if (tag == 'T') {
num prevtypes = types_lt(start);
num prevthms = thms_lt(start);
mark_thm(subid(start,&s,prevthms));
while (*s != '\n')
mark_type(subid(start,&s,prevtypes));
} else if (tag == 'A') {
num prevtypes = types_lt(start);
num prevterms = terms_lt(start);
num prevthms = thms_lt(start);
mark_type(subid(start,&s,prevtypes));
mark_term(subid(start,&s,prevterms));
mark_thm(subid(start,&s,prevthms));
} else {
know(tag == 'D');
num prevtypedefs = typedefs_lt(start);
mark_typedef(subid(start,&s,prevtypedefs));
}
}
static int selected(const char *s,const char **selections)
{
// empty list is specially defined as accepting everything
if (!selections[0]) return 1;
know(in_buf < s);
know(s-in_buf < in_bytes);
num slen = 0;
for (;;) {
if (s[slen] == '\n') break;
know(s[slen] != 0);
++slen;
know(s+slen-in_buf < in_bytes);
}
while (selections[0]) {
num i;
for (i = 0;i < slen;++i)
if (selections[0][i] != s[i])
break;
if (i == slen)
if (selections[0][i] == 0)
return 1;
++selections;
}
return 0;
}
// return ID of inference matching this c term (which is latest term)
// or -1 if not found (requiring "=" and "@")
static num find_constant(num termstart)
{
char termtag = in_buf[termstart];
const char *termstr = in_buf+termstart+1;
know(termtag == 'c');
know(vector_num_len(&type2data) == types_lt(termstart));
subid(termstart,&termstr,vector_num_len(&type2data));
noteol(termstart,termstr);
const char *termconstname = parse_str(&termstr);
num termconstlen = termstr-termconstname;
eol(termstart,termstr);
if (termconstlen == 1) {
if (*termconstname == '=') return -1;
if (*termconstname == '@') return -1;
}
// XXX: consider data structures removing the quadratic worst case here
num len = vector_num_len(&constinf);
for (num i = len-1;i >= 0;--i) {
num infid = vector_num_read(&constinf,i);
num infstart = vector_num_read(&inf2data,infid);
char inftag = in_buf[infstart];
const char *infstr = in_buf+infstart+1;
if (inftag == 'C') {
subid(infstart,&infstr,terms_lt(infstart));
const char *infconstname = parse_str(&infstr);
num infconstlen = infstr-infconstname;
eol(infstart,infstr);
if (infconstlen == termconstlen)
if (!memcmp(termconstname,infconstname,infconstlen))
return infid;
} else {
know(inftag == 'A');
subid(infstart,&infstr,types_lt(infstart));
subid(infstart,&infstr,terms_lt(infstart));
subid(infstart,&infstr,thms_lt(infstart));
skip_bracket(infstart,&infstr);
parse_str_bracket(&infstr);
skip_bracket(infstart,&infstr);
const char *absname = parse_str_bracket(&infstr);
num abslen = infstr-absname;
skip_bracket(infstart,&infstr);
const char *repname = parse_str_bracket(&infstr);
num replen = infstr-repname;
skip_bracket(infstart,&infstr);
if (abslen == termconstlen)
if (!memcmp(termconstname,absname,abslen))
return infid;
if (replen == termconstlen)
if (!memcmp(termconstname,repname,replen))
return infid;
}
}
reject(termstart,"unknown constant");
return -1;
}
// append to app2pos, app2len, app2inf
static void remember_abstypename(num start)
{
char tag = in_buf[start];
const char *s = in_buf+start+1;
know(tag == 'A');
subid(start,&s,types_lt(start));
subid(start,&s,terms_lt(start));
subid(start,&s,thms_lt(start));
skip_bracket(start,&s);
const char *abstypename = parse_str_bracket(&s);
num abstypelen = s-abstypename;
skip_bracket(start,&s);
vector_num_append(&app2pos,abstypename-in_buf);
vector_num_append(&app2len,abstypelen);
vector_num_append(&app2inf,infs_lt(start));
}
static void input_line(num start,const char **selections)
{
know(0 <= start);
know(start < in_bytes);
char tag = in_buf[start];
if (tag == '\n') reject(start,"blank line prohibited");
switch(tag) {
case 'a': case 'b':
vector_num_append(&type2data,start);
return;
case 'c': case 'd': case 'e': case 'f':
if (tag == 'c') {
vector_num_append(&constterm,vector_num_len(&term2data));
vector_num_append(&constterm2inf,find_constant(start));
}
vector_num_append(&term2data,start);
return;
case 't':
vector_num_append(&thm2data,start);
vector_num_append(&thm2name,-1);
return;
case 'A': case 'B': case 'C': case 'D':
case 'E': case 'F': case 'P': case 'R':
case 'S': case 'T': case 'U': case 'V':
case 'X': case 'Z':
if (tag == 'C' || tag == 'A')
vector_num_append(&constinf,vector_num_len(&inf2data));
if (tag == 'A') {
vector_num_append(&typedef2data,start);
vector_num_append(&typedef2inf,vector_num_len(&inf2data));
remember_abstypename(start);
}
vector_num_append(&inf2data,start);
return;
case ',': case ';':
// ProofTrace information; prune
return;
case '/':
const char *s = in_buf+start+1;
num th = subid(start,&s,vector_num_len(&thm2data));
noteol(start,s);
if (selected(s,selections))
mark_thm(th);
num prev = vector_num_read(&thm2name,th);
vector_num_write(&thm2name,th,vector_num_len(&name2pos));
know(vector_num_len(&name2pos) == vector_num_len(&name2next));
vector_num_append(&name2pos,s-in_buf);
vector_num_append(&name2next,prev);
return;
default:
fprintf(stderr,"line starting at %lld bytes: unrecognized tag %c\n",start,tag);
exit(100);
}
}
static void input(const char **selections)
{
num start = 0;
for (num i = 0;i < in_bytes;++i) {
char c = in_buf[i];
if (c == '\n') {
if (start) input_line(start,selections);
start = i+1;
continue;
}
if (c < 32 || c > 126) {
fprintf(stderr,"byte %d is not printable ASCII\n",(int) c);
exit(100);
}
}
}
// ===== output numbers
static num simpleencode_len(num x)
{
if (x == 0) return 1;
num result = 0;
if (x < 0) { ++result; x = -x; }
while (x > 0) {
result += 1;
x >>= 5;
}
return result;
}
static num compress(num x,num reference)
{
know(x >= 0);
num y = x-reference;
if (y >= 0) return x;
if (y >= -10) return y;
if (simpleencode_len(y) <= simpleencode_len(x)) return y;
return x;
}
static void output_simpleencode(num x)
{
if (-10 <= x && x <= 0) { out("0!@#$%^&*()"-x,1); return; }
know(x <= CUTOFF);
know(x >= -CUTOFF);
if (x < 0) { out("-",1); x = -x; }
num len = 0;
for (num y = x;y > 0;y >>= 5) ++len;
while (len > 0) {
--len;
out("0123456789ABCDEFGHIJKLMNOPQRSTUV"+((x>>(5*len))&31),1);
}
}
static int output_wasbase32;
// 0 means: no argument yet on this line, or last argument was a number encoded as one of the characters !@#$%^&*()
// 1 otherwise
static void output_tag(char tag)
{
out(&tag,1);
output_wasbase32 = 0;
}
static void output_subid(num x,num reference)
{
int newbase32 = (x >= reference) || (x < reference-10);
x = compress(x,reference);
if (output_wasbase32)
if (x >= 0)
out(" ",1);
output_simpleencode(x);
output_wasbase32 = newbase32;
}
static void output_str(const char *s)
{
know(in_buf < s);
know(s-in_buf < in_bytes);
char c = *s;
if (c == '\n') return;
if ((c == ' ') || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'V')))
if (output_wasbase32)
out(" ",1);
while (c != '\n') {
out(&c,1);
++s;
know(in_buf < s);
know(s-in_buf < in_bytes);
c = *s;
}
output_wasbase32 = 0;
}
// ===== high-level output functions
static num oldtypeid;
static num oldtermid;
static num oldthmid;
static num oldinfid;
static num oldtypedefid;
static num newtypeid;
static num newtermid;
static num newthmid;
static num newinfid;
static num newtypedefid;
static void translate_typeid(num start,const char **s)
{
num id = subid(start,s,oldtypeid);
id = vector_num_read(&type2data,id);
output_subid(id,newtypeid);
}
static void translate_termid(num start,const char **s)
{
num id = subid(start,s,oldtermid);
id = vector_num_read(&term2data,id);
output_subid(id,newtermid);
}
static void translate_thmid(num start,const char **s)
{
num id = subid(start,s,oldthmid);
id = vector_num_read(&thm2data,id);
output_subid(id,newthmid);
}
static void translate_typedefid(num start,const char **s)
{
num id = subid(start,s,oldtypedefid);
id = vector_num_read(&typedef2data,id);
output_subid(id,newtypedefid);
}
static void printnames(num name)
{
if (name < 0) return;
// print linked list in reverse order
printnames(vector_num_read(&name2next,name));
num pos = vector_num_read(&name2pos,name);
know(pos >= 0);
out("/!",2);
for (;;) {
know(pos < in_bytes);
char c = in_buf[pos];
if (c == '\n') break;
out(&c,1);
++pos;
}
out("\n",1);
}
static void output_line(num start)
{
know(0 <= start);
know(start < in_bytes);
char tag = in_buf[start];
const char *s = in_buf+start+1;
if (tag == '\n') reject(start,"blank line prohibited");
switch(tag) {
case 'a': case 'b':
if (vector_bit_read(&wanttype,oldtypeid)) {
output_tag(tag);
if (tag == 'a') {
output_str(s);
} else {
know(tag == 'b');
num arity = subid(start,&s,-1);
output_subid(arity,-1);
for (num i = 0;i < arity;++i)
translate_typeid(start,&s);
noteol(start,s);
output_str(s);
}
out("\n",1);
vector_num_write(&type2data,oldtypeid,newtypeid);
++newtypeid;
}
++oldtypeid;
return;
case 'c': case 'd': case 'e': case 'f':
if (vector_bit_read(&wantterm,oldtermid)) {
output_tag(tag);
if (tag == 'c' || tag == 'd') {
translate_typeid(start,&s);
noteol(start,s);
output_str(s);
} else {
know(tag == 'e' || tag == 'f');
translate_typeid(start,&s);
translate_termid(start,&s);
translate_termid(start,&s);
eol(start,s);
}
out("\n",1);
vector_num_write(&term2data,oldtermid,newtermid);
++newtermid;
}
++oldtermid;
return;
case 't':
if (vector_bit_read(&wantthm,oldthmid)) {
output_tag(tag);
while (*s != '\n')
translate_termid(start,&s);
out("\n",1);
vector_num_write(&thm2data,oldthmid,newthmid);
++newthmid;
printnames(vector_num_read(&thm2name,oldthmid));
}
++oldthmid;
return;
case 'A': case 'B': case 'C': case 'D':
case 'E': case 'F': case 'P': case 'R':
case 'S': case 'T': case 'U': case 'V':
case 'X': case 'Z':
if (vector_bit_read(&wantinf,oldinfid)) {
output_tag(tag);
switch(tag) {
case 'X': case 'B': case 'R': case 'U':
translate_termid(start,&s);
eol(start,s);
break;
case 'P': case 'V': case 'Z': case 'E':
translate_thmid(start,&s);
translate_thmid(start,&s);
eol(start,s);
break;
case 'F':
translate_termid(start,&s);
translate_thmid(start,&s);
eol(start,s);
break;
case 'S':
translate_thmid(start,&s);
while (*s != '\n')
translate_termid(start,&s);
break;
case 'T':
translate_thmid(start,&s);
while (*s != '\n')
translate_typeid(start,&s);
break;
case 'C':
translate_termid(start,&s);
noteol(start,s);
output_str(s);
break;
case 'A':
translate_typeid(start,&s);
translate_termid(start,&s);
translate_thmid(start,&s);
noteol(start,s);
output_str(s);
break;
default:
know(tag == 'D');
translate_typedefid(start,&s);
break;
}
out("\n",1);
vector_num_write(&inf2data,oldinfid,newinfid);
++newinfid;
if (tag == 'A') {
vector_num_write(&typedef2data,oldtypedefid,newtypedefid);
++newtypedefid;
}
}
++oldinfid;
if (tag == 'A')
++oldtypedefid;
return;
case ',': case ';': case '/':
return;
default:
fprintf(stderr,"line starting at %lld bytes: unrecognized tag %c\n",start,tag);
exit(100);
}
}
static void minusone(vector_num *v)
{
num len = vector_num_len(v);
for (num i = 0;i < len;++i)
vector_num_write(v,i,-1);
}
static void output(void)
{
minusone(&type2data);
minusone(&term2data);
minusone(&thm2data);
minusone(&inf2data);
minusone(&typedef2data);
out_string("HOLTrace 1\n");
num start = 0;
for (num i = 0;i < in_bytes;++i)
if (in_buf[i] == '\n') {
if (start) output_line(start);
start = i+1;
}
}
// ===== top-level driver
int main(int argc,char **argv)
{
maybesandbox();
in_init();
if (in_bytes < 11 || memcmp(in_buf,"HOLTrace 1\n",11)) {
fprintf(stderr,"input does not start with HOLTrace 1\n");
exit(100);
}
if (in_bytes > CUTOFF) {
fprintf(stderr,"refusing to handle more than %lld bytes\n",CUTOFF);
exit(100);
}
if (in_buf[in_bytes-1] != '\n') {
fprintf(stderr,"refusing to allow partial final line");
exit(100);
}
if (argc) ++argv;
input((const char **)argv);
output();
out_flush();
return 0;
}