-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathabstract_syntax.sml
executable file
·2695 lines (2434 loc) · 150 KB
/
abstract_syntax.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
(*======================================================================
This file defines Athena's abstract syntax and implements related functionality
(such as code for computing free identifier occurrences). The main datatypes are
expression, deduction, phrase, and pattern. Values of these types are tagged
with position information (line, column number, and file name).
=======================================================================*)
structure AbstractSyntax =
struct
structure S = Symbol
structure MS = ModSymbol
structure N = Names
val dummy_sym = S.symbol ")!dum_sym"
val posToString = Position.posToString
type position = Position.PosType
type pos = position
type symbol = Symbol.symbol
type mod_symbol = ModSymbol.mod_symbol
val dum_pos = Position.dummy_pos
exception SyntaxError of string * (position option)
fun commaHere(pos) = raise SyntaxError("A comma was expected here", SOME pos)
exception LexError of string * (position option)
type absyn_term = position SymTerm.tagged_term
type param = {name:symbol, pos:position}
fun getParams1(plist) =
let fun f([]:param list,names,res) = rev(res)
| f((p as {name,pos})::more,names,res) =
if Basic.isMemberEq(name,names,Symbol.symEq) then
raise SyntaxError("Duplicate entry found in identifier list: "^Symbol.name(name),SOME pos)
else
f(more,name::names,p::res)
in
f(plist,[],[])
end
type absyn_structure_constructor = {name:symbol,pos:position,selectors:param option list,
argument_types:absyn_term list}
type absyn_structure_profile = {name:symbol,pos:position,obtype_params:param list}
type absyn_structure = {name:symbol,pos:position,obtype_params:param list,free:bool,
constructors:absyn_structure_constructor list}
fun makeFree(ab_struc:absyn_structure as {name,pos,obtype_params,constructors,...}) =
{name=name,pos=pos,obtype_params=obtype_params,constructors=constructors,free=true}
fun makeNonFree(ab_struc:absyn_structure as {name,pos,obtype_params,constructors,...}) =
{name=name,pos=pos,obtype_params=obtype_params,constructors=constructors,free=false}
type absyn_domain = {name:symbol,arity:int,sort_predicate: mod_symbol option, pos:position}
datatype prop_con = notCon | andCon | orCon | ifCon | iffCon
| forallCon | existsCon | existsUniqueCon
datatype quant_con = universal | existential | unique_existential
fun quantConToString(universal) = N.forall_name
| quantConToString(existential) = N.exists_name
| quantConToString(unique_existential) = N.exists_unique_name
val not_con_passoc = {prec=ref(50),assoc=ref (NONE:bool option)}
val and_con_passoc = {prec=ref(30),assoc=ref (SOME false)}
val or_con_passoc = {prec=ref(20),assoc= ref (SOME false)}
val if_con_passoc = {prec=ref(10),assoc= ref (SOME false)}
val iff_con_passoc = {prec=ref(10),assoc= ref (SOME false)}
fun getPropConArityAndPrec(pc) =
(case pc of
notCon => (1,#prec(not_con_passoc))
| andCon => (2,#prec(and_con_passoc))
| orCon => (2,#prec(or_con_passoc))
| ifCon => (2,#prec(if_con_passoc))
| iffCon => (2,#prec(iff_con_passoc)))
fun set_PC_Assoc(notCon,b:bool) = #assoc(not_con_passoc) := SOME(b)
| set_PC_Assoc(andCon,b) = #assoc(and_con_passoc) := SOME(b)
| set_PC_Assoc(orCon,b) = #assoc(or_con_passoc) := SOME(b)
| set_PC_Assoc(ifCon,b) = #assoc(if_con_passoc) := SOME(b)
| set_PC_Assoc(iffCon,b) = #assoc(iff_con_passoc) := SOME(b)
fun isQuantPropCon(forallCon) = true
| isQuantPropCon(existsCon) = true
| isQuantPropCon(existsUniqueCon) = true
| isQuantPropCon(_) = false
fun propConToSymbol(notCon) = N.not_symbol
| propConToSymbol(andCon) = N.and_symbol
| propConToSymbol(orCon) = N.or_symbol
| propConToSymbol(ifCon) = N.if_symbol
| propConToSymbol(iffCon) = N.iff_symbol
| propConToSymbol(forallCon) = N.forall_symbol
| propConToSymbol(existsCon) = N.exists_symbol
| propConToSymbol(existsUniqueCon) = N.exists_unique_symbol
fun propConToString(notCon) = N.not_name
| propConToString(andCon) = N.and_name
| propConToString(orCon) = N.or_name
| propConToString(ifCon) = N.if_name
| propConToString(iffCon) = N.iff_name
| propConToString(forallCon) = N.forall_name
| propConToString(existsCon) = N.exists_name
| propConToString(existsUniqueCon) = N.exists_unique_name
fun hashPropCon(pc) = Basic.hashInt(S.code(propConToSymbol(pc)))
fun isPropCon(name) =
S.symEq(name,N.not_symbol) orelse
S.symEq(name,N.and_symbol) orelse
S.symEq(name,N.or_symbol) orelse
S.symEq(name,N.if_symbol) orelse
S.symEq(name,N.iff_symbol) orelse
S.symEq(name,N.forall_symbol) orelse
S.symEq(name,N.exists_symbol) orelse
S.symEq(name,N.exists_unique_symbol)
(* version for module symbols: *)
fun isPropConMS(name) =
ModSymbol.modSymEq(name,N.mnot_symbol) orelse
ModSymbol.modSymEq(name,N.mand_symbol) orelse
ModSymbol.modSymEq(name,N.mor_symbol) orelse
ModSymbol.modSymEq(name,N.mif_symbol) orelse
ModSymbol.modSymEq(name,N.miff_symbol) orelse
ModSymbol.modSymEq(name,N.mforall_symbol) orelse
ModSymbol.modSymEq(name,N.mexists_symbol) orelse
ModSymbol.modSymEq(name,N.mexists_unique_symbol)
fun isQuantName(name) =
S.symEq(name,N.forall_symbol) orelse
S.symEq(name,N.exists_symbol) orelse
S.symEq(name,N.exists_unique_symbol)
fun isPropConOpt(id) =
if (S.symEq(id,N.not_symbol) orelse S.symEq(id,N.alternate_not_symbol)) then SOME(notCon)
else
if (S.symEq(id,N.and_symbol) orelse S.symEq(id,N.alternate_and_symbol)) then SOME(andCon)
else
if (S.symEq(id,N.or_symbol) orelse S.symEq(id,N.alternate_or_symbol)) then SOME(orCon)
else
if (S.symEq(id,N.if_symbol) orelse S.symEq(id,N.alternate_if_symbol)) then SOME(ifCon)
else
if (S.symEq(id,N.iff_symbol) orelse S.symEq(id,N.alternate_iff_symbol)) then SOME(iffCon)
else
if S.symEq(id,N.forall_symbol) then SOME(forallCon)
else
if S.symEq(id,N.exists_symbol) then SOME(existsCon)
else
if S.symEq(id,N.exists_unique_symbol) then SOME(existsUniqueCon)
else NONE
fun propConToString(con) = Symbol.name(propConToSymbol(con))
fun makeFreshIds(syms) =
let fun f([],_,res) = res
| f(sym::more,i,res) = f(more,i+1,Symbol.symbol("!"^Int.toString(i))::res)
in
f(syms,1,[])
end
datatype ath_number = int_num of int * string ref | real_num of real * string ref
fun getRealMagnitude(int_num(i,_)) = Real.fromInt(i)
| getRealMagnitude(real_num(r,_)) = r
fun compareNumbers(int_num(i,_),int_num(j,_)) = String.compare(Int.toString(i),Int.toString(j))
| compareNumbers(real_num(i,_),real_num(j,_)) = String.compare(Real.toString(i),Real.toString(j))
| compareNumbers(int_num(i,_),real_num(j,_)) = String.compare(Int.toString(i),Real.toString(j))
| compareNumbers(real_num(i,_),int_num(j,_)) = String.compare(Real.toString(i),Int.toString(j))
fun hashANum(int_num(i,_)) = Basic.hashInt(i)
| hashANum(real_num(r,_)) = Basic.hashString(Real.toString(r))
fun convertRealStr(str) =
let val tokens = String.tokens (fn c => c = #".") str
in
case tokens of
[int,dec] =>
let val dec_len = String.size dec
val count = let fun f(i,count) = if i >= 0 andalso String.sub(dec,i) = #"0" then f(i-1,count+1) else count
in f(dec_len-1,0) end
val dec' = if count >= dec_len then "" else String.substring(dec,0,dec_len - count)
val new_dec = if dec' = "" then "0" else dec'
in int^"."^new_dec end
| _ => str
end
fun athenaNumberToString(int_num(i,box),SML_negative_number_format) =
let val box_cont = !box
in
if box_cont = "" then
let val res = if i < 0 then
(if SML_negative_number_format then "~"^(Int.toString (Int.abs i))
else "("^Names.subtraction_name^" "^(Int.toString (Int.abs i))^")")
else Int.toString(i)
val _ = box := res
in res end
else box_cont
end
| athenaNumberToString(real_num(r,box),SML_negative_number_format) =
let val box_cont = !box
in
if (box_cont = "" orelse SML_negative_number_format) then
let val res = if r < 0.0 then
(if SML_negative_number_format then "~"^(convertRealStr (Real.fmt (StringCvt.FIX NONE) (Real.abs r)))
else "("^(Names.subtraction_name)^" "^(convertRealStr (Real.fmt (StringCvt.FIX NONE) (Real.abs r)))^")")
else convertRealStr (Real.fmt (StringCvt.FIX NONE) r)
val _ = box := res
in res end
else let val _ = ()
in
box_cont
end
end
fun athNumberToString(x) = athenaNumberToString(x,false)
fun athNumEquality(int_num(i,_),int_num(j,_)) = i = j
| athNumEquality(real_num(r1,_),real_num(r2,_)) = Real.==(r1,r2)
| athNumEquality(int_num(i,_),real_num(r,_)) = Real.==(Real.fromInt(i),r)
| athNumEquality(real_num(r,_),int_num(i,_)) = Real.==(Real.fromInt(i),r)
fun literalAthNumEquality(a1,a2) = (athNumberToString(a1) = athNumberToString(a2))
val re_code_cell = ref(0)
fun getRECode() = Basic.returnAndInc(re_code_cell)
datatype expression = idExp of {msym:MS.mod_symbol, mods:S.symbol list,sym: S.symbol, no_mods:bool,pos:position}
| taggedConSym of {name:mod_symbol, sort_as_tagged_symterm: absyn_term,
sort_as_fterm:FTerm.term option,pos:position}
| opExp of {op_exp: expression, pos: position}
| numExp of {number:ath_number,pos:position}
| quotedIdeExp of {name:string,pos:position}
| unitExp of {pos:position}
| charExp of {code:int,pos:position}
| logicalAndExp of {args:phrase list,pos:position}
| logicalOrExp of {args:phrase list,pos:position}
| whileExp of {test:phrase,body:phrase,pos:position}
| beginExp of {members:phrase list,pos:position}
| stringExp of {str:int list,pos:position ,mem_index:int}
| termVarExp of {term_var:AthTermVar.ath_term_var,user_sort:absyn_term option,pos:position}
| propConExp of {con:prop_con,pos:position}
| checkExp of {clauses:check_clause list,pos:position}
| functionExp of {params:possibly_wildcard_param list, body:expression, pos:position}
| appExp of {proc:phrase, args: phrase list, pos:position,alt_exp: expression option ref}
| UAppExp of {proc:phrase, arg: phrase, pos:position}
| BAppExp of {proc:phrase, arg1: phrase, arg2:phrase, pos:position}
| listExp of {members:phrase list,pos:position}
| methodExp of {params:possibly_wildcard_param list, body:deduction,
pos:position, name:string ref}
| matchExp of {discriminant:phrase,clauses:match_clause list, pos:position}
| tryExp of {choices:expression list,pos:position}
| letExp of {bindings:binding list, body: expression, pos:position}
| letRecExp of {bindings:binding list, body: expression, pos:position}
| cellExp of {contents:phrase,pos:position}
| refExp of {cell_exp:expression,pos:position}
| setCellExp of {cell_exp:expression,set_phrase:phrase,pos:position}
| vectorInitExp of {length_exp:expression,init_val:phrase,pos:position}
| vectorSetExp of {vector_exp:expression,index_exp:expression,new_val:phrase,pos:position}
| vectorSubExp of {vector_exp:expression,index_exp:expression,pos:position}
and deduction =
assumeDed of {assumption: phrase,body:deduction,pos:position}
| byCasesDed of {disj:phrase,from_exps:expression list option,
arms:case_clause list,pos:position}
| infixAssumeDed of {bindings: binding list, body:deduction, pos:position}
| assumeLetDed of {bindings: binding list, body:deduction, pos:position}
| absurdDed of {hyp:phrase,body:deduction,pos:position}
| absurdLetDed of {named_hyp:binding, body:deduction, pos:position}
| methodAppDed of {method:expression, args: phrase list, pos:position}
| UMethAppDed of {method:expression, arg: phrase, pos:position}
| BMethAppDed of {method:expression, arg1: phrase, arg2: phrase, pos:position}
| matchDed of {discriminant:phrase,clauses:dmatch_clause list, pos:position}
| inductionDed of {prop:phrase,clauses:dmatch_clause list,
pos:position}
| structureCasesDed of {prop:phrase,term:expression option,clauses:dmatch_clause list, pos:position}
| tryDed of {choices:deduction list,pos:position}
| letDed of {bindings:binding list, body: deduction, pos:position}
| letRecDed of {bindings:binding list, body: deduction, pos:position}
| beginDed of {members:deduction list,pos:position}
| checkDed of {clauses:dcheck_clause list,pos:position}
| byDed of {wanted_res:expression,conc_name:param option,body:deduction,pos:position}
| fromDed of {conclusion:expression,premises:expression,pos:position}
| genOverDed of {eigenvar_exp:expression,body:deduction,pos:position}
| pickAnyDed of {eigenvars: possibly_typed_param list,body:deduction,pos:position}
| withWitnessDed of {eigenvar_exp:expression,ex_gen:phrase,body:deduction,pos:position}
| pickWitnessDed of {ex_gen:phrase,var_id:symbol,inst_id:symbol option,body:deduction,pos:position}
| pickWitnessesDed of {ex_gen:phrase,var_ids:symbol list,
inst_id:symbol option,body:deduction,pos:position}
and phrase = exp of expression | ded of deduction
and condition = boolCond of phrase | elseCond
and pattern = idPat of possibly_typed_param
| anyPat of {pos:position}
| funSymPat of {name:mod_symbol,sort_opt: FTerm.term option, sort_as_exp: expression option, arity:int,pos:position}
| propConPat of {pcon:prop_con,pos:position}
| numPat of {ath_num:ath_number,pos:position}
| constantTermVarPat of {term_var:AthTermVar.ath_term_var,pos:position}
| constantMetaIdPat of {name:symbol,pos:position}
| constantStringPat of {str:int list,pos:position}
| constantCharPat of {ch:int,pos:position}
| listPats of {member_pats:pattern list,pos:position} (* These are [pi_1 ... pi_n] *)
| listPat of {head_pat:pattern,tail_pat:pattern,pos:position}
| cellPat of {pat:pattern,pos:position}
| splitPat of {pats:pattern list,pos:position, re_form: (pattern,expression) GeneralRE.RE0,code:int}
| reStarPat of {pat:pattern,pos:position,re_form: (pattern,expression) GeneralRE.RE0,code:int}
| rePlusPat of {pat:pattern,pos:position,re_form: (pattern,expression) GeneralRE.RE0,code:int}
| reRepPat of {pat:pattern,times:int,pos:position,re_form: (pattern,expression) GeneralRE.RE0,code:int}
| reLitPat of {pat:pattern,pos:position}
| reRangePat of {from_pat:pattern,to_pat:pattern,lo:real,hi:real,pos:position}
| reOptPat of {pat:pattern,pos:position,re_form: (pattern,expression) GeneralRE.RE0,code:int}
| valOfPat of {id:param,lex_ad: (int * int) option, pos:position}
| valOfPat1 of {id:param,num:ath_number,pos:position}
| unitValPat of {pos:position}
| namedPat of {name:symbol,pat:pattern,pos:position}
| someCharPat of {id:possibly_wildcard_param,pos:position}
| someVarPat of {id:possibly_wildcard_param,pos:position}
| someQuantPat of {id:possibly_wildcard_param,pos:position}
| somePropConPat of {id:possibly_wildcard_param,pos:position}
| someTermPat of {id:possibly_wildcard_param,pos:position}
| someAtomPat of {id:possibly_wildcard_param,pos:position}
| somePropPat of {id:possibly_wildcard_param,pos:position}
| someFunctionPat of {id:possibly_wildcard_param,pos:position}
| someMethodPat of {id:possibly_wildcard_param,pos:position}
| someSymbolPat of {id:possibly_wildcard_param,pos:position}
| someSubPat of {id:possibly_wildcard_param,pos:position}
| someTablePat of {id:possibly_wildcard_param,pos:position}
| someMapPat of {id:possibly_wildcard_param,pos:position}
| someListPat of {id:possibly_wildcard_param,pos:position}
| someVectorPat of {id:possibly_wildcard_param,pos:position}
| someCellPat of {id:possibly_wildcard_param,pos:position}
| compoundPat of {head_pat:pattern,rest_pats:pattern list,pos:position}
| wherePat of {pat:pattern,guard:expression,pos:position}
| disjPat of {pats:pattern list,pos:position}
and possibly_wildcard_param = someParam of possibly_typed_param | wildCard of position
withtype binding = {bpat:pattern,def:phrase,pos:position}
and optBinding = {param:possibly_wildcard_param option,def:phrase,pos:position}
and match_clause = {pat:pattern,exp:expression}
and dmatch_clause = {pat:pattern,ded:deduction}
and case_clause = {case_name:param option,alt:expression,proof:deduction}
and check_clause = {test:condition,result:expression}
and dcheck_clause = {test:condition,result:deduction}
and possibly_typed_param = {name:symbol,pos:position,sort_as_sym_term:absyn_term option,op_tag: (int * int) option,
sort_as_fterm:FTerm.term option,sort_as_exp: expression option}
type absyn_fsym = {name:symbol,pos:position,obtype_params:param list,input_transformer: expression list option,
argument_types:absyn_term list,range_type:absyn_term,
prec: int option,assoc: bool option,overload_sym: param option}
fun isValOfPat(valOfPat({id={name,...},...})) = SOME(name)
| isValOfPat(_) = NONE
fun makeBinarySplitPats(pats as p1::p2::rest,pos,code_value) =
let fun f(pats as p1::p2::rest) = splitPat({pats=[p1,f(p2::rest)],pos=pos,re_form=GeneralRE.any0(GeneralRE.trivial_tag),code=(~1)})
| f([p]) = p
in
(case (f pats) of
splitPat({pats,pos,re_form,...}) => splitPat({pats=pats,pos=pos,re_form=re_form,code=code_value})
| p => p)
end
fun makePTP(sym) = let val res:possibly_typed_param = {name=sym,pos=dum_pos,sort_as_sym_term=NONE,
sort_as_fterm=NONE,sort_as_exp=NONE,op_tag=NONE}
in
res
end
fun makePTPWithPos(sym,p) =
let val res:possibly_typed_param = {name=sym,pos=p,sort_as_sym_term=NONE,sort_as_fterm=NONE,sort_as_exp=NONE,op_tag=NONE}
in res end
fun inapplicable(phrase) =
(case phrase of
exp(taggedConSym(_)) => true
| exp(numExp(_)) => true
| exp(termVarExp({user_sort,...})) =>
(case user_sort of
NONE => true
| SOME(sort) => (case SymTerm.isTaggedApp(sort) of
SOME(f,_,args) => not(MS.modSymEq(f,Names.fun_name_msym))
| _ => true))
| exp(quotedIdeExp(_)) => true
| exp(unitExp(_)) => true
| exp(charExp(_)) => true
| exp(stringExp(_)) => true
| exp(listExp(_)) => true
| exp(cellExp(_)) => true
| _ => false)
fun makePosLessIdPat(sym) = idPat({name=sym,pos=dum_pos,op_tag=NONE,sort_as_sym_term=NONE,sort_as_fterm=NONE,sort_as_exp=NONE})
fun posOfPWP(someParam({pos,...})) = pos
| posOfPWP(wildCard(pos)) = pos
fun pwpToString(someParam({name,...})) = Symbol.name(name)
| pwpToString(wildCard(_)) = Names.wild_card
fun pwpToSym(someParam({name,sort_as_fterm,...})) = name
| pwpToSym(wildCard(_)) = Names.wild_card_symbol
fun pwpToSymAndSortOpt(someParam({name,sort_as_fterm,...})) = (name,sort_as_fterm)
| pwpToSymAndSortOpt(wildCard(_)) = (Names.wild_card_symbol,NONE)
fun getPWParamNames(pwp_list) =
let fun f([]:possibly_wildcard_param list,res) = rev res
| f((wildCard(_))::more,res) = f(more,res)
| f(someParam({name,pos,...})::more,res) =
if Basic.isMemberEq(name,res,Symbol.symEq) then
raise SyntaxError("Duplicate entry found in identifier list: "^Symbol.name(name),SOME pos)
else f(more,name::res)
in
f(pwp_list,[])
end
fun getParamNames(plist) =
let fun f([]:param list,res) = rev(res)
| f({name,pos}::more,res) = if Basic.isMemberEq(name,res,Symbol.symEq) then
raise SyntaxError("Duplicate entry found in identifier list: "^
Symbol.name(name),SOME pos)
else
f(more,name::res)
in
f(plist,[])
end
fun getParams(plist) =
let fun f([]:param list,names,res) = rev(res)
| f((p as {name,pos})::more,names,res) =
if Basic.isMemberEq(name,names,Symbol.symEq) then
raise SyntaxError("Duplicate entry found in identifier list: "^Symbol.name(name),SOME pos)
else
f(more,name::names,p::res)
in
f(plist,[],[])
end
fun checkForDuplicateParams(possibly_typed_params) =
let fun f([]:possibly_typed_param list, res) = rev res
| f((p as {name,pos,...})::more,res) =
if Basic.isMemberEq(name,res,Symbol.symEq) then
raise SyntaxError("Duplicate entry found in identifier list: "^
Symbol.name(name),SOME pos)
else f(more,name::res)
in
(f(possibly_typed_params,[]);possibly_typed_params)
end
fun getBindings(obl) =
let fun f([],res) = rev res
| f(({param=popt,def,pos}::rest):optBinding list,res) =
(case popt of
SOME(pwp) => let val pat = (case pwp of
someParam(ptp) => idPat(ptp)
| wildCard(pos) => anyPat({pos=pos}))
val b:binding = {bpat=pat,def=def,pos=pos}
in f(rest,b::res) end
| _ => let val b = {bpat=anyPat({pos=pos}),def=def,pos=pos} in f(rest,b::res) end)
in
f(obl,[])
end
fun msym(s) = ModSymbol.makeModSymbol([],s,s)
val mSym = msym
fun msyms(sym_set) = let val syms = Symbol.listSymbols(sym_set)
val msyms = map msym syms
in
ModSymbol.symListToSet(msyms)
end
fun makeIdExp(str,pos) =
let val s = Symbol.makePrivateSymbol(str)
in
idExp({msym=msym(s),mods=[],sym=s,no_mods=true,pos=pos})
end
fun makeIdExpSimple(str,pos) =
let val s = Symbol.symbol(str)
in
idExp({msym=msym(s),mods=[],sym=s,no_mods=true,pos=pos})
end
fun makeIdExpSimple'(sym,pos) =
let val s = sym
in
idExp({msym=msym(s),mods=[],sym=s,no_mods=true,pos=pos})
end
fun makeIdExpSimpleNP(s) = idExp({msym=msym(s),mods=[],sym=s,no_mods=true,pos=dum_pos})
fun makeMapExp(bindings,map_pos) =
let val proc = makeIdExp(N.addMapFun_name,map_pos)
val empty_mapping = makeIdExp(N.empty_mapping_name,map_pos)
in
appExp({proc=exp(proc),args=[exp(empty_mapping),exp(listExp({members=bindings,pos=map_pos}))],pos=map_pos,alt_exp=ref(NONE)})
end
fun makeAbSynConjunction(exps) =
appExp({proc=exp(idExp({msym=N.mand_symbol,mods=[],sym=N.and_symbol,no_mods=true,pos=dum_pos})),args=(map exp exps),alt_exp=ref(NONE),pos=dum_pos})
type absyn_symbol_definition = {name:symbol,pos:position,condition:expression,abbreviated:bool}
fun isDeduction(ded(_)) = true
| isDeduction(_) = false
fun isNamedDeduction(ded(byDed({conc_name=SOME({name,...}),...}))) = (true,SOME name)
| isNamedDeduction(ded(byDed({wanted_res=idExp({msym,...}),...}))) = (true,SOME(MS.nameAsSymbol(msym)))
| isNamedDeduction(ded(inductionDed({prop=exp(idExp({msym,...})),...}))) = (true,SOME(MS.nameAsSymbol(msym)))
| isNamedDeduction(ded(structureCasesDed({prop=exp(idExp({msym,...})),...}))) = (true,SOME(MS.nameAsSymbol(msym)))
| isNamedDeduction(ded(_)) = (true,NONE)
| isNamedDeduction(_) = (false,NONE)
fun isExpression(exp(_)) = true
| isExpression(_) = false
val isTermApp =
let fun f([],arg_terms) = SOME(rev(arg_terms))
| f(exp(e)::more,arg_terms) = (case isTermApp(e) of
SOME(t) => f(more,t::arg_terms)
| _ => NONE)
| f(_,_) = NONE
and isTermApp(idExp({msym,...})) = SOME(SymTerm.makeConstant(msym))
| isTermApp(numExp({number,pos,...})) = SOME(SymTerm.makeConstant(msym(S.symbol(athNumberToString(number)))))
| isTermApp(appExp({proc=exp(idExp({msym,...})),args as _::_,pos,...})) =
(case f(args,[]) of
SOME(terms) => SOME(SymTerm.makeApp(msym,terms))
| _ => NONE)
| isTermApp(appExp({proc=exp(opExp({op_exp=idExp({msym,...}),...})),args as _::_,pos,...})) =
(case f(args,[]) of
SOME(terms) => SOME(SymTerm.makeApp(msym,terms))
| _ => NONE)
| isTermApp(_) = NONE
in
isTermApp
end
val isSExpApp =
let fun f([]) = true
| f(exp(e)::more) = isTermApp(e) andalso f(more)
| f(_) = false
and isTermApp(idExp(_)) = true
| isTermApp(taggedConSym(_)) = true
| isTermApp(unitExp(_)) = true
| isTermApp(charExp(_)) = true
| isTermApp(logicalAndExp(_)) = true
| isTermApp(logicalOrExp(_)) = true
| isTermApp(numExp({number,pos,...})) = true
| isTermApp(stringExp(_)) = true
| isTermApp(termVarExp(_)) = true
| isTermApp(propConExp(_)) = true
| isTermApp(quotedIdeExp({name,pos,...})) = true
| isTermApp(appExp({proc,args as _::_,pos,...})) = f(proc::args)
| isTermApp(_) = false
in
isTermApp
end
val isTermAppWithQuotedSymbolsAsVars =
let fun f([],arg_terms) = SOME(rev(arg_terms))
| f(exp(e)::more,arg_terms) = (case isTermApp(e) of
SOME(t) => f(more,t::arg_terms)
| _ => NONE)
| f(_,_) = NONE
and isTermApp(idExp({msym,...})) = SOME(SymTerm.makeConstant(msym))
| isTermApp(numExp({number,pos,...})) = SOME(SymTerm.makeConstant(msym(S.symbol(athNumberToString(number)))))
| isTermApp(quotedIdeExp({name,pos,...})) = SOME(SymTerm.makeVar(Symbol.symbol(name)))
| isTermApp(appExp({proc=exp(idExp({msym,...})),args as _::_,pos,...})) =
(case f(args,[]) of
SOME(terms) => SOME(SymTerm.makeApp(msym,terms))
| _ => NONE)
| isTermApp(appExp({proc=exp(opExp({op_exp=idExp({msym,...}),...})),args as _::_,pos,...})) =
(case f(args,[]) of
SOME(terms) => SOME(SymTerm.makeApp(msym,terms))
| _ => NONE)
| isTermApp(_) = NONE
in
isTermApp
end
fun posOfPat(idPat({pos,...})) = pos
| posOfPat(anyPat({pos})) = pos
| posOfPat(funSymPat({pos,...})) = pos
| posOfPat(propConPat({pos,...})) = pos
| posOfPat(numPat({pos,...})) = pos
| posOfPat(constantTermVarPat({pos,...})) = pos
| posOfPat(constantMetaIdPat({pos,...})) = pos
| posOfPat(constantStringPat({pos,...})) = pos
| posOfPat(constantCharPat({pos,...})) = pos
| posOfPat(listPat({pos,...})) = pos
| posOfPat(cellPat({pos,...})) = pos
| posOfPat(listPats({pos,...})) = pos
| posOfPat(splitPat({pos,...})) = pos
| posOfPat(reStarPat({pos,...})) = pos
| posOfPat(rePlusPat({pos,...})) = pos
| posOfPat(reRangePat({pos,...})) = pos
| posOfPat(reRepPat({pos,...})) = pos
| posOfPat(reLitPat({pos,...})) = pos
| posOfPat(reOptPat({pos,...})) = pos
| posOfPat(valOfPat({pos,...})) = pos
| posOfPat(valOfPat1({pos,...})) = pos
| posOfPat(someVarPat({pos,...})) = pos
| posOfPat(unitValPat({pos,...})) = pos
| posOfPat(namedPat({pos,...})) = pos
| posOfPat(someQuantPat({pos,...})) = pos
| posOfPat(somePropConPat({pos,...})) = pos
| posOfPat(someTermPat({pos,...})) = pos
| posOfPat(someAtomPat({pos,...})) = pos
| posOfPat(somePropPat({pos,...})) = pos
| posOfPat(someFunctionPat({pos,...})) = pos
| posOfPat(someMethodPat({pos,...})) = pos
| posOfPat(someSymbolPat({pos,...})) = pos
| posOfPat(someSubPat({pos,...})) = pos
| posOfPat(someListPat({pos,...})) = pos
| posOfPat(someTablePat({pos,...})) = pos
| posOfPat(someMapPat({pos,...})) = pos
| posOfPat(someVectorPat({pos,...})) = pos
| posOfPat(someCharPat({pos,...})) = pos
| posOfPat(someCellPat({pos,...})) = pos
| posOfPat(compoundPat({pos,...})) = pos
| posOfPat(disjPat({pos,...})) = pos
| posOfPat(wherePat({pos,...})) = pos
fun possiblyTypedParamToString(ptp:possibly_typed_param as {name,pos,sort_as_sym_term=SOME(t),...}) =
(Symbol.name(name))^":"^(SymTerm.taggedTermToString(t))
| possiblyTypedParamToString(ptp:possibly_typed_param as {name,pos,sort_as_fterm=SOME(sort),...}) =
(Symbol.name(name))^":"^(FTerm.toStringDefault(sort))
| possiblyTypedParamToString(ptp:possibly_typed_param as {name,pos,op_tag=SOME(i,j),...}) =
(Symbol.name(name))^(if j < 0 then (if i < 0 then "" else ":(OP "^(Int.toString(i))^")")
else ":(OP "^(Int.toString(i))^" "^(Int.toString(j))^")")
| possiblyTypedParamToString(ptp:possibly_typed_param as {name,...}) = (Symbol.name(name))
fun printInductivePattern(idPat(ptp)) = possiblyTypedParamToString(ptp)
| printInductivePattern(anyPat(_)) = Names.wild_card
| printInductivePattern(funSymPat({name,...})) = MS.name(name)
| printInductivePattern(numPat({ath_num,...})) = athNumberToString(ath_num)
| printInductivePattern(compoundPat({head_pat=funSymPat({name,...}),rest_pats,...})) =
"("^MS.name(name)^" "^printLst(rest_pats)^")"
| printInductivePattern(_) = ""
and
printLst([]) = ""
| printLst([pat]) = printInductivePattern(pat)
| printLst(pat1::pat2::more) = printInductivePattern(pat1)^" "^printLst(pat2::more)
(* This will return false (i.e., clauses will NOT be ok) iff an else condition *)
(* appears in a non-trailing position. *)
fun okClauses([]) = true
| okClauses({test=elseCond,result}::more) = if not(null(more)) then false else okClauses(more)
| okClauses(_::more) = okClauses(more)
fun checkClauses(clauses,pos) = if not(okClauses(clauses)) then
raise SyntaxError("Ill-formed check expression---else clause "^
"in non-trailing position",SOME(pos))
else ()
datatype directive = loadFile of expression * pos
| addPath of expression * pos
| loadOnly of phrase list * pos
| openModule of (mod_symbol * pos) list
| overload of (phrase * phrase * pos * pos * pos) list * pos * {inverted:bool}
| expandInput of phrase list * phrase * pos
| transformOutput of (phrase * phrase * {first_arg_pos:pos,second_arg_pos:pos,overall_pos:pos})
| setPrecedence of (mod_symbol * pos) list * expression
| setAssoc of (mod_symbol * pos) list * bool (* true: left-associative, false: right-associative *)
| useTermParser of {tp_name:param,file:string}
| usePropParser of {pp_name:param,file:string}
| expandNextProof of pos
| exitAthena of pos
| printStackTrace of pos
| clear_assum_base
| sortDefinition of symbol * phrase * bool
| definition of symbol * phrase * bool
| definitionLst of (pattern list) * (symbol option) * phrase * pos * bool
| definitions of (possibly_typed_param * expression) list * bool
| funSymDefinition of symbol * param list * expression
| assert of expression list
| assertClose of expression list
| assertCloseAsgn of (param * expression) list
| assertAsgn of param * expression
| retract of expression list
| ruleDefinition of symbol * expression
| findModel of expression * pos
| addDemon of expression * pos
| addDemons of expression list * pos
| setFlag of param * (string * pos)
fun makeFunDefinition({fun_name as {name,sort_as_sym_term,sort_as_fterm,sort_as_exp,op_tag,...}:possibly_typed_param,
fun_params,fun_body,pos,file}) =
let val arity = List.length(fun_params)
val fun_body_exp = functionExp({params=fun_params,body=fun_body,pos=pos})
in
(case op_tag of
NONE => ({name=name,
pos=(#pos(fun_name)),
sort_as_sym_term=sort_as_sym_term,
sort_as_fterm=sort_as_fterm,
sort_as_exp=sort_as_exp,
op_tag=SOME(arity,~1)},
fun_body_exp)
| _ => ({name=name,pos=(#pos(fun_name)),sort_as_sym_term=sort_as_sym_term,sort_as_fterm=sort_as_fterm,
sort_as_exp=sort_as_exp,op_tag=op_tag},fun_body_exp))
end
fun ptpToExp({name,pos,...}:possibly_typed_param) = makeIdExpSimple'(name,pos)
fun wildParamToExp(someParam(ptp)) = ptpToExp(ptp)
fun makeMemoizedFunDefinition({fun_name as {name,sort_as_sym_term,sort_as_fterm,sort_as_exp,op_tag,...}:possibly_typed_param,
fun_params,fun_body,pos,file}) =
let val arity = List.length(fun_params)
val fun_name_sym = name
val fun_params = map (fn wildCard(_) => someParam(makePTP(Symbol.freshSymbol(NONE)))
| p => p)
fun_params
fun makeList(params) = if arity < 2 then wildParamToExp(hd(fun_params)) else listExp({members=(map (fn p => exp(wildParamToExp(p))) fun_params),pos=dum_pos})
val param_list = makeList(fun_params)
val fun_body_exp =
let val ht = Symbol.symbol("H")
val make_table_call = appExp({proc=exp(makeIdExpSimple'(Symbol.makePrivateSymbol(Names.makeTableFun_name),dum_pos)),
alt_exp=ref(NONE),
args=[exp(numExp({number=int_num(50,ref("")),pos=dum_pos}))],pos=dum_pos})
val b:binding = {bpat=idPat(makePTP(ht)),def=exp(make_table_call),pos=dum_pos}
val try_1 = appExp({proc=exp(makeIdExpSimple'(Symbol.makePrivateSymbol(Names.findTableFun_name),dum_pos)),
alt_exp=ref(NONE),
args=[exp(makeIdExpSimple'(ht,dum_pos)),exp(param_list)],pos=dum_pos})
val res_sym = Symbol.symbol("res")
val res_binding:binding = {bpat=idPat(makePTP(res_sym)),pos=dum_pos,
def=exp(fun_body)}
val add_binding:binding = {bpat=anyPat({pos=dum_pos}),
def=exp(appExp({proc=exp(makeIdExpSimple'
(Symbol.makePrivateSymbol(Names.addTableFun_name),dum_pos)),
alt_exp=ref(NONE),
args=[exp(makeIdExpSimple'(ht,dum_pos)),
exp(listExp({members=[exp(param_list),
exp(makeIdExpSimple'(Symbol.makePrivateSymbol("-->"),dum_pos)),
exp(makeIdExpSimple'(res_sym,dum_pos))],
pos=dum_pos}))],
pos=dum_pos})),
pos=dum_pos}
val try_2 = letExp({bindings=[res_binding,add_binding],pos=dum_pos,
body=makeIdExpSimple'(res_sym,dum_pos)})
val big_lambda = functionExp({params=fun_params,body=tryExp({choices=[try_1,try_2],pos=dum_pos}),pos=dum_pos})
val big_letrec_binding:binding = {bpat=idPat(makePTP(fun_name_sym)),def=exp(big_lambda),pos=dum_pos}
val big_letrec = letRecExp({bindings=[big_letrec_binding],body=makeIdExpSimple'(fun_name_sym,dum_pos),pos=dum_pos})
val big_let = letExp({bindings=[b],body=big_letrec,pos=dum_pos})
in
exp(big_let)
end
in
fun_body_exp
end
fun desugarWithKeys(params,dict_exp,body,with_pos) =
let fun makeAppExp(rec_exp,field_name,pos) =
appExp({proc=exp(makeIdExp(N.mapApplyFun_name,pos)),
args=[exp(rec_exp),exp(quotedIdeExp({name=(S.name(field_name)),pos=pos}))],
pos=with_pos,alt_exp=ref(NONE)})
val rec_sym = Symbol.freshSymbol(NONE)
val rec_exp = makeIdExpSimple'(rec_sym,with_pos)
val binding1:binding = {bpat=idPat(makePTPWithPos(rec_sym,with_pos)),def=exp(dict_exp),pos=with_pos}
val bindings = map (fn p:param as {name,pos=id_pos} => let val b:binding = {bpat=idPat(makePTPWithPos(name,id_pos)),def=exp(makeAppExp(rec_exp,name,id_pos)),pos=id_pos}
in b end)
params
in
letExp({bindings=(binding1::bindings),body=body,pos=with_pos})
end
fun makeMethodDefinition({meth_name:possibly_typed_param,meth_params,meth_body,pos,file}) =
let val meth_body_exp = methodExp({params=meth_params,body=meth_body,pos=pos,
name=ref(S.name(#name(meth_name)))})
in
(meth_name,meth_body_exp)
end
datatype user_input = structureInput of absyn_structure
| structuresInput of absyn_structure list
| domainInput of absyn_domain
| domainsInput of absyn_domain list
| moduleInput of module_entry
| moduleExtension of module_entry
| subSortDeclaration of mod_symbol * pos * mod_symbol * pos
| subSortsDeclaration of ((mod_symbol * pos) list) * (mod_symbol * pos)
| functionSymbolInput of absyn_fsym list
| constantSymbolInput of absyn_fsym list
| phraseInput of phrase
| symbolDefinitionInput of absyn_symbol_definition
| direcInput of directive
withtype module_entry = {module_name: param, module_contents: user_input list, module_file: string ref}
fun printMetaId(str) = N.metaIdPrefix^str
fun makeUnProp([],p) = p
| makeUnProp(v::more,p) =
makeUnProp(more,appExp({proc=exp(idExp({msym=N.mforall_symbol,mods=[],no_mods=true,sym=N.forall_symbol,pos=dum_pos})),
args=[exp(termVarExp({term_var=v,user_sort=NONE,pos=dum_pos})),exp(p)],
alt_exp=ref(NONE),pos=dum_pos}))
fun makeRelSymCondition({rel_name,arg_vars,condition}) =
let val new_sym_prop = appExp({proc=exp(idExp({msym=mSym rel_name,mods=[],no_mods=true,sym=rel_name,pos=dum_pos})),alt_exp=ref(NONE),
args=(List.map (fn v => exp(termVarExp({term_var=v,user_sort=NONE,pos=dum_pos})))
arg_vars),
pos=dum_pos})
val cond_prop = appExp({proc=exp(idExp({msym=N.miff_symbol,mods=[],no_mods=true,sym=N.iff_symbol,pos=dum_pos})),alt_exp=ref(NONE),
args=[exp(new_sym_prop),exp(condition)],pos=dum_pos})
in
makeUnProp(rev(arg_vars),cond_prop)
end
fun makeFunSymCondition({fun_name,arg_vars,the_var,def_description}) =
let val fmods = []
val equality_prop = appExp({proc=exp(idExp({msym=N.mequal_logical_symbol,no_mods=true,mods=[],sym=N.equal_logical_symbol,pos=dum_pos})),
alt_exp=ref(NONE),
args=[exp(appExp({proc=exp(idExp({msym=mSym fun_name,mods=fmods,no_mods=true,sym=fun_name,pos=dum_pos})),
args=(List.map (fn v =>
exp(termVarExp({term_var=v,
user_sort=NONE,pos=dum_pos})))
arg_vars),alt_exp=ref(NONE),pos=dum_pos})),
exp(termVarExp({term_var=the_var,user_sort=NONE,pos=dum_pos}))],
pos=dum_pos})
val cond_prop = appExp({proc=exp(idExp({msym=N.miff_symbol,mods=[],no_mods=true,sym=N.iff_symbol,pos=dum_pos})),
alt_exp=ref(NONE),
args=[exp(equality_prop),exp(def_description)],
pos=dum_pos})
in
makeUnProp(the_var::rev(arg_vars),cond_prop)
end
fun makeFunOrRelSymCondition({fun_or_rel_name,arg_vars,term_or_condition}) =
let fun isPropExp(appExp({proc=exp(idExp({msym,...})),args,...})) = isPropConMS(msym)
| isPropExp(_) = false
in
if isPropExp(term_or_condition) then
makeRelSymCondition({rel_name=fun_or_rel_name,arg_vars=arg_vars,condition=term_or_condition})
else
let val fresh_the_var = AthTermVar.fresh()
val new_def_description = appExp({proc=exp(idExp({msym=N.mequal_logical_symbol,mods=[],no_mods=true,sym=N.equal_logical_symbol,
pos=dum_pos})),alt_exp=ref(NONE),
args=[exp(termVarExp({term_var=fresh_the_var,user_sort=NONE,
pos=dum_pos})),
exp(term_or_condition)],pos=dum_pos})
in
makeFunSymCondition({fun_name=fun_or_rel_name,arg_vars=arg_vars,the_var=fresh_the_var,
def_description=new_def_description})
end
end
fun makeConstantSymCondition({constant_name,the_var,def_description}) =
let val (mods,sym) = MS.split(constant_name)
val equality_prop = appExp({proc=exp(idExp({msym=N.mequal_logical_symbol,mods=[],no_mods=true,sym=N.equal_logical_symbol,pos=dum_pos})),
args=[exp(idExp({msym=constant_name,mods=mods,no_mods=null(mods),sym=sym,pos=dum_pos})),
exp(termVarExp({term_var=the_var,user_sort=NONE,pos=dum_pos}))],
alt_exp=ref(NONE),pos=dum_pos})
val cond_prop = appExp({proc=exp(idExp({msym=N.miff_symbol,mods=[],no_mods=true,sym=N.iff_symbol,pos=dum_pos})),
args=[exp(equality_prop),exp(def_description)],
alt_exp=ref(NONE),pos=dum_pos})
in
appExp({proc=exp(idExp({msym=N.mforall_symbol,mods=[],no_mods=true,sym=N.forall_symbol,pos=dum_pos})),
args=[exp(termVarExp({term_var=the_var,user_sort=NONE,pos=dum_pos})),exp(cond_prop)],
alt_exp=ref(NONE),pos=dum_pos})
end
fun isSomeQuantPat(someQuantPat(_)) = true
| isSomeQuantPat(_) = false
fun isSomeNamedQuantPat(someQuantPat({id=someParam(_),...})) = true
| isSomeNamedQuantPat(someQuantPat({id=wildCard(_),...})) = false
| isSomeNamedQuantPat(namedPat(_)) = true
| isSomeNamedQuantPat(_) = false
fun isListPat(someListPat(_)) = true
| isListPat(listPat(_)) = true
| isListPat(listPats(_)) = true
| isListPat(splitPat(_)) = true
| isListPat(reStarPat(_)) = true
| isListPat(rePlusPat(_)) = true
| isListPat(reRepPat(_)) = true
| isListPat(constantStringPat(_)) = true
| isListPat(wherePat({pat,...})) = isListPat(pat)
| isListPat(namedPat({pat,...})) = isListPat(pat)
| isListPat(_) = false
fun isListPatRelaxed(someListPat(_)) = true
| isListPatRelaxed(listPat(_)) = true
| isListPatRelaxed(listPats(_)) = true
| isListPatRelaxed(splitPat(_)) = true
| isListPatRelaxed(reStarPat(_)) = true
| isListPatRelaxed(rePlusPat(_)) = true
| isListPatRelaxed(reRepPat(_)) = true
| isListPatRelaxed(constantStringPat(_)) = true
| isListPatRelaxed(wherePat({pat,...})) = isListPatRelaxed(pat)
| isListPatRelaxed(namedPat({pat,...})) = isListPatRelaxed(pat)
| isListPatRelaxed(disjPat({pats,...})) = Basic.exists(pats,isListPatRelaxed)
| isListPatRelaxed(_) = false
val tt0 = GeneralRE.trivial_tag
fun concatLst([e]) = e
| concatLst(e1::rest) = GeneralRE.concat0(e1,concatLst(rest),tt0)
| concatLst([]) = GeneralRE.null0(tt0)
fun concatLst'([]) = GeneralRE.null0(tt0)
| concatLst'(e1::rest) = GeneralRE.concat0(e1,concatLst'(rest),tt0)
fun unionLst([e]) = e
| unionLst(e1::rest) = GeneralRE.union0(e1,unionLst(rest),tt0)
val (lparen,rparen,lbrack,rbrack,space) = (Basic.lparen,Basic.rparen,Basic.lbrack,Basic.rbrack,Basic.blank)
fun unparseExp(idExp({msym,...})) = MS.name(msym)
| unparseExp(taggedConSym({name,sort_as_tagged_symterm,sort_as_fterm,...})) =
(case sort_as_fterm of
NONE => MS.name(name) ^ ":" ^ (SymTerm.toString(SymTerm.stripTags(sort_as_tagged_symterm),printSymTermVar)) ^ " [NO FSORT]"
| SOME(sort) => "[FSORT: " ^ (FTerm.toStringDefault(sort)) ^ "]")
| unparseExp(opExp({op_exp,...})) = lparen^Names.infix_op_name^space^(unparseExp op_exp)^rparen
| unparseExp(numExp({number,...})) = athNumberToString(number)
| unparseExp(letExp({bindings,body,pos,...})) = lparen^"let "^lparen^(unparseBindings bindings)^rparen^space^(unparseExp body)^rparen
| unparseExp(letRecExp({bindings,body,pos,...})) = lparen^"letrec "^lparen^(unparseBindings bindings)^rparen^space^(unparseExp body)^rparen
| unparseExp(quotedIdeExp({name,...})) = (Names.metaIdPrefix)^name
| unparseExp(unitExp(_)) = "()"
| unparseExp(charExp({code,...})) = Char.toString(Char.chr(code))
| unparseExp(logicalAndExp({args,...})) = lparen^(Names.logical_and_name)^(Basic.printSExpListStr(args,unparsePhrase))^rparen
| unparseExp(logicalOrExp({args,...})) = lparen^(Names.logical_or_name)^(Basic.printSExpListStr(args,unparsePhrase))^rparen
| unparseExp(stringExp({str,...})) = (Basic.string_quote)^(implode (map Char.chr str))^(Basic.string_quote)
| unparseExp(termVarExp({term_var,user_sort=NONE,...})) = (Names.variable_prefix)^(AthTermVar.name(term_var))
| unparseExp(termVarExp({term_var,user_sort=SOME(sort),...})) = (Names.variable_prefix)^(AthTermVar.name(term_var))^":"^
(SymTerm.toString(SymTerm.stripTags(sort),printSymTermVar))
| unparseExp(propConExp({con,...})) = propConToString(con)
| unparseExp(checkExp({clauses,...})) = lparen^(Names.check_name)^space^(unparseCheckClauses clauses)^rparen
| unparseExp(matchExp({discriminant,clauses,...})) =
lparen^"match "^(unparsePhrase discriminant)^space^"\n"^(unparseMatchClauses clauses)^rparen
| unparseExp(functionExp({params,body,...})) = lparen^(Names.lambda_name)^space^lparen^(unparsePWCParams params)^rparen^space^
(unparseExp body)^rparen
| unparseExp(methodExp({params,body,...})) = lparen^(Names.method_name)^space^lparen^(unparsePWCParams params)^rparen^space^
(unparseDed body)^rparen
| unparseExp(appExp({proc,args,...})) =
(lparen^(unparsePhrase proc)^space^(Basic.printSExpListStr(args,unparsePhrase))^rparen)
| unparseExp(BAppExp({proc,arg1,arg2,pos,...})) =
lparen^(unparsePhrase proc)^space^(Basic.printSExpListStr([arg1,arg2],unparsePhrase))^rparen
| unparseExp(UAppExp({proc,arg,pos,...})) =
lparen^(unparsePhrase proc)^space^(Basic.printSExpListStr([arg],unparsePhrase))^rparen
| unparseExp(listExp({members,...})) = lbrack^(Basic.printSExpListStr(members,unparsePhrase))^rbrack
| unparseExp(beginExp({members,...})) = lparen^"seq "^(Basic.printSExpListStr(members,unparsePhrase))^rparen
| unparseExp(cellExp({contents,...})) = lparen^"cell "^(unparsePhrase contents)^rparen
| unparseExp(refExp({cell_exp,...})) = lparen^"ref "^(unparseExp cell_exp)^rparen
| unparseExp(tryExp({choices,...})) = lparen^"try "^(Basic.printSExpListStr(choices,unparseExp))^rparen
| unparseExp(setCellExp({cell_exp,set_phrase,...})) = lparen^"set! "^(unparseExp cell_exp)^space^(unparsePhrase set_phrase)^rparen
| unparseExp(_) = "(Don't know how to unparse this yet.)"
and unparseExpAndStop(e') = ""
and printSymTermVar(sym) = (Names.sort_variable_prefix)^(Symbol.name sym)
and unparsePTParam({name,sort_as_sym_term,sort_as_exp,...}:possibly_typed_param) =
(case (sort_as_sym_term,sort_as_exp) of
(SOME(sort),_) => SymTerm.toString(SymTerm.stripTags(sort),printSymTermVar)
| (_,SOME(e)) => S.name(name)^":"^(unparseExp(e))
| _ => S.name(name))
and unparsePWCParam(pwcp:possibly_wildcard_param) =
(case pwcp of
someParam(ptp) => unparsePTParam(ptp)
| wildCard(_) => Names.wild_card)
and unparsePWCParams(pwcparams) = Basic.printSExpListStr(pwcparams,unparsePWCParam)