-
Notifications
You must be signed in to change notification settings - Fork 5
/
analysis.sml
706 lines (635 loc) · 24.3 KB
/
analysis.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
structure Analysis :> sig
eqtype ext
val ext_to_string : ext -> string
eqtype symbol
val sym_to_string : symbol -> string
eqtype abt
val abt_to_string : abt -> string
eqtype sort
val sort_to_string : sort -> string
structure AbtArity : sig
datatype t
= Param of string (* paramaterized arity *)
| SymbolUse of symbol (* symbol use *)
| SortUse of sort (* sort use *)
| SymbolBinding of symbol (* symbol binding *)
| SortBinding of sort (* sort binding *)
| Prod of t list
| List of t
| AppExt of ext * t list
| AppAbt of abt * t list
| Dot of t * t (* Binds bindings on the left in the right. *)
end
structure SortArity : sig
datatype t
= SymbolUse of symbol (* symbol use *)
| SortUse of sort (* sort use *)
| SymbolBinding of symbol (* symbol binding *)
| SortBinding of sort (* sort binding *)
| Prod of t list
| List of t
| AppExt of ext * t list
| AppAbt of abt * t list
| Dot of t * t (* Binds bindings on the left in the right. *)
end
datatype abt_or_sort = Abt of abt | Sort of sort
val abt_or_sort_to_string : abt_or_sort -> string
type ana = {
(* External abts and their arguements. *)
externals : (ext * string list) list,
(* All symbols. *)
symbols : symbol list,
(* All internal abts and sorts and their associated operations and aritys.
* They are grouped with their mutual dependencies and occur
* later in the outer list than their strict dependencies. *)
abts_and_sorts
: (abt * (string list * (string * AbtArity.t option) list),
sort * (string * SortArity.t option) list) Util.Sum2.t list list,
(* Whether the list abt structure needs to be included. *)
haslist : bool,
(* Argument strings for the given abt. *)
args : abt -> string list,
(* Whether the given sort can be bound to a variable. *)
hasvar : sort -> bool,
(* Abts and sorts that the given abt or sort refers to. *)
dependencies : abt_or_sort -> abt list * sort list,
(* Returns true iff the first abt or sort given depends on the second. *)
dependson : abt_or_sort -> abt_or_sort -> bool,
(* Returns all the mutually dependent abts and sorts
* of a given abt or sort, including itself. *)
mutual : abt_or_sort -> abt list * sort list,
(* Returns true iff the given abts or sorts are mutually dependent. *)
mutualwith : abt_or_sort -> abt_or_sort -> bool
}
val analyze : string list StringTable.table
* StringTable.Set.set
* (string list * AbbotSyntax.oper list) StringTable.table
* AbbotSyntax.oper list StringTable.table
-> ana
end = struct
open Util
open StringTable
type ext = string
fun ext_to_string x = x
type symbol = string
fun sym_to_string x = x
type abt = string
fun abt_to_string x = x
type sort = string
fun sort_to_string x = x
structure AbtArity = struct
datatype t
= Param of string (* paramaterized arity *)
| SymbolUse of symbol (* symbol use *)
| SortUse of sort (* sort use *)
| SymbolBinding of symbol (* symbol binding *)
| SortBinding of symbol (* sort binding *)
| Prod of t list
| List of t
| AppExt of ext * t list
| AppAbt of abt * t list
| Dot of t * t (* Binds bindings on the left in the right. *)
end
structure SortArity = struct
datatype t
= SymbolUse of symbol (* symbol use *)
| SortUse of sort (* sort use *)
| SymbolBinding of symbol (* symbol binding *)
| SortBinding of symbol (* sort binding *)
| Prod of t list
| List of t
| AppExt of ext * t list
| AppAbt of abt * t list
| Dot of t * t (* Binds bindings on the left in the right. *)
end
datatype abt_or_sort = Abt of abt | Sort of sort
fun abt_or_sort_to_string (Abt s | Sort s) = s
type ana = {
externals : (ext * string list) list,
symbols : symbol list,
abts_and_sorts
: (abt * (string list * (string * AbtArity.t option) list),
sort * (string * SortArity.t option) list) Util.Sum2.t list list,
haslist : bool,
args : abt -> string list,
hasvar : sort -> bool,
dependencies : abt_or_sort -> abt list * sort list,
dependson : abt_or_sort -> abt_or_sort -> bool,
mutual : abt_or_sort -> abt list * sort list,
mutualwith : abt_or_sort -> abt_or_sort -> bool
}
local open AbtArity in
fun convert_abt_arity (exts, symbs, abts, sorts) arity =
case arity of
AbbotSyntax.Param name => Param name
| AbbotSyntax.Use name =>
(if Set.find sorts name
then SortUse name
else if Set.find symbs name
then SymbolUse name
else if Set.find abts name
then AppAbt (name, [])
else if Set.find exts name
then AppExt (name, [])
else raise Fail (name ^ " is not a defined symbol, abt, or sort.")) (*???location*)
| AbbotSyntax.Binding name =>
(if Set.find sorts name
then SortBinding name
else if Set.find symbs name
then SymbolBinding name
else raise Fail (name ^ " is not a sort or symbol, so it cannot be bound.")) (*???location*)
| AbbotSyntax.Prod aritys =>
Prod (List.map (convert_abt_arity (exts, symbs, abts, sorts)) aritys)
| AbbotSyntax.App ("list", [arity]) =>
List (convert_abt_arity (exts, symbs, abts, sorts) arity)
| AbbotSyntax.App (name, aritys) =>
(if Set.find abts name
then
AppAbt
(name,
List.map
(convert_abt_arity (exts, symbs, abts, sorts))
aritys)
else if Set.find exts name
then
AppExt
(name,
List.map
(convert_abt_arity (exts, symbs, abts, sorts))
aritys)
else raise Fail (name ^ " is not a defined abt.")) (*???location*)
| AbbotSyntax.Dot (binding, arity) =>
Dot
(convert_abt_arity (exts, symbs, abts, sorts) binding,
convert_abt_arity (exts, symbs, abts, sorts) arity)
val depth = ref 0
fun abt_arity_binds abt abts arity =
case arity of
(Param _ | SymbolUse _ | SortUse _) => false
| (SymbolBinding _ | SortBinding _) => true
| List arity => abt_arity_binds abt abts arity
| (Prod aritys | AppExt (_, aritys)) =>
List.exists (abt_arity_binds abt abts) aritys
| AppAbt (abt', aritys) =>
List.exists (abt_arity_binds abt abts) aritys
orelse
if abt = abt' then false else (* ???This will diverge under mutual dependence *)
let val (args, opers) = valOf (find abts abt') in
List.exists
(fn (_, arity_opt) =>
case arity_opt of
NONE => false
| SOME arity => abt_arity_binds abt' abts arity)
opers
end
| Dot (_, arity) => abt_arity_binds abt abts arity
end
local open SortArity in
fun convert_sort_arity (exts, symbs, abts, sorts) arity =
case arity of
AbbotSyntax.Param _ =>
raise Fail "Sorts may not have parameters." (*???location, also this might be a bad error message*)
| AbbotSyntax.Use name =>
(if Set.find sorts name
then SortUse name
else if Set.find symbs name
then SymbolUse name
else if Set.find exts name
then AppExt (name, [])
else if Set.find abts name
then AppAbt (name, [])
else raise Fail (name ^ " is not a defined symbol, abt, or sort.")) (*???location*)
| AbbotSyntax.Binding name =>
(if Set.find sorts name
then SortBinding name
else if Set.find symbs name
then SymbolBinding name
else raise Fail (name ^ " is not a sort or symbol, so it cannot be bound.")) (*???location*)
| AbbotSyntax.Prod aritys =>
Prod (List.map (convert_sort_arity (exts, symbs, abts, sorts)) aritys)
| AbbotSyntax.App ("list", [arity]) =>
List (convert_sort_arity (exts, symbs, abts, sorts) arity)
| AbbotSyntax.App (name, aritys) =>
(if Set.find exts name
then
AppExt
(name,
List.map
(convert_sort_arity (exts, symbs, abts, sorts))
aritys)
else if Set.find abts name
then
AppAbt
(name,
List.map
(convert_sort_arity (exts, symbs, abts, sorts))
aritys)
else raise Fail (name ^ " is not a defined abt.")) (*???location*)
| AbbotSyntax.Dot (binding, arity) =>
Dot
(convert_sort_arity (exts, symbs, abts, sorts) binding,
convert_sort_arity (exts, symbs, abts, sorts) arity)
fun sort_arity_binds abts arity =
case arity of
(SymbolUse _ | SortUse _) => false
| (SymbolBinding _ | SortBinding _) => true
| List arity => sort_arity_binds abts arity
| (Prod aritys | AppExt (_, aritys)) =>
List.exists (sort_arity_binds abts) aritys
| AppAbt (abt, aritys) =>
List.exists (sort_arity_binds abts) aritys
orelse
let val (args, opers) = valOf (find abts abt) in
List.exists
(fn (_, arity_opt) =>
case arity_opt of
NONE => false
| SOME arity => abt_arity_binds abt abts arity)
opers
end
| Dot (_, arity) => sort_arity_binds abts arity
end
local open AbtArity in
fun abt_arity_deps arity =
case arity of
(Param _ | SymbolUse _ | SymbolBinding _) =>
(Set.empty (), Set.empty ())
| (SortUse srt | SortBinding srt) => (Set.empty (), Set.singleton srt)
| Prod aritys =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.empty (), Set.empty ())
(List.map abt_arity_deps aritys)
| List arity =>
abt_arity_deps arity
| AppExt (ext, aritys) =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.empty (), Set.empty ())
(List.map abt_arity_deps aritys)
| AppAbt (abt, aritys) =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.singleton abt, Set.empty ())
(List.map abt_arity_deps aritys)
| Dot (binding, arity) =>
let
val (abts1, sorts1) = abt_arity_deps binding
val (abts2, sorts2) = abt_arity_deps arity
in
(Set.union (abts1, abts2), Set.union (sorts1, sorts2))
end
end
local open SortArity in
fun sort_arity_deps arity =
case arity of
(SortUse srt | SortBinding srt) => (Set.empty (), Set.singleton srt)
| (SymbolUse _ | SymbolBinding _) => (Set.empty (), Set.empty ())
| Prod aritys =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.empty (), Set.empty ())
(List.map sort_arity_deps aritys)
| List arity =>
sort_arity_deps arity
| AppExt (ext, aritys) =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.empty (), Set.empty ())
(List.map sort_arity_deps aritys)
| AppAbt (abt, aritys) =>
List.foldl
(fn ((abts1, sorts1), (abts2, sorts2)) =>
(Set.union (abts1, abts2), Set.union (sorts1, sorts2)))
(Set.singleton abt, Set.empty ())
(List.map sort_arity_deps aritys)
| Dot (binding, arity) =>
let
val (abts1, sorts1) = sort_arity_deps binding
val (abts2, sorts2) = sort_arity_deps arity
in
(Set.union (abts1, abts2), Set.union (sorts1, sorts2))
end
end
local open AbtArity in
fun sorts_bound_in_abt_arity arity =
case arity of
(Param _ | SortUse _ | SymbolUse _ | SymbolBinding _) => Set.empty ()
| SortBinding srt => Set.singleton srt
| List arity =>
sorts_bound_in_abt_arity arity
| (Prod aritys | AppExt (_, aritys) | AppAbt (_, aritys)) =>
List.foldl Set.union (Set.empty ())
(List.map sorts_bound_in_abt_arity aritys)
| Dot (binding, arity) =>
Set.union
(sorts_bound_in_abt_arity binding, sorts_bound_in_abt_arity arity)
end
local open SortArity in
fun sorts_bound_in_sort_arity arity =
case arity of
(SortUse _ | SymbolUse _ | SymbolBinding _) => Set.empty ()
| SortBinding srt => Set.singleton srt
| List arity =>
sorts_bound_in_sort_arity arity
| (Prod aritys | AppExt (_, aritys) | AppAbt (_, aritys)) =>
List.foldl Set.union (Set.empty ())
(List.map sorts_bound_in_sort_arity aritys)
| Dot (binding, arity) =>
Set.union
(sorts_bound_in_sort_arity binding, sorts_bound_in_sort_arity arity)
end
local open AbtArity in
fun abt_arity_haslist arity =
case arity of
(Param _
| SortUse _
| SymbolUse _
| SortBinding _
| SymbolBinding _) => false
| List arity => true
| (Prod aritys | AppExt (_, aritys) | AppAbt (_, aritys)) =>
List.exists abt_arity_haslist aritys
| Dot (binding, arity) =>
abt_arity_haslist binding orelse abt_arity_haslist arity
end
local open SortArity in
fun sort_arity_haslist arity =
case arity of
(SortUse _
| SymbolUse _
| SortBinding _
| SymbolBinding _) => false
| List arity => true
| (Prod aritys | AppExt (_, aritys) | AppAbt (_, aritys)) =>
List.exists sort_arity_haslist aritys
| Dot (binding, arity) =>
sort_arity_haslist binding orelse sort_arity_haslist arity
end
fun analyze (exts, symbs, abts, sorts) : ana =
let
fun string_to_abt_or_sort str =
case find abts str of
SOME _ => SOME (Abt str)
| NONE =>
(case find sorts str of
SOME _ => SOME (Sort str)
| NONE => NONE)
val set_to_list = Seq.toList o Set.toSeq
val all_names = (domain exts, symbs, domain abts, domain sorts)
val abts =
map
(fn (args, opers) =>
(args,
List.map
(fn (oper, arity_opt) =>
(oper,
(case arity_opt of
NONE => NONE
| SOME arity =>
SOME (convert_abt_arity all_names arity))))
opers))
abts
val args = #1 o valOf o find abts
val sorts =
map
(List.map
(fn (oper, arity_opt) =>
(oper,
(case arity_opt of
NONE => NONE
| SOME arity =>
let
val arity' = convert_sort_arity all_names arity
in
if sort_arity_binds abts arity'
then raise Fail "All bindings must come before a dot" (*???location*)
else SOME arity'
end))))
sorts
val haslist =
List.exists
(fn (_, opers) =>
List.exists
(fn (oper, arity_opt) =>
case arity_opt of
NONE => false
| SOME arity =>
abt_arity_haslist arity)
opers)
(Seq.toList (range abts))
orelse
List.exists
(List.exists
(fn (_, arity_opt) =>
case arity_opt of
NONE => false
| SOME arity =>
sort_arity_haslist arity))
(Seq.toList (range sorts))
local
fun neighbors G F =
Seq.reduce Set.union (Set.empty ()) (range (extract (G, F)))
fun bfs G frontier visited =
if Set.size frontier = 0
then visited
else let
val visited' = Set.union (visited, frontier)
val frontier' = Set.difference (neighbors G frontier, visited')
in
bfs G frontier' visited'
end
val direct_abt_dep_table =
map
(fn (args, opers) =>
List.foldl Set.union (Set.empty ())
(List.map
(fn (oper, arity_opt) =>
case arity_opt of
NONE => Set.empty ()
| SOME arity =>
Set.union (abt_arity_deps arity))
opers))
abts
val direct_sort_dep_table =
map
(fn opers =>
List.foldl Set.union (Set.empty ())
(List.map
(fn (oper, arity_opt) =>
case arity_opt of
NONE => Set.empty ()
| SOME arity =>
Set.union (sort_arity_deps arity))
opers))
sorts
val direct_dep_table =
merge
(fn (_, _) => raise Fail "Found abt and sort with the same name.")
(direct_abt_dep_table, direct_sort_dep_table)
in
val merged_dep_table =
mapk
(fn (srt, _) =>
bfs direct_dep_table (Set.singleton srt) (Set.singleton srt))
direct_dep_table
val dep_table =
map
(fn deps =>
(Set.filter
(fn str =>
case string_to_abt_or_sort str of
SOME (Abt _) => true
| SOME (Sort _) => false
| NONE => raise Fail "Internal Abbot Error")
deps,
Set.filter
(fn str =>
case string_to_abt_or_sort str of
SOME (Abt _) => false
| SOME (Sort _) => true
| NONE => raise Fail "Internal Abbot Error")
deps))
merged_dep_table
end
fun dependson (Abt str | Sort str) abt_or_sort =
let val SOME (abts, sorts) = find dep_table str in
case abt_or_sort of
Abt abt => Set.find abts abt
| Sort sort => Set.find sorts sort
end
fun dependencies (Abt str | Sort str) =
let val SOME (abts, sorts) = find dep_table str in
(set_to_list abts, set_to_list sorts)
end
val hasvar_set =
Set.union
(reduce Set.union (Set.empty ())
(map
(fn (args, opers) =>
List.foldl Set.union (Set.empty ())
(List.map
(fn (_, arity_opt) =>
case arity_opt of
NONE => Set.empty ()
| SOME arity =>
sorts_bound_in_abt_arity arity)
opers))
abts),
reduce Set.union (Set.empty ())
(map
(fn opers =>
List.foldl Set.union (Set.empty ())
(List.map
(fn (_, arity_opt) =>
case arity_opt of
NONE => Set.empty ()
| SOME arity =>
sorts_bound_in_sort_arity arity)
opers))
sorts))
val hasvar = Set.find hasvar_set
val mutual_table =
mapk
(fn (s, (abt_deps, sort_deps)) =>
let
val abts =
Set.filter
(fn s' => Set.find (valOf (find merged_dep_table s')) s)
abt_deps
val sorts =
Set.filter
(fn s' =>
case find merged_dep_table s' of
NONE => raise Fail "bar"
| SOME set => Set.find set s)
sort_deps
in
case valOf (string_to_abt_or_sort s) of
Abt abt =>
(Set.insert abt abts, sorts)
| Sort sort =>
(abts, Set.insert sort sorts)
end)
dep_table
fun mutual' (Abt s | Sort s) =
valOf (find mutual_table s)
fun mutual abt_or_sort =
let val (abts, sorts) = mutual' abt_or_sort in
(set_to_list abts, set_to_list sorts)
end
fun mutualwith s1 s2 =
let val (abts, sorts) = mutual' s1 in
case s2 of
Abt abt => Set.find abts abt
| Sort sort => Set.find sorts sort
end
local open Sum2 in
val abts_and_sorts =
Seq.iter
(fn (ls, s) =>
if
(case s of
Abt abt =>
List.exists
(List.exists (fn In1 (x, _) => x = abt | _ => false))
ls
| Sort sort =>
List.exists
(List.exists (fn In2 (x, _) => x = sort | _ => false))
ls)
then ls
else
let val (mut_abts, mut_sorts) = mutual s in
(List.map
(fn s' => In1 (s', valOf (find abts s')))
mut_abts
@ List.map
(fn s' => In2 (s', valOf (find sorts s')))
mut_sorts)
:: ls
end)
[]
(Seq.append
(Seq.map Abt (Set.toSeq (domain abts)),
Seq.map Sort (Set.toSeq (domain sorts))))
fun group_compare ((In1 (l, _) | In2 (l, _))::ls, (In1 (r, _) | In2 (r, _))::rs) =
let
val lgeqr =
case find merged_dep_table l of
NONE => false
| SOME deps => Set.find deps r
val rgeql =
case find merged_dep_table r of
NONE => false
| SOME deps => Set.find deps l
in
if lgeqr
then GREATER
else if rgeql
then LESS
else EQUAL
end
end
val abts_and_sorts =
Seq.toList (Seq.sort group_compare (Seq.fromList (abts_and_sorts)))
in
{
externals = Seq.toList (toSeq exts),
symbols=set_to_list symbs,
abts_and_sorts=abts_and_sorts,
haslist=haslist,
dependencies=dependencies,
dependson=dependson,
args=args,
hasvar=hasvar,
mutual=mutual,
mutualwith=mutualwith
}
end
end