-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalpha.sml
1487 lines (1455 loc) · 86.4 KB
/
alpha.sml
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
(*=================================================================================
Proof evaluation with type-alpha certificate production.
=======================================================================*)
structure Alpha =
struct
structure S = Symbol
structure A = AbstractSyntax
structure AT = AthTerm
type prop = Prop.prop
type symbol = S.symbol
type variable = AthTerm.variable
open Semantics
datatype hypothesis = hypothesis of symbol option * prop
datatype alpha_val = term of AthTerm.term | sent of prop
datatype certificate = ruleApp of {rule:symbol, args: alpha_val list}
| assumeProof of {hyp: hypothesis, body: certificate}
| supAbProof of {hyp: hypothesis, body: certificate}
| composition of {left: certificate, right: certificate}
| pickAny of {eigen_var: symbol, actual_fresh: variable, body: certificate}
| conclude of {expected_conc: prop, body: certificate}
| block of certificate list
val trivial_cert = ruleApp({rule=S.symbol("TRIVIAL_RULE"),args=[]})
fun simpleCert(ruleApp(_)) = true
| simpleCert(_) = false
fun compsToBlocks(D) =
let fun B(composition({left,right})) = (B left)@(B right)
| B(D) = [D]
fun c2b(D as composition(_)) = block(map c2b (B D))
| c2b(assumeProof({hyp,body})) = assumeProof({hyp=hyp,body=c2b(body)})
| c2b(supAbProof({hyp,body})) = supAbProof({hyp=hyp,body=c2b(body)})
| c2b(pickAny({eigen_var,actual_fresh,body})) = pickAny({eigen_var=eigen_var,actual_fresh=actual_fresh,body=c2b(body)})
| c2b(conclude({expected_conc,body})) = conclude({expected_conc=expected_conc,body=c2b(body)})
| c2b(block(Ds)) = block(map c2b Ds)
| c2b(D) = D
in
c2b(D)
end
fun certToStringNaive(D) =
let fun argToString(term(t)) = AT.toStringDefault(t)
| argToString(sent(p)) = Prop.makeTPTPPropSimple(p)
fun argsToString(args) = Basic.printListStr(args,argToString,", ")
fun f(ruleApp({rule,args,...})) = "[" ^ (S.name rule) ^ " on " ^ (argsToString args) ^ "]"
| f(assumeProof({hyp as hypothesis(name_opt,p),body})) = "assume " ^ (Prop.makeTPTPPropSimple p) ^ " { " ^ (f body) ^ " } "
| f(supAbProof({hyp as hypothesis(name_opt,p),body})) = "suppose-absurd " ^ (Prop.makeTPTPPropSimple p) ^ " { " ^ (f body) ^ " } "
| f(block(proofs)) = " BLOCK_START " ^ Basic.printListStr(proofs,f," || ") ^ " BLOCK_END "
| f(composition({left,right})) = (f left) ^ " ;; " ^ (f right)
| f(D) = "NOT IMPLEMENTED YET"
in
f(D)
end
fun hasSubproof(D,pred) =
let fun find(D) =
if pred(D) then let val _ = print("\nFOUND TARGET PROOF: " ^ (certToStringNaive D) ^ "\n") in true end else
(case D of
composition({left,right}) => find(left) orelse find(right)
| assumeProof({body,...}) => find(body)
| supAbProof({body,...}) => find(body)
| pickAny({body,...}) => find(body)
| conclude({body,...}) => find(body)
| block(certs) => Basic.exists(certs,find)
| _ => false)
in
find D
end
fun compFree(D) =
if hasSubproof(D,fn D' => (case D' of composition(_) => true | _ => false))
then false else true
fun certToString(D) =
let val spaces = Basic.spaces
fun argToString(term(t)) = AT.toStringDefault(t)
| argToString(sent(p)) = Prop.makeTPTPPropSimple(p)
fun argsToString(args) = Basic.printListStr(args,argToString,", ")
fun c2s(ruleApp({rule,args,...}),offset) = (spaces offset) ^ (S.name rule) ^ " on " ^ (argsToString args)
| c2s(assumeProof({hyp as hypothesis(name_opt,p),body}),offset) =
(spaces offset) ^ "assume " ^ (Prop.makeTPTPPropSimple p) ^ " {\n" ^ (c2s(body,offset+2)) ^ "\n" ^ (spaces (offset + 1)) ^"}"
| c2s(supAbProof({hyp as hypothesis(name_opt,p),body}),offset) =
(spaces offset) ^ "suppose-absurd " ^ (Prop.makeTPTPPropSimple p) ^ "\n" ^ (c2s(body,offset+2))
| c2s(composition({left,right}),offset) = (c2s(left,offset+2)) ^ ";\n" ^ (c2s(right,offset+2))
| c2s(block([D]),offset) = c2s(D,offset)
| c2s(block(D1::(more as (_::_))),offset) = c2s(D1,offset) ^ ";\n" ^ (c2s(block(more),offset))
| c2s(conclude({expected_conc,body}),offset) =
(spaces offset) ^ (Prop.makeTPTPPropSimple expected_conc) ^ " BY " ^ (if simpleCert(body) then c2s(body,0) else ("\n" ^ c2s(body,offset + 2)))
val D' = compsToBlocks(D)
in
(case D' of
block(_) => "\n{\n" ^ (c2s(D',2)) ^ "\n}"
| _ => (c2s(D',0)))
end
type alpha_ded_info = {proof: certificate, conc: Prop.prop, fa: Prop.prop list}
fun makeAlphaDed() = let val res: alpha_ded_info = {proof=ruleApp({rule=S.symbol("foo"),args=[]}),conc=Prop.true_prop, fa = []}
in
res
end
fun getProp(v) = (case coerceValIntoProp(v) of SOME(p) => p)
fun getFA(method_sym,vals: value list,ab) =
let val method_name = S.name(method_sym)
val props = Basic.mapSelect(getProp,vals,fn _ => true)
in
case method_name of
"left-either" => [(Basic.first props)]
| "right-either" => [(Basic.second props)]
| "either" => let val [p1,p2] = props
val fas = ref([])
val _ = if not(ABase.isMember(p1,ab)) then fas := [p1] else ()
val _ = if not(ABase.isMember(p2,ab)) then fas := p1::(!fas) else ()
in
!fas
end
| _ => props
end
fun extractHypothesisName(map,pval,hypothesis_name) =
let val symbol_and_value_pairs = Symbol.listSymbolsAndImages(map)
in
(case Basic.constructiveExists(symbol_and_value_pairs,fn (symbol,value) => valEq(value,pval)) of
SOME((symbol,_)) => hypothesis_name := S.name(symbol)
| _ => ())
end
fun propUnion(prop_list_1,prop_list_2) = Basic.listUnion(prop_list_1,prop_list_2,Prop.alEq)
fun propDiff(prop_list_1,prop_list_2) = Basic.listDiff(prop_list_1,prop_list_2,Prop.alEq)
fun reconcile(tail_ded_info,[]) = tail_ded_info
| reconcile(tail_ded_info,(ded_info_1 as {conc=conc1,fa=fa1,proof=proof1}:alpha_ded_info)::more) =
let val {conc=tail_conc,fa=tail_fa,proof=tail_proof}:alpha_ded_info = reconcile(tail_ded_info,more)
val final_fas = propUnion(fa1,propDiff(tail_fa,[conc1]))
val final_conc = tail_conc
val final_proof = composition({left=proof1,right=tail_proof})
in
{conc=tail_conc,fa=final_fas,proof=final_proof}
end
fun getNewEnvAndAb(dval,bpat,env1,ab1,ab) =
(case matchPat(dval,bpat,makeEvalExpFunction (env1,ab)) of
SOME(map,_) => let val (vmap,mod_map) = getValAndModMaps(!env1)
val new_env = ref(valEnv({val_map=Symbol.augment(vmap,map),mod_map=mod_map}))
val new_ab = (case dval of
propVal(p) => ABaseAugment(ab1,Prop.decomposeConjunctions p)
| _ => ab1)
in
(new_env,new_ab)
end
| _ => evError("Dlet pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon dval),
SOME(A.posOfPat(bpat))))
val evalDedAlpha =
let val iarm_stack:iarm_type LStack.stack ref = ref(LStack.empty)
fun initIndStack() = iarm_stack := LStack.empty
fun initCallStack() = call_stack := LStack.empty
fun getAlphaVal(v) = (case coerceValIntoTerm(v) of
SOME(t) => term(t)
| _ => (case coerceValIntoProp(v) of
SOME(p) => sent(p)))
fun evPhrase(phr,env,ab) =
(case phr of
A.ded(d) => (case evDed(d,env,ab) of (x,y) => (x,SOME(y)))
| A.exp(e) => (evalExp(e,env,ab),NONE))
and evDed(method_app as A.BMethAppDed({method,arg1,arg2,pos}),env,ab) =
((let val head_val = evalExp(method,env,ab)
in
(case head_val of
primBMethodVal(M,method_sym) =>
(let val (v1,ded_1_info_opt) = evPhrase(arg1,env,ab)
val (v2,ded_2_info_opt) = evPhrase(arg2,env,ab)
val arg_ded_infos = Basic.mapSelect(fn SOME(ded_info) => ded_info,[ded_1_info_opt,ded_2_info_opt],fn _ => true)
val ab' = if A.isDeduction(arg1) then putValIntoAB(v1,ab) else ab
val ab'' = if A.isDeduction(arg2) then putValIntoAB(v2,ab') else ab'
val res_val = M(v1,v2,env,ab'')
val (av1, av2) = (getAlphaVal(v1), getAlphaVal(v2))
val tail_ded_info = {conc=getProp(res_val),
fa=getFA(method_sym,[v1,v2],ab''),
proof=ruleApp({rule=method_sym,args=[av1,av2]})}
val ded_info = reconcile(tail_ded_info,arg_ded_infos)
in
(res_val,ded_info)
end handle PrimError(msg) => evError(msg,SOME(pos))
| AthenaError(msg) => let val (_,right_pos) = chooseAthenaErrorPosition()
in
evError(msg,SOME(right_pos))
end)
| closBMethodVal(d,s1,s2,clos_env as ref(valEnv({val_map,mod_map,...})),name) =>
let val v1 = evalPhrase(arg1,env,ab)
val v2 = evalPhrase(arg2,env,ab)
val ab' = if A.isDeduction(arg1) then putValIntoAB(v1,ab) else ab
val ab'' = if A.isDeduction(arg2) then putValIntoAB(v2,ab') else ab'
val vm = Symbol.enter(Symbol.enter(val_map,s1,v1),s2,v2)
val _ = addPos(!name,pos)
in
evDed(d,ref(valEnv({val_map=vm,mod_map=mod_map})),ab'')
end
| _ => evalMethodApp(method,[arg1,arg2],env,ab,pos))
end))
| evDed(A.UMethAppDed({method,arg,pos}),env,ab) =
((let val head_val = evalExp(method,env,ab)
in
(case head_val of
primUMethodVal(f,method_sym) =>
(let val (arg_val,ded_1_info_opt) = evPhrase(arg,env,ab)
val ab' = if A.isDeduction(arg) then putValIntoAB(arg_val,ab) else ab
val conclusion_val = f(arg_val,env,ab')
val ded_info = (case ded_1_info_opt of
NONE => {conc=getProp(conclusion_val),
fa=getFA(method_sym,[arg_val],ab'),
proof=ruleApp({rule=method_sym,args=[getAlphaVal(arg_val)]})}
| SOME({conc=conc1,fa=fa1,proof=proof1,...}) =>
let val final_fas = propUnion(fa1,propDiff(getFA(method_sym,[arg_val],ab'),[conc1]))
in
{conc=getProp(conclusion_val),
fa=final_fas,
proof=composition({left=proof1,right=ruleApp({rule=method_sym,args=[getAlphaVal(arg_val)]})})}
end)
in
(conclusion_val,ded_info)
end handle PrimError(msg) => evError(msg,SOME(pos))
| AthenaError(msg) => let val (_,right_pos) = chooseAthenaErrorPosition()
in
evError(msg,SOME(right_pos))
end)
| closUMethodVal(d,s,clos_env as ref(valEnv({val_map,mod_map,...})),clos_name) =>
let val arg_val = evalPhrase(arg,env,ab)
val vm = Symbol.enter(val_map,s,arg_val)
val ab' = if A.isDeduction(arg) then putValIntoAB(arg_val,ab) else ab
val _ = addPos(!clos_name,pos)
in
evDed(d,ref(valEnv({val_map=vm,mod_map=mod_map})),ab')
end)
end))
| evDed(method_app as A.methodAppDed({method,args,pos=app_pos}),env,ab) = evalMethodApp(method,args,env,ab,app_pos)
| evDed(A.matchDed({discriminant,clauses,pos}),env,ab) =
let val (discrim_value,ded_info_opt) = evPhrase(discriminant,env,ab)
val disc_ded_infos = Basic.mapSelect(fn SOME(ded_info) => ded_info,[ded_info_opt],fn _ => true)
val e_fun = makeEvalExpFunction (env,ab)
val new_ab = if A.isDeduction(discriminant) then
(case coerceValIntoProp(discrim_value) of
SOME(P) => ABase.insert(P,ab)
| _ => evError("Impossible error encountered in dmatch"^
"---the discriminant is a deduction and yet it "^
"did not produce a sentence",
SOME(A.posOfPhrase(discriminant))))
else ab
fun tryClauses([]:A.dmatch_clause list) =
evError("dmatch failed---the "^valLCTypeAndStringNoColon(discrim_value)^
"\ndid not match any of the given patterns",SOME(pos))
| tryClauses({pat,ded}::more) =
(case matchPat(discrim_value,pat,e_fun) of
SOME(map,_) =>
let val new_env = ref(augmentWithMap(!env,map))
val result as (res_val,body_ded_info) = evDed(ded,new_env,new_ab)
val final_ded_info = reconcile(body_ded_info,disc_ded_infos)
in
(res_val,final_ded_info)
end
| _ => tryClauses(more))
in
tryClauses(clauses)
end
| evDed(A.beginDed({members,pos}),env,ab) =
let fun doAll([d],ab) = evDed(d,env,ab)
| doAll(d1::rest,ab) =
(case evDed(d1,env,ab) of
(propVal(p),{proof=proof1,conc=conc1,fa=fa1}) =>
(case doAll(rest,ABaseInsert(p,ab)) of
(res_val,{proof=proof_rest,conc=conc_rest,fa=fa_rest}) =>
(res_val,{proof=composition({left=proof1,right=proof_rest}),
fa=propUnion(fa1,propDiff(fa_rest,[conc1])),
conc=conc_rest})))
in
doAll(members,ab)
end
| evDed(A.checkDed({clauses,pos}),env,ab) =
(case evalDCheckClauses(clauses,env,ab) of
SOME(d) => evDed(d,env,ab)
| _ => evError("dcheck failed: no alternative holds",SOME(pos)))
| evDed(A.letDed({bindings,body,pos}),env,ab) =
let fun doBindings([],env1,ab1,ded_infos) =
let val result as (res_val,body_ded_info) = evDed(body,env1,ab1)
val final_ded_info = reconcile(body_ded_info,rev(ded_infos))
in
(res_val,final_ded_info)
end
| doBindings({bpat,def=A.ded(d),pos}::more,env1,ab1,ded_infos) =
let val (ded_val,ded_info as {proof,conc,fa}) = evDed(d,env1,ab1)
val (env2,ab2) = getNewEnvAndAb(ded_val,bpat,env1,ab1,ab)
in
doBindings(more,env2,ab2,ded_info::ded_infos)
end
| doBindings({bpat,def=A.exp(e),pos}::more,env1,ab1,ded_infos) =
let val exp_val = evalExp(e,env1,ab1)
val (env2,_) = getNewEnvAndAb(exp_val,bpat,env1,ab1,ab)
in
doBindings(more,env2,ab1,ded_infos)
end
in
doBindings(bindings,env,ab,[])
end
| evDed(A.assumeDed({assumption,body,pos}),env,ab) =
let val aval = evalPhrase(assumption,env,ab)
in
(case coerceValIntoProp(aval) of
SOME(antecedent) =>
let val asms = Prop.decomposeConjunctions(antecedent)
val ab' = ABase.augment(ab,asms)
in
(case evDed(body,env,ab') of
(body_val,body_ded_info as {proof=body_proof,conc=body_conc,fa=body_fa}) =>
(case coerceValIntoProp(body_val) of
SOME(consequent) => let val conditional_conclusion = Prop.makeConditional(antecedent,consequent)
val final_ded_info = {proof=assumeProof({hyp=hypothesis(NONE,antecedent), body=body_proof}),
conc=conditional_conclusion,
fa=propDiff(body_fa,[antecedent])}
in
(propVal(conditional_conclusion),final_ded_info)
end
| _ => evError("In a deduction of the form (assume F D), the value of F"^
" must\nbe a sentence, but here it was a "^valLCTypeAndString(aval),
SOME(A.posOfPhrase(assumption)))))
end)
end
| evDed(A.absurdDed({hyp,body,pos}),env,ab) = doSupposeAbsurd(hyp,NONE,body,pos,env,ab)
| evDed(A.absurdLetDed({named_hyp,body,pos}),env,ab) =
let val (hyp_name_pat,hyp) = (#bpat(named_hyp),#def(named_hyp))
in
doSupposeAbsurd(hyp,SOME(hyp_name_pat),body,pos,env,ab)
end
| evDed(A.letRecDed({bindings,body,pos}),env,ab) =
let val rec_env = ref(!env)
fun getVals([],map) = map
| getVals((b:A.binding as {bpat,def,pos})::more,map) =
let val mv = evalPhrase(def,rec_env,ab)
in
(case matchPat(mv,bpat,makeEvalExpFunction (rec_env,ab)) of
SOME(map',_) => getVals(more,Symbol.augment(map,map'))
| _ => evError("dletrec pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon mv),
SOME(A.posOfPat(bpat))))
end
val pmap = getVals(bindings,S.empty_mapping)
val (vmap,mod_map) = getValAndModMaps(!env)
val new_env = valEnv({val_map=Symbol.augment(vmap,pmap),mod_map=mod_map})
val _ = rec_env := new_env
in
evDed(body,ref(new_env),ab)
end
| evDed(A.tryDed({choices,pos}),env,ab) =
let fun tryDeds([]) = NONE
| tryDeds(d::more) =
(case (SOME(evDed(d,env,ab)) handle _ => NONE) of
NONE => tryDeds(more)
| r => r)
in
(case tryDeds(choices) of
NONE => evError("Try deduction error; all "^
" alternatives failed.\n",SOME(pos))
| (SOME v) => v)
end
| evDed(A.assumeLetDed({bindings,body,pos}),env,ab) =
let fun getProp(F,is_ded,env,ab) =
let val Fval = evalPhrase(F,env,ab)
in
(case coerceValIntoProp(Fval) of
SOME(P) => P
| _ => let val Fkind = if is_ded then "body" else "hypothesis"
in
evError("assume-let "^Fkind^" failed to result in a sentence. Instead, it "^
"produced a "^valLCTypeAndString(Fval),SOME(A.posOfPhrase(F)))
end)
end
fun doBindings([]:A.binding list,assumptions,env1) =
let val asms' = Basic.flatten(map Prop.decomposeConjunctions assumptions)
val new_ab = ABaseAugment(ab,asms')
in
evDed(body,env1,new_ab)
end
| doBindings({bpat,def,...}::more,assumptions,env1) =
let val new_assumption = getProp(def,false,env1,ab)
val hyp_name = ref("")
val res as (pval,ded_info as {conc=rest_conc,proof=rest_proof,fa=rest_fa}) =
(case matchPat(propVal(new_assumption),bpat,makeEvalExpFunction (env1,ab)) of
SOME(map,_) => let val (vmap,mmap) = getValAndModMaps(!env1)
val env1' = ref(valEnv({val_map=Symbol.augment(vmap,map),mod_map=mmap}))
val _ = extractHypothesisName(map,propVal(new_assumption),hyp_name)
in
doBindings(more,new_assumption::assumptions,env1')
end
| _ => evError("Assume pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon (propVal new_assumption)),
SOME(A.posOfPat(bpat))))
val (new_conclusion,body_conclusion) =
(case coerceValIntoProp(pval) of
SOME(p) => (Prop.makeConditional(new_assumption,p),p))
val hyp_name_opt = if (!hyp_name) = "" then NONE else SOME(S.symbol(!hyp_name))
in
(propVal(new_conclusion),{conc=new_conclusion,
proof=assumeProof({hyp=hypothesis(hyp_name_opt,new_assumption), body=rest_proof}),
fa=propDiff(rest_fa,new_assumption::(Prop.decomposeConjunctions new_assumption))})
end
in
doBindings(bindings,[],env)
end
| evDed(D,env,ab) =
let val _ = print("\n***************************************** UNHANDLED CERT CASE: " ^ (A.unparseDed(D)) ^ "\n")
in
(evalDed(D,env,ab),{proof=trivial_cert,conc=Prop.true_prop,fa=[]})
end
(*******************************************************************************************************************************************************************************
| evDed(A.letDed({bindings,body,pos}),env,ab) =
let fun doLetDed([]:A.binding list,env1,ab1) = evDed(body,env1,ab1)
| doLetDed({bpat,def=A.ded(d),pos}::more,env1,ab1) =
let val dval = evDed(d,env1,ab1)
in
(case matchPat(dval,bpat,makeEvalExpFunction (env1,ab)) of
SOME(map,_) => let val (vmap,mod_map) = getValAndModMaps(!env1)
val new_env = ref(valEnv({val_map=Symbol.augment(vmap,map),mod_map=mod_map}))
val new_ab = (case dval of
propVal(p) => ABaseAugment(ab1,Prop.decomposeConjunctions p)
| _ => ab1)
in
doLetDed(more,new_env,new_ab)
end
| _ => evError("Dlet pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon dval),
SOME(A.posOfPat(bpat))))
end
| doLetDed({bpat,def=A.exp(e),pos}::more,env1,ab1) =
let val eval = evalExp(e,env1,ab1)
in
(case matchPat(eval,bpat,makeEvalExpFunction (env1,ab)) of
SOME(map,_) => let val (vmap,mod_map) = getValAndModMaps(!env1)
val new_env = ref(valEnv({val_map=Symbol.augment(vmap,map),mod_map=mod_map}))
in
doLetDed(more,new_env,ab1)
end
| _ => evError("Dlet pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon eval),
SOME(A.posOfPat(bpat))))
end
in
doLetDed(bindings,env,ab)
end
| evDed(A.assumeDed({assumption,body,pos}),env,ab) =
let val aval = evalPhrase(assumption,env,ab)
in
(case coerceValIntoProp(aval) of
SOME(p1) => let val asms = Prop.decomposeConjunctions(p1)
val ab' = ABase.augment(ab,asms)
in
(case coerceValIntoProp(evDed(body,env,ab')) of
SOME(p2) => propVal(P.makeConditional(p1,p2))
| _ => evError("Impossible error encountered in assume deduction: the body of "^
"the deduction did not produce a sentence",
SOME(A.posOfDed(body))))
end
| _ => evError("In a deduction of the form (assume F D), the value of F"^
" must\nbe a sentence, but here it was a "^valLCTypeAndString(aval),
SOME(A.posOfPhrase(assumption))))
end
| evDed(A.assumeLetDed({bindings,body,pos}),env,ab) =
let fun getProp(F,is_ded,env,ab) =
let val Fval = evalPhrase(F,env,ab)
in
(case coerceValIntoProp(Fval) of
SOME(P) => P
| _ => let val Fkind = if is_ded then "body" else "hypothesis"
in
evError("assume-let "^Fkind^" failed to result in a sentence. Instead, it "^
"produced a "^valLCTypeAndString(Fval),SOME(A.posOfPhrase(F)))
end)
end
fun doBindings([]:A.binding list,assumptions,env1) =
let val asms' = Basic.flatten(map Prop.decomposeConjunctions assumptions)
in
propVal(Prop.foldConditionals(rev(assumptions),
getProp(A.ded(body),true,env1,ABaseAugment(ab,asms'))))
end
| doBindings({bpat,def,...}::more,assumptions,env1) =
let val new_assumption = getProp(def,false,env1,ab)
in
(case matchPat(propVal(new_assumption),bpat,makeEvalExpFunction (env1,ab)) of
SOME(map,_) => let val (vmap,mmap) = getValAndModMaps(!env1)
val env1' = ref(valEnv({val_map=Symbol.augment(vmap,map),mod_map=mmap}))
in
doBindings(more,new_assumption::assumptions,env1')
end
| _ => evError("Assume pattern failed to match the corresponding value, the\n "^
(valLCTypeAndStringNoColon (propVal new_assumption)),
SOME(A.posOfPat(bpat))))
end
in
doBindings(bindings,[],env)
end
| evDed(A.absurdDed({hyp,body,pos}),env,ab) = doSupposeAbsurd(hyp,NONE,body,pos,env,ab)
| evDed(A.absurdLetDed({named_hyp,body,pos}),env,ab) =
let val (hyp_name_pat,hyp) = (#bpat(named_hyp),#def(named_hyp))
in
doSupposeAbsurd(hyp,SOME(hyp_name_pat),body,pos,env,ab)
end
| evDed(A.inductionDed({prop,clauses,pos}),env:value_environment ref,ab) =
let val (uvar,body,goal) = let val pval = evalPhrase(prop,env,ab)
in
(case (coerceValIntoProp(pval)) of
SOME(P) => (case P.isUGen(P) of
SOME(v,body) => (v,body,P)
| _ => evError((prefix ())^(msg3 pval),
SOME(A.posOfPhrase(prop))))
| _ => evError((prefix ())^(msg3 pval),SOME(A.posOfPhrase(prop))))
end
val uvar_sort = ATV.getSort(uvar)
val _ = if StructureAnalysis.isStructureType(uvar_sort) then () else
evError(msg1(2,ATV.toStringWithSort(uvar),P.toString(body,makeVarSortPrinter()),
F.toStringDefault(uvar_sort)),
SOME(A.posOfPhrase(prop)))
fun makeNewClause(clause as {pat:A.pattern,ded:A.deduction}) =
let fun evSortExp(e) =
let val mod_path = (case (!Paths.open_mod_paths) of
[] => []
| mp::_ => mp)
val (e',_,fids) = SV.preProcessExp(e,mod_path)
val env' = makeEnv(fids,!env)
in
evalExp(e',env',ab)
end
val (pat_as_ath_term,pat_ids_paired_with_fresh_vars,is_named_pat) = makePatIntoAthenaTerm(pat,uvar_sort,evSortExp)
in
(pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded,is_named_pat)
end
val new_clauses = map makeNewClause clauses
val pats_as_ath_terms = map (fn (_,b,_,_,_) => b) new_clauses
val _ = (case StructureAnalysis.exhaustStructure(pats_as_ath_terms,uvar_sort) of
SOME(t) => evError(msg2(F.toStringDefault(uvar_sort),
AT.toStringDefault(t)),SOME(pos))
| _ => ())
fun property(t) = P.replace(uvar,t,body)
fun doNewClause((pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded:A.deduction,is_named_pat:bool),previous_clauses,ab) =
let fun emsg(req,gotten) = "One of the given cases of this inductive deduction failed "^
"to establish the right conlusion.\nWanted: "^
Util.decide(pprintProp(req),50)^
"\nGotten: "^Util.decide(pprintProp(gotten),50)^"\n"
val (pat_ids,fresh_vars) = ListPair.unzip(pat_ids_paired_with_fresh_vars)
val new_iarm = {uvar=uvar,uvar_sort=uvar_sort,uprop=body,
uv_pattern=pat_as_ath_term}
val _ = iarm_stack := LStack.push(new_iarm,!iarm_stack)
val (vmap,mmap) = getValAndModMaps(!env)
val e_fun = makeEvalExpFunction (env,ab)
val new_env_0 = ref(valEnv({val_map=enterLst(vmap,pat_ids,map (fn v => termVal(AT.makeVar(v))) fresh_vars),mod_map=mmap}))
val new_env = if is_named_pat then
(case matchPat(termVal(pat_as_ath_term),pat,e_fun) of
SOME(map,_) => ref(augmentWithMap(!new_env_0,map))
| _ => new_env_0)
else new_env_0
val real_current_goal = property(pat_as_ath_term)
fun getReflexiveFreshvars(vars,res) =
(case vars of
[] => (rev res)
| fv::more => let val fresh_var_sort = ATV.getSort(fv)
in
(case F.isVarOpt(fresh_var_sort) of
SOME(_) => getReflexiveFreshvars(more,res)
| _ => (case F.unify(fresh_var_sort,F.rename(uvar_sort)) of
SOME(sub) => let val new = ATV.applySortSub(sub,fv)
in
getReflexiveFreshvars(more,new::res)
end
| _ => getReflexiveFreshvars(more,res)))
end)
val reflexive_fresh_vars = getReflexiveFreshvars(fresh_vars,[])
val reflexive_vars_as_terms = map AT.makeVar reflexive_fresh_vars
val current_ind_hyps = map property reflexive_vars_as_terms
fun sameRootSymbol(t1,t2) =
(case (AT.isApp(t1),AT.isApp(t2)) of
(SOME(f,_),SOME(g,_)) => MS.modSymEq(f,g)
| _ => false)
val diff_facts:P.prop list =
if not(!(Options.fundef_mlstyle_option)) orelse null(previous_clauses) then []
else (let val previous_pats_as_terms:AT.term list = map (fn (_,y,_,_,_) => y) previous_clauses
val previous_pats_as_terms = List.filter (fn t => sameRootSymbol(t,pat_as_ath_term)) previous_pats_as_terms
val previous_pat_string = Basic.printSExpListStr(previous_pats_as_terms,fn t => (valToString (termVal t)))
val pstr = getSafeName("diff*")
val str = "("^pstr^" "^(valToString (termVal pat_as_ath_term))^" ["^previous_pat_string^"])"
val evalString = !evaluateString
val lv = evalString(str)
in
case coerceValIntoProp(lv) of
SOME(p) => let val res = Prop.decomposeConjunctions(p)
in
res
end
end) handle _ => []
val ind_hyps = (current_ind_hyps@
(if (AT.height(pat_as_ath_term) = 0) then []
else getInductiveHypotheses(!iarm_stack,fresh_vars,real_current_goal,pat_as_ath_term)))
handle Basic.FailLst(msgs) => evError("Error in generating inductive "^
"hypotheses..."^Basic.failMsgsToStr(msgs),
SOME(pos))
val new_ab = ABaseAugment(ab,diff_facts@ind_hyps)
val res_prop = (case evDed(ded,new_env,new_ab) of
propVal(P) => P
| _ => raise Basic.Never)
in
(case provesTheRightInductiveCase(res_prop,uvar,body,pat_as_ath_term,
pat_ids,fresh_vars,"inductive") of
SOME(msg) => evError(msg,SOME(A.posOfDed(ded)))
| _ => (iarm_stack := LStack.pop(!iarm_stack);
let val conjunction = P.makeConjunction(ind_hyps)
val body = if null(ind_hyps) then res_prop else P.makeConditional(conjunction,res_prop)
val res = P.makeUGen(fresh_vars,body)
in res
end))
end
fun doNewClauses([],_,_) = propVal(P.makeUGen([uvar],body))
| doNewClauses(clause::more_clauses,previous_clauses,ab) =
let val res = doNewClause(clause,previous_clauses,ab)
in
doNewClauses(more_clauses,clause::previous_clauses,ab)
end
in
doNewClauses(new_clauses,[],ab)
end
| evDed(A.structureCasesDed({prop,clauses,pos,term}),env:value_environment ref,ab) =
(case term of
SOME(dt_exp) =>
let fun prefix(n) = let val str = "first"
in
"Invalid argument given to the datatype-cases form.\n"^
"In every deduction of the form (datatype-cases E v D), the "^
"value of E must\nbe a sentence P and v must be (or denote) a variable"
end
fun msg1(n,var_str,P_str,obtype) = prefix(n)^". In addition, the\nsort of v in P must be a datatype."^
" But here "^"v and P are "^var_str^" and\n"^P_str^
"\nrespectively, where "^var_str^" ranges over "^obtype^
"---and "^obtype^" is not a datatype"
fun msg2(str1,str2) = "Ill-formed datatype-cases deduction---the given patterns do not exhaust\nthe "^
"datatype "^str1^". Here is a counter-example: "^str2
fun msg3(n,v) = prefix(n)^".\nBut here E was a "^valLCTypeAndString(v)
fun msg4(e) = "A variable was expected here"
val dt_exp_pos = A.posOfExp(dt_exp)
val var = (case evalExp(dt_exp,env,ab) of
termVal(t) => (case AT.isVarOpt(t) of
SOME(v) => v
| _ => evError(msg4(dt_exp),SOME(dt_exp_pos)))
| _ => evError(msg4(dt_exp),SOME(dt_exp_pos)))
val var_term = AthTerm.makeVar(var)
val body = let val pval = evalPhrase(prop,env,ab)
in
(case coerceValIntoProp(pval) of
SOME(P) => P
| _ => evError(msg3(2,pval),SOME(A.posOfPhrase(prop))))
end
val var_type = ATV.getSort(var)
val _ = if StructureAnalysis.isStructureType(var_type) then () else
evError(msg1(2,Terms.printAthTermVar(var),prettyValToString(propVal(body)),Terms.printFTermSExp(var_type)),
SOME(A.posOfPhrase(prop)))
fun makeNewClause(clause as {pat:A.pattern,ded:A.deduction}) =
let fun evSortExp(e) =
let val mod_path = (case (!Paths.open_mod_paths) of
[] => []
| mp::_ => mp)
val (e',_,fids) = SV.preProcessExp(e,mod_path)
val env' = makeEnv(fids,!env)
in
evalExp(e',env',ab)
end
val (pat_as_ath_term,pat_ids_paired_with_fresh_vars,is_named_pat) = makePatIntoAthenaTerm(pat,var_type,evSortExp)
in
(pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded,is_named_pat)
end
val new_clauses = map makeNewClause clauses
val pats_as_ath_terms = map (fn (_,b,_,_,_) => b) new_clauses
val _ = (case StructureAnalysis.exhaustStructure(pats_as_ath_terms,var_type) of
SOME(t) => evError(msg2(Terms.printFTermSExp(var_type),
Terms.printAthTermSExp(t)),SOME(pos))
| _ => ())
fun doNewClause((pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded:A.deduction,is_named_pat:bool),ab) =
let val (pat_ids,fresh_vars) = ListPair.unzip(pat_ids_paired_with_fresh_vars)
val (vmap,mmap) = getValAndModMaps(!env)
val e_fun = makeEvalExpFunction (env,ab)
val new_env_0 = ref(valEnv({val_map=enterLst(vmap,pat_ids,map (fn v => termVal(AT.makeVar(v))) fresh_vars),mod_map=mmap}))
val new_env = if is_named_pat then
let
in
(case matchPat(termVal(pat_as_ath_term),pat,e_fun) of
SOME(map,_) => ref(augmentWithMap(!new_env_0,map))
| _ => new_env_0)
end
else
let
in
new_env_0
end
val new_ab = ABaseAugment(ab,[Prop.makeEquality(var_term,pat_as_ath_term),Prop.makeEquality(pat_as_ath_term,var_term)])
val res_prop = (case evDed(ded,new_env,new_ab) of
propVal(P) => P)
in
(case provesTheRightInductiveCase(res_prop,var,body,pat_as_ath_term,
pat_ids,fresh_vars,"structure-cases") of
SOME(msg) => evError(msg,SOME(A.posOfDed(ded)))
| _ => P.makeUGen(fresh_vars,res_prop))
end
fun doNewClauses([],_) = propVal(body)
| doNewClauses(clause::more_clauses,ab) =
let val res = doNewClause(clause,ab)
in
doNewClauses(more_clauses,ABaseInsert(res,ab))
end
in
doNewClauses(new_clauses,ab)
end
| NONE =>
let fun prefix(n) = let val str = "first"
in
"Invalid "^str^" argument given to the structure-cases form. "^
"In every deduction\nof the form (structure-cases E D), the "^
"value of E must be a sentence P of the form (forall v Q)"
end
fun msg1(n,var_str,P_str,obtype) = prefix(n)^".\nIn addition, the sort of v in Q must be a structure."^
" But here\n"^"v and P are "^var_str^" and "^P_str^
", respectively, where "^var_str^" in "^P_str^" ranges over "^obtype^
"---and "^obtype^" is not a structure"
fun msg2(str1,str2) = "Ill-formed structure-cases deduction---the given patterns do not exhaust\nthe "^
"structure "^str1^". Here is a counter-example: "^str2
fun msg3(n,v) = prefix(n)^".\nBut here E was a "^valLCTypeAndString(v)
val (uvar,body) = let val pval = evalPhrase(prop,env,ab)
in
(case coerceValIntoProp(pval) of
SOME(P) => (case P.isUGen(P) of
SOME(uv,ub) => (uv,ub)
| _ => evError(msg3(2,pval),SOME(A.posOfPhrase(prop))))
| _ => evError(msg3(2,pval),SOME(A.posOfPhrase(prop))))
end
val uvar_sort = ATV.getSort(uvar)
val _ = if StructureAnalysis.isStructureType(uvar_sort) then () else
evError(msg1(2,ATV.toStringDefault(uvar),P.toString(body,makeVarSortPrinter()),
F.toStringDefault(uvar_sort)),
SOME(A.posOfPhrase(prop)))
fun makeNewClause(clause as {pat:A.pattern,ded:A.deduction}) =
let fun evSortExp(e) =
let val mod_path = (case (!Paths.open_mod_paths) of
[] => []
| mp::_ => mp)
val (e',_,fids) = SV.preProcessExp(e,mod_path)
val env' = makeEnv(fids,!env)
in
evalExp(e',env',ab)
end
val (pat_as_ath_term,pat_ids_paired_with_fresh_vars,is_named_pat) = makePatIntoAthenaTerm(pat,uvar_sort,evSortExp)
in
(pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded,is_named_pat)
end
val new_clauses = map makeNewClause clauses
val pats_as_ath_terms = map (fn (_,b,_,_,_) => b) new_clauses
val _ = (case StructureAnalysis.exhaustStructure(pats_as_ath_terms,uvar_sort) of
SOME(t) => evError(msg2(F.toStringDefault(uvar_sort),
AT.toStringDefault(t)),SOME(pos))
| _ => ())
fun doNewClause((pat,pat_as_ath_term,pat_ids_paired_with_fresh_vars,ded:A.deduction,is_named_pat:bool),ab) =
let val (pat_ids,fresh_vars) = ListPair.unzip(pat_ids_paired_with_fresh_vars)
val (vmap,mmap) = getValAndModMaps(!env)
val e_fun = makeEvalExpFunction (env,ab)
val new_env_0 = ref(valEnv({val_map=enterLst(vmap,pat_ids,map (fn v => termVal(AT.makeVar(v))) fresh_vars),mod_map=mmap}))
val new_env = if is_named_pat then
let
in
(case matchPat(termVal(pat_as_ath_term),pat,e_fun) of
SOME(map,_) => ref(augmentWithMap(!new_env_0,map))
| _ => new_env_0)
end
else
let
in
new_env_0
end
val new_ab = ab
val res_prop = (case evDed(ded,new_env,new_ab) of
propVal(P) => P
| _ => raise Basic.Never)
in
(case provesTheRightInductiveCase(res_prop,uvar,body,pat_as_ath_term,
pat_ids,fresh_vars,"structure-cases") of
SOME(msg) => evError(msg,SOME(A.posOfDed(ded)))
| _ => P.makeUGen(fresh_vars,res_prop))
end
fun doNewClauses([],_) = propVal(P.makeUGen([uvar],body))
| doNewClauses(clause::more_clauses,ab) =
let val res = doNewClause(clause,ab)
in
doNewClauses(more_clauses,ABaseInsert(res,ab))
end
in
doNewClauses(new_clauses,ab)
end)
| evDed(A.byCasesDed({disj,from_exps,arms,pos}),env,ab) =
let val disj_val = evalPhrase(disj,env,ab)
val disj_prop = (case coerceValIntoProp(disj_val) of
SOME(P) => P
| _ => evError("A sentence (disjunction) was expected here. Instead, a\n"^
"value of type "^valLCTypeAndString(disj_val)^" was found.",
SOME(A.posOfPhrase(disj))))
val disj_holds = if ABase.isMember(disj_prop,ab) orelse A.isDeduction(disj) then true
else
(case from_exps of
NONE => evError("By-cases disjunction doesn't hold",
SOME(A.posOfPhrase(disj)))
| SOME(exps) =>
let fun er() = evError("Error in cases proof: unable to "^
"automatically derive the disjunction "^
prettyValToStringNoTypes(disj_val),SOME(pos))
val atp_call = A.methodAppDed({
method=A.idExp({msym=msym N.vpfPrimMethod_symbol,no_mods=true,mods=[],sym=N.vpfPrimMethod_symbol,pos=pos}),
args=[disj,A.exp(A.listExp({members=(map A.exp exps),
pos=A.dum_pos}))],pos=pos})
val atp_val = evDed(atp_call,env,ab) handle EvalError(str,_) =>
(print(str);raise Basic.Never)
in
(case coerceValIntoProp(atp_val) of
SOME(dp) => if P.alEq(dp,disj_prop) then true else er()
| _ => er())
end)
val alts = P.getDisjuncts(disj_prop)
fun getAltProp(v,pos) = (case coerceValIntoProp(v) of
SOME(P) => P
| _ => evError("A sentence was expected here. Instead, a\n"^
"value of type "^valLCTypeAndString(v)^" was found.",
SOME(pos)))
fun casesLeftUntreated(P) = evError("Non-exhaustive cases proof.\nThis case was not "^
"considered:\n"^P.toPrettyString(0,P,makeVarSortPrinter()),
SOME(pos))
fun gratuitousCase(P,pos) = evError("Gratuitous case considered in proof by cases:\n"^
P.toPrettyString(0,P,makeVarSortPrinter()),
SOME(pos))
val {case_name,alt=first_case,proof=first_ded}:A.case_clause = hd arms
val (first_case_considered,conclusion,cases_left) =
let val first_prop = getAltProp(evalExp(first_case,env,ab),A.posOfExp(first_case))
val (vmap,mmap) = getValAndModMaps(!env)
val env' = (case case_name of
SOME({name,...}) => ref(valEnv({val_map=Symbol.enter(vmap,name,propVal(first_prop)),mod_map=mmap}))
| _ => env)
val first_concl = (case coerceValIntoProp(evDed(first_ded,env',
ABaseInsert(first_prop,ab))) of
SOME(P) => P | _ => raise Basic.Never)
val (cases_left,is_mem) = Basic.removeAndCheckMemEq(first_prop,alts,P.alEq)
in
(first_prop,first_concl,cases_left)
end
fun checkConclusion(d,case_considered, new_env) =
let val conc = evDed(d,new_env,ABaseInsert(case_considered,ab))
in
(case coerceValIntoProp(conc) of
SOME(P) => if P.alEq(P,conclusion) then ()
else let val varSortPrinter = makeVarSortPrinter()
in
evError("The sentence "^
P.toPrettyString(0,conclusion,varSortPrinter)^
" was expected here.\n"^
"Instead, the following sentence was produced: "^
P.toPrettyString(0,P,varSortPrinter),
SOME(A.posOfDed(d)))
end
| _ => raise Basic.Never)
end
fun doArms([],cases_considered,cases_left) = if Basic.subsetEq(cases_left,cases_considered,P.alEq)
then propVal(conclusion)
else casesLeftUntreated(hd cases_left)
| doArms((cc:A.case_clause as {case_name,alt,proof})::rest,
cases_considered,cases_left) =
let val new_case_val = evalExp(alt,env,ab)
handle EvalError(str,_) =>
(print(str);raise Basic.Never)
| ObTypeCheckFailure =>
(print("\nSort failure\n");raise Basic.Never)
| _ => (print("\n Unknown error.\n"); raise Basic.Never)
val new_case = getAltProp(new_case_val,A.posOfExp(alt))
val (vmap,mmap) = getValAndModMaps(!env)
val new_env = (case case_name of
SOME({name,...}) => ref(valEnv({val_map=Symbol.enter(vmap,name,propVal(new_case)),mod_map=mmap}))
| _ => env)
val (cases_left',is_mem) = Basic.removeAndCheckMemEq(new_case,cases_left,P.alEq)
val _ = checkConclusion(proof,new_case,new_env)
in
doArms(rest,new_case::cases_considered,cases_left')
end
in
doArms(tl arms,[first_case_considered],cases_left)
end
| evDed(A.checkDed({clauses,pos}),env,ab) =
(case evalDCheckClauses(clauses,env,ab) of
SOME(d) => evDed(d,env,ab)
| _ => evError("dcheck failed: no alternative holds",SOME(pos)))
| evDed(A.beginDed({members,pos}),env,ab) =
let fun doAll([d],ab') = evDed(d,env,ab')
| doAll(d1::(rest as (d2::more)),ab') =
(case evDed(d1,env,ab') of
propVal(p) => doAll(rest,ABaseInsert(p,ab'))
| _ => evError("Impossible error---a component deduction of a dbegin "^
"failed to produce a sentence",SOME(A.posOfDed(d1))))
in
doAll(members,ab)
end
| evDed(A.byDed({wanted_res,body,pos,conc_name}),env,ab) =
let fun msg(P,Q) = "Failed conclusion annotation.\nThe expected conclusion was:\n"^
P.toPrettyString(0,P,makeVarSortPrinter())^"\nbut the obtained result was:\n"^
P.toPrettyString(0,Q,makeVarSortPrinter())
fun msg2(v) = "In a deduction of the form (E BY D), the value of E must be a sentence,\n"^
"but here it was a "^valLCTypeAndString(v)
fun indent(level, s) = if level = 0 then () else (print(s); indent(level - 1, s))
fun tracemsg1(level) = (A.posToString pos)^":Proving at level "^Int.toString(level)^":\n"
fun tracemsg2(level) = "Done at level "^Int.toString(level)^".\n"
fun pprint(n, P) = P.toPrettyString(n,P,makeVarSortPrinter())
fun openTracing(P,level) = if (!Options.conclude_trace) then
(level := !level + 1;
print((A.posToString pos)^":Proving at level "^Int.toString(!level)^":\n");
indent(!level," ");
print(" "^pprint(4*(!level)+2,P)^"\n"))
else ()
fun closeTracing(level,success) = if (!Options.conclude_trace) then
(level := !level - 1;
indent(!level+1," ");
if success then print("Done at level "^Int.toString(!level+1)^".\n")
else print("Failed at level "^Int.toString(!level+1)^".\n"
^"in dtry clause at "^(A.posToString pos)^".\n"))
else ()
val wv = evalExp(wanted_res,env,ab)
val env' = (case conc_name of
SOME({name,...}) => let val (vmap,mmap) = getValAndModMaps(!env)
in
ref(valEnv({val_map=Symbol.enter(vmap,name,wv),mod_map=mmap}))
end
| _ => env)
in
(case coerceValIntoProp(wv) of
SOME(P) => (openTracing(P,level);
case (evDed(body,env',ab)
handle ex => (closeTracing(level,false);raise ex)) of
res as propVal(Q) => if Prop.alEq(P,Q)
then (closeTracing(level,true);propVal(P))
else (closeTracing(level,false);
evError(msg(P,Q),SOME(pos))))
| _ => evError(msg2(wv),SOME(A.posOfExp(wanted_res))))
end
| evDed(A.pickAnyDed({eigenvars,body,pos}),env,ab) =
let val names = map #name eigenvars
fun makeFreshVar({name,pos,sort_as_sym_term=NONE,sort_as_fterm=NONE,...}:A.possibly_typed_param) =
ATV.freshWithPrefix(Symbol.name(name)^"_")
| makeFreshVar({name,pos,sort_as_sym_term=SOME(sym_term),sort_as_fterm=NONE,sort_as_exp=SOME(se),...}) =
let val sort_string = AthStringToMLString(evalExp(se,env,ab))
fun isVar(str) = let val str_lst = explode str
in not(null(str_lst)) andalso
hd(str_lst) = Names.sort_variable_prefix_as_char
end
in
(case Terms.parseSymTerm(sort_string,isVar) of
SOME(sort_as_sym_term) => let val fsort = Terms.carbonSymTermToFTerm(sort_as_sym_term)
in ATV.freshWithSortAndPrefix(Symbol.name(name)^"_",fsort)
end)
end
| makeFreshVar({name,pos,sort_as_sym_term,sort_as_fterm=SOME(sort),...}) =
(let val res = ATV.freshWithSortAndPrefix(Symbol.name(name)^"_",sort)
in
res
end)
val fresh_vars = map makeFreshVar eigenvars
val (vmap,mmap) = getValAndModMaps(!env)
val new_env = ref(valEnv({val_map=enterLst(vmap,names,map (fn v => termVal(AT.makeVar(v))) fresh_vars),mod_map=mmap}))
val res = evDed(body,new_env,ab)
in
case res of
propVal(p) => let val P_safe = P.makeUGen(fresh_vars,p)
val P_preferable = SOME(P.renameQProp(List.map Symbol.name names,P_safe))
handle _ => NONE
in
(case P_preferable of
SOME(Q) => if P.alEq(P_safe,Q) then propVal(Q)
else propVal(P_safe)
| _ => propVal(P_safe))
end
end
| evDed(A.pickWitnessDed({ex_gen,var_id,inst_id,body=main_body,pos}),env,ab) =
let val egv = evalPhrase(ex_gen,env,ab)
fun showError() = evError("In a deduction of the form (pick-witness I F D), the value "^
"of F must be an exisentially\nquantified sentence---but here "^