File: proof_rec.ml

package info (click to toggle)
hol88 2.02.19940316-35
  • links: PTS
  • area: main
  • in suites: buster
  • size: 65,988 kB
  • ctags: 21,623
  • sloc: ml: 199,939; ansic: 9,666; sh: 7,118; makefile: 6,095; lisp: 2,747; yacc: 894; sed: 201; cpp: 87; awk: 5
file content (797 lines) | stat: -rw-r--r-- 25,776 bytes parent folder | download | duplicates (11)
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
%=============================================================================%
%                               HOL 88 Version 2.1                            %
%                                                                             %
%     FILE NAME:        proof_rec.ml                                          %
%                                                                             %
%     DESCRIPTION:      function for recording and transforming proofs        %
%                                                                             %
%     AUTHOR:           Mike Gordon, Wai Wong                                 %
%                                                                             %
%     USES FILES:       hol-syn.ml                                            %
%                                                                             %
%                       University of Cambridge                               %
%                       Hardware Verification Group                           %
%                       Computer Laboratory                                   %
%                       New Museums Site                                      %
%                       Pembroke Street                                       %
%                       Cambridge  CB2 3QG                                    %
%                       England                                               %
%                                                                             %
%     COPYRIGHT:        University of Edinburgh                               %
%     COPYRIGHT:        University of Cambridge                               %
%     COPYRIGHT:        INRIA                                                 %
%                                                                             %
%=============================================================================%

%< --------------------------------------------------------------------------
Proof recording:

When proff recording is enabled,  every inference step performed by
the system will be recorded in a list whose elements are of type step.
The low level functions for managing this feature are in the file
hol-syn.ml. They are reproduced below for easy reference.
 The step list is then processed by the function MakeProof to become a
line list. This list can then be save into a disk file. 

% We hide the flag and the list in local variables. Four functions are
provided for the users to control the recording of proof %

begin_section `record_proof`;;

letref steplist = [] : step list;;
letref record_proof_flag = false;;
letref suspended = false;;

let is_recording_proof (():void) = record_proof_flag;;

let record_proof b = 
    (if b then (steplist := [];()); 
     record_proof_flag := b; ()) ;;

let suspend_recording () = 
    if record_proof_flag
     then (record_proof_flag := false;
       	   suspended := true; ());;

let resume_recording () =
    if (suspended & (not record_proof_flag))
     then (record_proof_flag := true;
    	   suspended := false; ());;

let RecordStep step =
    if  record_proof_flag
     then (steplist := (step.steplist); ());;

let get_steps (():void) = steplist;;

(record_proof,is_recording_proof,RecordStep,get_steps,
 suspend_recording,resume_recording);;
end_section `record_proof`;;

let (record_proof,is_recording_proof,RecordStep,get_steps,
     suspend_recording,resume_recording) = it;;
---------------------------------------------------------------------------->%

%----------------------------------------------------------------------------%
% Proof lines                                                                %
%----------------------------------------------------------------------------%

type justification =
   Hypothesis
 | Assume of term
 | Refl of term
 | Subst of (int#term)list # term # int
 | BetaConv of term
 | Abs of term # int
 | InstType of (type#type)list # int
 | Disch of term # int
 | Mp of int # int
 | MkComb of int # int
 | MkAbs of int
 | Alpha of term # term
 | AddAssum of term # int
 | Sym of int
 | Trans of int # int
 | ImpTrans of int # int
 | ApTerm of term # int
 | ApThm of int # term
 | EqMp of int # int
 | EqImpRuleR of int
 | EqImpRuleL of int
 | Spec of term # int
 | EqtIntro of int
 | Gen of term # int
 | EtaConv of term
 | Ext of int
 | Exists of (term # term) # int
 | Choose of (term # int) # int
 | ImpAntisymRule of int # int
 | MkExists of int
 | Subs of int list # int
 | SubsOccs of (int list # int) list # int
 | SubstConv of (int # term) list # term # term
 | Conj of int # int
 | Conjunct1 of int
 | Conjunct2 of int
 | Disj1 of int # term
 | Disj2 of term # int
 | DisjCases of int # int # int
 | NotIntro of int
 | NotElim of int
 | Contr of term # int
 | Ccontr of term # int
 | Inst of (term # term) list # int
 | StoreDefinition of string # term
 | Definition of string # string
 | DefExistsRule of term
 | NewAxiom of string # term
 | Axiom of string # string
 | Theorem of string # string
 | NewConstant of string # type
 | NewType of int # string
 | NumConv of term;;


type line = Line of (int # thm # justification);;

%-------------------------------------------------------------------%
% MakeProof converts a step list into a line list.		    %
%-------------------------------------------------------------------%
let MakeProof steplst =
 letref steps  = rev steplst
 and    proof     = [] : line list
 and    hyplist   = [] : (int # thm) list
 and    thmlist   = [] : (int # thm) list
 and    linecount = 1
 and    hypcount  = 0
 and    thmcount  = thm_count ()
 in
 let Pos th =
  fst(rev_assoc th thmlist)
  ? fst(rev_assoc th hyplist)
  ? (hypcount := hypcount-1;
     hyplist := (hypcount,th).hyplist;
     hypcount)
 and AddLine just th =
  (proof := Line(linecount,th,just).proof; 
   thmlist := (linecount,th).thmlist;
   linecount := linecount+1)
 and AddHypLines () =
  (proof := rev proof;
   itlist (\(n,th) l. Line(n,th,Hypothesis).l)  hyplist proof)
 in
 (suspend_recording (); 
  while not(null steps)
  do ((case (hd steps) of
        AssumeStep w . 
         AddLine (Assume w) (ASSUME w)
      | ReflStep t . 
         AddLine (Refl t) (REFL t)
      | SubstStep(thvars,w,lhsthm) . 
         AddLine (Subst(map (Pos # I) thvars, w, Pos lhsthm)) 
         (SUBST thvars w lhsthm)
      | BetaConvStep t .
         AddLine (BetaConv t) (BETA_CONV t)
      | AbsStep(x,th) .
         AddLine (Abs(x,Pos th)) (ABS x th)
      | InstTypeStep(inst_tylist,th) . 
         AddLine (InstType(inst_tylist,Pos th)) 
         (INST_TYPE inst_tylist th)
      | DischStep(w,th) . 
         AddLine (Disch(w,Pos th)) (DISCH w th)
      | MpStep(thi,th) .
         AddLine (Mp(Pos thi,Pos th)) (MP thi th)
      | MkCombStep(funth,argth) .
         AddLine (MkComb(Pos funth,Pos argth)) (MK_COMB(funth,argth))
      | MkAbsStep th.
         AddLine (MkAbs(Pos th)) (MK_ABS th)
      | AlphaStep(t1,t2) . 
          AddLine (Alpha(t1,t2)) (ALPHA t1 t2)
      | AddAssumStep(t,th) .
          AddLine (AddAssum(t,Pos th)) (ADD_ASSUM t th)
      | SymStep th .
         AddLine (Sym(Pos th)) (SYM th)
      | TransStep(th1,th2) .
         AddLine (Trans(Pos th1,Pos th2)) (TRANS th1 th2)
      | ImpTransStep(th1,th2) .
         AddLine (ImpTrans(Pos th1,Pos th2)) (IMP_TRANS th1 th2)
      | ApTermStep(tm,th) .
         AddLine (ApTerm(tm,Pos th)) (AP_TERM tm th)
      | ApThmStep(th,tm) .
         AddLine (ApThm(Pos th,tm)) (AP_THM th tm)
      | EqMpStep(th1,th2) .
         AddLine (EqMp(Pos th1,Pos th2)) (EQ_MP th1 th2)
      | EqImpRuleStep th .
         (AddLine (EqImpRuleR(Pos th)) (fst(EQ_IMP_RULE th));
          AddLine (EqImpRuleL(Pos th)) (snd(EQ_IMP_RULE th)))
      | SpecStep(t,th) .
         AddLine (Spec(t,Pos th)) (SPEC t th)
      | EqtIntroStep th .
         AddLine (EqtIntro(Pos th)) (EQT_INTRO th)
      | GenStep(x,th) .
         AddLine (Gen(x,Pos th)) (GEN x th)
      | EtaConvStep tm .
         AddLine (EtaConv tm) (ETA_CONV tm)
      | ExtStep th .
         AddLine (Ext(Pos th)) (EXT th)
      | ExistsStep((w,t),th) .
         AddLine (Exists((w,t),Pos th)) (EXISTS (w,t) th)
      | ChooseStep((v,th1),th2) .
         AddLine (Choose((v,Pos th1),Pos th2)) (CHOOSE (v,th1) th2)
      | ImpAntisymRuleStep(th1,th2) .
         AddLine (ImpAntisymRule(Pos th1,Pos th2)) 
         (IMP_ANTISYM_RULE th1 th2)
      | MkExistsStep bodyth .
         AddLine (MkExists(Pos bodyth)) (MK_EXISTS bodyth)
      | SubsStep(ths,th) .
         AddLine (Subs(map Pos ths,Pos th)) (SUBS ths th)
      | SubsOccsStep(nlths,th) .
         AddLine (SubsOccs(map (I # Pos) nlths,Pos th)) 
         (SUBS_OCCS nlths th)
      | SubstConvStep(thvars,template,tm) .
         AddLine (SubstConv(map (Pos # I) thvars, template, tm)) 
         (SUBST_CONV thvars template tm)
      | ConjStep(th1,th2) .
         AddLine (Conj(Pos th1,Pos th2)) (CONJ th1 th2)
      | Conjunct1Step th .
         AddLine (Conjunct1(Pos th)) (CONJUNCT1 th)
      | Conjunct2Step th .
         AddLine (Conjunct2(Pos th)) (CONJUNCT2 th)
      | Disj1Step(th,w) .
         AddLine (Disj1(Pos th,w)) (DISJ1 th w)
      | Disj2Step(w,th) .
         AddLine (Disj2(w,Pos th)) (DISJ2 w th)
      | DisjCasesStep(dth,ath,bth) .
         AddLine (DisjCases(Pos dth,Pos ath,Pos bth)) 
         (DISJ_CASES dth ath bth)
      | NotIntroStep th .
         AddLine (NotIntro(Pos th)) (NOT_INTRO th)
      | NotElimStep th .
         AddLine (NotElim(Pos th)) (NOT_ELIM th)
      | ContrStep(w,fth) .
         AddLine (Contr(w,Pos fth)) (CONTR w fth)
      | CcontrStep(w,fth) .
         AddLine (Ccontr(w,Pos fth)) (CCONTR w fth)
      | InstStep(inst_list,th) .
         AddLine (Inst(inst_list,Pos th)) (INST inst_list th)
      | StoreDefinitionStep(name,t) .
         AddLine (StoreDefinition(name,t)) (definition(current_theory()) name)
      | DefinitionStep(thy,ax) .
         AddLine (Definition(thy,ax)) (definition thy ax)
      | DefExistsRuleStep tm .
         AddLine (DefExistsRule tm) (DEF_EXISTS_RULE tm)
      | NewAxiomStep(tk,tm) .
         AddLine (NewAxiom(tk,tm)) (new_axiom(tk,tm))
      | AxiomStep(thy,ax) .
         AddLine (Axiom(thy,ax)) (axiom thy ax)
      | TheoremStep(thy,factname) .
         AddLine (Theorem(thy,factname)) (theorem thy factname)
      | NewConstantStep(s,ty) .
         AddLine (NewConstant(s,ty)) (TRUTH)
      | NewTypeStep(arity,tok) .
         AddLine (NewType(arity,tok)) (TRUTH)
      | NumConvStep tm .
        AddLine (NumConv tm) (num_CONV tm));
     steps := tl steps);
  resume_recording(); set_thm_count thmcount;
  AddHypLines());;

% let ShowProof() = MakeProof (get_steps());; %

%-------------------------------------------------------------------%
% The functions below are for outputting proof lines to a disk file %
%-------------------------------------------------------------------%
begin_section `output_proof`;;

let output_strings port sl =   % : (string -> string list -> void) %
   do (map (\s. (write(port,s); write(port, `\L`))) sl);;


let write_pair port (fl,fr) =
    \(l,r). (write(port, `{`);
    	     (fl port l);
    	     (fr port r);
    	     write(port, `}`));;

let write_list port fn l =
    (write(port, `[`);
     (map (fn port) l);
     write(port, `]`));;

letrec write_type port ty =
    if (is_vartype ty)
    then (write (port, (`(v `));
    	  write(port, (dest_vartype ty));
    	  write(port, `)`))
    else (
      let tyop,tyl = dest_type ty in
      if null tyl
      then (write (port, (`(c `));
    	    write (port, tyop);
            write (port, `)`))
      else (write (port, (`(o `));
    	    write (port, tyop);
            write_list port write_type tyl;
            write (port, `)`)));;

letrec write_term port tm =
    let opt_delim s =
        let lastchar s = (last o explode) s in
        if (is_alphanum(lastchar s)) then () else write(port,` `)  in
    if (is_var tm)
    then (let v,ty = dest_var tm in
    	  (write(port, (`(V `));
    	   write(port, (v)); opt_delim v;
    	   write_type port ty;
    	   write(port, `)`)))
    else if (is_const tm)
    then (let v,ty = dest_const tm in
    	  (write(port, (`(C `));
    	   write(port, (v)); opt_delim v;
    	   write_type port ty;
    	   write(port, `)`)))
    else if (is_comb tm)
    then (let ro,ra = dest_comb tm in
    	  (write(port, (`(A `));
           write_term port ro; write_term port ra;
           write(port, `)`)))
    else (let ro,ra = dest_abs tm in
    	  (write(port, (`(L `));
           write_term port ro; write_term port ra;
           write(port, `)`)));;

let write_thm port thm =
    (write_term port (concl thm);
     write(port, `\L`));;

let write_all_thm port thm =
    let asml,con = dest_thm thm in
    (write(port, `(THM `);
     write_list port write_term asml;
     write_term port con;
     write(port, `)\L`));;

let write_int port i =
    (write(port, ` `);
     write(port, (string_of_int i)));;

let write_just port j =
    case j of
    Hypothesis .
    	 write(port, `(Hypothesis)`)
 | (Assume tm) . 
    	(write(port, `(Assume `);
    	 write_term port tm;
    	 write(port, `)`))
 | (Refl tm) .
    	(write(port, `(Refl `);
    	 write_term port tm;
    	 write(port, `)`))
 | (Subst (itl,tm,i)) .
    	(write(port, `(Subst `);
    	 write_list port (\p. write_pair p (write_int,write_term)) itl;
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (BetaConv tm) .
    	(write(port, `(BetaConv `);
    	 write_term port tm;
    	 write(port, `)`))
 | (Abs (tm,i)) .
    	(write(port, `(Abs `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (InstType (tyl,i)) .
    	(write(port, `(InstType `);
    	 write_list port (\p. write_pair p (write_type,write_type)) tyl;
         write_int port i;
    	 write(port, `)`))
 | (Disch (tm,i)) .
    	(write(port, `(Disch `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (Mp (i1,i2)) .
    	(write(port, `(Mp `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (MkComb (i1,i2)) .
    	(write(port, `(MkComb `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (MkAbs i) .
    	(write(port, `(MkAbs `);
    	 write_int port i;
    	 write(port, `)`))
 | (Alpha (tm1,tm2)) .
    	(write(port, `(Alpha `);
    	 write_term port tm1;
    	 write_term port tm2;
    	 write(port, `)`))
 | (AddAssum (tm,i)) .
    	(write(port, `(AddAssum `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (Sym i) .
    	(write(port, `(Sym `);
    	 write_int port i;
    	 write(port, `)`))
 | (Trans (i1,i2)) .
    	(write(port, `(Trans `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (ImpTrans (i1,i2)) .
    	(write(port, `(ImpTrans `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (ApTerm (tm,i)) .
    	(write(port, `(ApTerm `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (ApThm (i,tm)) .
    	(write(port, `(ApThm `);
    	 write_int port i;
    	 write_term port tm;
    	 write(port, `)`))
 | (EqMp (i1,i2)) .
    	(write(port, `(EqMp `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (EqImpRuleR i).
    	(write(port, `(EqImpRuleR `);
    	 write_int port i;
    	 write(port, `)`))
 | (EqImpRuleL i).
    	(write(port, `(EqImpRuleL `);
    	 write_int port i;
    	 write(port, `)`))
 | (Spec (tm,i)) .
    	(write(port, `(Spec `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (EqtIntro i) .
    	(write(port, `(EqtIntro `);
    	 write_int port i;
    	 write(port, `)`))
 | (Gen (tm,i)) .
    	(write(port, `(Gen `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (EtaConv tm) .
    	(write(port, `(EtaConv `);
    	 write_term port tm;
    	 write(port, `)`))
 | (Ext i) . 
    	(write(port, `(Ext `);
    	 write_int port i;
    	 write(port, `)`))
 | (Exists (tm,i)) .
    	(write(port, `(Exists `);
    	 write_pair port (write_term,write_term) tm;
    	 write_int port i;
    	 write(port, `)`))
 | (Choose (tmi,i)) .
    	(write(port, `(Choose `);
    	 write_pair port (write_term,write_int) tmi;
    	 write_int port i;
    	 write(port, `)`))
 | (ImpAntisymRule (i1,i2)) .
    	(write(port, `(ImpAntisymRule `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (MkExists i) .
    	(write(port, `(MkExists `);
    	 write_int port i;
    	 write(port, `)`))
 | (Subs (il,i)) .
    	(write(port, `(Subs `);
    	 write_list port write_int il;
    	 write_int port i;
    	 write(port, `)`))
 | (SubsOccs (il,i)) . 
    	(write(port, `(SubsOccs `);
    	 write_list port 
    	 (\p (ill,ii). (write(p,`(`); 
            	        write_list p write_int ill;
    	    	    	write_int p ii;
    	    	        write(p,`)`))) il;
    	 write_int port i;
    	 write(port, `)`))
 | (SubstConv (itl, tm1, tm2)) .
    	(write(port, `(SubstConv `);
    	 write_list port (\p. write_pair p (write_int,write_term)) itl;
    	 write_term port tm1;
    	 write_term port tm2;
    	 write(port, `)`))
 | (Conj (i1,i2)) .
    	(write(port, `(Conj `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, `)`))
 | (Conjunct1 i) .
    	(write(port, `(Conjunct1 `);
    	 write_int port i;
    	 write(port, `)`))
 | (Conjunct2 i) . 
    	(write(port, `(Conjunct2 `);
    	 write_int port i;
    	 write(port, `)`))
 | (Disj1 (i,tm)) .
    	(write(port, `(Disj1 `);
    	 write_int port i;
    	 write_term port tm;
    	 write(port, `)`))
 | (Disj2 (tm,i)) .
    	(write(port, `(Disj2 `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (DisjCases (i1,i2,i3)) .
    	(write(port, `(DisjCases `);
    	 write_int port i1;
    	 write(port, ` `);
    	 write_int port i2;
    	 write(port, ` `);
    	 write_int port i3;
    	 write(port, `)`))
 | (NotIntro i) .
    	(write(port, `(NotIntro `);
    	 write_int port i;
    	 write(port, `)`))
 | (NotElim i) . 
    	(write(port, `(NotElim `);
    	 write_int port i;
    	 write(port, `)`))
 | (Contr (tm,i)) .
    	(write(port, `(Contr `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (Ccontr (tm,i)) .
    	(write(port, `(Ccontr `);
    	 write_term port tm;
    	 write_int port i;
    	 write(port, `)`))
 | (Inst (ttl,i)) . 
    	(write(port, `(Inst `);
    	 write_list port (\p. write_pair p (write_term,write_term)) ttl;
    	 write_int port i;
    	 write(port, `)`))
 | (StoreDefinition (s,tm)) . 
    	(write(port, (`(StoreDefinition `^s));
    	 write_term port tm;
    	 write(port, `)`))
 | (Definition (s1,s2)) .
    	(write(port, (`(Definition `^s1^` `^s2^`)`)))
 | (DefExistsRule tm) .
    	(write(port, (`(DefExistsRule `));
    	 write_term port tm;
    	 write(port, `)`))
 | (NewAxiom (s,tm)) . 
    	(write(port, (`(NewAxiom `^s));
    	 write_term port tm;
    	 write(port, `)`))
 | (Axiom (s1,s2)) .
    	(write(port, (`(Axiom `^s1^` `^s2^`)`)))
 | (Theorem (s1,s2)) .
    	(write(port, (`(Theorem `^s1^` `^s2^`)`)))
 | (NewConstant (s,ty)) . 
    	(write(port, (`(NewConstant `^s));
    	 write_type port ty;
    	 write(port, `)`))
 | (NewType (i,s)) .
    	(write(port, (`(NewType `));
    	 write_int port i;
    	 write(port, (` `^s^`)`)))
 | (NumConv tm) .
    	(write(port, `(NumConv `);
    	 write_term port tm;
    	 write(port, `)`));;

let write_line port (Line (ct, thm, just)) =
    (write(port, `(LINE `);
     write(port, (string_of_int ct));
     write_just port just;
     write(port,`\L`);
     write_all_thm port thm;
     write(port, `)\L\L`));;

let write_tyconst port (arity,tyname) =
    (write_pair port ((\p s.write(p,s)), write_int) (tyname, arity));;

let write_sig port (cname, cty) =
    (write_pair port ((\p s.write(p,s)), write_type) (cname, cty));;

let write_env ofile =
    let holthy = `HOL` . (ancestors `HOL`) in
    let thisthy = current_theory() in
    let allthy = thisthy . (ancestors thisthy) in
    let ths = subtract allthy holthy in
    let tylst = flat (map types ths)
    and conlst = map dest_const (flat (map constants ths)) in
    (write(ofile, `(ENV `);
     write(ofile, thisthy);
     write_list ofile write_tyconst tylst;
     write_list ofile write_sig conlst;
     write(ofile, `)\L`));;

let write_thm_list port thml = write_list port write_all_thm thml;;

(write_line,write_thm_list,write_env);;
end_section `output_proof`;;
let (write_line,write_thm_list,write_env) = it;;

%-------------------------------------------------------------------%
% The functions below are the user level functions for recording    %
% proofs    	    	    %
%-------------------------------------------------------------------%

begin_section `proof_interface`;;

let format_version = `(VERSION PRF FORMAT 1.0 EXTENDED)\L`;;

let write_proof_add_to =
    let format_name = (explode format_version) in
    let read_line port =
    	letref ls = [] in
        letref c = read port in
    	(if (c = `nil`) then (c := ``; ())
         if not(c = `\L`) loop (ls := (c. ls); c := read port);
    	 (ls := c.ls); 
         rev ls) in
    let check_file fname = 
        let path = search_path() in
    	(set_search_path [``] ; 
         let fname' = find_file fname ? 
            (let ofile = openw fname in
    	     (write(ofile, format_version);
    	      write_env ofile;
    	      close ofile; fname)) in
         let ifile = openi fname' in
         let fformat = read_line ifile in
    	 (set_search_path path;
          (fformat  = format_name))) in
  \file name thms prf.
    if check_file file then
      (let ofile = append_openw file in
       (write(ofile, (`\L(PROOF `^name^`\L`));
        write_thm_list ofile thms;
    	write(ofile, `\L[\L`);
        map (write_line ofile) prf;
        write(ofile, `])\L`);
        close ofile))
    else failwith 
        (`write_proof_add_to: ` ^file ^ ` not in the correct format`);;

let write_proof_to file pname thms prf =
    let ofile = openw file in
    (write(ofile, format_version);
     write_env ofile;
     write(ofile, (`(PROOF `^pname^`\L`));
     write_thm_list ofile thms;
     write(ofile, `\L[\L`);
     map (write_line ofile) (prf);
     write(ofile, `])\L`);
     close ofile);;

letref proof_file_name = ``;;
letref proof_file_port = ``;;
letref proof_name = ``;;
letref proof_count = 0;;
letref current_goals = []:thm list;;

let write_last_proof =
  \name thms.
    (if (proof_file_port = ``) then failwith `no proof file`
     else (
    	write(proof_file_port, (`\L(PROOF `^name^`\L`));
     	write_thm_list proof_file_port thms;
        write(proof_file_port, `\L[\L`);
     	map (write_line proof_file_port) (MakeProof (get_steps()));
     	write(proof_file_port, `])\L`)))
    ?\s failwith (`write_last_proof: `^s);;

let current_proof_file (():void) = proof_file_name;;
let current_proof (():void) = proof_name;;

let close_proof_file (():void) =
    (close proof_file_port;
     proof_file_port := ``;
     let c  = system (`gzip `^ proof_file_name) in
     if not(c = 0) then
       tty_write(`WARNING: cannot compress proof file (gzip error `^
    	    	 (string_of_int c) ^`)`)
     else ();
     proof_file_name := ``; 
     proof_count := 0; ());;

let new_proof_file name = 
    (if not(proof_file_name = ``) then(
    	close_proof_file();
    	failwith `a proof file is already opened`)
     else(
         proof_file_name := name;
    	 proof_count := 0;
         let ofile = openw proof_file_name in
    	     (write(ofile, format_version);
    	      write_env ofile);
         proof_file_port := ofile; ()))
    ?\s failwith (`new_proof_file: `^s);;

let begin_proof name =
    (if not(proof_name = ``) then failwith (`last proof is not saved yet`)
     else (proof_name := name; 
    	proof_count := proof_count + 1;
        current_goals := [];
    	record_proof true; ()))
    ?\s failwith (`begin_proof: `^s);;

let end_proof name =
    (if (proof_name = ``) then ()
     else
      (write_last_proof proof_name current_goals;
       record_proof false;
       proof_name := ``;
       current_goals := []; ()))
    ?\s failwith (`end_proof: `^s);;
     
%-------------------------------------------------------------------%
% The functions below are the user level functions for recording    %
% tactical proofs    	    	    %
%-------------------------------------------------------------------%
let sanitise str =
    let sps = explode `.;,:"!@#$^&*()|\\` in
    let c' = `_` in
    let san c = if (mem c sps) then c' else c in
    letrec sani sl =
    	if (null sl) then []
    	else let h = san (hd sl) in (h . (sani (tl sl))) in
     implode (sani (explode str));;

let TAC_PROOF (gl,tac) =
    (let thm = TAC_PROOF(gl,tac) in
     	(current_goals := (mk_thm gl) . current_goals;
     	 thm));;
     
let PROVE (tm,tac) =
    (let thm = PROVE(tm,tac) in
     (current_goals := (mk_thm([],tm)) . current_goals;
      thm));;

let prove = \(t,tac). TAC_PROOF(([],t), tac);;

let prove_thm (name,tm,tac) =
    (let thm = prove_thm(name,tm,tac) in
     	(current_goals := (mk_thm([],tm)) . current_goals;
         thm));;

(write_proof_add_to, write_proof_to, write_last_proof,
 current_proof, current_proof_file,
 new_proof_file, close_proof_file, begin_proof, end_proof,
 TAC_PROOF, PROVE, prove, prove_thm);;
 
end_section `proof_interface`;;
let (write_proof_add_to, write_proof_to, write_last_proof,
 current_proof, current_proof_file,
 new_proof_file, close_proof_file, begin_proof, end_proof,
 TAC_PROOF, PROVE, prove, prove_thm) = it;;