-
Notifications
You must be signed in to change notification settings - Fork 4
/
semant_val.sml
executable file
·1931 lines (1814 loc) · 96.8 KB
/
semant_val.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 definition of Athena's semantic values and associated code. This file
also contains the AST preprocessor used before evaluation.
=======================================================================*)
structure SemanticValues =
struct
structure S = Symbol
structure A = AbstractSyntax
structure Ab = ABase
structure B = Basic
structure N = Names
structure F = FTerm
structure Ab = ABase
structure Pos = Position
structure P = Prop
structure D = Data
structure ATV = AthTermVar
structure AT = AthTerm
structure MS = ModSymbol
type pos = A.pos
datatype fun_sym = regFSym of Data.ath_fun_sym
| athNumFSym of A.ath_number
| metaIdFSym of string
fun hashFunSym(regFSym(f)) = Data.hashAthFunSym(f)
| hashFunSym(athNumFSym(n)) = A.hashANum(n)
| hashFunSym(metaIdFSym(str)) = Basic.hashString(str)
fun funSymPrec(regFSym(Data.declared({prec,...}))) = prec
| funSymPrec(regFSym(Data.defined({prec,...}))) = prec
| funSymPrec(_) = ref(0)
fun funSymName(regFSym(f)) = MS.name(Data.fsymName(f))
| funSymName(athNumFSym(n)) = A.athNumberToString(n)
| funSymName(metaIdFSym(str)) = str
fun funSymAssoc(regFSym(Data.declared({assoc,...}))) = assoc
| funSymAssoc(_) = ref(NONE)
fun funSymEq(regFSym(sym1),regFSym(sym2)) = MS.modSymEq(Data.fsymName(sym1),Data.fsymName(sym2)) andalso
(Data.fsymArity(sym1) = Data.fsymArity(sym2))
| funSymEq(metaIdFSym(str1),metaIdFSym(str2)) = (str1 = str2)
| funSymEq(athNumFSym(anum1),athNumFSym(anum2)) = A.athNumEquality(anum1,anum2)
| funSymEq(_,_) = false
type method_code = Symbol.symbol
datatype value = termVal of AT.term |
charVal of int |
termConVal of fun_sym |
propConVal of A.prop_con |
propVal of Prop.prop |
cellVal of value ref |
listVal of value list |
vectorVal of value array |
primUFunVal of ((value * value_environment ref * Ab.assum_base) -> value) * {prec:int ref,name:string} |
primBFunVal of ((value * value * value_environment ref * Ab.assum_base) -> value) * {prec:int ref,assoc:bool option ref,name:string} |
funVal of prim_fun_type * string * {arity:int,prec:int ref, assoc: bool option ref} |
nprimUFunVal of ((value * Ab.assum_base) -> value) * {prec:int ref,name:string} |
nprimBFunVal of ((value * value * Ab.assum_base) -> value) * {prec:int ref,assoc:bool option ref,name:string} |
nfunVal of nprim_fun_type * string * {arity:int,prec:int ref, assoc: bool option ref} |
closFunVal of A.expression * value_environment ref * {prec:int ref, assoc:bool option ref,name:string ref} |
closUFunVal of A.expression * S.symbol * value_environment ref * {name:string ref,prec:int ref} |
closBFunVal of A.expression * S.symbol * S.symbol * value_environment ref * {name:string ref,prec:int ref, assoc:bool option ref} |
primUMethodVal of ((value * value_environment ref * Ab.assum_base) -> value) * method_code |
primBMethodVal of ((value * value * value_environment ref * Ab.assum_base) -> value) * method_code |
methodVal of ((value list * value_environment ref * Ab.assum_base) -> value) * method_code |
closMethodVal of A.expression * value_environment ref |
closUMethodVal of A.deduction * S.symbol * value_environment ref * string ref |
closBMethodVal of A.deduction * S.symbol * S.symbol * value_environment ref * string ref |
subVal of AT.sub |
mapVal of (value,value) Maps.mapping |
tableVal of val_hash_table |
unitVal
and
value_environment = valEnv of {val_map:value Symbol.mapping,mod_map:module Symbol.mapping}
withtype module = {mod_name:ModSymbol.mod_symbol, env: value_environment, open_mod_directives: S.symbol list list}
and prim_fun_type = value list * value_environment ref * Ab.assum_base -> value
and nprim_fun_type = value list * Ab.assum_base -> value
and val_hash_table = (value,value) HashTable.hash_table
fun recurse f (listVal vals) = listVal(List.map (recurse f) vals)
| recurse f (vectorVal V) = let val L = Basic.arrayToList(V)
in
vectorVal(Array.fromList (List.map (recurse f) L))
end
| recurse f (cellVal(ref(v))) = cellVal(ref((recurse f) v))
| recurse f v = f v
fun default_fv_pa(a) = {arity=a,prec=ref(Options.defaultPrec(a)),assoc=ref(NONE:bool option)}
fun default_ufv_pa(n) = {prec=ref(Options.lowest_fsymbol_precedence),name=n}
fun default_bfv_pa(n) = {prec=ref(Options.lowest_fsymbol_precedence),assoc=ref(NONE:bool option),name=n}
fun default_ufv_pa_for_procs(n) = {prec=ref(Options.lowest_fsymbol_precedence),name=n}
fun default_bfv_pa_for_procs(n) = {prec=ref(Options.lowest_fsymbol_precedence),assoc=ref(NONE:bool option),name=n}
val default_fv_pa_for_procs = default_fv_pa
fun getClosureArity(closFunVal(A.functionExp({params,...}),_,_)) = List.length(params)
| getClosureArity(closUFunVal(_)) = 1
| getClosureArity(closBFunVal(_)) = 2
fun getMethodClosureArity(A.methodExp({params,...})) = List.length(params)
fun makeVarSortPrinter() = F.varToString
val termType = "Term"
val termLCType = "term"
val metaIdType = "Meta-identifier"
val metaIdLCType = "meta-identifier"
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 closBFunType = "Procedure"
val closBFunLCType = "procedure"
val closUFunType = "Procedure"
val closUFunLCType = "procedure"
val closFunType = "Procedure"
val closFunLCType = "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 tableType = "Table"
val mapType = "Map"
val mapLCType = "map"
val tableLCType = "table"
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(primBFunVal(_)) = functionType
| getType(closFunVal(_)) = closFunType
| getType(closUFunVal(_)) = closUFunType
| getType(closBFunVal(_)) = closBFunType
| getType(methodVal(_)) = methodType
| getType(primUMethodVal(_)) = methodType
| getType(primBMethodVal(_)) = methodType
| getType(closUMethodVal(_)) = closMethodType
| getType(closBMethodVal(_)) = closMethodType
| getType(closMethodVal(_)) = closMethodType
| getType(subVal(_)) = subType
| getType(unitVal) = unitType
| getType(cellVal(_)) = cellType
| getType(charVal(_)) = charType
| getType(tableVal(_)) = tableType
| getType(mapVal(_)) = mapType
| 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(funVal(_)) = functionLCType
| getLCType(primBFunVal(_)) = 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
exception GenEx of string * (AbstractSyntax.pos option)
fun genError(msg,pos_opt) = (raise GenEx(msg,pos_opt))
val true_term = AT.true_term
val false_term = AT.false_term
val true_val = termVal(true_term)
val false_val = termVal(false_term)
val true_prop_val = propVal(Prop.true_prop)
val false_prop_val = propVal(Prop.false_prop)
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 isClosMethod(closMethodVal(_)) = true
| isClosMethod(closUMethodVal(_)) = true
| isClosMethod(closBMethodVal(_)) = true
| isClosMethod(_) = false
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 function",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 valToString(v as termVal(_)) = prettyValToString(v)
| valToString(termConVal(regFSym(some_fsym))) = MS.name(D.fsymName(some_fsym))
| valToString(termConVal(athNumFSym(anum))) = A.athNumberToString(anum)
| valToString(termConVal(metaIdFSym(str))) = N.metaIdPrefix^str
| valToString(propConVal(con)) = A.propConToString(con)
| valToString(v as propVal(_)) = "\n"^(prettyValToString(v))^"\n"
| valToString(funVal(_,name,_)) = name
| valToString(primUFunVal(_,{name,...})) = name ^ " (primitive)"
| valToString(primBFunVal(_,{name,...})) = name ^ " (primitive)"
| valToString(closFunVal(A.functionExp({pos,...}),_,{name,...})) =
let val name' = !name
val pos_info = (A.posToString pos)
in
(name')^(if pos_info = "" then "" else
" (defined at "^(A.posToString pos)^")")
end
| valToString(closFunVal(_)) = ""
| valToString(closUFunVal(e,_,_,{name,...})) =
let val pos_info = (A.posToString (A.posOfExp e))
in
(if pos_info = "" then (!name) ^ "(defined procedure)"
else (!name) ^ " (defined at "^pos_info^")")
end
| valToString(closBFunVal(e,_,_,_,{name,...})) =
let val pos_info = (A.posToString (A.posOfExp e))
in
(if pos_info = "" then (!name) ^ " (defined procedure)"
else (!name) ^ " (defined at "^pos_info^")")
end
| valToString(methodVal(_,code)) = (S.name code) ^ " (primitive)"
| valToString(primUMethodVal(_,code)) = (S.name code) ^ " (primitive)"
| valToString(primBMethodVal(_,code)) = (S.name code) ^ " (primitive)"
| valToString(closUMethodVal(d,_,_,name)) =
let val pos_info = (A.posToString (A.posOfDed d))
in
(if pos_info = "" then (!name) else (!name) ^ " (defined at "^pos_info^")")
end
| valToString(closBMethodVal(d,_,_,_,name)) =
let val pos_info = (A.posToString (A.posOfDed d))
in
(if pos_info = "" then (!name) else (!name) ^ " (defined at "^pos_info^")")
end
| valToString(closMethodVal(A.methodExp({name,pos,...}),_)) =
let val pos_info = (A.posToString pos)
in
(!name)^(if pos_info = "" then "" else " (defined at "^pos_info^")")
end
| valToString(listVal(vals)) = "["^valsToStrings(vals)^"]"
| valToString(vectorVal(val_vec)) = "|"^valsToStrings(Basic.arrayToList(val_vec))^"|"
| valToString(subVal(sub)) = Terms.subToString(sub)
| valToString(unitVal) = "()"
| valToString(cellVal(vr)) = "cell"
| valToString(charVal(i)) = "`"^charValToString(i)
| valToString(tableVal(tab)) = "|[" ^ (tableToString tab) ^ "]|"
| valToString(mapVal(m)) = mapToString(m)
| valToString(_) = "Not sure how to print this value...\n"
and valToStringUnlimited(listVal(vals)) = "["^allValsToStrings(vals)^"]"
| valToStringUnlimited(v) = valToString(v)
and prettyValToStringWithSpacesUnlimited(v,spaces) =
let val varSortPrinter = makeVarSortPrinter()
fun f(propVal(P)) = P.toPrettyString(spaces,P,varSortPrinter)
| f(termVal(t)) = AT.toPrettyString(spaces,t,varSortPrinter)
| f(v) = valToStringUnlimited(v)
in
f v
end
and prettyValToStringUnlimited(v) = prettyValToStringWithSpacesUnlimited(v,0)
and valToStringAlt(v as propVal(_)) =
let val s = (prettyValToString(v))
in
if (String.size(s) < 50) then s else "\n"^s^"\n"
end
| valToStringAlt(v) = valToString(v)
and bindingsToString(L,opening,closing) =
let val (L,chopped) = if length(L) > 256 then (List.take(L,256),true) else (L,false)
val string_pairs = map (fn (a,b) => ((valToStringAlt a), (valToString b))) L
val total_size = List.foldl (fn ((s1,s2),sum) => String.size(s1) + String.size(s2) + sum) 0 string_pairs
val sep = if total_size > 50 then "\n" else ", "
val str = Basic.printListStr(string_pairs,fn (s1,s2) => s1 ^ " " ^ (Names.map_key_val_separator) ^ " " ^ s2,sep)
val str = if chopped then str^"\n...\n" else str
in
if null(L) then opening ^ closing else
if total_size > 50 then "\n"^opening^"\n"^str^"\n"^closing
else opening^str^closing
end
and tableToString(tab) =
let val L = HashTable.listItemsi tab
in
bindingsToString(L,"","")
end
and mapToString(m) =
let val L = Maps.listKeyValuePairs(m)
in
bindingsToString(L,"|{","}|")
end
and
allValsToStrings(vals) = Basic.printListStr(vals,valToString," ")
and
valsToStrings(vals) = if length(vals) > 512 then (allValsToStrings(List.take(vals,512)))^" ... "
else allValsToStrings(vals)
and
prettyValToStringWithSpaces(v,spaces) =
let val varSortPrinter = makeVarSortPrinter()
fun f(propVal(P)) = P.toPrettyString(spaces,P,varSortPrinter)
| f(termVal(t)) = AT.toPrettyString(spaces,t,varSortPrinter)
| f(v) = valToString(v)
in
f v
end
and
prettyValToStringWithSpacesFlush(v,spaces) =
let val varSortPrinter = makeVarSortPrinter()
fun f(propVal(P)) = P.toPrettyString(spaces,P,varSortPrinter)
| f(termVal(t)) = AT.toPrettyString(spaces,t,varSortPrinter)
| f(listVal(vals)) = "[\n"^(valsToStringWithLines(vals,spaces+1))^"\n"^(Basic.spaces spaces)^"]"
| f(v) = (valToString v)
in
f v
end
and
prettyValToString(v) = prettyValToStringWithSpaces(v,0)
and valsToStringWithLines([],_) = ""
| valsToStringWithLines([v],space_count) = (Basic.spaces space_count)^(prettyValToStringWithSpacesFlush(v,space_count))
| valsToStringWithLines(v::more,space_count) = (Basic.spaces space_count)^(prettyValToStringWithSpacesFlush(v,space_count))^"\n"^valsToStringWithLines(more,space_count)
fun valTypeAndString(v) = getType(v)^": "^valToString(v)
fun prettyValToString'(v) =
(case isStringValConstructive(v) of
SOME(str) => str
| _ => prettyValToString(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
res
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
res
end
| prettyValToStringNoTypes(v) = valToString(v)
fun printVal(v) =
let val sortVarPrinter = makeVarSortPrinter()
fun 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 subVal(sub)) = (print("\n");print(subType^": ");Terms.printSub(sub);print("\n"))
| f(v as tableVal(tab)) = (print("\n");print(tableType^": "^(tableToString tab));print("\n"))
| f(v as mapVal(tab)) = (print("\n");print(mapType^": "^(mapToString tab));print("\n"))
| f(v) = (print("\n"^getType(v));
let val vs = 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(t)) = print(mapToString t)
| printValNoType(v) =
let val vs = valToString(v)
in
if vs = "" then print("\n") else
print(vs)
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 empty_val_map:value Symbol.mapping = Symbol.empty_mapping
val empty_mod_map:module Symbol.mapping = Symbol.empty_mapping
val top_val_env:value_environment ref = ref(valEnv({val_map=empty_val_map,mod_map=empty_mod_map}))
val top_val_env_backup = ref(!top_val_env)
val top_assum_base = ABase.top_assum_base
fun getValAndModMaps(valEnv({val_map,mod_map})) = (val_map,mod_map)
val empty_val_env_functional:value_environment = valEnv({val_map=Symbol.empty_mapping,mod_map=Symbol.empty_mapping})
fun flipOrder(LESS) = GREATER
| flipOrder(GREATER) = LESS
| flipOrder(EQUAL) = EQUAL
fun compare(termVal(t1),termVal(t2)) = AT.compare(t1,t2)
| compare(termVal(t),propVal(p)) =
(case Prop.isAtom(p) of
SOME(t') => AT.compare(t,t')
| _ => LESS)
| compare(termVal(_),_) = LESS
| compare(termConVal(regFSym(s1)),termConVal(regFSym(s2))) = MS.compare(Data.fsymName(s1),Data.fsymName(s2))
| compare(charVal(c1),charVal(c2)) = Int.compare(c1,c2)
| compare(charVal(c1),termVal(_)) = GREATER
| compare(charVal(c1),propVal(p)) =
(case Prop.isAtom(p) of
SOME(_) => GREATER
| _ => LESS)
| compare(charVal(c1),_) = LESS
| compare(listVal(L1),listVal(L2)) = Basic.lexOrder(L1,L2,compare)
| compare(listVal(L1),termVal(_)) = GREATER
| compare(listVal(L1),charVal(_)) = GREATER
| compare(listVal(L1),propVal(p)) =
(case Prop.isAtom(p) of
SOME(_) => GREATER
| _ => LESS)
| compare(propVal(p1),propVal(p2)) = Prop.compare(p1,p2)
| compare(propVal(p),termVal(t)) =
(case Prop.isAtom(p) of
SOME(t') => AT.compare(t,t')
| _ => GREATER)
| compare(propVal(p1),charVal(_)) = GREATER
| compare(propVal(p1),listVal(_)) = GREATER
| compare(propVal(p1),v) = LESS
| compare(v,v' as termVal(_)) = flipOrder(compare(v',v))
| compare(v,v' as charVal(_)) = flipOrder(compare(v',v))
| compare(v,v' as listVal(_)) = flipOrder(compare(v',v))
| compare(v,v' as propVal(_)) = flipOrder(compare(v',v))
| compare(v,v') = String.compare(valToString(v),valToString(v'))
structure ValueKey : ORD_KEY =
struct
type ord_key = value
val compare = compare
end
structure ValueMap: ORDERED_MAP = makeOrdMap(ValueKey)
fun hashVal(termVal(s)) = AT.fastHash(s)
| hashVal(propVal(p)) = Prop.hash(p)
| hashVal(termConVal(fsym)) = hashFunSym(fsym)
| hashVal(propConVal(c)) = A.hashPropCon(c)
| hashVal(listVal(L)) = Basic.hashWordList(L,hashVal)
| hashVal(v as (cellVal(cv as ref(v')))) = hashVal(v')
(* Should preferably be replaced by MLton.hash(cv) in MLton-produced versions of Athena. *)
| hashVal(charVal(c)) = Basic.hashInt(c)
| hashVal(subVal(s)) = Basic.hashString(Terms.subToString(s))
| hashVal(tableVal(tab)) = Basic.hashString(tableToString(tab))
| hashVal(mapVal(tab)) = Basic.hashString("MAP"^(mapToString(tab)))
| hashVal(primUFunVal(_,{name,...})) = Basic.hashString(name)
| hashVal(primBFunVal(_,{name,...})) = Basic.hashString(name)
| hashVal(funVal(_,name,_)) = Basic.hashString(name)
| hashVal(closBFunVal(_,_,_,_,{name,...})) = Basic.hashString(!name)
| hashVal(closFunVal(_,_,{name,...})) = Basic.hashString(!name)
| hashVal(closUFunVal(_,_,_,{name,...})) = Basic.hashString(!name)
| hashVal(primUMethodVal(_,code)) = Basic.hashString(S.name(code))
| hashVal(primBMethodVal(_,code)) = Basic.hashString(S.name(code))
| hashVal(methodVal(_,code)) = Basic.hashString(S.name(code))
| hashVal(closUMethodVal(_,_,_,name)) = Basic.hashString(!name)
| hashVal(closBMethodVal(_,_,_,_,name)) = Basic.hashString(!name)
| hashVal(closMethodVal(A.methodExp({name,...}),_)) = Basic.hashString(!name)
(* Should preferably be replaced by MLton.hash(v) in MLton-produced versions of Athena: *)
| hashVal(v) = Basic.hashString(valToString(v))
fun makeValMapping(key_value_pairs: (value * value) list) =
let val empty_map: (value,value) Maps.mapping = Maps.makeMap(compare)
val mapping = Maps.insertLst(empty_map,key_value_pairs)
in
mapVal(mapping)
end
fun env2String(valEnv({val_map,mod_map})) =
let fun valEntryString(sym,v) = (Symbol.name sym)^" --> "^(prettyValToString v)
fun modEntryString(sym,m) = (Symbol.name sym)^" --> module ... "
val val_entries = map valEntryString (Symbol.listSymbolsAndImages val_map)
val mod_entries = map modEntryString (Symbol.listSymbolsAndImages mod_map)
val str1 = Basic.printListStr(val_entries,Basic.id,"\n")
val str2 = Basic.printListStr(mod_entries,Basic.id,"\n")
in
str1^(if str2 = "" then "" else "\n"^str2)
end
fun env2String(valEnv({val_map,mod_map}),space_count) =
let val offset = 3
fun valEntryString(sym,v) =
let val str = Symbol.name sym
val offset' = String.size(str) + 5
in
(Basic.spaces space_count)^str^" --> "^(prettyValToStringWithSpacesFlush(v,space_count+offset'))
end
val val_entries = map valEntryString (Symbol.listSymbolsAndImages val_map)
val mod_entries = map (fn (s,m) => modEntryString(s,m,space_count)) (Symbol.listSymbolsAndImages mod_map)
val str1 = Basic.printListStr(val_entries,Basic.id,"\n")
val str2 = Basic.printListStr(mod_entries,Basic.id,"\n")
in
str1^(if str2 = "" then "" else "\n"^str2)
end
and modEntryString(sym,m:module as {env,...},space_count) =
let val offset = (String.size(Symbol.name sym)) + 4 + 3
val (tv_map,tm_map) = getValAndModMaps(env)
in
(Basic.spaces space_count)^(Symbol.name sym)^" --> module { ### "^(Int.toString(Symbol.domainSize(tv_map) + Symbol.domainSize(tm_map)))^" bindings \n"^
(env2String(env,space_count+offset))^(Basic.spaces (space_count + offset))^"\n"^(Basic.spaces(space_count+offset-2))^"}"
end
fun envToString(e) = env2String(e,0)
fun modEnvToString(mod_name,e) = modEntryString(mod_name,e,0)
fun coerceFunSymbolIntoTerm(termConVal(regFSym(some_fsym))) =
if D.fsymArity(some_fsym) > 0 then NONE
else
SOME(AT.makeConstant(D.fsymName(some_fsym)))
| coerceFunSymbolIntoTerm(termConVal(athNumFSym(anum))) = SOME(AT.makeNumTerm(anum))
| coerceFunSymbolIntoTerm(termConVal(metaIdFSym(str))) = SOME(AT.makeIdeConstant(str))
| coerceFunSymbolIntoTerm(_) = NONE
fun coerceValIntoProp(propVal(P)) = SOME(P)
| coerceValIntoProp(termVal(t)) = P.makeAtomOpt(t)
| coerceValIntoProp(v as termConVal(_)) = (case coerceFunSymbolIntoTerm(v) of
SOME(t) => P.makeAtomOpt(t)
| _ => NONE)
| coerceValIntoProp(v) = NONE
fun makeAthenaInt(i) = termVal(AthTerm.makeNumTerm(A.int_num(i,ref "")))
fun makeAthenaReal(x) = termVal(AthTerm.makeNumTerm(A.real_num(x,ref "")))
fun makeEmptyModule(name) = let val m:module = {mod_name=A.msym(name),env=empty_val_env_functional,open_mod_directives=[]}
in
m
end
val empty_val_env:value_environment ref = ref(valEnv({val_map=Symbol.empty_mapping,mod_map=Symbol.empty_mapping}))
fun msym(s) = ModSymbol.makeModSymbol([],s,s)
fun msyms(sym_set) = let val syms = Symbol.listSymbols(sym_set)
val msyms = map msym syms
in
ModSymbol.symListToSet(msyms)
end
fun qualifyNameStr(str,mod_path) =
(case mod_path of
[] => str
| _ => (Basic.printListStr(mod_path,Symbol.name,"."))^"."^str)
fun qualifyName(name,mod_path) =
(case mod_path of
[] => ModSymbol.makeModSymbol([],name,name)
| m::rest => let val str = (Basic.printListStr(mod_path,Symbol.name,"."))^"."^(Symbol.name(name))
in
ModSymbol.makeModSymbol(mod_path,name,Symbol.symbol str)
end)
(***
Use fullyQualifySort (or the qualifySort immediately below) to qualify a sort term,
such as (List-Of (Pair-Of N.Nat A.B.Foo) Int). Use fullyQualifySortString below
to qualify a simple structure or domain name, such as "List-Of".
***)
fun fullyQualifySort(absyn_type) =
let fun debugPrint(str) = ()
fun decide(sort_name) =
let val sort_name_as_symbol = MS.nameAsSymbol(sort_name)
fun loop([]) = sort_name
| loop(path::rest) = let val sort_name' = qualifyName(sort_name_as_symbol,path)
in
if Data.isAnySortFlexible(sort_name') then sort_name'
else (case MS.find(Data.sort_abbreviations,sort_name') of
SOME(_) => sort_name'
| _ => loop(rest))
end
in
(case MS.split(sort_name) of
(first_mod::rest_mods,main_name) => if Symbol.symEq(first_mod,N.top_module_symbol) then
MS.makeModSymbol'(rest_mods,main_name)
else loop(!(Paths.open_mod_paths))
| _ => loop(!(Paths.open_mod_paths)))
end
fun transformSortName(sort_name) = decide(sort_name)
val absyn_type' = SymTerm.transform transformSortName absyn_type
val res = Terms.replaceTaggedSymTermConstants(absyn_type',fn sym => MS.find(Data.sort_abbreviations,sym),A.dum_pos)
in
res
end
fun qualifySort(absyn_type) = fullyQualifySort(absyn_type)
fun fullyQualifySortString(structure_name) =
let fun debugPrint(str) = print(str)
val structure_symbol = Symbol.symbol structure_name
fun decide(name:Symbol.symbol) =
let fun loop([]) = name
| loop(path::rest) = let val name' = qualifyName(name,path)
in
if Data.isAnySort(name') then MS.nameAsSymbol(name')
else
(case MS.find(Data.sort_abbreviations,name') of
SOME(struc) => Symbol.symbol(SymTerm.toStringDefault(struc))
| _ => loop(rest))
end
in
loop(!(Paths.open_mod_paths))
end
in
Symbol.name(decide(structure_symbol))
end
(***
For fullyQualifyString, pass a value of 0 for the parameter sort_or_fsym
to qualify a sort name, and pass a value of 1 to qualify a function symbol name.
***)
fun fullyQualifyString(name_str,sort_or_fsym) =
let val decider = if sort_or_fsym = 0 then Data.isAnySort else Data.isTermConstructorBool
fun decide(name:Symbol.symbol) =
let fun loop([]) = name
| loop(path::rest) = let val name' = qualifyName(name,path)
in
if decider(name') then MS.nameAsSymbol(name')
else (if sort_or_fsym = 0 then
(case MS.find(Data.sort_abbreviations,name') of
SOME(struc_name) => Symbol.symbol(SymTerm.toStringDefault(struc_name))
| _ => loop(rest))
else loop(rest))
end
in
loop(!(Paths.open_mod_paths))
end
val name_as_symbol = Symbol.symbol name_str
in
Symbol.name(decide(name_as_symbol))
end
fun fullyQualifySortString(structure_name) = fullyQualifyString(structure_name,0)
fun fullyQualifyFSymString(fsym_name) = fullyQualifyString(fsym_name,1)
fun tryFullQualification(fsym_name) =
if Data.isTermConstructorBool(fsym_name) then fsym_name
else let fun debugPrint(str) = ()
fun loop([]) = fsym_name
| loop(path::rest) = let val full_name = qualifyName(MS.lastName(fsym_name),path)
in
if Data.isTermConstructorBool(full_name) then full_name else loop(rest)
end
in
loop(!(Paths.open_mod_paths))
end
fun qualifyFSym(fsym_name,mod_path) =
let val qualified_fsym_name = qualifyName(MS.lastName(fsym_name),mod_path)
in
if Data.isTermConstructorBool(qualified_fsym_name) then qualified_fsym_name
else tryFullQualification(fsym_name)
end
fun qualifyFSym(fsym_name) =
let fun debugPrint(str) = print(str)
val fsym_name_as_symbol = MS.nameAsSymbol(fsym_name)
fun loop([]) = fsym_name
| loop(path::rest) = let val fsym_name' = qualifyName(fsym_name_as_symbol,path)
in
if Data.isTermConstructorBool(fsym_name') then fsym_name' else loop(rest)
end
in
(case MS.split(fsym_name) of
(first_mod::rest_mods,main_name) => if Symbol.symEq(first_mod,N.top_module_symbol) then
MS.makeModSymbol'(rest_mods,main_name)
else loop(!(Paths.open_mod_paths))
| _ => loop(!(Paths.open_mod_paths)))
end
val string_literals:value RSA.rs_array = RSA.makeRSArray(128,listVal([]))
val _ = RSA.setIncrement(256)
val global_string_literal_array_index = ref(0)
fun coerceTermIntoTermCon(t) =
(case AT.isConstant(t) of
SOME(f) => (case MS.find(Data.fsym_table,f) of
SOME(fsym) => SOME(regFSym(fsym))
| _ => NONE)
| _ => (case AT.isNumConstantOpt(t) of
SOME(anum) => SOME(athNumFSym(anum))
| _ => (case AT.isIdeConstant(t) of
SOME(str) => SOME(metaIdFSym(str))
| _ => NONE)))
fun isGloballyBound(msym,sym) =
(case !top_val_env of
(e as (valEnv({val_map,...}))) =>
(case Symbol.lookUp(val_map,sym) of
SOME(termVal(t)) => (case AT.isConstant(t) of
SOME(f) => not(Symbol.symEq(MS.nameAsSymbol(f),sym))
| _ => true)
| SOME(_) => true
| _ => false))
val (preProcessExp,preProcessDed,preProcessPhrase,
preProcessExpLst,preProcessDedLst,preProcessPhraseLst,
preProcessPattern,preProcessPattern',preProcessPatternLst,preProcessPatternLst') =
let val mod_path_cell:Symbol.symbol list ref = ref([])
val sort_var_table: FTerm.term S.htable = S.makeHTable()
fun debug() = false
fun dbprint(s) = if debug() then print(s) else ()
val match_sort_var_table: FTerm.term S.htable = S.makeHTable()
fun clearSortVarTables() = (S.clearHTable(sort_var_table);
S.clearHTable(match_sort_var_table))
fun matchFind(sort_var) = S.find(match_sort_var_table,sort_var)
fun find(sort_var) = (case S.find(sort_var_table,sort_var) of
NONE => S.find(match_sort_var_table,sort_var)
| res => res)
fun printSymTermVar(sym) = (Names.sort_variable_prefix)^(Symbol.name sym)
fun symSortToString(sym_tagged_sort) = SymTerm.toPrettyString(14,(SymTerm.stripTags(sym_tagged_sort)),printSymTermVar)
fun untaggedSymSortToString(sym_sort) = SymTerm.toPrettyString(14,sym_sort,printSymTermVar)
fun invalidSort(sort_as_tagged_symterm,pos) =
let val pos = SymTerm.getTopTag(sort_as_tagged_symterm)
in
Data.genEx("Invalid sort: "^(symSortToString sort_as_tagged_symterm),SOME(pos),!Paths.current_file)
end
fun translateSort(sort_as_symterm,tagged_vars) =
let val _ = if debug() then
print("\nAbout to translate this sort_as_symterm: " ^ (untaggedSymSortToString sort_as_symterm) ^ "\n")
else ()
val res = (case SymTerm.isVar(sort_as_symterm) of
SOME(v) => (case find(v) of
SOME(v') => let val _ = if debug() then print("FOUND THIS for th var sym_sort " ^
(untaggedSymSortToString sort_as_symterm) ^ ": " ^ (F.toStringDefault(v')) ^ "\n")
else ()
in
v'
end
| _ => (let val _ = if debug() then print("\nNOTHING FOUND for this var sym_sort: " ^
(untaggedSymSortToString sort_as_symterm) ^ ".\n")
else ()
val v' = if tagged_vars then FTerm.makeTaggedFreshVar() else FTerm.makeFreshVar()
val _ = Symbol.insert(sort_var_table,v,v')
in
v'
end))
| _ => (case SymTerm.isApp(sort_as_symterm) of
SOME(fsym,sorts) => let val sorts' = map (fn sort => translateSort(sort,tagged_vars)) sorts
in
FTerm.makeApp(fsym,sorts')
end
| _ => raise Basic.Never))
in
res
end
fun translateSortAux(sort_as_symterm) =
(case SymTerm.isVar(sort_as_symterm) of
SOME(v) => (case matchFind(v) of
SOME(v') => v'
| _ => (let val v' = FTerm.makeFreshVar()
val _ = Symbol.insert(match_sort_var_table,v,v')
in
v'
end))
| _ => (case SymTerm.isApp(sort_as_symterm) of
SOME(fsym,sorts) => let val sorts' = map translateSortAux sorts
in
FTerm.makeApp(fsym,sorts')
end
| _ => raise Basic.Never))
fun getFTermSortFromSTermSort(sort_as_sym_term_opt,name,pos:Position.PosType) =
(case sort_as_sym_term_opt of
SOME(tagged_sort) =>
let val tagged_sort' = qualifySort(tagged_sort)
val sort' = SymTerm.stripTags(tagged_sort')
val sort = Terms.replaceSymTermConstants(sort',fn sym => MS.find(Data.sort_abbreviations,qualifyName(MS.lastName(sym),!mod_path_cell)))
val cond = SymTerm.isLegalFlex(sort,Data.sortConstructorArity,fn _ => true,Names.fun_name_msym)
in
if cond
then let val ts = translateSort(sort,true)
in SOME(ts) end
else NONE
end
| _ => NONE)
fun getFTermSortFromSTermSortWithError(sort_as_sym_term_opt,name,pos:Position.PosType) =
(case sort_as_sym_term_opt of
SOME(tagged_sort) =>
let val tagged_sort' = qualifySort(tagged_sort)
val sort' = SymTerm.stripTags(tagged_sort')
val sort = Terms.replaceSymTermConstants(sort',fn sym => MS.find(Data.sort_abbreviations,sym))
in
if SymTerm.isLegalFlex(sort,Data.sortConstructorArity,fn _ => true,Names.fun_name_msym)
then SOME(translateSort(sort,true))
else invalidSort(tagged_sort',pos)
end
| _ => NONE)
fun getFTermSortFromSTermSortAux(sort_as_sym_term_opt,name,pos:Position.PosType) =
(case sort_as_sym_term_opt of
SOME(tagged_sort) =>
let val tagged_sort' = qualifySort(tagged_sort)
val sort' = SymTerm.stripTags(tagged_sort')
val sort = Terms.replaceSymTermConstants(sort',fn sym => MS.find(Data.sort_abbreviations,sym))
in
if SymTerm.isLegalFlex(sort,Data.sortConstructorArity,fn _ => true,Names.fun_name_msym)
then SOME(translateSortAux(sort))
else invalidSort(tagged_sort',pos)
end
| _ => NONE)
fun ppExp(e as A.idExp({msym,pos,sym,no_mods,...}),vars,bound_ids) =
let val full_name = MS.name(msym)
val name' = qualifyFSym(msym)
val igb = isGloballyBound(name',sym)
val cond = (Data.isConstantTermConstructor(name') andalso Data.isPolySymbol(name') andalso
(not(no_mods) orelse (not(Symbol.isMember(sym,bound_ids)) andalso not(igb))))
in
if cond
then let val sym_sig as (arg_sorts,res_sort,is_poly,involves_pred_based_sorts) = Data.getSignature(name')
val sort_string = F.toStringDefault(res_sort)
fun isVar(str) = let val str_lst = explode str
in not(null(str_lst)) andalso
hd(str_lst) = Names.sort_variable_prefix_as_char