@@ -373,7 +373,7 @@ type argmap = {
373
373
n2t : (D .term * D.Constants .t ) StrMap .t ;
374
374
n2i : int StrMap .t ;
375
375
}
376
- [@@ deriving show ]
376
+ [@@ deriving show , ord ]
377
377
378
378
let empty_amap = {
379
379
nargs = 0 ;
@@ -405,29 +405,29 @@ type preterm = {
405
405
loc : Loc .t ;
406
406
spilling : bool ;
407
407
}
408
- [@@ deriving show ]
408
+ [@@ deriving show , ord ]
409
409
410
410
type type_declaration = {
411
411
tname : D .constant ;
412
412
ttype : preterm ;
413
413
tloc : Loc .t ;
414
414
}
415
- [@@ deriving show ]
415
+ [@@ deriving show , ord ]
416
416
417
417
type type_abbrev_declaration = {
418
418
taname : D .constant ;
419
419
tavalue : preterm ;
420
420
taparams : int ;
421
421
taloc : Loc .t ;
422
422
}
423
- [@@ deriving show ]
423
+ [@@ deriving show , ord ]
424
424
425
425
type presequent = {
426
426
peigen : D .term ;
427
427
pcontext : D .term ;
428
428
pconclusion : D .term ;
429
429
}
430
- [@@ deriving show ]
430
+ [@@ deriving show , ord ]
431
431
type prechr_rule = {
432
432
pto_match : presequent list ;
433
433
pto_remove : presequent list ;
@@ -438,7 +438,7 @@ type prechr_rule = {
438
438
pifexpr : string option ;
439
439
pcloc : Loc .t ;
440
440
}
441
- [@@ deriving show ]
441
+ [@@ deriving show , ord ]
442
442
443
443
(* ***************************************************************************
444
444
Intermediate program representation
@@ -447,6 +447,64 @@ type prechr_rule = {
447
447
open Data
448
448
module C = Constants
449
449
450
+ module Types = struct
451
+
452
+ type typ = {
453
+ tindex : Ast.Structured .tattribute ;
454
+ decl : type_declaration
455
+ }
456
+ [@@ deriving show , ord ]
457
+
458
+ module Set = Util.Set. Make (struct
459
+ type t = typ
460
+ let compare = compare_typ
461
+ let show = show_typ
462
+ let pp = pp_typ
463
+ end )
464
+
465
+ type types = {
466
+ set : Set .t ;
467
+ lst : typ list ;
468
+ def : typ ;
469
+ } [@@ deriving show , ord ]
470
+
471
+ let make t = { set = Set. singleton t; lst = [t]; def = t }
472
+
473
+ let merge t1 t2 =
474
+ let l2 = List. filter (fun t -> not @@ Set. mem t t1.set) t2.lst in
475
+ match l2 with
476
+ | [] -> t1
477
+ | _ :: _ ->
478
+ {
479
+ set = Set. union t1.set t2.set;
480
+ lst = t1.lst @ l2;
481
+ def = t2.def;
482
+ }
483
+
484
+ let smart_map (f : typ -> typ ) (t : types ) : types =
485
+ let fold t accu =
486
+ let t' = f t in
487
+ if t' == t then accu
488
+ else Set. add t' (Set. remove t accu)
489
+ in
490
+ let set' = Set. fold fold t.set t.set in
491
+ let lst' = smart_map f t.lst in
492
+ let def' = f t.def in
493
+ if set' == t.set && lst' == t.lst && def' == t.def then t
494
+ else { set = set'; lst = lst'; def = def' }
495
+
496
+ let append x t = {
497
+ set = Set. add x t.set;
498
+ lst = x :: t .lst;
499
+ def = t.def;
500
+ }
501
+
502
+ let fold f accu t = List. fold_left f accu t.lst
503
+ let iter f t = List. iter f t.lst
504
+ let for_all f t = List. for_all f t.lst
505
+
506
+ end
507
+
450
508
module Structured = struct
451
509
452
510
type program = {
@@ -455,7 +513,7 @@ type program = {
455
513
toplevel_macros : macro_declaration ;
456
514
}
457
515
and pbody = {
458
- types : typ list C.Map .t ;
516
+ types : Types .types C.Map .t ;
459
517
type_abbrevs : type_abbrev_declaration C.Map .t ;
460
518
modes : (mode * Loc .t ) C.Map .t ;
461
519
body : block list ;
@@ -467,18 +525,14 @@ and block =
467
525
| Namespace of string * pbody
468
526
| Shorten of C .t Ast.Structured .shorthand list * pbody
469
527
| Constraints of constant list * prechr_rule list * pbody
470
- and typ = {
471
- tindex : Ast.Structured .tattribute ;
472
- decl : type_declaration
473
- }
474
- [@@ deriving show ]
528
+ [@@ deriving show , ord ]
475
529
476
530
end
477
531
478
532
module Flat = struct
479
533
480
534
type program = {
481
- types : Structured .typ list C.Map .t ;
535
+ types : Types .types C.Map .t ;
482
536
type_abbrevs : type_abbrev_declaration C.Map .t ;
483
537
modes : (mode * Loc .t ) C.Map .t ;
484
538
clauses : (preterm ,Ast.Structured .attribute ) Ast.Clause .t list ;
495
549
module Assembled = struct
496
550
497
551
type program = {
498
- types : Structured .typ list C.Map .t ;
552
+ types : Types .types C.Map .t ;
499
553
type_abbrevs : type_abbrev_declaration C.Map .t ;
500
554
modes : (mode * Loc .t ) C.Map .t ;
501
555
clauses_rev : (preterm ,attribute) Ast.Clause .t list ;
@@ -538,7 +592,7 @@ module WithMain = struct
538
592
539
593
(* The entire program + query, but still in "printable" format *)
540
594
type 'a query = {
541
- types : Structured .typ list C.Map .t ;
595
+ types : Types .types C.Map .t ;
542
596
type_abbrevs : type_abbrev_declaration C.Map .t ;
543
597
modes : mode C.Map .t ;
544
598
clauses_rev : (preterm ,Assembled .attribute ) Ast.Clause .t list ;
@@ -859,10 +913,10 @@ module ToDBL : sig
859
913
(* Exported since also used to flatten (here we "flatten" locals) *)
860
914
val prefix_const : State .t -> string list -> C .t -> State .t * C .t
861
915
val merge_modes : State .t -> (mode * Loc .t ) Map .t -> (mode * Loc .t ) Map .t -> (mode * Loc .t ) Map .t
862
- val merge_types :
863
- Structured .typ list C.Map .t ->
864
- Structured .typ list C.Map .t ->
865
- Structured .typ list C.Map .t
916
+ val merge_types : State .t ->
917
+ Types .types C.Map .t ->
918
+ Types .types C.Map .t ->
919
+ Types .types C.Map .t
866
920
val merge_type_abbrevs : State .t ->
867
921
type_abbrev_declaration C.Map .t ->
868
922
type_abbrev_declaration C.Map .t ->
@@ -1234,7 +1288,7 @@ let query_preterm_of_ast ~depth macros state (loc, t) =
1234
1288
let state, ttype =
1235
1289
preterms_of_ast ~on_type: true loc ~depth: lcs F.Map. empty state (fun ~depth :_ state x -> state, [loc,x]) ty in
1236
1290
let ttype = assert (List. length ttype = 1 ); List. hd ttype in
1237
- state, { Structured . tindex = attributes; decl = { tname; ttype; tloc = loc } }
1291
+ state, { Types . tindex = attributes; decl = { tname; ttype; tloc = loc } }
1238
1292
1239
1293
let funct_of_ast state c =
1240
1294
try
@@ -1258,21 +1312,16 @@ let query_preterm_of_ast ~depth macros state (loc, t) =
1258
1312
state, C.Map. add mname (args,loc) modes
1259
1313
1260
1314
let merge_modes state m1 m2 =
1315
+ if C.Map. is_empty m1 then m2 else
1261
1316
C.Map. fold (fun k v m ->
1262
1317
check_duplicate_mode state k v m;
1263
1318
C.Map. add k v m)
1264
1319
m2 m1
1265
-
1266
- let merge_types t1 t2 =
1267
- C.Map. merge (fun _ l1 l2 ->
1268
- match l1, l2 with
1269
- | None , None -> None
1270
- | Some _ as l , None -> l
1271
- | None , (Some _ as l ) -> l
1272
- | Some l1 , Some l2 ->
1273
- Some (l1 @ (List. filter (fun x -> not @@ List. mem x l1) l2))) t1 t2
1320
+ let merge_types _s t1 t2 =
1321
+ C.Map. union (fun _ l1 l2 -> Some (Types. merge l1 l2)) t1 t2
1274
1322
1275
1323
let merge_type_abbrevs s m1 m2 =
1324
+ if C.Map. is_empty m2 then m1 else
1276
1325
C.Map. fold (fun _ v m -> add_to_index_type_abbrev s m v) m1 m2
1277
1326
1278
1327
let rec toplevel_clausify loc ~depth state t =
@@ -1351,9 +1400,9 @@ let query_preterm_of_ast ~depth macros state (loc, t) =
1351
1400
let map_append k v m =
1352
1401
try
1353
1402
let l = C.Map. find k m in
1354
- C.Map. add k (v:: l) m
1403
+ C.Map. add k (Types. append v l) m
1355
1404
with Not_found ->
1356
- C.Map. add k [v] m
1405
+ C.Map. add k ( Types. make v) m
1357
1406
1358
1407
let run (state : State.t ) ~toplevel_macros p =
1359
1408
(* FIXME: otypes omodes - NO, rewrite spilling on data.term *)
@@ -1365,7 +1414,7 @@ let query_preterm_of_ast ~depth macros state (loc, t) =
1365
1414
let type_abbrevs = List. fold_left (add_to_index_type_abbrev state) C.Map. empty type_abbrevs in
1366
1415
let state, types =
1367
1416
map_acc (compile_type lcs) state types in
1368
- let types = List. fold_left (fun m t -> map_append t.Structured . decl.tname t m) C.Map. empty types in
1417
+ let types = List. fold_left (fun m t -> map_append t.Types . decl.tname t m) C.Map. empty types in
1369
1418
let state, modes = List. fold_left compile_mode (state,C.Map. empty) modes in
1370
1419
let defs_m = defs_of_modes modes in
1371
1420
let defs_t = defs_of_types types in
@@ -1391,7 +1440,7 @@ let query_preterm_of_ast ~depth macros state (loc, t) =
1391
1440
compile_program macros lcs state p in
1392
1441
let defs = C.Set. union defs symbols in
1393
1442
let modes = merge_modes state modes mp in
1394
- let types = merge_types types tp in
1443
+ let types = merge_types state types tp in
1395
1444
let type_abbrevs = merge_type_abbrevs state type_abbrevs ta in
1396
1445
let state = set_varmap state orig_varmap in
1397
1446
let lcs, state, types, type_abbrevs, modes, defs, compiled_rest =
@@ -1514,12 +1563,12 @@ let subst_amap state f { nargs; c2i; i2n; n2t; n2i } =
1514
1563
t,c) n2t in
1515
1564
{ nargs; c2i; i2n; n2t; n2i }
1516
1565
1517
- let smart_map_type state f ({ Structured . tindex; decl = { tname; ttype; tloc } } as tdecl ) =
1566
+ let smart_map_type state f ({ Types . tindex; decl = { tname; ttype; tloc } } as tdecl ) =
1518
1567
let tname1 = f tname in
1519
1568
let ttype1 = smart_map_term ~on_type: true state f ttype.term in
1520
1569
let tamap1 = subst_amap state f ttype.amap in
1521
1570
if tname1 == tname && ttype1 == ttype.term && ttype.amap = tamap1 then tdecl
1522
- else { Structured . tindex; decl = { tname = tname1; tloc; ttype = { term = ttype1; amap = tamap1; loc = ttype.loc; spilling = ttype.spilling } } }
1571
+ else { Types . tindex; decl = { tname = tname1; tloc; ttype = { term = ttype1; amap = tamap1; loc = ttype.loc; spilling = ttype.spilling } } }
1523
1572
1524
1573
1525
1574
let map_sequent state f { peigen; pcontext; pconclusion } =
@@ -1577,7 +1626,7 @@ let subst_amap state f { nargs; c2i; i2n; n2t; n2i } =
1577
1626
1578
1627
let apply_subst_types ?live_symbols st s tm =
1579
1628
let ksub = apply_subst_constant ?live_symbols s in
1580
- C.Map. fold (fun k tl m -> C.Map. add (ksub k) (smart_map (smart_map_type st ksub) tl) m) tm C.Map. empty
1629
+ C.Map. fold (fun k tl m -> C.Map. add (ksub k) (Types. smart_map (smart_map_type st ksub) tl) m) tm C.Map. empty
1581
1630
1582
1631
let apply_subst_type_abbrevs ?live_symbols st s = tabbrevs_map st (apply_subst_constant ?live_symbols s)
1583
1632
@@ -1611,15 +1660,15 @@ let subst_amap state f { nargs; c2i; i2n; n2t; n2i } =
1611
1660
| [] -> types, type_abbrevs, modes, clauses, chr
1612
1661
| Shorten (shorthands , { types = t ; type_abbrevs = ta ; modes = m ; body; symbols = s } ) :: rest ->
1613
1662
let insubst = push_subst_shorthands shorthands s subst in
1614
- let types = ToDBL. merge_types (apply_subst_types ~live_symbols state insubst t) types in
1663
+ let types = ToDBL. merge_types state (apply_subst_types ~live_symbols state insubst t) types in
1615
1664
let type_abbrevs = ToDBL. merge_type_abbrevs state (apply_subst_type_abbrevs ~live_symbols state insubst ta) type_abbrevs in
1616
1665
let modes = ToDBL. merge_modes state (apply_subst_modes ~live_symbols insubst m) modes in
1617
1666
let types, type_abbrevs, modes, clauses, chr =
1618
1667
compile_body live_symbols state lcs types type_abbrevs modes clauses chr insubst body in
1619
1668
compile_body live_symbols state lcs types type_abbrevs modes clauses chr subst rest
1620
1669
| Namespace (extra , { types = t ; type_abbrevs = ta ; modes = m ; body; symbols = s } ) :: rest ->
1621
1670
let state, insubst = push_subst state extra s subst in
1622
- let types = ToDBL. merge_types (apply_subst_types ~live_symbols state insubst t) types in
1671
+ let types = ToDBL. merge_types state (apply_subst_types ~live_symbols state insubst t) types in
1623
1672
let type_abbrevs = ToDBL. merge_type_abbrevs state (apply_subst_type_abbrevs ~live_symbols state insubst ta) type_abbrevs in
1624
1673
let modes = ToDBL. merge_modes state (apply_subst_modes ~live_symbols insubst m) modes in
1625
1674
let types, type_abbrevs, modes, clauses, chr =
@@ -1630,7 +1679,7 @@ let subst_amap state f { nargs; c2i; i2n; n2t; n2i } =
1630
1679
let clauses = clauses @ cl in
1631
1680
compile_body live_symbols state lcs types type_abbrevs modes clauses chr subst rest
1632
1681
| Constraints (clique , rules , { types = t ; type_abbrevs = ta ; modes = m ; body } ) :: rest ->
1633
- let types = ToDBL. merge_types (apply_subst_types ~live_symbols state subst t) types in
1682
+ let types = ToDBL. merge_types state (apply_subst_types ~live_symbols state subst t) types in
1634
1683
let type_abbrevs = ToDBL. merge_type_abbrevs state (apply_subst_type_abbrevs ~live_symbols state subst ta) type_abbrevs in
1635
1684
let modes = ToDBL. merge_modes state (apply_subst_modes ~live_symbols subst m) modes in
1636
1685
let chr = apply_subst_chr ~live_symbols state subst (clique,rules) :: chr in
@@ -1697,16 +1746,16 @@ module Spill : sig
1697
1746
1698
1747
1699
1748
val spill_clause :
1700
- State .t -> types :Structured . typ list C.Map .t -> modes :(constant -> mode ) ->
1749
+ State .t -> types :Types . types C.Map .t -> modes :(constant -> mode ) ->
1701
1750
(preterm , 'a ) Ast.Clause .t -> (preterm , 'a ) Ast.Clause .t
1702
1751
1703
1752
val spill_chr :
1704
- State .t -> types :Structured . typ list C.Map .t -> modes :(constant -> mode ) ->
1753
+ State .t -> types :Types . types C.Map .t -> modes :(constant -> mode ) ->
1705
1754
(constant list * prechr_rule list ) -> (constant list * prechr_rule list )
1706
1755
1707
1756
(* Exported to compile the query *)
1708
1757
val spill_preterm :
1709
- State .t -> Structured .typ list C.Map .t -> (C .t -> mode ) -> preterm -> preterm
1758
+ State .t -> Types .types C.Map .t -> (C .t -> mode ) -> preterm -> preterm
1710
1759
1711
1760
end = struct (* {{{ *)
1712
1761
@@ -1722,7 +1771,7 @@ end = struct (* {{{ *)
1722
1771
1723
1772
let type_of_const types c =
1724
1773
try
1725
- let { Structured . decl = { ttype } } = List. hd @@ List. rev @@ C.Map. find c types in
1774
+ let { Types . decl = { ttype } } = ( C.Map. find c types). Types. def in
1726
1775
read_ty ttype.term
1727
1776
with
1728
1777
Not_found -> `Unknown
@@ -2037,7 +2086,7 @@ let assemble flags state code (ul : compilation_unit list) =
2037
2086
state, code in
2038
2087
let modes = ToDBL. merge_modes state m1 m2 in
2039
2088
let type_abbrevs = ToDBL. merge_type_abbrevs state ta1 ta2 in
2040
- let types = ToDBL. merge_types t1 t2 in
2089
+ let types = ToDBL. merge_types state t1 t2 in
2041
2090
let cl2 = filter_if flags clause_name cl2 in
2042
2091
let cl2 = List. map (Spill. spill_clause state ~types ~modes: (fun c -> fst @@ C.Map. find c modes)) cl2 in
2043
2092
let c2 = List. map (Spill. spill_chr state ~types ~modes: (fun c -> fst @@ C.Map. find c modes)) c2 in
@@ -2206,19 +2255,19 @@ let is_builtin state tname =
2206
2255
let check_all_builtin_are_typed state types =
2207
2256
Constants.Set. iter (fun c ->
2208
2257
if not (match C.Map. find c types with
2209
- | l -> l |> List . for_all (fun { Structured . tindex;_} -> tindex = Ast.Structured. External )
2258
+ | l -> l |> Types . for_all (fun { Types . tindex;_} -> tindex = Ast.Structured. External )
2210
2259
| exception Not_found -> false ) then
2211
2260
error (" Built-in without external type declaration: " ^ Symbols. show state c))
2212
2261
(Builtins. all state);
2213
- C.Map. iter (fun tname tl -> tl |> List . iter (fun { Structured . tindex; decl = { tname; tloc } } ->
2262
+ C.Map. iter (fun tname tl -> tl |> Types . iter (fun { Types . tindex; decl = { tname; tloc } } ->
2214
2263
if tindex = Ast.Structured. External && not (is_builtin state tname) then
2215
2264
error ~loc: tloc (" external type declaration without Built-in: " ^
2216
2265
Symbols. show state tname)))
2217
2266
types
2218
2267
;;
2219
2268
2220
2269
let check_no_regular_types_for_builtins state types =
2221
- C.Map. iter (fun tname l -> l |> List . iter (fun {Structured . tindex; decl = { tloc } } ->
2270
+ C.Map. iter (fun tname l -> l |> Types . iter (fun {Types . tindex; decl = { tloc } } ->
2222
2271
if tindex <> Ast.Structured. External && is_builtin state tname then
2223
2272
anomaly ~loc: tloc (" type declaration for Built-in " ^
2224
2273
Symbols. show state tname ^ " must be flagged as external" );
@@ -2467,7 +2516,7 @@ let run
2467
2516
map
2468
2517
with Not_found ->
2469
2518
C.Map. add name (mode,index) map in
2470
- let map = C.Map. fold (fun tname l acc -> l |> List. fold_left (fun acc { Structured . tindex } -> add_indexing_for tname (Some tindex) acc) acc) types C.Map. empty in
2519
+ let map = C.Map. fold (fun tname l acc -> Types. fold (fun acc { Types . tindex } -> add_indexing_for tname (Some tindex) acc) acc l ) types C.Map. empty in
2471
2520
let map = C.Map. fold (fun k _ m -> add_indexing_for k None m) modes map in
2472
2521
map in
2473
2522
let state, clauses_rev =
@@ -2715,8 +2764,9 @@ let static_check ~exec ~checker:(state,program)
2715
2764
let time = `Compiletime in
2716
2765
let state, p,q = quote_syntax time state q in
2717
2766
let state, tlist = C.Map. fold (fun tname l (state ,tl ) ->
2767
+ let l = l.Types. lst in
2718
2768
let state, l =
2719
- List. rev l |> map_acc (fun state { Structured .decl = { ttype } } ->
2769
+ List. rev l |> map_acc (fun state { Types .decl = { ttype } } ->
2720
2770
let state, c = mkQCon time ~compiler_state state ~on_type: false tname in
2721
2771
let ttypet = unfold_type_abbrevs ~compiler_state initial_depth type_abbrevs ttype in
2722
2772
let state, ttypet = quote_preterm time ~compiler_state state ~on_type: true ttypet in
0 commit comments