-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshullivan.c
7290 lines (6166 loc) · 159 KB
/
shullivan.c
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
77
78
79
80
81
82
83
84
85
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* shullivan.c - C implementation of Ghilbert theorem checker */
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <assert.h>
#include "shullivan.h"
#define ALLOC_ERR() \
fprintf (stderr, "Allocation error (%s, line %d)\n", __FILE__, __LINE__)
typedef struct _GROWBUF {
void * buf;
size_t size;
} GROWBUF;
#define COMMAND_ERR -1
#define COMMAND_EOF 0
#define COMMAND_CONTINUE_GOOD 1
#define COMMAND_CONTINUE_BAD 2
typedef enum _SCAN_RESULT {
SCAN_ERR = -1,
SCAN_EOF = 0,
SCAN_ID = 1, /* identifier */
SCAN_LPAREN = 2,
SCAN_RPAREN = 3,
/* values greater than 3 indicate keywords in a scanner-
specific fashion */
} SCAN_RESULT;
typedef enum _SCAN_FLAGS {
SF_EOF = 1, /* read_more has reached EOF */
SF_WS = 2, /* insert implicit white-space char before next
read_more () */
} SCAN_FLAGS;
/*
* Minimum buffer length for scanner identifier buffer.
* This ensures there is space for all our keywords, etc.
*/
#define SCAN_ID_BUFLEN_MIN 1024
typedef struct _SCANNER SCANNER;
typedef int (*SCANNER_GET_TOKEN) (SCANNER * scan);
typedef char * (*SCANNER_READ_MORE) (SCANNER * scan, void * ctx);
typedef void (*SCANNER_FREE_LINE) (char * line,
SCANNER * scan, void * ctx);
typedef int (*SCANNER_PROMPT_SET) (SCANNER * scan, char * prompt);
typedef void (*SCANNER_CLEANUP) (SCANNER * scan);
struct _SCANNER {
char * curline;
int len;
int ix;
uint32_t flags;
SCANNER_GET_TOKEN get_token;
SCANNER_READ_MORE read_more;
SCANNER_FREE_LINE free_line;
SCANNER_PROMPT_SET prompt_set;
SCANNER_CLEANUP cleanup;
void * ctx;
GROWBUF gb;
int idlen; /* length of identifier in buf */
unsigned int lineno;
};
typedef enum _HTYPE {
HT_IMPORT,
HT_EXPORT,
HT_THM,
HT_DEF,
HT_VAROLDKIND, /* Record a variable's old kind. Must be immediately
followed by HT_VARNEWKIND. */
HT_VARNEWKIND, /* Record a variable's old kind. Must be immediately
followed by HT_VAR. */
HT_VAR, /* Add a variable */
HT_KINDBIND0, /* records old kind for immeiately following
kindbind */
HT_KINDBIND /* a .gh-style kindbind */
} HTYPE;
typedef struct _HISTORY_ITEM {
HTYPE htype;
void * it;
} HISTORY_ITEM;
/*
* The shullivan history is a two-level array of HISTORY_ITEMs.
* Each entry in the top level array is a pointer to a 'chunk'
* of 1024 history items.
*/
#define HISTORY_CHUNK_SIZE 1024
typedef struct _HISTORY_CHUNK {
HISTORY_ITEM h [HISTORY_CHUNK_SIZE];
} HISTORY_CHUNK;
#define HISTORY_TOP_LEVEL_SIZE 16384
typedef struct _DVWORK {
int i;
IDENT * id;
} DVWORK;
typedef struct _SHULLIVAN {
ITEM * freeItems;
MAPPING * interfaces; /* imports */
MAPPING * syms; /* theorems/statements,
variables */
MAPPING * terms; /* terms & defs */
MAPPING * kinds; /* kinds */
MAPPING * varIndex; /* Scratch variable index mapping
used by parseStatement() and
load_def() and export_stmt() */
GROWBUF vi; /* shared buffer for EXPR_VARINFO's
used by parseStatement() and
load_def() and export_stmt() */
GROWBUF env; /* scratch buffer for environment
info in proof steps. Used in
proofStepApply() */
int ndvvars; /* number of vars pairs of which
will fit in dvbits */
int dvsize; /* size in bytes of dvbits buffer */
uint32_t * dvbits; /* scratch buffer for DV calcs in
proofStepApply() */
GROWBUF dvw; /* DV work buffer */
MAPPING * fvi; /* scratch free variable sets */
MAPPING * fvj; /* fvi, fvj used in proofStepApply() */
ARENA_BLK * backing;
char delim; /* search path delimiter character,
default ':' */
char ** path; /* array of directories for imports */
int ndirs; /* number of dirs in the search path */
HISTORY_CHUNK ** history;
unsigned long histlen;
unsigned long histmax; /* size of top level array */
int saveProof; /* if not 0, save proof info */
int verbose; /* verbosity level */
#define SHUL_VERB_PROGRESS 1 /* note import, export, thm verify */
#define SHUL_VERB_COMMANDS 2 /* echo all commands */
#define SHUL_VERB_PROOF 4 /* print all results in proof */
#define SHUL_VERB_PRFWILD 8 /* print wild var hyps in pf. */
#define SHUL_VERB_PRINT_PRETTY 16 /* pretty-print expressions */
unsigned long flags;
#define MULTIPLE_CONCLUSIONS 1 /* Allow 0 or more conclusions */
#define LOOSE_VAR_KINDS 2 /* allow variable kind inferrence */
#define EXPORT_LOOSE_VAR_KINDS 4 /* var kind inference in export */
#define EXPORT_WARN_DV 8 /* Warn of unneeded DVs in export */
#define REQ_MULT_CONC_SYNTAX 16 /* Multiple conclusion syntax req'd. */
#define DEF_JUSTIFY 32 /* Require justification proof for
definitions with dummy variables. */
} SHULLIVAN;
typedef int (*COMMAND_FUNC) (SHULLIVAN * shul, ITEM * item);
typedef void * (*DV_ENUMERATE_FUNC) (int i, int j, void * ctx);
static void * dvEnumerate (int nvars, uint32_t * dvbits,
DV_ENUMERATE_FUNC f, void * ctx);
static void sexpr_print (FILE * f, ITEM * item);
static void exprPrint (FILE * f, EXPR * exp, unsigned long verbose,
int indent);
static int port (SHULLIVAN * shul, ITEM * args, int importing);
static void ifaceFree (SHULLIVAN * shul, INTERFACE * iface);
static char * shulPrompt = "shul=> ";
static char * shulContinuePrompt = "> ";
/*
* Increase the size of gb's buffer to at least n bytes.
* If gb's buffer is already at least n bytes, the function returns
* successfully without changing the buffer size. Otherwise, the buffer
* is grown (keeping its current contents).
*
* To avoid too many calls to this routine, the buffer size is
* doubled until it reaches at least n bytes, or hits a very
* large ceiling size.
*
* Returns 0 if buffer is at least n bytes after the call.
* Returns -1 if the buffer is less than n bytes after the call.
* In either case, the old buffer contents (if any) are retained.
*/
static int
growBuf (GROWBUF * gb, size_t n)
{
void * p;
size_t m;
assert (gb != NULL);
m = gb->size;
if (m >= n)
return 0;
if (m == 0)
m = 256;
while (m < n && m * 2 < (size_t)0x80000000)
m = m * 2;
if (m < n)
return -1;
p = realloc (gb->buf, m);
if (p == NULL)
return -1;
gb->buf = p;
gb->size = m;
return 0;
}
static int
growBufInit (GROWBUF * gb, size_t n)
{
assert (gb != NULL && n > 0);
if (gb->buf != NULL)
free (gb->buf);
gb->size = 0;
if ((gb->buf = malloc (n)) == NULL)
return -1;
gb->size = n;
return 0;
}
static int
histAdd (SHULLIVAN * shul, HTYPE htype, void * it)
{
unsigned long len = shul->histlen;
unsigned long topi = len / HISTORY_CHUNK_SIZE;
HISTORY_CHUNK * chunk;
len = len % HISTORY_CHUNK_SIZE;
if (topi >= shul->histmax) {
HISTORY_CHUNK ** ppChunk;
assert (topi == shul->histmax && len == 0);
/* Add top-level space for a 1024*1024 more items */
fprintf (stderr, "Growing top-level history from "
"%lu to %lu\n",
shul->histlen, shul->histlen + 1024);
ppChunk = realloc (shul->history,
((shul->histmax + 1024) *
sizeof (*shul->history)));
if (ppChunk == NULL) {
perror ("histAdd:realloc");
return -1;
}
memset (shul->history + shul->histmax, 0,
1024 * sizeof (*shul->history));
shul->history = ppChunk;
shul->histmax += 1024;
}
chunk = shul->history[topi];
if (chunk == NULL) {
assert (len == 0);
chunk = malloc (sizeof (*chunk));
if (chunk == NULL) {
perror ("histAdd:malloc");
return -1;
}
shul->history[topi] = chunk;
}
chunk->h[len].htype = htype;
chunk->h[len].it = it;
shul->histlen++;
return 0;
}
static void
exprStackInit (EXPR_STACK * s, ARENA * a)
{
assert (s != NULL);
s->count = 0;
s->maxsize = 0;
s->exprs = NULL;
s->arena = a;
}
static int
exprStackPush (EXPR_STACK * s, EXPR * e)
{
EXPR ** expr;
EXPR ** nexpr;
expr = s->exprs;
if (s->count >= s->maxsize) {
assert (s->count == s->maxsize);
if (s->maxsize == 0)
s->maxsize = 32;
else
s->maxsize *= 2;
nexpr = arenaAlloc (s->arena,
s->maxsize * sizeof (EXPR *));
if (nexpr == NULL) {
ALLOC_ERR ();
return -1;
}
memcpy (nexpr, expr, s->count * sizeof (EXPR *));
s->exprs = nexpr;
expr = nexpr;
}
expr[s->count++] = e;
return 0;
}
static EXPR *
specialize (EXPR * e, EXPR ** env, ARENA * arena)
{
int i;
S_EXPR * sexpr;
int arity;
/*
* e is a conclusion of the statment being applied, not
* of the theorem being proven. As such, all its variables
* are of the ET_IVAR type. All the corresponding expressions in
* the environment are allocated in tip->arena, so we can
* simply return the pointer.
*/
if (e->ex.etype == ET_IVAR)
return env[e->vi.index];
assert (e->ex.etype == ET_SEXPR);
arity = e->sx.t->arity;
sexpr = arenaAlloc (arena,
offsetof (S_EXPR, args) + arity * sizeof (EXPR *));
if (sexpr == NULL) {
ALLOC_ERR ();
return NULL;
}
sexpr->ex.etype = ET_SEXPR;
sexpr->ex.kind = e->ex.kind;
sexpr->t = e->sx.t;
for (i = 0; i < arity; ++i) {
if ((sexpr->args[i] = specialize (e->sx.args[i], env, arena))
== NULL)
return NULL;
}
return (EXPR *)sexpr;
}
static int
match (EXPR * e1, EXPR * e2, EXPR ** env)
{
int i;
/*
* e1 is the actual expression provided on the proof stack
* while e2 is the expression of the statement's formal hypothesis.
* When env is NULL, we are checking an exact match for a formal
* variable matched earlier.
*/
#if 0
printf ("match: ");
exprPrint (stdout, e1, 0, 0);
printf (" vs. ");
exprPrint (stdout, e2, 0, 0);
printf (" env %s\n", env ? "non-NULL" : "NULL");
#endif
if (e2->ex.etype == ET_SEXPR) {
if (e1->ex.etype != ET_SEXPR) {
fprintf (stderr,
"Term mismatch. Wanted '(%s ...)', got "
"non-term.\n",
e2->sx.t->sym.ident->name);
return -1;
}
if (e1->sx.t != e2->sx.t) {
fprintf (stderr,
"Term mismatch. Wanted '(%s ...)', got "
"(%s ...)\n",
e2->sx.t->sym.ident->name,
e1->sx.t->sym.ident->name);
return -1;
}
/*
* It has already been checked that the number of arguments
* matches the arity for sexpr1 (when sexpr1 was being
* constructed).
*/
for (i = 0; i < e2->sx.t->arity; ++i) {
if (match (e1->sx.args[i], e2->sx.args[i], env) != 0)
return -1;
}
return 0;
}
/* All variables encountered have been converted to ET_IVARs */
assert (e2->ex.etype == ET_IVAR);
if (env == NULL) {
char * name1;
char * name2;
/* We are performing an exact match involving
* variables of the statement being proved (ET_IVAR), or in
* the global symbol table (ET_VAR).
*/
if (e2 == e1)
return 0;
name2 = e2->vi.id->name;
if (e1->ex.etype == ET_SEXPR) {
fprintf (stderr, "Expected variable '%s', got "
"term (required exact match).\n", name2);
return -1;
}
assert (e1->ex.etype == ET_IVAR);
name1 = e1->vi.id->name;
fprintf (stderr, "*** Expected variable '%s', got "
"variable '%s' (required exact match).\n",
name2, name1);
return -1;
}
if (e1->ex.kind->rep != e2->ex.kind->rep) {
fprintf (stderr,
"Kind mismatch (wanted %s, got %s)\n",
e2->ex.kind->id->name,
e1->ex.kind->id->name);
return -1;
}
if (env[e2->vi.index] == NULL) {
#if 0
printf ("map %s (%d) to ", e2->vi.id->name, e2->vi.index);
exprPrint (stdout, e1, 0, 0);
printf ("\n");
#endif
env[e2->vi.index] = e1;
return 0;
}
/* This ivar already assigned, check for exact match */
return match (e1, env[e2->vi.index], NULL);
}
typedef struct _DVC {
uint32_t * dvbits; /* DV bitmap for theorem being proved */
EXPR ** env;
MAPPING * fvi;
MAPPING * fvj;
} DVC;
typedef struct _VMAP {
EXPR_VARINFO * vi;
EXPR * ex;
} VMAP;
typedef struct _DVC2 {
uint32_t * dvbits; /* DV bitmap for theorem being proved */
int missing;
int bad;
int nvars; /* stat->nhvars + stat->nWild for theorem in prog. */
int nhvars; /* stat->nhvars */
EXPR_VARINFO * vi;
VMAP * vmap;
} DVC2;
static int
findVars (EXPR * e, MAPPING * m)
{
MAP_ELEM * me;
int i;
if (e->ex.etype != ET_SEXPR) {
assert (e->ex.etype == ET_IVAR);
me = mapElemInsert (e, m);
if (me == NULL) {
ALLOC_ERR ();
return -1;
}
/* Don't care about me->v, we're just using this as a set */
return 0;
}
for (i = 0; i < e->sx.t->arity; ++i) {
if (findVars (e->sx.args[i], m) != 0)
return -1;
}
return 0;
}
static int
findVar (EXPR_VARINFO * vi, EXPR * e)
{
int i;
if (e->ex.etype != ET_SEXPR) {
if (vi == &e->vi)
return 1;
return 0;
}
for (i = 0; i < e->sx.t->arity; ++i) {
if (findVar (vi, e->sx.args[i]) != 0)
return 1;
}
return 0;
}
static void *
getDvsForStat (int i, int j, void * ctx)
{
DVC * dv = ctx;
MAPPING * fvi = dv->fvi;
MAPPING * fvj = dv->fvj;
EXPR * ei;
EXPR * ej;
MAP_ELEM * mei;
MAP_ELEM * meinext;
MAP_ELEM * mej;
MAP_ELEM * mejnext;
uint32_t * bmap = dv->dvbits;
void * ret;
assert (fvi->size == 0 && fvj->size == 0);
/*
* Hmm, it's somewhat inefficient to find the variables on each
* iteration this way, since we may have to find variables for the
* same expression (env entry) more than once.
*/
findVars (dv->env[i], fvi);
findVars (dv->env[j], fvj);
MAP_ELEM_ITER_SAFE (fvi, mei, meinext) {
MAP_ELEM_ITER_SAFE (fvj, mej, mejnext) {
ei = mei->obj;
ej = mej->obj;
if (ei == ej) {
fprintf (stderr,
"*** disjoint violation for '%s'\n",
ei->vi.id->name);
ret = ei; /* arbitrary non-NULL */
goto mapEmpty;
}
i = ei->vi.index;
j = ej->vi.index;
assert (i != j); /* since ei != ej */
if (j < i) {
int k = i;
i = j;
j = k;
}
/* Here we just accumulate all DV conditions
involving variables occuring in the theorem
hypotheses, conclusions, or anywhere in the proof.
We'll do the checking that the theorem specifies
the needed subset of these pairs */
BIT_SET (bmap, PAIRNUM(i,j));
}
}
ret = NULL;
mapEmpty:
mappingEmpty (fvi, NULL, NULL);
mappingEmpty (fvj, NULL, NULL);
return ret;
}
static void *
checkDvsForStat (int i, int j, void * ctx)
{
DVC2 * dv = ctx;
EXPR * ex;
EXPR_VARINFO * vi;
EXPR_VARINFO * vj;
/* Note, we are guaranteed that i < j */
if (j < dv->nvars) {
/*
* Both variables occur in the hypotheses or conclusions
* of the theorem being proved, so the theorem's DV
* set must contain the pair.
*
* Actually, if one of the variables occurs in the conclusions
* but not the hypotheses and also doesn't occur in the
* remnant, then the variable occurs on the LHS but not the
* RHS of a def occurring in the conclusion and is discarded
* in the def expansion. In this case, the pair need not be
* included.
*/
if (BIT (dv->dvbits, PAIRNUM (i, j)) != 0)
return NULL; /* Good, continue */
if (j >= dv->nhvars &&
dv->vmap[j - dv->nhvars].ex == NULL)
return NULL; /* j did not occur in remnant */
if (i >= dv->nhvars &&
dv->vmap[i - dv->nhvars].ex == NULL)
return NULL; /* i did not occur in remnant */
if (!dv->missing) {
fprintf (stderr, "Missing DV pairs (");
dv->missing = 1;
}
fprintf (stderr, "(%s %s)",
dv->vi[i].id->name,
dv->vi[j].id->name);
dv->bad = 1;
return NULL; /* not OK, but continue to look for more */
}
/*
* OK, at least one of the variables occurred in the proof
* steps but not in the hypotheses or conclusions.
* If one of the variables occurred in the remnant and was
* bound to a def dummy, and the other variable was
* not present in the corresponding sub-expression matched
* against the def expansion causing the binding of the first
* variable, consider this a failure.
*
* Note, it seems semi-reasonable to me that the variable bound to
* the def dummy shouldn't have DV restrictions with other variables.
*
* The way I look at it, in order to prove a conclusion involving
* def terms, one would have to show that one could prove the
* same conclusion with all the def terms expanded, with arbitrary
* independent substitutions for the def dummies. (This is a bit
* stronger than what Ghilbert requires. For example, Ghilbert allows
* me to prove (-> (d1) (d1)) as a special case of id even where
* (d1) is a def term of kind wff which may contain dummies, and
* where expanding the two (d1)'s with different choices for the
* dummies would not be a valid result.)
*
* I'm not sure why it's not a violation if the 'other variable' was
* part of the sub-expression matched against the def expansion.
* There may be some assumption that the dummy variables in a def
* is always chosen distinct from the variables occurring in the
* actual def term arguments and the other dummy variables.
*/
vj = dv->vmap[j - dv->nhvars].vi;
ex = dv->vmap[j - dv->nhvars].ex;
/* If ex is NULL, variable j doesn't occur in the remnant or the
* hypotheses (or the conclusions either).
*/
if (ex == NULL)
return NULL;
/* At this point we know that variable j occurs in the remnant */
if (i >= dv->nhvars && dv->vmap[i - dv->nhvars].ex == NULL)
return NULL; /* variable i doesn't occur in remnant
or the hypotheses */
/*
* Note, for dv->nhvars <= k, variable k occurred in the remnant
* iff dv->vmap[k - dv->nhvars].ex != NULL. But if
* dv->vmap[k - dv->nhvars].ex == (EXPR *)dv->vmap, then k
* occurs in the remnant but is not bound to a definition dummy.
*/
if (ex != (EXPR *)dv->vmap) {
if (i < dv->nvars)
vi = &dv->vi[i];
else
vi = dv->vmap[i - dv->nhvars].vi;
if (findVar (vi, ex) == 0) {
if (dv->missing) {
fprintf (stderr, ")\n");
dv->missing = 0;
}
fprintf (stderr,
"*** def dummy %s not distinct from %s\n",
vj->id->name,
vi->id->name);
dv->bad = 1;
}
}
/*
* If i < dv->nvars, it can't have been bound to a definition
* dummy; that's checked by checkConclusion().
*/
if (i < dv->nvars)
return NULL;
ex = dv->vmap[i - dv->nhvars].ex;
if (ex == (EXPR *)dv->vmap)
return NULL; /* i is in remnant, but not bound to def
dummy */
if (findVar (vj, ex) == 0) {
if (dv->missing) {
fprintf (stderr, ")\n");
dv->missing = 0;
}
vi = dv->vmap[i - dv->nhvars].vi;
fprintf (stderr,
"*** def dummy %s not distinct from %s\n",
vi->id->name,
vj->id->name);
dv->bad = 1;
}
return NULL;
}
static int
proofStepApply (SHULLIVAN * shul, TIP * tip, PROOF_STEP * s)
{
STATEMENT * stat;
EXPR * e1;
EXPR * e2;
EXPR ** env;
DVC dv;
int i;
int j;
int nvars;
int indent;
if (s->s.type == STEPT_HYP) {
if (tip->wvs.count != 0) {
fprintf (stderr,
"*** Hypothesis pushed with non-empty "
"wild variable stack.\n");
return -1;
}
i = s->hyp.index;
e1 = tip->t->stmt->hyps[i];
if (shul->verbose & SHUL_VERB_PROOF) {
j = printf ("H%d %s ", i+1, tip->t->hypnam[i]->name);
exprPrint (stdout, e1, shul->verbose, j);
printf ("\n");
}
return exprStackPush (&tip->ps, e1);
}
if (s->s.type == STEPT_EXPR) {
/*
* We allow pushing any expression onto the wild
* variable hypothesis stack. All checking is done when
* a theorem reference is applied. If there is stuff
* left on the wvs stack at the end of the proof, a
* warning is generated.
*/
if (shul->verbose & SHUL_VERB_PRFWILD) {
j = printf ("W ");
exprPrint (stdout, s->expr.x, shul->verbose, j);
printf ("\n");
}
return exprStackPush (&tip->wvs, s->expr.x);
}
assert (s->s.type == STEPT_REF);
stat = s->ref.stat;
if (stat->nHyps > tip->ps.count) {
fprintf (stderr,
"*** Proof stack underflow, needed %d items\n",
stat->nHyps);
return -1;
}
/*
* We don't allow saving items on the wild
* variable stack between references, so the counts
* must match exactly.
*/
if (stat->nWild != tip->wvs.count) {
fprintf (stderr,
"*** Expected %d wild var hypotheses, "
"had %d\n", stat->nWild, tip->wvs.count);
return -1;
}
/*
* env holds information about the values assigned
* to variables occurring in the formal hypotheses
* and wild variables of the statement being
* referenced during the matching with the provided
* actual hypotheses.
*/
nvars = stat->nhvars + stat->nWild;
if (nvars * sizeof (EXPR *) > shul->env.size &&
growBufInit (&shul->env, 2 * nvars * sizeof (EXPR *)) != 0) {
perror ("proofStepApply:growBuf");
return -1;
}
env = shul->env.buf;
for (i = 0; i < nvars; ++i)
env[i] = NULL;
/* check explicit hypotheses */
j = tip->ps.count - stat->nHyps;
for (i = 0; i < stat->nHyps; ++i, ++j) {
e1 = tip->ps.exprs[j];
e2 = stat->hyps[i];
if (match (e1, e2, env) != 0) {
fprintf (stderr, "*** Hypothesis mismatch.\n");
return -1;
}
}
j = stat->nhvars;
/* Assign wild hypothesis variables */
for (i = 0; i < stat->nWild ; ++i) {
assert (env[i+j] == NULL);
if (stat->vi[i+j].ex.kind->rep !=
tip->wvs.exprs[i]->ex.kind->rep) {
fprintf (stderr,
"Kind mismatch got %s wanted %s for %s\n",
tip->wvs.exprs[i]->ex.kind->id->name,
stat->vi[i+j].ex.kind->id->name,
stat->vi[i+j].id->name);
return -1;
}
env[i+j] = tip->wvs.exprs[i];
#if 0
printf ("mv map %s (%d) to ", stat->vi[i+j].id->name, i+j);
exprPrint (stdout, tip->wvs.exprs[i], 0, 0);
printf ("\n");
#endif
}
/* check distinct variable conditions */
if (tip->varIndex->size > shul->ndvvars) {
int n = tip->varIndex->size + 8;
int pairs = n * (n - 1) / 2; /* sum (i=1..(n-1) | i) */
int newsize = BIT_MAP_SIZE (pairs);
uint32_t * dvbits;
dvbits = calloc (1, newsize);
if (dvbits == NULL) {
perror ("proofStepApply:calloc");
return -1;
}
assert (newsize > shul->dvsize);
memcpy (dvbits, shul->dvbits, shul->dvsize);
memset ((char *)dvbits + shul->dvsize, 0,
newsize - shul->dvsize);
shul->ndvvars = n;
shul->dvsize = newsize;
free (shul->dvbits);
shul->dvbits = dvbits;
}
dv.dvbits = shul->dvbits;
dv.env = env;
dv.fvi = shul->fvi;
dv.fvj = shul->fvj;
if (dvEnumerate (nvars, stat->dvbits,
getDvsForStat, &dv) != NULL) {
return -1;
}
/* Clear the wild variable stack */
tip->wvs.count = 0;
/*
* OK, everything's good. Pop off the actual hypotheses
* and add the specialized conclusions:
*/
tip->ps.count -= stat->nHyps;
indent = 0;
if (shul->verbose & SHUL_VERB_PROOF) {
if (stat->nCons == 0)
printf ("-%d %s\n", stat->nHyps,
stat->sym.ident->name);
else
indent = printf ("-%d %s ", stat->nHyps,
stat->sym.ident->name);
}
for (i = 0; i < stat->nCons; ++i, ++j) {
e1 = specialize (stat->cons[i], env, &tip->arena);
if (e1 == NULL)
return -1;
if (shul->verbose & SHUL_VERB_PROOF) {
if (i != 0)
indent = printf ("+ ");
exprPrint (stdout, e1, shul->verbose, indent);
printf ("\n");
}
if (exprStackPush (&tip->ps, e1) != 0)
return -1;
}
return 0;
}
static void
scannerInit (SCANNER * scan,
SCANNER_READ_MORE readMore,
void * readMoreCtx,
SCANNER_FREE_LINE freeLine,
SCANNER_GET_TOKEN getToken,
SCANNER_PROMPT_SET promptSet,
SCANNER_CLEANUP cleanup)
{
memset (scan, 0, sizeof (*scan));
scan->get_token = getToken;
scan->read_more = readMore;
scan->ctx = readMoreCtx;
scan->free_line = freeLine;
scan->prompt_set = promptSet;
scan->cleanup = cleanup;
scan->gb.buf = NULL;
scan->gb.size = 0;
}
#define SIMPLE_SCAN_LINE_LEN 4096
static char *
simpleScan (SCANNER * scan, void * ctx)
{
char * line;
char * prompt = (char *)ctx;
line = malloc (SIMPLE_SCAN_LINE_LEN);
if (line == NULL) {
perror ("simpleScan:malloc");
return NULL;