-
Notifications
You must be signed in to change notification settings - Fork 4
/
semantics.sml
executable file
·6397 lines (6098 loc) · 356 KB
/
semantics.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
(*=================================================================================
The implementation of Athena's operational semantics.
In particular, this file contains the definition of the main interpreter/evaluator
functions (evalExp, evalDed, etc.), pattern matching (matchPat), as well as value
equality (valEq), various conversions of values to strings (including pretty printers),
and a lot of auxiliary needed functionality.
=======================================================================*)
structure Semantics =
struct
open StructureAnalysis
fun makeVarSortPrinter() = F.makeVarSortPrinter()
val level = ref(0)
fun decrementLevel() = ()
structure SV = SemanticValues
structure MS = ModSymbol
fun msym(s) = ModSymbol.makeModSymbol([],s,s)
open SV
val evaluateString:(string -> value) ref = ref (fn _ => unitVal)
val evaluateStringFlexible:((string * value_environment ref) -> value) ref = ref (fn _ => unitVal)
val processString:((string * (Symbol.symbol list) * value_environment ref * value_environment ref) -> unit) ref = ref (fn _ => ())
val (ABaseInsert,ABaseAugment) = (ABase.insert,ABase.augment)
fun putValIntoAB(propVal(P),ab) = ABase.insert(P,ab)
| putValIntoAB(_,ab) = ab
val getSafeName = Names.getSafeName
exception EvalError of string * (A.position option)
exception PrimError of string
exception AthenaError of string
fun getSameFilePositions(stack) =
if null(stack) then stack
else let val top_pos as (_,{file=top_file,...}) = hd stack
fun loop([],res) = rev res
| loop((p as (_,{line,pos,file}))::more,res) =
if file = top_file then loop(more,p::res) else rev(res)
in
loop(tl stack,[top_pos])
end
fun builtIn(file) =
let val toks = String.tokens (fn c => let val code = Char.ord(c)
in code = 92 orelse code = 47
end)
file
in
if null(toks) then false
else let val name = hd(rev(toks))
in
Basic.isMember(name,Names.built_in_file_names)
end
end
fun makeErrorWithPosInfo(msg,SOME(pos)) = A.posToString(pos)^": Error: "^msg^"."
| makeErrorWithPosInfo(msg,_) = msg
fun evError(msg,pos_opt) = raise EvalError(msg,pos_opt)
fun primError(str) = raise PrimError(str)
fun printStack([]) = ()
| printStack(p::more) = (print("\nPOSITION:"^(A.posToString(p)));
printStack(more))
fun getWord(name) =
(case MS.find(StructureAnalysis.constructor_table,name) of
SOME({bits,...}) => bits
| _ => (case MS.find(StructureAnalysis.fsym_table,name) of
SOME(declared({bits,...})) => bits
| SOME(defined({bits,...})) => bits
| _ => evError("Symbol not found: "^MS.name(name),NONE)))
val pprintProp = let val varSortPrinter = makeVarSortPrinter()
in
fn P => P.toPrettyString(0,P,varSortPrinter)
end
val propAlphaEq = P.alEq
type pos = A.pos
fun getPos(pos_ar,i) = Array.sub(pos_ar,i)
fun getPosOpt(pos_ar,i) = SOME(getPos(pos_ar,i))
fun warning(msg,pos) = print("\nWarning, "^A.posToString(pos)^": "^msg^".\n")
fun lookUpSym(valEnv({val_map,...}),id) = Symbol.lookUp(val_map,id)
fun lookUpStr(valEnv({val_map,...}),str) = Symbol.lookUp(val_map,Symbol.symbol str)
fun updateValMap(env_ref as ref(valEnv({val_map,mod_map})),global_name,v) =
(env_ref := valEnv({val_map = Symbol.enter(val_map,global_name,v),mod_map=mod_map}))
fun updateValMapFunctional(valEnv({val_map,mod_map}),global_name,v) =
valEnv({val_map = Symbol.enter(val_map,global_name,v),mod_map=mod_map})
fun nextAvailableEvalProcName(str,eval_env,seed_opt) =
let val gsuf = Char.toString(Names.standardEvalProcNameTailCharacter)
val starting_suffix = Basic.copies(gsuf,1+(!Data.max_prime_suffix))
val inc_suffix = gsuf
val seed = (case seed_opt of SOME(w) => w | _ => "")
fun loop(suffix) = let val str' = str^suffix
in
(case (lookUpStr(!eval_env,str'),lookUpStr(!eval_env,Names.standardDeductiveEvalNamePrefix^str')) of
(SOME(_),_) => loop(inc_suffix^suffix)
| (_,SOME(_)) => loop(inc_suffix^suffix)
| _ => if str' = seed then loop(inc_suffix^suffix) else suffix)
end
in
str^(loop starting_suffix)
end
fun nextAvailableReduceProcName(str,eval_env,seed_opt) =
let val gsuf = Char.toString(Names.standardReduceProcNameTailCharacter)
val starting_suffix = Basic.copies(gsuf,1+(!Data.max_prime_suffix))
val inc_suffix = gsuf
val seed = (case seed_opt of SOME(w) => w | _ => "")
fun loop(suffix) = let val str' = str^suffix
in
(case lookUpStr(!eval_env,str') of
SOME(_) => loop(inc_suffix^suffix)
| _ => if str' = seed then loop(inc_suffix^suffix) else suffix)
end
in
str^(loop starting_suffix)
end
fun updateTopValEnv(env,global_name:symbol,value,last_value_binding) =
let fun resetName(name) = if !name = "" then name := Symbol.name(global_name) else ()
val value' = if not(last_value_binding) then
(case value of
SV.closUFunVal(body,s,env,{prec,name,...}) => resetName(name)
| SV.closBFunVal(body,s1,s2,env,{prec,name,assoc,...}) => resetName(name)
| SV.closFunVal(body,env,{prec,assoc,name,...}) => resetName(name)
| SV.closUMethodVal(body,s,env,name) => resetName(name)
| SV.closBMethodVal(body,s1,s2,env,name) => resetName(name)
| SV.closMethodVal(A.methodExp({params,body,pos,name,...}),env) => resetName(name)
| _ => ())
else ()
val name_str = Symbol.name(global_name)
val _ = (case Prop.isEvalProcName(name_str) of
SOME(f) => let val is_deductive = String.size(name_str) > 0 andalso (String.substring(name_str,0,1) = Names.standardDeductiveEvalNamePrefix)
in
(case MS.find(Prop.fsym_def_table,f) of
SOME({eval_proc_name=ep_name,eval_proc_name_with_full_mod_path=ep_name_with_mod_path,
evaluator_being_defined=ebd,occurring_syms=osyms,needed_by=nb,
obsolete_axioms=obs_axioms,red_code=red_code_opt,
code=code_opt,dcode=dcode_opt,guard_syms=gsyms,defining_equations=def_eqs,
defining_equations_finished = def_eq_finished,
bicond_sources=bslist}) =>
if !ebd = true then ()
else
(if not(ep_name = name_str orelse name_str = Names.standardDeductiveEvalNamePrefix^ep_name)
then ()
else let val ep_name' = nextAvailableEvalProcName(MS.name(f),env,SOME(name_str))
val new_full_name = (case A.splitForModules(ep_name_with_mod_path) of
([],_) => ep_name'
| (mods,_) => (Basic.printListStr(mods,fn x => x,".")) ^ "." ^ ep_name')
val d_ep_name' = Names.standardDeductiveEvalNamePrefix^ep_name'
val d_global_name = S.symbol(Names.standardDeductiveEvalNamePrefix^name_str)
val _ = MS.insert(Prop.fsym_def_table,f,{eval_proc_name=ep_name',
eval_proc_name_with_full_mod_path=new_full_name,
evaluator_being_defined=ebd,
obsolete_axioms=obs_axioms,
needed_by=nb,
guard_syms=gsyms,red_code=red_code_opt,
code=code_opt,dcode=dcode_opt,
occurring_syms=osyms,defining_equations=def_eqs,
defining_equations_finished = def_eq_finished,
bicond_sources=bslist})
val _ = HashTable.insert Prop.proc_name_table (ep_name',f)
val flip_symbol = if is_deductive then Symbol.symbol(String.substring(name_str,1,String.size(name_str)-1))
else d_global_name
val (top_val_map,top_mod_map) = getValAndModMaps(!env)
val sym = Symbol.symbol(ep_name')
val dsym = Symbol.symbol(d_ep_name')
val _ = (case (lookUpSym(!env,global_name),lookUpSym(!env,flip_symbol)) of
(SOME(closure1),SOME(closure2)) =>
if is_deductive then
let val new_vmap = Symbol.enter(top_val_map,dsym,closure1)
val new_vmap' = Symbol.enter(new_vmap,sym,closure2)
in
env := valEnv({val_map = new_vmap',mod_map=top_mod_map})
end
else
let val new_vmap = Symbol.enter(top_val_map,sym,closure1)
val new_vmap' = Symbol.enter(new_vmap,dsym,closure2)
in
env := valEnv({val_map = new_vmap',mod_map=top_mod_map})
end
| _ => ())
in
()
end)
| _ => ())
end
| _ => ())
in
updateValMap(env,global_name,value)
end
fun updateTopValEnvLst(env,global_names_and_values,last_value_binding) =
List.app (fn (name,value) => updateTopValEnv(env,name,value,last_value_binding))
global_names_and_values
fun removeTopValEnvBinding(name) =
(let val (val_map,mod_map) = getValAndModMaps(!top_val_env)
in
top_val_env := valEnv({val_map = #1(Symbol.removeBinding(val_map,name)),mod_map=mod_map})
end) handle _ => ()
fun removeEnvBinding(env,name) =
(let val (val_map,mod_map) = getValAndModMaps(!env)
in
env := valEnv({val_map = #1(Symbol.removeBinding(val_map,name)),mod_map=mod_map})
end) handle _ => ()
fun makePrivate(str) =
let val psym = Symbol.makePrivateSymbol(str)
val (top_val_map,_) = getValAndModMaps(!top_val_env)
val svalue = (case Symbol.lookUp(top_val_map,Symbol.symbol(str)) of
SOME(v) => v
| _ => (print("\nUnable to secure internal procedure names, specifically: "^str^".\n");Basic.fail("")))
val _ = updateTopValEnv(top_val_env,psym,svalue,false)
in
()
end
fun makePrivateLst(names) = List.app makePrivate names
val term_parser: value option ref = ref(NONE)
val prop_parser: value option ref = ref(NONE)
val termType = "Term"
val termLCType = "term"
val constantTermType = "Constant"
val constantTermLCType = "constant"
val atomType = "Atom"
val atomLCType = "atom"
val numTermType = "Numeric term"
val numTermLCType = "numeric term"
val varType = "Variable"
val varLCType = "variable"
val termConType = "Symbol"
val termConLCType = "symbol"
val quantType = "Quantifier"
val quantLCType = "quantifier"
val propConType = "Sentential constructor"
val propConLCType = "sentential constructor"
val propType = "Sentence"
val propLCType = "sentence"
val listType = "List"
val listLCType = "list"
val functionType = "Procedure"
val functionLCType = "procedure"
val closFunType = "Procedure"
val closFunLCType = "procedure"
val closUFunType = "Procedure"
val closUFunLCType = "procedure"
val closBFunType = "Procedure"
val closBFunLCType = "procedure"
val methodType = "Method"
val methodLCType = "method"
val closMethodType = "Method"
val closMethodLCType = "method"
val ruleType = "Method"
val ruleLCType = "method"
val subType = "Substitution"
val subLCType = "substitution"
val unitType = "Unit"
val vectorType = "Vector"
val vectorLCType = "vector"
val unitLCType = "unit"
val cellType = "Cell"
val cellLCType = "cell"
val cellType = "Cell"
val charType = "Character"
val charLCType = "character"
val stringType = "String ("^charLCType^" list)"
val stringLCType = "string ("^charLCType^" list)"
fun getType(termVal(_)) = termType
| getType(termConVal(_)) = termConType
| getType(propConVal(A.forallCon)) = quantType
| getType(propConVal(A.existsCon)) = quantType
| getType(propConVal(A.existsUniqueCon)) = quantType
| getType(propConVal(_)) = propConType
| getType(propVal(_)) = propType
| getType(listVal(_)) = listType
| getType(funVal(_)) = functionType
| getType(primUFunVal(_)) = functionType
| getType(primBFunVal(_)) = functionType
| getType(closFunVal(_)) = closFunType
| getType(closBFunVal(_)) = closBFunType
| getType(closUFunVal(_)) = closUFunType
| getType(primUMethodVal(_)) = methodType
| getType(primBMethodVal(_)) = methodType
| getType(methodVal(_)) = methodType
| getType(closUMethodVal(_)) = closMethodType
| getType(closBMethodVal(_)) = closMethodType
| getType(closMethodVal(_)) = closMethodType
| getType(subVal(_)) = subType
| getType(tableVal(_)) = tableType
| getType(mapVal(_)) = mapType
| getType(unitVal) = unitType
| getType(cellVal(_)) = cellType
| getType(charVal(_)) = charType
| getType(vectorVal(_)) = vectorType
fun getLCType(termVal(_)) = termLCType
| getLCType(termConVal(_)) = termConLCType
| getLCType(propConVal(A.forallCon)) = quantLCType
| getLCType(propConVal(A.existsCon)) = quantLCType
| getLCType(propConVal(A.existsUniqueCon)) = quantLCType
| getLCType(propConVal(_)) = propConLCType
| getLCType(propVal(_)) = propLCType
| getLCType(listVal(_)) = listLCType
| getLCType(primUFunVal(_)) = functionLCType
| getLCType(primBFunVal(_)) = functionLCType
| getLCType(funVal(_)) = functionLCType
| getLCType(closFunVal(_)) = closFunLCType
| getLCType(closUFunVal(_)) = closUFunLCType
| getLCType(closBFunVal(_)) = closBFunLCType
| getLCType(primUMethodVal(_)) = methodLCType
| getLCType(primBMethodVal(_)) = methodLCType
| getLCType(methodVal(_)) = methodLCType
| getLCType(closUMethodVal(_)) = closMethodLCType
| getLCType(closBMethodVal(_)) = closMethodLCType
| getLCType(closMethodVal(_)) = closMethodLCType
| getLCType(subVal(_)) = subLCType
| getLCType(tableVal(_)) = tableLCType
| getLCType(mapVal(_)) = mapLCType
| getLCType(unitVal) = unitLCType
| getLCType(vectorVal(_)) = vectorLCType
| getLCType(cellVal(_)) = cellLCType
| getLCType(charVal(_)) = charLCType
val makeNewPWId =
let val pw_id_index = ref(0)
in
fn () => (Basic.inc(pw_id_index);"!9"^(Int.toString(!pw_id_index)))
end
fun updateTopValEnvLstOldAndFast(name_val_list) =
let val (top_val_map,top_mod_map) = getValAndModMaps(!top_val_env)
fun loop([],vm) = vm
| loop((name,value)::rest,vm) = loop(rest,Symbol.enter(vm,name,value))
val vm = loop(name_val_list,top_val_map)
in
top_val_env := valEnv({val_map = vm,mod_map=top_mod_map})
end
val true_term = AT.true_term
val false_term = AT.false_term
val true_val = termVal(true_term)
val false_val = termVal(false_term)
fun MLBoolToAthBoolVal(true) = true_val
| MLBoolToAthBoolVal(false) = false_val
val MLBoolToAth = MLBoolToAthBoolVal
fun isStringVal(listVal(vals)) = List.all (fn v => (case v of charVal(_) => true | _ => false)) vals
| isStringVal(_) = false
fun MLStringToAthString(str) =
let val int_lst = map ord (explode(str))
in
listVal(map charVal int_lst)
end
fun MLNumStringToAthString(str) =
let val int_lst = map ord (explode(str))
val new_int_lst = (case int_lst of
126::more => 45::more
| _ => int_lst)
in
listVal(map charVal new_int_lst)
end
fun AthStringToMLString(listVal(vals)) =
let val char_list = map (fn v => (case v of charVal(i) => chr(i) | _ =>
genError("String conversion error",NONE))) vals
in
implode(char_list)
end
| AthStringToMLString(_) = genError("String conversion error",NONE)
fun isStringValConstructive(v) = SOME(AthStringToMLString(v)) handle _ => NONE
fun charValToString(c) =
(case c of
0 => "^@"
| 1 => "^A"
| 2 => "^B"
| 3 => "^C"
| 4 => "^D"
| 5 => "^E"
| 6 => "^F"
| 7 => "\\a"
| 8 => "\\b"
| 9 => "\\t"
| 10 => "\\n"
| 11 => "\\v"
| 12 => "\\f"
| 13 => "\\r"
| 14 => "^N"
| 15 => "^O"
| 16 => "^P"
| 17 => "^Q"
| 18 => "^R"
| 19 => "^S"
| 20 => "^T"
| 21 => "^U"
| 22 => "^V"
| 23 => "^W"
| 24 => "^X"
| 25 => "^Y"
| 26 => "^Z"
| 27 => "^["
| 28 => "^/"
| 29 => "^]"
| 30 => "^^"
| 31 => "^_"
| 32 => "\\blank"
| _ => (if c < 127 then Char.toString(chr(c)) else
if c = 127 then "\\127" else
genError("Illegal character code passed to character output procedure",NONE)))
fun stringValToString([]) = ""
| stringValToString(c::more) = charValToString(c)^stringValToString(more)
fun printStringVal([]) = ()
| printStringVal(charVal(10)::more) = (print("\n");printStringVal(more))
| printStringVal(charVal(9)::more) = (print("\t");printStringVal(more))
| printStringVal(charVal(32)::more) = (print(" ");printStringVal(more))
| printStringVal(charVal(c)::more) = (print(charValToString(c));printStringVal(more))
| printStringVal(_::more) = genError("Wrong kind of argument encountered in the "^
" list given to print",NONE)
fun valTypeAndString(v) = getType(v)^": "^valToString(v)
fun prettyValToStringNoTypes(propVal(P)) =
let val x = !Options.print_var_sorts
val _ = Options.print_var_sorts := false
val res = P.toPrettyString(0,P,makeVarSortPrinter())
val _ = Options.print_var_sorts := x
in
"\n"^res^"\n"
end
| prettyValToStringNoTypes(termVal(t)) =
let val x = !Options.print_var_sorts
val _ = Options.print_var_sorts := false
val res = AT.toPrettyString(0,t,makeVarSortPrinter())
val _ = Options.print_var_sorts := x
in
if AT.height(t) > 0 then "\n"^res^"\n" else res
end
| prettyValToStringNoTypes(listVal(vals)) = "["^prettyValsToStringNoTypes(vals)^"]"
| prettyValToStringNoTypes(v) = valToString(v)
and
prettyValsToStringNoTypes([]) = ""
| prettyValsToStringNoTypes([v]) = prettyValToStringNoTypes(v)
| prettyValsToStringNoTypes(v1::v2::more) = prettyValToStringNoTypes(v1)^" "^prettyValsToStringNoTypes(v2::more)
fun printVal(v) =
let val sortVarPrinter = makeVarSortPrinter()
fun f(v as subVal(sub)) = (print("\n");print(subType^": ");Terms.printSub(sub);print("\n"))
| f(v as propVal(P)) = (print("\n");print(propType^": "^
P.toPrettyString(String.size(propType)+2,P,sortVarPrinter));
print("\n"))
| f(v as termVal(t)) = (print("\n");print(termType^": "^
AT.toPrettyStringDefault(String.size(termType)+2,t));
print("\n"))
| f(v as tableVal(tab)) = (print("\n");print(tableType^": "^(tableToString tab));print("\n"))
| f(v as mapVal(m)) = (print("\n");print(mapType^": "^(mapToString m));print("\n"))
| f(v) = (print("\n"^getType(v));
let val vs = (case v of listVal(_) => prettyValToStringNoTypes(v) | _ => valToString(v))
in
if vs = "" then print("\n") else
(print(": ");print(vs);print("\n"))
end)
in
f v
end
fun printValNoType(v as subVal(sub)) = Terms.printSub(sub)
| printValNoType(v as propVal(P)) = print(P.toPrettyString(0,P,makeVarSortPrinter()))
| printValNoType(v as termVal(t)) = print(AT.toPrettyStringDefault(0,t))
| printValNoType(v as tableVal(t)) = print(tableToString t)
| printValNoType(v as mapVal(m)) = print(mapToString m)
| printValNoType(v) =
let val vs = valToString(v)
in
if vs = "" then print("\n") else
print(vs)
end
fun printValWithTypedStrings(v as subVal(sub)) = (print(subType^": ");Terms.printSub(sub);print("\n"))
| printValWithTypedStrings(v) =
(print(getType(v));
let val vs = valToString(v)
in
if vs = "" then print("\n") else
(print(": ");print(vs);print("\n"))
end)
fun valLCTypeAndString(v) = let val str = prettyValToString(v)
in
if Util.small(str,20) then getLCType(v)^": "^str
else getLCType(v)^":\n"^str
end
fun valTypeAndStringNoColon(v) = let val str = prettyValToString(v)
in
if Util.small(str,20) then getType(v)^": "^str
else getType(v)^":\n"^str
end
fun valLCTypeAndStringNoColon(v) = let val str = prettyValToString(v)
in
if Util.small(str,20) then getLCType(v)^" "^str
else getLCType(v)^"\n"^str
end
fun ordinal(i) = let fun suffix(1) = "st"
| suffix(2) = "nd"
| suffix(3) = "rd"
| suffix(_) = "th"
in
(case i of
1 => "first"
| 2 => "second"
| 3 => "third"
| 4 => "fourth"
| 5 => "fifth"
| 6 => "sixth"
| 7 => "seventh"
| 8 => "eighth"
| 9 => "ninth"
| 10 => "tenth"
| _ => if (((i mod 100) = 11) orelse ((i mod 100) = 12)) then Int.toString(i)^"th"
else Int.toString(i)^suffix(i mod 10))
end
val try_space = " "
val try_space_chars = String.explode(try_space)
fun processTryMessage(str) =
let val blank = #" "
fun f(c) = if c = #"\n" then c::try_space_chars else [c]
in
String.implode(Basic.flatten(List.map f (String.explode(str))))
end
fun desugarPW(A.pickWitnessesDed({ex_gen,var_ids as [vid],inst_id,body=main_body,pos}),env,ab,ev) =
A.pickWitnessDed({ex_gen=ex_gen,var_id=vid,inst_id=inst_id,body=main_body,pos=pos})
| desugarPW(A.pickWitnessesDed({ex_gen,var_ids as vid::more_vars,inst_id,body=main_body,pos}),env,ab,ev) =
let val unique_name = Symbol.symbol(makeNewPWId())
val ded1 = A.pickWitnessesDed(
{ex_gen=A.exp(A.idExp({msym=msym unique_name,mods=[],sym=unique_name,no_mods=true,pos=A.dum_pos})),var_ids=more_vars,inst_id=inst_id,
body=main_body,pos=pos})
val ds1 = desugarPW(ded1,env,ab,ev)
in
A.pickWitnessDed({ex_gen=ex_gen,var_id=vid,inst_id=SOME(unique_name),
body=ds1,pos=pos})
end
| desugarPW(d,_,_,_) = d
fun dexpect(str1,P) = str1^" was expected, but the given sentence was \n"^P.propKind(P)^": "^
Util.decide(P.toPrettyString(0,P,makeVarSortPrinter()),50)
fun dwrongArgKind(name,i,str1,P) = "Wrong kind of sentence given as "^ordinal(i)^" argument\nto "^name^
"---"^dexpect(str1,P)
fun expect(str,v) = let val vstr = valLCTypeAndString(v)
in
"a "^str^" was expected,\nbut the given argument was a "^Util.decide(vstr,20)
end
fun expectInOneLine(str,v) = "a "^str^" was expected, but the given value was a "^valLCTypeAndString(v)
fun expectUC(str,v) = "A "^str^" was expected, but the given value was a "^valLCTypeAndString(v)
fun termExpect(str,t) = "a "^str^" was expected, but the given term was of sort\n"^
(F.toStringDefault(AT.getSort(t)))
fun expectExplain(str,expl,v) = "a "^str^" "^expl^" was expected,\nbut the given value "^
"was a "^valLCTypeAndString(v)
fun wrongTermKind(name,i,str,t) = "Wrong sort of term given as "^ordinal(i)^" argument\nto "^name^
"---"^termExpect(str,t)
fun wrongArgKindGeneric(name,str) =
"Wrong kind of value given as argument to "^name^"---a "^str^" was expected"
fun wrongArgKind(name,i,str,v) = "Wrong kind of value given as "^ordinal(i)^" argument\nto "^name^"---"^expect(str,v)
fun wrongArgKindExpectationOnly(str,v) = "Wrong kind of value found here---"^expect(str,v)
fun wrongArgKindInOneLine(name,i,str,v) = "Wrong kind of value found here---"^expectInOneLine(str,v)
fun argNumToString(i) = if i = 0 then "zero arguments are" else if i = 1 then "one argument is" else
Int.toString(i)^" arguments are"
fun wrongArgNumber(name,given,required) = "Wrong number of arguments ("^Int.toString(given)^") given\nto "^
name^"---exactly "^argNumToString(required)^" required"
fun wrongArgNumberFlex(name,given,nums) =
"Wrong number of arguments ("^Int.toString(given)^") given to "^
name^"---either "^(Basic.printListStr(nums,Int.toString," or "))^" arguments are required"
fun propLstToAthPropLst(props) = listVal(map propVal props)
fun athTermVarToAth(v) = termVal(AT.makeVar(v))
fun listOfAthVarsToAth(l) = listVal(map athTermVarToAth l)
fun makeTermConVal(x as regFSym(some_fsym)) =
let val (name,arity) = (D.fsymName(some_fsym),D.fsymArity(some_fsym))
in
if (arity > 0) then termConVal(x)
else termVal(AT.makeConstant(name))
end
fun makeTermConValFromStructureConstructor(acon as {name,arity:int,range_type,bits:Word8.word,...}:D.ath_struc_constructor) =
if (arity > 0) then termConVal(regFSym(D.struc_con(acon)))
else
let val poly = Data.isPolyWord(bits)
in
termVal(AT.makeSortedConstantAux(name,range_type,poly))
end
fun coerceTermConValIntoTermVal(v) = (case coerceFunSymbolIntoTerm(v) of
SOME(t) => SOME(termVal(t))
| _ => NONE)
fun coerceTermIntoTermConVal(t) = (case coerceTermIntoTermCon(t) of
SOME(fsym) => SOME(termConVal(fsym))
| _ => NONE)
fun coerceTermValIntoTermConVal(termVal(t)) = coerceTermIntoTermConVal(t)
| coerceTermValIntoTermConVal(v as termConVal(_)) = SOME(v)
| coerceTermValIntoTermConVal(v as propVal(P)) =
(case P.isAtom(P) of
SOME(t) => coerceTermIntoTermConVal(t)
| _ => NONE)
| coerceTermValIntoTermConVal(_) = NONE
fun isGeneralApp(t) =
(case AT.isApp(t) of
SOME(fsym,args) => (case D.isTermConstructorAsFSymOption(fsym) of
SOME(afunsym) => SOME(regFSym afunsym,args)
| _ => NONE)
| _ => (case AT.isNumConstantOpt(t) of
SOME(anum) => SOME(athNumFSym(anum),[])
| _ => (case AT.isIdeConstant(t) of
SOME(str) => SOME(metaIdFSym(str),[])
| _ => NONE)))
fun applyTermConstructor(name,arity,term_args,pos) =
AT.makeApp(name,term_args)
handle Basic.FailLst(msgs) => evError(Basic.failMsgsToStr(msgs),SOME(pos))
fun applyBinaryTermConstructor(name,term_arg_1,term_arg_2,pos) =
(AT.makeAppBinary(name,term_arg_1,term_arg_2)
handle Basic.FailLst(msgs) =>
(let val msg = Basic.failMsgsToStr(msgs)
in
evError(msg,SOME(pos))
end))
fun applyUnaryTermConstructor(name,term_arg,pos) =
AT.makeAppUnary(name,term_arg)
handle Basic.FailLst(msgs) => evError(Basic.failMsgsToStr(msgs),SOME(pos))
fun applyTermConstructorNoPos(name,term_args) =
AT.makeApp(name,term_args)
handle Basic.FailLst(msgs) => primError(Basic.failMsgsToStr(msgs))
fun isBooleanTerm(t) = (case AT.isApp(t) of
SOME(fsym,_) => hasBooleanRangeType(fsym)
| _ => (case AT.isVarOpt(t) of
SOME(_) => true
| _ => false))
fun coerceValIntoTerm(termVal(t)) = SOME(t)
| coerceValIntoTerm(v as termConVal(_)) =
(case coerceFunSymbolIntoTerm(v) of
SOME(t) => SOME(t)
| _ => NONE)
| coerceValIntoTerm(propVal(P)) = P.isAtom(P)
| coerceValIntoTerm(_) = NONE
fun coerceValIntoTermCon(v as termConVal(tc)) = SOME(tc)
| coerceValIntoTermCon(v as termVal(t)) = coerceTermIntoTermCon(t)
| coerceValIntoTermCon(v as propVal(P)) = (case P.isAtom(P) of
SOME(t) => coerceTermIntoTermCon(t)
| _ => NONE)
| coerceValIntoTermCon(_) = NONE
fun coerceValIntoPropVal(v) = (case coerceValIntoProp(v) of
SOME(P) => SOME(propVal(P))
| _ => NONE)
fun coerceValIntoTermVal(v) = case coerceValIntoTerm(v) of
SOME(t) => SOME(termVal(t))
| _ => NONE
fun coerceValIntoTermConVal(v) = case coerceValIntoTermCon(v) of
SOME(tc) => SOME(termConVal(tc))
| _ => NONE
fun isApplicable(closFunVal(_)) = (true,false)
| isApplicable(closUFunVal(_)) = (true,false)
| isApplicable(closBFunVal(_)) = (true,false)
| isApplicable(funVal(_)) = (true,false)
| isApplicable(primUFunVal(_)) = (true,false)
| isApplicable(primBFunVal(_)) = (true,false)
| isApplicable(propConVal(_)) = (true,false)
| isApplicable(subVal(_)) = (true,false)
| isApplicable(v) = (case coerceValIntoTermCon(v) of
SOME(regFSym(some_fsym)) => let val arity = D.fsymArity some_fsym in (arity > 0,arity = 0) end
| _ => (false,false))
fun appSubToTerm(sub,t,pos) = termVal(AT.applySub(sub,t))
fun appSubToTermNoPos(sub,t) = termVal(AT.applySub(sub,t))
fun appSubToProp(sub,p,pos) = propVal(P.applySub(sub,p))
fun appSubToPropNoPos(sub,p) = propVal(P.applySub(sub,p))
fun applySubToValPosLst(sub,arg_val_pos_lst) =
let fun applySub(arg_val_pos_lst) =
let fun f([],res) = rev(res)
| f((v,pos)::more,res) =
(case coerceValIntoTerm(v) of
SOME(t) => f(more,appSubToTerm(sub,t,pos)::res)
| _ => (case coerceValIntoProp(v) of
SOME(P) => f(more,appSubToProp(sub,P,pos)::res)
| _ => evError("Wrong kind of value found in the argument list of "^
"a substitution application; "^expect(termLCType,v),SOME(pos))))
in
listVal(f(arg_val_pos_lst,[]))
end
in
applySub(arg_val_pos_lst)
end
fun applySubToValLst(sub,arg_vals,pos) =
let fun applySub(arg_val_lst) =
let fun f([],res) = rev(res)
| f(v::more,res) =
(case coerceValIntoTerm(v) of
SOME(t) => f(more,appSubToTerm(sub,t,pos)::res)
| _ => (case coerceValIntoProp(v) of
SOME(P) => f(more,appSubToProp(sub,P,pos)::res)
| _ => evError("Wrong kind of value found in the argument list of "^
"a substitution application; "^expect(termLCType,v),SOME(pos))))
in
listVal(f(arg_val_lst,[]))
end
in
applySub(arg_vals)
end
fun applySubToValLstNoPos(sub,arg_vals:value list) =
let fun applySub(arg_val_lst) =
let fun f([],res) = rev(res)
| f(v::more,res) =
(case coerceValIntoTerm(v) of
SOME(t) => f(more,appSubToTermNoPos(sub,t)::res)
| _ => (case coerceValIntoProp(v) of
SOME(P) => f(more,appSubToPropNoPos(sub,P)::res)
| _ => evError("Wrong kind of value found in the argument list of "^
"a substitution application; "^expect(termLCType,v),NONE)))
in
listVal(f(arg_val_lst,[]))
end
in
applySub(arg_vals)
end
fun coerceValIntoProps(tvals) = map coerceValIntoProp tvals
val coerceProp = P.isAtom
fun coercibleTermVal(v) = (case coerceValIntoProp(v) of NONE => false | _ => true)
fun coercibleProp(p) = (case coerceProp(p) of NONE => false | _ => true)
val (isTrueTerm,isFalseTerm) = (AT.isTrueTerm,AT.isFalseTerm)
fun isTrueTermFromProp(p) =
(case P.isAtom(p) of
SOME(t) => isTrueTerm(t)
| _ => false)
fun isFalseTermFromProp(p) =
(case P.isAtom(p) of
SOME(t) => isFalseTerm(t)
| _ => false)
fun isFalseTerm(t) = (case AT.isConstant(t) of SOME(fsym) => MS.modSymEq(fsym,N.mfalse_logical_symbol)
| _ => false)
fun getBool(v) = case coerceValIntoTerm(v) of
SOME(t) => if isTrueTerm(t) then SOME(true)
else if isFalseTerm(t) then SOME(false)
else NONE
| _ => NONE
fun logError("and",pos,v) =
evError("Non-boolean term given to the short-circuit 'and' operator &&, namely: " ^ valToString(v),SOME(pos))
| logError("or",pos,v) =
evError("Non-boolean term given to the short-circuit 'or' operator &&",SOME(pos))
| logError(_) = raise Basic.Never
fun valEq(termVal(s),v) = (case coerceValIntoTerm(v) of
SOME(t) => AT.termEq(s,t)
| _ => false)
| valEq(propVal(P),v) = (case coerceValIntoProp(v) of
SOME(Q) => P.alEq(P,Q)
| _ => false)
| valEq(termConVal(fsym),v) = (case coerceValIntoTermCon(v) of
SOME(fsym') => funSymEq(fsym,fsym')
| _ => false)
| valEq(propConVal(con1),propConVal(con2)) = (con1 = con2)
| valEq(listVal(vl1),listVal(vl2)) = valEqLst(vl1,vl2)
| valEq(unitVal,unitVal) = true
| valEq(vectorVal(v1),vectorVal(v2)) = (v1 = v2)
| valEq(cellVal(v1),cellVal(v2)) = (v1 = v2)
| valEq(charVal(i),charVal(j)) = (i = j)
| valEq(subVal(sub1),subVal(sub2)) = AT.subEq(sub1,sub2)
| valEq(tableVal(tab1),tableVal(tab2)) = tableEq(tab1,tab2)
| valEq(mapVal(m1),mapVal(m2)) = mapEq(m1,m2)
| valEq(_,_) = false
and
valEqLst([],[]) = true
| valEqLst(v1::more1,v2::more2) = if valEq(v1,v2) then valEqLst(more1,more2) else false
| valEqLst(_,_) = false
and tableEq(t1,t2) =
let val dom1 = map (fn (x,_) => x) (HashTable.listItemsi t1)
val dom2 = map (fn (x,_) => x) (HashTable.listItemsi t2)
fun sameVal(key) = (case ((HashTable.find t1 key),(HashTable.find t2 key)) of
(SOME(v1),SOME(v2)) => valEq(v1,v2)
| _ => false)
in
(List.all sameVal dom1) andalso (List.all sameVal dom2)
end
and mapEq(t1,t2) =
let val dom1 = map (fn (x,_) => x) (Maps.listKeyValuePairs t1)
val dom2 = map (fn (x,_) => x) (Maps.listKeyValuePairs t2)
fun sameVal(key) = (case (Maps.find(t1,key),Maps.find(t2,key)) of
(SOME(v1),SOME(v2)) => valEq(v1,v2)
| _ => false)
in
(List.all sameVal dom1) andalso (List.all sameVal dom2)
end
(*** Handling of structure equality: **********)
fun strucValEq0(t1,t2) =
(case FTerm.isApp(AT.getSort(t1)) of
SOME(struc_name,sort_args) =>
if isNonFreeDT(struc_name) then
(case MS.find(Prop.structure_equality_table,struc_name) of
SOME(f) => let val p = f(t1,t2)
val evalString = !evaluateString
val pstr = Prop.toStringDefault(p)
val eval1_safe_name = getSafeName("eval1")
val str = "(" ^ eval1_safe_name ^ " " ^ pstr ^ ")"
val res = evalString(str)
in
(case res of
termVal(t) => isTrueTerm(t)
| _ => (print("\nHere we are!\n");raise Basic.Never))
end
| _ => AT.termEq(t1,t2))
else genTermEq(t1,t2)
| _ => AT.termEq(t1,t2))
and strucValEqLst([],[]) = true
| strucValEqLst(t1::more1,t2::more2) = strucValEq0(t1,t2) andalso strucValEqLst(more1,more2)
| strucValEqLst(_) = false
and genTermEq(t1,t2) =
(case (AT.isApp(t1),AT.isApp(t2)) of
(SOME(f1,args1),SOME(f2,args2)) =>
if null(args1) orelse null(args2) then AT.termEq(t1,t2)
else MS.modSymEq(f1,f2) andalso strucValEqLst(args1,args2)
| _ => AT.termEq(t1,t2))
fun strucValEq(v1 as (termVal(t1)),v2 as (termVal(t2))) = strucValEq0(t1,t2)
| strucValEq(v1,v2) = valEq(v1,v2)
fun env2HTVal(valEnv({val_map,mod_map})) =
let val ht1: val_hash_table = HashTable.mkTable(SemanticValues.hashVal, valEq) (!(Options.default_table_size),Data.GenEx("Failed table creation"))
val ht2: val_hash_table = HashTable.mkTable(SemanticValues.hashVal, valEq) (!(Options.default_table_size),Data.GenEx("Failed table creation"))
fun makeValEntry(pair as (name_sym,value)) =
let val name_str = Symbol.name(name_sym)
in
HashTable.insert ht1 (MLStringToAthString(name_str),value)
end
fun makeModEntry(name_sym,m:module as {env,...}) =
let val name_str = Symbol.name(name_sym)
in
HashTable.insert ht2 (MLStringToAthString(name_str),env2HTVal(env))
end
val _ = List.app makeValEntry (Symbol.listSymbolsAndImages val_map)
val _ = List.app makeModEntry (Symbol.listSymbolsAndImages mod_map)
in
listVal([tableVal(ht1),tableVal(ht2)])
end
datatype term_num = inum of int | rnum of real
local exception Ex in
fun areStringVals(vals) =
let fun f([],strings) = rev(strings)
| f(v::more,strings) = (case isStringValConstructive(v) of
SOME(str) => f(more,str::strings)
| _ => raise Ex)
in
SOME(f(vals,[])) handle Ex => NONE
end
end
fun withAtom(P,f,sort_sub) =
(case P.isAtom(P) of
SOME(t) => SOME(f t,sort_sub)
| _ => NONE)
fun makeQuantProp(SOME(A.forallCon),vars,body) = P.makeUGen(vars,body)
| makeQuantProp(SOME(A.existsCon),vars,body) = P.makeEGen(vars,body)
| makeQuantProp(SOME(A.existsUniqueCon),vars,body) = P.makeEGenUnique(vars,body)
| makeQuantProp(_,_,P) = P
(***************************************************************************************
I N T E R P R E T E R
***************************************************************************************)
fun isBinaryProp(P) =
(case P.isConj(P) of
SOME(props) => SOME(propConVal(A.andCon),map propVal props)
| _ => (case P.isDisj(P) of
SOME(props) => SOME(propConVal(A.orCon),map propVal props)
| _ => (case P.isCond(P) of
SOME(p1,p2) => SOME(propConVal(A.ifCon),[propVal(p1),propVal(p2)])
| _ => (case P.isBiCond(P) of
SOME(p1,p2) => SOME(propConVal(A.iffCon),[propVal(p1),propVal(p2)])
| _ => NONE))))
fun isQuant(P) =
(case P.isUGen(P) of
SOME(v,p) => SOME(propConVal(A.forallCon),termVal(AT.makeVar(v)),propVal(p))
| _ => (case P.isEGen(P) of
SOME(v,p) => SOME(propConVal(A.existsCon),termVal(AT.makeVar(v)),propVal(p))
| _ => (case P.isEGenUnique(P) of
SOME(v,p) => SOME(propConVal(A.existsUniqueCon),termVal(AT.makeVar(v)),propVal(p))
| _ => NONE)))
fun patStringEq(v,int_lst) =
let fun f([],[]) = true
| f(charVal(i)::more1,j::more2) = if i = j then f(more1,more2) else false
| f(_,_) = false
in
case v of
listVal(vals) => f(vals,int_lst)
| _ => false
end
fun unifySorts(arg_sort,param_sort) =
let val U = F.chooseUnificationProcedureForSorts(param_sort,arg_sort)
val res = SOME(U([arg_sort],[param_sort],~1)) handle _ => NONE
in
res
end
fun consolidateSubs(init_sub,dom1,val_map,dom2,list_val_map) =
let val sub' = Symbol.augment(init_sub,val_map)
fun loop([],m) = m
| loop(s::rest,m) =
let val values = (case Symbol.lookUp(list_val_map,s) of
SOME(v) => v)
in