-
Notifications
You must be signed in to change notification settings - Fork 4
/
ath_term.sml
executable file
·3010 lines (2739 loc) · 152 KB
/
ath_term.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
(*======================================================================
Implementation of Athena terms (satisfying the ATHENA_TERM signature).
=======================================================================*)
structure AthTerm: ATHENA_TERM =
struct
structure S = ModSymbol
structure A = AbstractSyntax
structure F = FTerm
structure D = Data
structure N = Names
structure MS = ModSymbol
structure ATV = AthTermVar
type variable = ATV.ath_term_var
type sort = FTerm.term
type fsymbol = MS.mod_symbol
val varEq = ATV.athTermVarEq
val fsymEq = MS.modSymEq
val failLst = Basic.failLst
fun msym(s) = ModSymbol.makeModSymbol([],s,s)
abstype term = Var of variable
| App of {root: fsymbol,
args: term list,
sort: sort,
bits: Word8.word,
hash_code:Word.word,
vars: variable list,
poly_constants: (fsymbol * sort) list }
| numTerm of A.ath_number
| ideTerm of MetaId.meta_id
with
(************************************************************************************
Info on term bit fields
Every tree-structured Athena term has a word attached to its root. The bits
of that word provide fast access to a host of information about the term,
which in turn speeds up many operations. (For instance, suppose we need
to apply a substitution to a very large term t. If we can tell that t is
ground in O(1) time then we avoid traversing t, thereby saving a lot of time.)
The lowest bit is the "poly field". It denotes whether or not the term has
*any* subterms with polymorphic sorts, i.e., any subterm t such that
FTerm.not(isGround(getSort(t))).
The next highest bit is the "has-vars field". This denotes whether the
term has variables, i.e. whether it's not ground. This seems roundabout --
why not keep a "ground" bit? The reason is that with the two lowest bits
set like this, we can very quickly compute the word of a new term
when we apply a function symbol f to terms t1,...,tn: we compute
the "or" of the words of t1,...,tn.
The next highest bit is the "is-non-canonical" field.
This denotes whether the term is non-canonical, where a canonical
term is one that only contains constructors and possibly numerals.
This is again represented in a roundabout way to enable
quick "or"-ing.
Finally, the highest bit indicates whether or not the term
contains any sorts that are predicate-based. More precisely:
whether or not the term contains any subterm (f ...) where
the signature of f involves a domain that has a sort predicate.
Hence, a word is interpreted according to the following
scheme, on the basis of its last three bits:
0000 --> canonical, ground, monomorphic, no predicate-based sorts
1000 --> canonical, ground, monomorphic, with predicate-based sorts
0001 --> canonical, ground, polymorphic, no predicate-based sorts
1001 --> canonical, ground, polymorphic, with predicate-based sorts
0010 --> cannot arise, as all canonical terms are ground
1010 --> cannot arise, as all canonical terms are ground
0100 --> non-canonical, ground, monomorphic, no predicate-based sorts
1100 --> non-canonical, ground, monomorphic, with predicate-based sorts
0011 --> cannot arise, same reason as above
1011 --> cannot arise, same reason as above
0101 --> non-canonical, ground, polymorphic, no predicate-based sorts
1101 --> non-canonical, ground, polymorphic, with predicate-based sorts
0110 --> non-canonical, non-ground, monomorphic, no predicate-based sorts
1110 --> non-canonical, non-ground, monomorphic, with predicate-based sorts
0111 --> non-canonical, non-ground, polymorphic, no predicate-based sorts
1111 --> non-canonical, non-ground, polymorphic, with predicate-based sorts
************************************************************************************)
val orWords = Word8.orb
fun isPolyWord(w:Word8.word) = (Word8.andb(w,0w1) = 0w1)
fun isPoly(Var(v)) = ATV.isPoly(v)
| isPoly(App({bits,...})) = isPolyWord(bits)
| isPoly(_) = false
fun hasVarsWord(w:Word8.word) = (Word8.andb(w,0w2) = 0w2)
fun isNonCanonicalWord(w:Word8.word) = (Word8.andb(w,0w4) = 0w4)
fun hasVars(Var(_)) = true
| hasVars(App{bits,...}) = hasVarsWord(bits)
| hasVars(_) = false
fun hasTaggedSortVars(Var(v)) = ATV.isTagged(v)
| hasTaggedSortVars(App({sort,args,...})) = F.someTaggedVars(sort) orelse (List.exists hasTaggedSortVars args)
| hasTaggedSortVars(_) = false
fun makeWord({involves_pred_based_sorts=false,non_canonical=false,hasVars=false,poly=false}):Word8.word = 0w0 (* value of 0 *)
| makeWord({involves_pred_based_sorts=true,non_canonical=false,hasVars=false,poly=false}):Word8.word = 0w8 (* value of 8 *)
| makeWord({involves_pred_based_sorts=false,non_canonical=false,hasVars=false,poly=true}):Word8.word = 0w1 (* value of 1 *)
| makeWord({involves_pred_based_sorts=true,non_canonical=false,hasVars=false,poly=true}):Word8.word = 0w9 (* value of 9 *)
| makeWord({involves_pred_based_sorts=false,non_canonical=false,hasVars=true,poly=false}):Word8.word = 0w2 (* value of 2; cannot arise *)
| makeWord({involves_pred_based_sorts=true, non_canonical=false,hasVars=true,poly=false}):Word8.word = 0w10 (* value of 10; cannot arise *)
| makeWord({involves_pred_based_sorts=false,non_canonical=false,hasVars=true,poly=true}):Word8.word = 0w3 (* value of 3; cannot arise *)
| makeWord({involves_pred_based_sorts=true, non_canonical=false,hasVars=true,poly=true}):Word8.word = 0w11 (* value of 11; cannot arise *)
| makeWord({involves_pred_based_sorts=false,non_canonical=true,hasVars=false,poly=false}):Word8.word = 0w4 (* value of 4 *)
| makeWord({involves_pred_based_sorts=true, non_canonical=true,hasVars=false,poly=false}):Word8.word = 0w12 (* value of 12 *)
| makeWord({involves_pred_based_sorts=false,non_canonical=true,hasVars=false,poly=true}):Word8.word = 0w5 (* value of 5 *)
| makeWord({involves_pred_based_sorts=true, non_canonical=true,hasVars=false,poly=true}):Word8.word = 0w13 (* value of 13 *)
| makeWord({involves_pred_based_sorts=false,non_canonical=true,hasVars=true,poly=false}):Word8.word = 0w6 (* value of 6 *)
| makeWord({involves_pred_based_sorts=true, non_canonical=true,hasVars=true,poly=false}):Word8.word = 0w14 (* value of 14 *)
| makeWord({involves_pred_based_sorts=false,non_canonical=true,hasVars=true,poly=true}):Word8.word = 0w7 (* value of 7 *)
| makeWord({involves_pred_based_sorts=true, non_canonical=true,hasVars=true,poly=true}):Word8.word = 0w15 (* value of 15 *)
val zeroWord:Word8.word = 0w0
val polyWord:Word8.word = 0w1
val varsWord:Word8.word = 0w6 (* If it has vars it's non-canonical, so 6 instead of 2 is now the right value for this mask. *)
val hasPredBasedSortsWord:Word8.word = 0w8
val nonCanonicalWord:Word8.word = 0w4
fun isCanonical(App({bits,...})) = not(isNonCanonicalWord(bits))
| isCanonical(numTerm(_)) = true
| isCanonical(_) = false
fun involvesPredBasedSortsWord(w:Word8.word) = (Word8.andb(w,0w8) = 0w8)
fun getBits(App({bits,...})) = bits
fun involvesPredBasedSorts(Var(v)) = F.findOutDynamicallyIfASortHasPredicateBasedSortSymbolsInIt(ATV.getSort(v))
| involvesPredBasedSorts(App({bits,...})) = involvesPredBasedSortsWord(bits)
| involvesPredBasedSorts(_) = false
fun involvePredBasedSorts(terms) = List.exists involvesPredBasedSorts terms
fun compareNums(A.int_num(i,_),A.int_num(j,_)) = Int.compare(i,j)
| compareNums(A.int_num(i,_),A.real_num(_)) = LESS
| compareNums(A.real_num(i,_),A.real_num(j,_)) = Real.compare(i,j)
| compareNums(A.real_num(i,_),A.int_num(_)) = GREATER
fun compare(ideTerm(x),ideTerm(y)) = MetaId.compare(x,y)
| compare(ideTerm(x),_) = LESS
| compare(Var(x),Var(y)) = ATV.compare(x,y)
| compare(Var(x),ideTerm(_)) = GREATER
| compare(Var(x),_) = LESS
| compare(numTerm(s),numTerm(t)) = compareNums(s,t)
| compare(numTerm(s),App(_)) = LESS
| compare(numTerm(s),_) = GREATER
| compare(App({root=root1,args=args1,...}),App({root=root2,args=args2,...})) =
let val c = Int.compare(MS.code(root1),MS.code(root2))
in
if c = EQUAL then Basic.lexOrder(args1,args2,compare) else c
end
| compare(App(_),_) = GREATER
fun fastHash(Var(v)) = ATV.hash(v)
| fastHash(App({hash_code,...})) = hash_code
| fastHash(numTerm(A.int_num(i,_))) = Basic.hashInt(i)
| fastHash(numTerm(A.real_num(r,_))) = Basic.hashString(Real.toString(r))
| fastHash(ideTerm(x)) = MetaId.hash(x)
fun isTrueTerm(App({root,...})) = MS.modSymEq(root,N.mtrue_logical_symbol)
| isTrueTerm(_) = false
fun isFalseTerm(App({root,...})) = MS.modSymEq(root,N.mfalse_logical_symbol)
| isFalseTerm(_) = false
fun sizeCumulative(App({args,...}),n) = sizeLst1(args,n+1)
| sizeCumulative(_,n) = n + 1
and sizeLst1([],n) = n
| sizeLst1(t::more,n) = sizeLst1(more,sizeCumulative(t,n))
fun size(t) = sizeCumulative(t,0)
fun sizeLst(terms) = sizeLst1(terms,0)
fun getSort(App({sort,...})) = sort
| getSort(Var(v)) = ATV.getSort(v)
| getSort(numTerm(A.int_num(_))) = D.int_sort
| getSort(numTerm(A.real_num(_))) = D.real_sort
| getSort(ideTerm(_)) = D.ide_sort
val global_tccs:(term * sort) list ref = ref([])
val (termSymbols,termSymbolsLst) =
let fun f(App({root,args,...})) = fLst(args,[root])
| f(_) = []
and fLst([],res) = res
| fLst(t::more,res) = fLst(more,(f t)@res)
in
(fn t => f(t),fn L => fLst(L,[]))
end
fun termSymbolsFastAux(t:term,ht:MS.mod_symbol MS.htable) =
let fun loop(App({root,args,...})) = (MS.insert(ht,root,root);List.app loop args)
| loop(_) = ()
in
loop(t)
end
fun termSymbolsFast(t) =
let val ht: MS.mod_symbol MS.htable = MS.makeHTable()
val _ = termSymbolsFastAux(t,ht)
in
MS.listItems(ht)
end
fun termSymbolsLstFast(terms) =
let val ht: MS.mod_symbol MS.htable = MS.makeHTable()
val _ = List.app (fn t => termSymbolsFastAux(t,ht)) terms
in
MS.listItems(ht)
end
fun tagTerm(App({root,args,sort,bits,vars,hash_code,poly_constants,...})) =
App({root=root,args = map tagTerm args,sort=F.tagSort(sort),bits=bits,vars=map ATV.tagVar vars,hash_code=hash_code,
poly_constants=map (fn (c,sort) => (c,F.tagSort(sort))) poly_constants})
| tagTerm(Var(v)) = Var(ATV.tagVar(v))
datatype term_root = NUM of A.ath_number | META_ID of string | FSYM of fsymbol | TERM_VAR of variable
fun termRootToString(tr) =
let val (lparen,rparen,comma) = (Basic.lparen,Basic.rparen,Basic.comma)
fun f(TERM_VAR(v)) = "TERM_VAR"^lparen^(ATV.toStringDefault v)^rparen
| f(FSYM(f)) = "FSYM"^lparen^(S.name(f))^rparen
| f(META_ID(str)) = "META_ID"^lparen^str^rparen
| f(NUM(a)) = "NUM"^lparen^(A.athNumberToString(a))^rparen
in
f tr
end
fun termRoot(App({root,...})) = FSYM(root)
| termRoot(Var(v)) = TERM_VAR(v)
| termRoot(numTerm(a)) = NUM(a)
| termRoot(ideTerm(x)) = META_ID(MetaId.name(x))
fun termChildren(App({args,...})) = (List.map termRoot args)
| termChildren(_) = []
fun sortOfVarInTerm(v,Var(v')) = if ATV.nameEq(v,v) then SOME(ATV.getSort(v')) else NONE
| sortOfVarInTerm(v,App({vars,...})) =
(case Basic.findInList(vars,fn v' => ATV.nameEq(v,v')) of
SOME(v') => SOME(ATV.getSort(v'))
| _ => NONE)
| sortOfVarInTerm(_) = NONE
fun getVars(App({vars,...})) = vars
| getVars(Var(v)) = [v]
| getVars(_) = []
fun getVarsIO(Var(v)) = [v]
| getVarsIO(App({args,...})) = getVarsIOLst(args,[])
| getVarsIO(_) = []
and getVarsIOLst([],res) = List.foldl op@ [] res
| getVarsIOLst(t::more,res) = getVarsIOLst(more,(getVarsIO t)::res)
fun weedOutDups(vars) =
let val T = ATV.makeHTableWithVarEq(ATV.nameEq)
fun loop([],res) = rev(res)
| loop(v::more,res) =
(case ATV.find(T,v) of
SOME(_) => loop(more,res)
| _ => (ATV.insert(T,v,v);loop(more,v::res)))
in
loop(vars,[])
end
fun getVarsInOrder(t) = weedOutDups(getVarsIO(t))
fun getVarsInOrderLst(terms) = weedOutDups(getVarsIOLst(terms,[]))
fun getVarsLst(terms) =
let fun f(t::more,res) = f(more,(getVars t)@res)
| f([],res) = Basic.removeDuplicatesEq(res,varEq)
in
f(terms,[])
end
fun height(App({args as _::_,...})) = 1 + Basic.maxLst(map height args)
| height(_) = 0
val mlprint = fn x => ()
fun getNumVal(t) =
let fun f(numTerm(a),negative) = SOME(a,negative)
| f(App({root,args=[t],...}),negative) =
if MS.modSymEq(root,Names.msubtraction_symbol) then f(t,not negative) else
if MS.modSymEq(root,Names.maddition_symbol) then f(t,negative) else NONE
| f(_) = NONE
in
f(t,false)
end
fun constantSymToString(fsym,sort,printSortVar) =
let val (_,res_sort,is_poly,has_pred_based_sorts) = Data.getSignature(fsym)
val str = S.name fsym
val tagged_str = str^":"^(F.toString(sort,printSortVar))
in
if not(!Options.print_var_sorts) then str
else (if is_poly then tagged_str
else (if not(F.sortEq(sort,res_sort)) then tagged_str else str))
end
fun makeInternalAppTerm(f,args,hc,sort,bits,pc,vs) =
App({root=f,args=args,hash_code=hc,sort=sort,bits=bits,poly_constants=pc,vars=vs})
fun constantSymToPrettyString(start,fsym,sort,printSortVar) =
let val (_,res_sort,is_poly,_) = Data.getSignature(fsym)
val str = S.name fsym
val len = String.size str
val tagged_str = str^":"^(F.toPrettyString(start+len+1,sort,printSortVar))
in
if not(!Options.print_var_sorts) then str
else (if is_poly then tagged_str
else (if not(F.sortEq(sort,res_sort)) then tagged_str else str))
end
fun toString(t,printSortVar) =
let fun f(Var(v)) = ATV.toString(v,printSortVar)
| f(numTerm(n)) = A.athNumberToString(n)
| f(ideTerm(x)) = Names.metaIdPrefix^(MetaId.name(x))
| f(App({root,args as [],sort,...})) = constantSymToString(root,sort,printSortVar)
| f(App({root,args,...})) = "("^MS.name(root)^(Basic.blank)^Basic.printSExpListStr(args,f)^")"
in
f t
end
fun toStringDefault(t) = toString(t,F.varToString)
fun tccToString((t,sort)) = (toStringDefault(t))^" :: "^(F.toStringDefault(sort))
fun tccsToString(tccs) = Basic.printListStr(tccs,tccToString,"\n")
fun toPrettyString(start,t,printSortVar) =
let fun pp(s,Var(v)) = ATV.toPrettyString(s,v,printSortVar)
| pp(s,App({root,args as [],sort,...})) = constantSymToPrettyString(s,root,sort,printSortVar)
| pp(s,App({root,args,...})) =
let val str = MS.name(root)
val break_up = List.exists (fn t => (height(t) > 0
orelse F.height(getSort(t)) > 0)) args
in
(Basic.lparen)^str^(Basic.blank)^
ppArgs(s+String.size(str)+2,args,break_up)^(Basic.rparen)
end
| pp(s,t) = toString(t,printSortVar)
and ppArgs(s,args,false) = Basic.printSExpListStr(args,fn t => toString(t,printSortVar))
| ppArgs(s,args,true) = doArgs(s,args)
and doArgs(s,[]) = ""
| doArgs(s,[t]) = pp(s,t)
| doArgs(s,t1::t2::more) = pp(s,t1)^Basic.new_line^Basic.spaces(s)^doArgs(s,t2::more)
in
pp(start,t)
end
fun toPrettyStringDefault(s,t) = toPrettyString(s,t,F.varToString)
fun showTerms(terms) = Basic.printLnList(terms,fn t => toPrettyString(0,t,F.varToString))
fun showVars([]) = ()
| showVars(vars::rest) = (List.app (fn v => (print(ATV.toPrettyString(0,v,F.varToString));print("\n"))) vars;
if null(rest) then print("\n") else print("\nand\n");
showVars(rest))
fun fauxTermToPrettyString(start,f,args) =
let val sortVarPrinter = F.makeVarSortPrinter()
val str = MS.name(f)
val break_up = List.exists (fn t => height(t) > 0) args
fun doArgs(s,[]) = ""
| doArgs(s,[t]) = toPrettyString(s,t,sortVarPrinter)
| doArgs(s,t1::t2::more) = (toPrettyString(s,t1,sortVarPrinter))^
Basic.new_line^Basic.spaces(s)^(doArgs(s,t2::more))
and ppArgs(s,args,false) = Basic.printSExpListStr(args,fn t => toString(t,sortVarPrinter))
| ppArgs(s,args,true) = doArgs(s,args)
in
"("^str^" "^ppArgs(start+String.size(str)+2,args,break_up)^")"
end
fun bitsToString(w:Word8.word) =
(case w of
0w0 => "Pred-Based-sorts: no, canonical:yes, poly: no, vars: no"
| 0w1 => "Pred-Based-sorts: no, canonical:yes, poly: yes, vars: no"
| 0w2 => "(Should not arise!) Pred-Based-sorts: no, canonical:yes, poly: no, vars: yes"
| 0w3 => "(Should not arise!) Pred-Based-sorts: no, canonical:yes, poly: yes, vars: yes"
| 0w4 => "Pred-Based-sorts: no, canonical: no, poly: no, vars: no"
| 0w5 => "Pred-Based-sorts: no, canonical: no, poly: yes, vars: no"
| 0w6 => "Pred-Based-sorts: no, canonical: no, poly: no, vars: yes"
| 0w7 => "Pred-Based-sorts: no, canonical: no, poly: yes, vars: yes"
| 0w8 => "Pred-Based-sorts: no, canonical: yes, poly: no, vars: no"
| 0w9 => "Pred-Based-sorts: yes, canonical: yes, poly: yes, vars: no"
| 0w10 => "Pred-Based-sorts: yes, canonical: yes, poly: no, vars: yes"
| 0w11 => "(Should not arise!): Pred-Based-sorts: yes, canonical: yes, poly: yes, vars: yes"
| 0w12 => "Pred-Based-sorts: yes, canonical: yes, poly: no, vars: no"
| 0w13 => "Pred-Based-sorts: yes, canonical: yes, poly: yes, vars: no"
| 0w14 => "Pred-Based-sorts: yes, canonical: yes, poly: no, vars: yes"
| 0w15 => "Pred-Based-sorts: yes, canonical: yes, poly: yes, vars: yes"
| _ => "Invalid word (greater than 15)")
fun showBits(Var(_)) = ""
| showBits(App({bits,...})) = bitsToString(bits)
| showBits(_) = ""
fun displayTerm(t) = (print("\nTerm: "^(toPrettyString(7,t,F.varToString)));
print("\nIts sort: "^(F.toString(getSort(t),F.varToString)));
(case t of
App({bits,...}) => print("\nIts bits are given by the word: " ^ (Word8.toString(bits)) ^ ", which is interpreted as: "^(showBits t))
| _ => print("\nNo bits...\n"));
print("\nIts variables: ");showVars([getVars(t)]);print("\n"))
val display = displayTerm
fun displayLst(terms) = List.app display terms
fun makeConservativeName(str) =
let fun normalChar c = Char.isAlphaNum(c) orelse Char.ord(c) = 95
fun f(c) = if normalChar(c) then Char.toString(c) else
"b_"^(Int.toString(Char.ord(c)))^"_b"
val cl = String.explode str
in
List.foldr (op^) "" (map f cl)
end
fun makeTPTPTerm(t,fvars) =
let fun f(Var(v)) = let val vname = ATV.name(v)
val cvn = makeConservativeName(vname)
in
if Basic.isMemberEq(v,fvars,ATV.nameEq) then
"a"^cvn else "X"^cvn
end
| f(numTerm(an as A.int_num(_))) =
let val cname = A.athNumberToString(an)
in
cname
end
| f(numTerm(an as A.real_num(r,str))) =
let val cname = let val (floor,ceil) = (Int.toString(Real.floor(Real.realFloor(r))),Int.toString(Real.ceil(Real.realCeil(r))))
in "r"^floor^ceil end
in
cname
end
| f(ideTerm(x)) = "ident"^(makeConservativeName(MetaId.name(x)))
| f(App({root,args as [],...})) = "a"^( makeConservativeName(MS.name(root)))
| f(App({root,args,...})) = let val is_eq = MS.modSymEq(root,Names.mequal_logical_symbol)
val fname = if is_eq then "equal" else
makeConservativeName("f"^(MS.name(root)))
in
fname^"("^fLst(args)^")"
end
and
fLst([]) = ""
| fLst([t]) = f(t)
| fLst(t1::(rest as _::_)) = f(t1)^","^fLst(rest)
in
f(t)
end
fun makeTSTPTerm(t,fvars,boolean_term) =
let fun f(Var(v)) = let val vname = ATV.name(v)
val cvn = makeConservativeName(vname)
in
if Basic.isMemberEq(v,fvars,ATV.nameEq) then
"a"^cvn else "X"^cvn
end
| f(numTerm(an)) = A.athNumberToString(an)
| f(ideTerm(x)) = "ide_$nti_$fier_$_"^(makeConservativeName(MetaId.name(x)))
| f(App({root,args as [],...})) = if boolean_term then
"("^"a"^(makeConservativeName(MS.name(root)))^"())"
else "a"^( makeConservativeName(MS.name(root)))
| f(App({root,args,...})) = let val is_eq = MS.modSymEq(root,Names.mequal_logical_symbol)
val fname = if is_eq then "equal" else
makeConservativeName("f"^(MS.name(root)))
in
if is_eq then "("^(f (Basic.first args))^" = "^(f (Basic.second args))^")"
else fname^"("^fLst(args)^")"
end
and
fLst([]) = ""
| fLst([t]) = f(t)
| fLst(t1::(rest as _::_)) = f(t1)^","^fLst(rest)
in
f(t)
end
fun makeTSTPTermFast(t,fvars,boolean_term,A,i) =
let fun write(str,i) = Basic.writeStringToCharArray(str,A,i)
val (lp,rp,blank) = (Basic.lparen,Basic.rparen,Basic.blank)
fun f(Var(v),i) = let val vname = ATV.name(v)
val cvn = makeConservativeName(vname)
val cvn' = if Basic.isMemberEq(v,fvars,ATV.nameEq) then "a"^cvn else "X"^cvn
in
write(cvn',i)
end
| f(numTerm(an),i) = write(A.athNumberToString(an),i)
| f(ideTerm(x),i) = write("ide_$nti_$fier_$_"^(makeConservativeName(MetaId.name(x))),i)
| f(App({root,args,...}),i) = let val is_eq = MS.modSymEq(root,Names.mequal_logical_symbol)
val fname = if is_eq then "equal" else
makeConservativeName("f"^(MS.name(root)))
in
if is_eq then
let val j1 = write(Basic.lparen,i)
val j2 = f(Basic.first(args),j1)
val j3 = write(" = ",j2)
val j4 = f(Basic.second(args),j3)
in write(rp,j4) end
else let val j0 = write(fname,i)
val j1 = write(lp,j0)
val j2 = fLst(args,j1)
in write(rp,j2)
end
end
and fLst([],i) = i
| fLst(t::more,i) = (f(t,i);fLst(more,i));
in
f(t,i)
end
fun leaves(App({args,...})) = Basic.flatten (List.map leaves args)
| leaves(t) = [t]
val false_code = MS.code(Names.mfalse_logical_symbol)
val evaluateString = ref (fn x:string => ())
fun isBuiltIn(f) = ModSymbol.compare(f,Names.mfalse_logical_symbol) = LESS
fun isBuiltInCVC(f) = (ModSymbol.compare(f,Names.mfalse_logical_symbol) = LESS andalso
(not(MS.modSymEq(f,Names.mfalse_logical_symbol))) andalso (not(MS.modSymEq(f,Names.mtrue_logical_symbol))))
val smt_names = Unsafe.Array.create(Int.+(1,false_code),"")
val _ = let val update = Unsafe.Array.update
in
(update(smt_names,0,Names.addition_name);
update(smt_names,1,N.subtraction_name);
update(smt_names,2,N.multiplication_name);
update(smt_names,3,N.division_name);
update(smt_names,4,"mod");
update(smt_names,5,N.formal_less_name);
update(smt_names,6,N.formal_greater_name);
update(smt_names,7,N.formal_leq_name);
update(smt_names,8,N.formal_geq_name);
update(smt_names,9,N.equal_logical_name);
update(smt_names,10,N.ite_name);
update(smt_names,11,"and");
update(smt_names,12,"or");
update(smt_names,13,"not");
update(smt_names,14,"=>");
update(smt_names,15,"<=>");
update(smt_names,16,N.true_logical_name);
update(smt_names,17,N.false_logical_name))
end
fun realToRat(x) =
let val x_str = A.athenaNumberToString(x,true)
val [integral,decimal] = String.tokens (fn c => c = #".") x_str
val d = Basic.pow(10,String.size(decimal))
val str = integral^decimal
val n = (case IntInf.fromString(str) of
SOME(r) => r
| _ => Basic.fail("\nError in converting a real number to SMT format.\n"))
val n_str = if IntInf.<(n,IntInf.fromInt(0)) then "-"^(IntInf.toString(IntInf.abs n)) else IntInf.toString(n)
in
(n_str,Int.toString(d))
end
val (symTermToFTermWithSub,symTermsToFTermsWithSub) =
let fun convert(terms) =
let val vars = SymTerm.getVarsLst(terms)
val new_vars = map (fn (_) => FTerm.makeFreshVarCounter()) vars
val empty_mapping = Symbol.empty_mapping
val mapping = Symbol.enterLst(empty_mapping,Basic.zip(vars,new_vars))
fun doIt(t) =
case SymTerm.isVar(t) of
SOME(v) => (case Symbol.lookUp(mapping,v) of
SOME(v') => F.makeVar(v')
| _ => raise Basic.Never)
| NONE => (case SymTerm.isApp(t) of
SOME(fsym,args) =>
let val arg_results = map doIt args
in
FTerm.makeApp(fsym,arg_results)
end
| _ => raise Basic.Never)
in
(map doIt terms,mapping)
end
in
(fn (t) => convert([t]),fn (terms) => convert(terms))
end
val fmap_module = Symbol.symbol("DMap")
val (fmap_sort_name,
fmap_empty_map_name,
fmap_update_name,
fmap_apply_name) = ("DMap","empty-map","update","apply")
val (fmap_sort_symbol,
fmap_empty_map_symbol,
fmap_update_symbol,
fmap_apply_symbol) = (Symbol.symbol fmap_sort_name,
Symbol.symbol fmap_empty_map_name,
Symbol.symbol fmap_update_name,
Symbol.symbol fmap_apply_name)
val (mfmap_sort_symbol,mfmap_empty_map_symbol,mfmap_update_symbol,mfmap_apply_symbol) =
(ModSymbol.makeModSymbol'([fmap_module],fmap_sort_symbol),
ModSymbol.makeModSymbol'([fmap_module],fmap_empty_map_symbol),
ModSymbol.makeModSymbol'([fmap_module],fmap_update_symbol),
ModSymbol.makeModSymbol'([fmap_module],fmap_apply_symbol))
val (lp_define_blank,lp_define_type_blank,define,int_sort,cvc_int_sort,real_sort,cvc_real_sort,bool_sort,cvc_bool_sort,lp,rp,blank,double_rp_newline,colon,double_colon) =
("\n(define ","\n(define-type ", "define","int","INT","real","REAL","bool","BOOLEAN","(",")"," ","))\n",":","::")
val (var_prefix,fsym_prefix,poly_con_name_prefix) = (Names.smt_var_prefix,Names.smt_fsym_prefix,Names.smt_poly_con_name_prefix)
val (int_sort_code,real_sort_code,boolean_sort_code) = (MS.code(Names.mint_sort_symbol),MS.code(Names.mreal_sort_symbol),MS.code(Names.mboolean_sort_symbol))
fun sortNameSMT(sort) =
let val res = (case F.isApp(sort) of
SOME(msym,args) =>
let val scode = MS.code(msym)
val main_name = if scode = int_sort_code then int_sort
else if scode = boolean_sort_code then bool_sort
else if scode = real_sort_code then real_sort
else S.name(msym)
in
if null(args) then main_name
else if MS.modSymEq(msym,mfmap_sort_symbol)
then let val arg_string = Basic.printListStr(args,sortNameSMT," ")
in
"(-> "^arg_string^")"
end
else F.toOneStringDefault(sort)
end
| _ => F.toStringDefault(sort))
in
res
end
fun writeSMT(t,{domain_stream,
dec_stream,
main_stream,
var_table,
fsym_table,
domain_table,
sel_counter,
poly_to_mono_sort_table}) =
let
fun debugPrint(s) = print(s)
fun makePolyConName(name,range_type_str,arg_sorts) =
let val con_name = S.name name
val con_name = if (con_name = "::") then "CONS" else con_name
val len_str = Int.toString(String.size(con_name))
val arg_sort_string = Basic.printListStr(map F.toOneStringDefault arg_sorts,fn x => x,"_")
val poly_con_name = poly_con_name_prefix^len_str^"_"^arg_sort_string^"_"^range_type_str^"_"^con_name
in
poly_con_name
end
val domain_decs: (MS.mod_symbol * string) list ref = ref([])
fun addDomain(pair as (sort_name:MS.mod_symbol,dec_string)) = domain_decs := pair::(!domain_decs)
fun writeDec(str) = TextIO.output(dec_stream,str)
fun writeMain(str) = TextIO.output(main_stream,str)
fun fsymTableInsert(fsym) = S.insert(fsym_table,fsym,true)
fun constructorString(c:Data.ath_struc_constructor as {name,arity,
selectors,argument_types,absyn_argument_types,range_type,sym_range_type,...},obtype_params,sort_args) =
(fsymTableInsert(name);
if null(obtype_params) then
if arity < 1 then fsym_prefix^(S.name name)
else
let fun getSelName(NONE:MS.mod_symbol option) =
let val str = fsym_prefix^(Names.smt_default_selector_fun_sym_prefix)^(Int.toString(Basic.incAndReturn(sel_counter)))
in
str
end
| getSelName(SOME(ms)) = (fsymTableInsert(ms);fsym_prefix^MS.name(ms))
val arg_sorts_and_sels = Basic.zip(argument_types,selectors)
fun makeSelString(arg_sort,sel) =
let val str = getSelName(sel)
val _ = possiblyDeclareSort(arg_sort)
in
str^double_colon^(sortNameSMT arg_sort)
end
val sel_string = Basic.printListStr(arg_sorts_and_sels,makeSelString," ")
in
lp^(fsym_prefix^(S.name name))^blank^sel_string^rp
end
else
let val argument_types_as_SymTerms = map SymTerm.stripTags absyn_argument_types
val (types_as_FTerms,mapping) =
symTermsToFTermsWithSub(sym_range_type::argument_types_as_SymTerms)
val range_type_as_fterm = hd types_as_FTerms
val arg_sorts_as_fterms = tl types_as_FTerms
fun makeSub([],_,res) = res
| makeSub(obtype_param::more,sort_arg::rest,res) =
(case Symbol.lookUp(mapping,obtype_param) of
SOME(fvar) => makeSub(more,rest,F.incSub(res,(fvar,sort_arg)))
| _ => makeSub(more,rest,res))
val fsub = makeSub(obtype_params,sort_args,F.empty_sub)
val argument_types' = F.applySubLst(fsub,arg_sorts_as_fterms)
val range_type_as_fterm' = F.applySub(fsub,range_type_as_fterm)
val range_type_str = F.toOneStringDefault(range_type_as_fterm')
val argument_types = argument_types'
fun getSelName(NONE:MS.mod_symbol option,arg_sort) = fsym_prefix^(Names.smt_default_selector_fun_sym_prefix)^(Int.toString(Basic.incAndReturn(sel_counter)))
| getSelName(SOME(ms),arg_sort) =
let val sel_str = makePolyConName(ms,F.toOneStringDefault(arg_sort),[range_type_as_fterm'])
val _ = HashTable.insert poly_to_mono_sort_table (sel_str,([range_type_as_fterm'],arg_sort))
val sel_ms' = msym(Symbol.symbol(sel_str))
val _ = fsymTableInsert(ms)
in
sel_str
end
val arg_sorts_and_sels = Basic.zip(argument_types,selectors)
fun makeSelString(arg_sort,sel) =
let val str = getSelName(sel,arg_sort)
val _ = possiblyDeclareSort(arg_sort)
in
str^double_colon^(sortNameSMT(arg_sort))
end
val sel_string = Basic.printListStr(arg_sorts_and_sels,makeSelString," ")
val poly_con_name = makePolyConName(name,range_type_str,argument_types')
val _ = HashTable.insert poly_to_mono_sort_table (poly_con_name,(argument_types',range_type_as_fterm'))
val poly_con_msym = msym(Symbol.symbol(poly_con_name))
val _ = fsymTableInsert(poly_con_msym)
in
if arity < 1 then poly_con_name else lp^poly_con_name^blank^sel_string^rp
end)
and constructorsString([],_,_,res) = res
| constructorsString(c::more,obtype_params,sort_args,res) = constructorsString(more,obtype_params,sort_args,
blank^(constructorString(c,obtype_params,sort_args))^res)
and declareSort(whole_sort,sort_root,sort_args,large_sort_sym) =
(case Data.findStructure(sort_root) of
SOME(struc as {constructors,arity,obtype_params,free,...}) =>
if (not free) then
let val poly_structure = (arity > 0)
val str = lp_define_type_blank^(S.name large_sort_sym)^rp
val _ = addDomain(large_sort_sym,str)
in
()
end
else
if MS.modSymEq(sort_root,mfmap_sort_symbol) then ()
else
let val str = lp_define_type_blank^(MS.name large_sort_sym)^" (datatype "^
(constructorsString(constructors,obtype_params,sort_args,""))^double_rp_newline
val _ = addDomain(large_sort_sym,str)
in
()
end
| _ => if (!Options.ground_doms) then
let
val whole_sort_name = F.toStringDefault(whole_sort)
val syms = (case (HashTable.find Data.domains_as_datatypes_table whole_sort_name) of
SOME(fsym_list) => fsym_list
| _ => let fun ev(s:string) = (!evaluateString)(s)
val str = "(make-fresh-constants \"" ^ whole_sort_name ^ "\" (get-flag \"dom-dt-default-size\"))"
val _ = ev(str)
in
(case (HashTable.find Data.domains_as_datatypes_table whole_sort_name) of
SOME(fsym_list) => fsym_list)
end)
val sym_names = map (fn fsym => let val name = Data.fsymName(fsym)
val _ = fsymTableInsert(name)
in name end) syms
val sym_names' = List.take(sym_names, (!Options.dt_for_dom_default_size))
val str = lp_define_type_blank^(MS.name large_sort_sym)^" (datatype "^
(Basic.printListStr(sym_names',fn x => fsym_prefix^(MS.name x), " ")) ^ double_rp_newline
in
addDomain(large_sort_sym,str)
end
else
let val str = lp_define_type_blank^(S.name large_sort_sym)^rp
in
addDomain(large_sort_sym,str)
end)
and possiblyDeclareSort(sort) =
let
val (sort_root,sort_args,large_sort_sym) =
(case F.isApp(sort) of
SOME(root,[]) => (root,[],root)
| SOME(root,args) => (root,args,msym(Symbol.symbol(F.toOneStringDefault(sort))))
| _ => raise Basic.Fail("\npossiblyDeclareSort failed on this sort: "^(F.toStringDefault(sort))))
in
(case S.find(domain_table,large_sort_sym) of
SOME(_) => ()
| _ => (
S.insert(domain_table,large_sort_sym,true);
declareSort(sort,sort_root,sort_args,large_sort_sym)))
end
fun writeVarDec(name,sort) = (TextIO.output(dec_stream,lp_define_blank);
TextIO.output(dec_stream,name);TextIO.output(dec_stream,"::");
TextIO.output(dec_stream,sort);TextIO.output(dec_stream,rp))
fun declareVar(v,vname) =
let val sort = ATV.getSort(v)
in
(writeVarDec(vname,sortNameSMT(sort));possiblyDeclareSort(sort))
end
fun inInstantiatedTable(str) = (case (HashTable.find poly_to_mono_sort_table str) of
SOME(_) => true | _ => false)
fun declareFSymAndItsSorts(fsym,fsym_name,arg_sorts,output_sort,is_poly) =
let val _ = (List.app possiblyDeclareSort arg_sorts;
possiblyDeclareSort(output_sort))
fun loop([]) = ()
| loop(sort::more) = (writeDec(sortNameSMT(sort));writeDec(blank);loop(more))
val isConstructorOrSelectorAndInTheTable =
(case S.find(fsym_table,fsym) of
SOME(_) => Data.isStructureConstructorBool(fsym) orelse Data.isSelector(fsym) orelse inInstantiatedTable(fsym_name)
| _ => false)
val _ = if is_poly andalso not(isConstructorOrSelectorAndInTheTable)
then HashTable.insert poly_to_mono_sort_table (fsym_name,(arg_sorts,output_sort))
else ()
in
(* It's conceivable that fsym is a constructor that was just inserted into fsym_table
during the above applications of possiblyDeclareSort; if so, don't redeclare: *)
if isConstructorOrSelectorAndInTheTable then ()
else
let val _ = fsymTableInsert(fsym)
in
if null(arg_sorts) then
(writeDec(lp_define_blank);writeDec(fsym_name);writeDec("::");
writeDec(sortNameSMT(output_sort));writeDec(")\n"))
else
(writeDec(lp_define_blank);writeDec(fsym_name);writeDec("::(-> ");
loop(arg_sorts);writeDec(sortNameSMT(output_sort));writeDec("))\n"))
end
end
fun loop(Var(v)) =
(case ATV.find(var_table,v) of
NONE => let val vname = var_prefix^(ATV.name(v))
val _ = declareVar(v,vname)
val _ = ATV.insert(var_table,v,v)
in
TextIO.output(main_stream,vname)
end
| _ => TextIO.output(main_stream,var_prefix^(ATV.name(v))))
| loop(t as App({root,args,sort,...})) =
let val code = S.code(root)
val is_built_in = code <= false_code
val is_poly_symbol = Data.isPolySymbol(root)
val name_str = if is_built_in then Unsafe.Array.sub(smt_names,code)
else
(case Data.isStructureConstructorOpt(root) of
SOME(_) => if is_poly_symbol then
makePolyConName(root,F.toOneStringDefault(sort),map getSort args)
else fsym_prefix^(S.name root)
| _ => if is_poly_symbol then makePolyConName(root,F.toOneStringDefault(sort),map getSort args)
else
fsym_prefix^(S.name root))
val _ = if is_built_in then ()
else (case S.find(fsym_table,root) of
SOME(_) => let val arg_sorts = map getSort args
val res = if is_poly_symbol then declareFSymAndItsSorts(root,name_str,arg_sorts,sort,is_poly_symbol) else ()
in
res
end
| _ => let val arg_sorts = map getSort args
val res = declareFSymAndItsSorts(root,name_str,arg_sorts,sort,is_poly_symbol)
in
res
end)
in
(case args of
[] => writeMain(name_str)
| s::more => if null(more) andalso MS.modSymEq(root,Names.msubtraction_symbol) then
( writeMain("(- 0 ");
loop(s);
writeMain(rp)
)
else
if MS.modSymEq(mfmap_empty_map_symbol,root) then
let val arg_sort = (case F.isApp(sort) of
SOME(_,[asort,_]) => asort)
in
(writeMain("(lambda (a::");
writeMain(sortNameSMT(arg_sort));
writeMain(") ");loop(s);writeMain(")"))
end
else if MS.modSymEq(mfmap_update_symbol,root) then
let val _ = ()
val (key,value) = (case s of
App({args,...}) => (hd args, hd(tl(args))))
val m = hd(more)
in
(writeMain("(update ");loop(m);writeMain(" (");
loop(key);writeMain(") ");loop(value);writeMain(")"))
end
else if MS.modSymEq(mfmap_apply_symbol,root) then
(writeMain(lp);loop(s);writeMain(blank);loop(hd more);writeMain(rp))
else (writeGenericTerm(name_str,args)))
end
| loop(numTerm(n)) = (case n of
A.int_num(_) => writeMain(A.athNumberToString(n))
| A.real_num(r,_) => let val (num,denum) = realToRat(n)
in
writeMain(num^"/"^denum)
end)
| loop(ideTerm(x)) = let val str' = (Names.metaIdPrefix)^(MetaId.name(x))
val sym = msym (Symbol.symbol str')
in
(case MS.find(fsym_table,sym) of
SOME(_) => writeMain(str')
| _ => let val _ = possiblyDeclareSort(D.ide_sort)
val _ = fsymTableInsert(sym)
in
(writeDec(lp_define_blank);
writeDec(str');
writeDec(double_colon^(Names.ide_sort_name)^")\n");
writeMain(str'))
end)
end
and loopLst([]) = ()
| loopLst([t]) = loop(t)
| loopLst(t1::(rest as (_::_))) = (loop(t1);writeMain(blank);loopLst(rest))
and writeGenericTerm(f_name,args) = (writeMain(lp^f_name);writeMain(blank);loopLst(args);writeMain(rp))
in
(loop(t);!domain_decs)
end
fun writeCVC(t,{domain_stream,dec_stream,main_stream,
var_table,inverse_var_table,
fsym_table:string ModSymbol.htable,
inverse_fsym_table,
domain_table,
inverse_domain_table,
fsym_counter,dom_counter,sel_counter,var_counter}) =
let
fun debugPrint(s) = ()
fun newFSymName(fsym_counter) = "f"^(Int.toString(Basic.incAndReturn(fsym_counter)))
fun newDomName(dom_str) =
(case (HashTable.find domain_table dom_str) of
SOME(str) => str
| _ => let val new_dom_name = "D"^(Int.toString(Basic.incAndReturn(dom_counter)))
val _ = (HashTable.insert domain_table (dom_str,new_dom_name))
val _ = (HashTable.insert inverse_domain_table (new_dom_name,dom_str))
in
new_dom_name
end)
val domain_decs: (string * string) list ref = ref([])
fun addDomain(pair as (sort_name:string,dec_string)) = domain_decs := pair::(!domain_decs)
fun constructorString(c:Data.ath_struc_constructor as {name,arity,
selectors,argument_types,absyn_argument_types,range_type,sym_range_type,...},obtype_params,sort_args) =
(if null(obtype_params) then (* This is a monomorphic constructor, fairly easy to translate.
Selectors will be introduced by default if none were given in the Athena declaration. *)
if arity < 1 then makeMonoFSymName(name)
else
let val _ = debugPrint("\nGetting the declaration of this non-nullary mono constructor: " ^ (MS.name(name)))
fun getSelName(NONE:MS.mod_symbol option) = fsym_prefix^(Names.smt_default_selector_fun_sym_prefix)^(Int.toString(Basic.incAndReturn(sel_counter)))
| getSelName(SOME(ms)) = (case MS.find(fsym_table,ms) of
SOME(fname) => fname
| _ => let val sel_name = newFSymName(fsym_counter)
val _ = (MS.insert(fsym_table,ms,sel_name);(HashTable.insert inverse_fsym_table (sel_name,ms)))
in
sel_name
end)
val arg_sorts_and_sels = Basic.zip(argument_types,selectors)
fun makeSelString(arg_sort,sel) =