-
Notifications
You must be signed in to change notification settings - Fork 125
/
imp_SumCase.ml
1503 lines (1396 loc) · 47.6 KB
/
imp_SumCase.ml
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
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* depends *)
module Format = Base.Format
module Hashtbl = Base.Hashtbl
module List = Base.List
(* alias *)
module Array = Base.Array
module Common = Imp_Common
module FieldSet = StringSet
module FieldMap = StringMap
module PatternAnalysis = Imp_PatternAnalysis
module TypeVar = QmlTypeVars.TypeVar
(* shorthands *)
module Q = QmlAst
(* -- *)
(* type alias *)
type field = string
type path = field list
type rev_path = field list
type rev_prefix_path = field list
type to_dot = int StringMap.t
type sum_ident = int
type sum_index = int
let compare_path a b = List.make_compare String.compare a b
module SumCondition =
struct
(**
A type for controling field addition into a tree.
The standard life of the tag for a tree is the following:
-it starts [Open 0]
-as long as fields are added to an open tree, the tree remains open,
and grows up [Open x++]
-a [size] guard may be added to a tree taged as [Open n], where [n <= size],
is this case, the tag becomes :
[Partial (n, size)] if [n < size], or [Closed size] if [n = size].
Any other case leads to an inconsistency.
-as long as fields are added to a partial tree, the tree remains partial,
and grows up [Partial (current++, size)] until the limit of [size] is reached,
in this last case, the tree becomes closed, [Closed size].
An invariant is that the size of the tree (the FieldMap) is equal to the [current]
value stored in the tag.
[Node ((Open n | Partial (n, _) | Closed n), tree) => size(tree) = n]
*)
type tree_tag =
| Open of int
(**
[Open i] means that the tree has currently [i] present fields,
and there is yet no guard about the size of the tree.
*)
| Partial of int * int
(**
[Partial (current, size)] means that the tree has currently [current]
present fields, and there is a guard about the [size].
In this case, an invariant is [0 <= current < size]
*)
| Closed of int
(**
[Closed size] means that the tree contains already all its field.
Not any further fields may be added to the tree (inconsistency).
*)
module NodeTag =
struct
let is_open = function
| Open _ -> true
| _ -> false
end
type node =
| Node of tree
| Abs
| PConst of QmlAst.const_expr
and tree = {
tag : tree_tag ;
map : node FieldMap.t ;
}
type check =
| Field of bool
| Const of QmlAst.const_expr
| Size of int
type decision =
| True
| Check of check * rev_path
| And of decision list
type negation_item =
| Decision of decision
| NotSumCase of sum_ident * sum_index
type negation = negation_item list
let empty_tree = { tag = Open 0 ; map = FieldMap.empty }
let is_empty_tree t = t == empty_tree
let present rev_path = Check (Field true, rev_path)
let absent rev_path = Check (Field false, rev_path)
let check_field present rev_path = Check (Field present, rev_path)
let const rev_path const = Check (Const const, rev_path)
let size rev_path size = Check (Size size, rev_path)
(*
A type for representing negation of indexed sum.
The index of the map is the sum ident,
the values in the set are the index already invalided.
*)
type sum_case_negation = IntSet.t IntMap.t
let add_sum_case_negation (ident : sum_ident) (index : sum_index) map =
let set = Option.default IntSet.empty (IntMap.find_opt ident map) in
let set = IntSet.add index set in
IntMap.add ident set map
(*
so that t is private for optimized is_empty test,
ensuring with a typing check
*)
module T :
sig
type t = private {
tree : tree ;
(**
The tree representing the condition
*)
sum_case_negation : sum_case_negation ;
(**
Enriched only by the negation function.
This are extra informations for keeping in mide invalided patterns.
*)
implications : (tree * negation) list ;
(**
Implication algebra, list of pending implications.
Invariant:
for any [(_, d)] in [implications], we have [not (tree => d)]
The operation of normalization will add in the tree all decisions
associated to a condition implied by the main tree
*)
}
val empty : t
val cons : tree -> (tree * negation) list -> t
val full_cons : tree -> sum_case_negation -> (tree * negation) list -> t
val tree : tree -> t
end =
struct
type t = {
tree : tree ;
sum_case_negation : sum_case_negation ;
implications : (tree * negation) list ;
}
let empty = {
tree = empty_tree ;
sum_case_negation = IntMap.empty ;
implications = [] ;
}
let cons tree implications =
if is_empty_tree tree && implications = []
then
empty
else {
tree ;
sum_case_negation = IntMap.empty ;
implications ;
}
let full_cons tree sum_case_negation implications =
if is_empty_tree tree && implications = [] && IntMap.is_empty sum_case_negation
then
empty
else {
tree ;
sum_case_negation ;
implications ;
}
let tree tree =
if is_empty_tree tree
then
empty
else {
tree ;
sum_case_negation = IntMap.empty ;
implications = [] ;
}
end
let empty = T.empty
let is_empty e = e == empty
type t = T.t
type implication = t * negation
let pp_tree_tag fmt = function
| Open i -> Format.fprintf fmt "<open:%d>" i
| Partial (i, j) -> Format.fprintf fmt "<partial:%d/%d>" i j
| Closed i -> Format.fprintf fmt "<closed:%d>" i
let rec pp_tree_map fmt t =
if FieldMap.is_empty t then Format.pp_print_string fmt "<empty>" else
let sep = ref false in
FieldMap.iter (
fun key node ->
(if !sep then Format.fprintf fmt "@\n" ; sep := true) ;
Format.fprintf fmt "@[<2>%S:%a@]" key pp_node node
) t
and pp_tree fmt t = pp_node fmt (Node t)
and pp_node fmt = function
| Node t ->
Format.fprintf fmt "Node %a@\n%a" pp_tree_tag t.tag pp_tree_map t.map
| Abs ->
Format.pp_print_string fmt "Abs"
| PConst c ->
Format.fprintf fmt "PConst(%a)" QmlPrint.pp#const c
let pp_check fmt = function
| Field b -> Format.pp_print_string fmt (if b then "present" else "absent")
| Const c -> QmlPrint.pp#const fmt c
| Size i -> Format.fprintf fmt "size:%d" i
let rec pp_decision fmt = function
| True -> Format.pp_print_string fmt "True"
| Check (check, rev_path) ->
Format.fprintf fmt "Check (%a, [ %a ])" pp_check check Common.pp_path rev_path
| And decs ->
Format.fprintf fmt "And [ %a ]" (Format.pp_list " ; " pp_decision) decs
let pp_negation_item fmt = function
| Decision decision -> pp_decision fmt decision
| NotSumCase (ident, index) -> Format.fprintf fmt "NotSumCase(%d[%d])" ident index
let pp_negation fmt negation = Format.pp_list " / " pp_negation_item fmt negation
let pp_implication_tree fmt (tree, neg) =
Format.fprintf fmt "@[<2>(@\n%a@\n =>@\n%a@]@\n)" pp_tree tree pp_negation neg
let pp_implication fmt (t, d) = pp_implication_tree fmt (t.T.tree, d)
let pp fmt t =
if t.T.implications = [] && IntMap.is_empty t.T.sum_case_negation then pp_tree fmt t.T.tree else
Format.fprintf fmt (
"{@[<2>@\n@[<2>@{<bright>tree@}:@\n%a@]@\n"^^
"@[<2>@{<bright>implications@}:@\n%a@]@\n"^^
"@[<2>@{<bright>sum_case_neg@}:@\n%a@]@\n@]}"
)
pp_tree t.T.tree
(Format.pp_list ";@\n" pp_implication_tree) t.T.implications
(IntMap.pp ", " (
fun fmt key set ->
Format.fprintf fmt "%d[%a]" key (IntSet.pp "," Format.pp_print_int) set
)
) t.T.sum_case_negation
exception Inconsistency
let is_conjonction = function
| And _ -> true
| _ -> false
let flatten list =
let rec aux acc = function
| [] -> List.rev acc
| hd :: tl -> (
let acc =
match hd with
| And decs ->
List.rev_append decs acc
| True -> acc
| _ -> hd :: acc
in
aux acc tl
)
in
aux [] list
let conjonction list =
let conj =
match list with
| [] -> True
| [ hd ] -> hd
| _ -> (
match flatten list with
| [] -> True
| list -> And list
)
in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumCondition.conjonction">
OManager.printf "@[<2>@{<bright>conjonction@} [ %a ] ==>@\n%a@]@\n@." (Format.pp_list " ; " pp_decision) list pp_decision conj
#<End>
in
conj
let lief check =
match check with
| Field present ->
if present
then
Node empty_tree
else
Abs
| Const c ->
PConst c
| Size size ->
Node { tag = Partial (0, size) ; map = FieldMap.empty }
let add_path ~check t path =
let inconsistency () =
let () =
#<If:JS_MATCH_COMPILATION $contains "inconsistency">
OManager.printf (
"@{<bright>INCONSISTENCY@} ~check:%a t %a@\n@[<2>where t is:@\n%a@]@\n@[<2>Inconsistency@]@\n@."
)
pp_check check Common.pp_path path
pp_tree t
#<End>
in
raise Inconsistency
in
let incr_tag = function
| Open i -> Open (succ i)
| Partial (i, size) ->
let i = succ i in
if i > size
then
inconsistency ()
else
if i = size
then
Closed size
else
Partial (i, size)
| Closed _ ->
inconsistency ()
in
let already_in = ref false in
let rec aux rev_path t = function
| [] -> (
match check with
| Size size -> (
match t.tag with
| Open i ->
if i > size
then
inconsistency ()
else
let tag =
if i = size
then
Closed i
else
Partial (i, size)
in
{ t with
tag ;
}
| Partial (_, closed) | Closed closed ->
if size <> closed
then
inconsistency ()
else (
already_in := true ;
t
)
)
| _ ->
(*
This is an internal error, it correspond to a empty rev_path
with a presence or const check:
{[
Check (present, [ ]), Check (Const "5", [ ])
]}
This is not handled by this function, but would be
*)
assert false
)
| [ hd ] -> (
match FieldMap.find_opt hd t.map with
| Some node -> (
match node with
| Node tree -> (
let tag = tree.tag and map = tree.map in
match check with
| Field present ->
if present
then (
already_in := true ;
t
)
else
inconsistency ()
| Const c ->
if is_empty_tree tree
then
{ t with
map = FieldMap.add hd (PConst c) t.map ;
}
else
inconsistency ()
| Size size -> (
match tag with
| Open i ->
if i > size
then
inconsistency ()
else
let hd_tag =
if i = size
then
Closed i
else
Partial (i, size)
in
{ t with
map = FieldMap.add hd (Node { tag = hd_tag ; map }) t.map ;
}
| Partial (_, closed) | Closed closed ->
if size <> closed
then
inconsistency ()
else (
already_in := true ;
t
)
)
)
| Abs -> (
match check with
| Field false ->
already_in := true ;
t
| _ ->
inconsistency ()
)
| PConst c -> (
match check with
| Field true ->
already_in := true ;
t
| Const c2 ->
if QmlAstUtils.Const.equal c c2
then (
already_in := true ;
t
)
else
inconsistency ()
| _ ->
inconsistency ()
)
)
| None -> (
(*
We are adding a totaly new hd-tree, we should be starting by building it.
*)
let hd_tree = lief check in
(*
Then, in any case (but the adding of the absence of hd), we should
check the consistency of tag/
*)
let new_tag =
match check with
| Field false ->
(*
We are adding the absence of a field, the tag remains the same
*)
t.tag
| _ -> incr_tag t.tag
in
{
tag = new_tag ;
map = FieldMap.add hd hd_tree t.map ;
}
)
)
| hd :: tl -> (
let rev_path = hd :: rev_path in
let hd_tree, do_incr_tag =
match FieldMap.find_opt hd t.map with
| Some (Node tree) ->
tree, false
| Some Abs | Some (PConst _) ->
inconsistency ()
| None ->
empty_tree, true
in
let hd_tree = aux rev_path hd_tree tl in
{
tag = if do_incr_tag then incr_tag t.tag else t.tag ;
map = FieldMap.add hd (Node hd_tree) t.map ;
}
)
in
let res = aux [] t path in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumCondition.add_path">
OManager.printf (
"@{<bright>add_path@} ~check:%a t %a@\n@[<2>where t is:@\n%a@]@\n@[<2>returns:@\n%a@]@\n@."
)
pp_check check Common.pp_path path
pp_tree t
pp_tree res
#<End>
in
!already_in, res
let add_tree d t =
let rec aux t d =
match d with
| True -> t
| And list ->
List.fold_left aux t list
| Check (check, rev_path) ->
snd (add_path ~check t (List.rev rev_path))
in
let res = aux t d in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumCondition.add">
OManager.printf (
"@{<bright>add@} d t@\n@[<2>where @{<bright>d@} is:@\n%a@]@\n@[<2>and @{<bright>t@} is:@\n%a@]@\n@[<2>returns:@\n%a@]@\n@."
)
pp_decision d
pp_tree t
pp_tree res
#<End>
in
res
(*
( a => b ) <=> ( b included in a )
*)
let implies_tag a b =
match a, b with
| Open a, Open b -> a >= b
| Open _, _ -> false
| Partial (a, _), Open b -> a >= b
| Partial (a, sa), Partial (b, sb) -> sa = sb && a >= b
| Partial _, _ -> false
| Closed a, Open b -> a >= b
| Closed a, Partial (_, b) -> a = b
| Closed a, Closed b -> a = b
let implies_tree a b =
let res =
(is_empty_tree b) || not (is_empty_tree a) && (
Return.set_checkpoint (
fun label ->
let not_included () = Return.return label false in
let rec is_included b a =
let iter_b field node_b =
match FieldMap.find_opt field a.map with
| None -> not_included ()
| Some node_a -> (
match node_b, node_a with
| Abs, Abs -> ()
| Node b, Node a -> is_included b a
| PConst b, PConst a -> if not (QmlAstUtils.Const.equal b a) then not_included ()
| Node b, PConst _ -> if not (is_empty_tree b) then not_included ()
| _ -> not_included ()
)
in
if not (implies_tag a.tag b.tag)
then
not_included ()
else
FieldMap.iter iter_b b.map
in
is_included b a ;
true
)
)
in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumCondition.implies">
OManager.printf (
"@{<bright>implies@} a b@\n@[<2>where @{<bright>a@} is:@\n%a@]@\n@[<2>and @{<bright>b@} is:@\n%a@]@\n@[<2>returns: %b@]@\n@."
)
pp_tree a
pp_tree b
res
#<End>
in
res
let filter_tree t d =
let rec aux d =
match d with
| True -> True
| Check (check, rev_path) ->
if fst (add_path ~check t (List.rev rev_path))
then True
else d
| And list ->
let filter_map d =
let d = aux d in
if d = True then None else Some d
in
let list = List.filter_map filter_map list in
conjonction list
in
let res =
if is_empty_tree t
then
d
else
aux d
in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumCondition.filter">
OManager.printf (
"@{<bright>filter@} t d@\n@[<2>where @{<bright>t@} is:@\n%a@]@\n@[<2>and @{<bright>d@} is:@\n%a@]@\n@[<2>returns:@\n%a@]@\n@."
)
pp_tree t
pp_decision d
pp_decision res
#<End>
in
res
let implies_decision_tree t d = d = True || filter_tree t d = True
let normalize tree sum_case_negation implications =
let fold (((tree, _) as cpl), implications) ((cond, negation) as implication) =
if implies_tree tree cond
then (
let fold (tree, sum_case_negation) item =
match item with
| Decision dec ->
let tree = add_tree dec tree in
tree, sum_case_negation
| NotSumCase (ident, index) ->
let sum_case_negation = add_sum_case_negation ident index sum_case_negation in
tree, sum_case_negation
in
let cpl = List.fold_left fold cpl negation in
cpl, implications
)
else
let implications = implication :: implications in
cpl, implications
in
let (tree, sum_case_negation), implications =
List.fold_left fold ((tree, sum_case_negation), []) implications in
T.full_cons tree sum_case_negation implications
let decision d =
let tree = add_tree d empty_tree in
T.tree tree
let add d t =
let tree = add_tree d t.T.tree in
normalize tree t.T.sum_case_negation t.T.implications
let add_implication (cond, negation) t =
assert (IntMap.is_empty cond.T.sum_case_negation) ;
assert (cond.T.implications = []) ;
normalize t.T.tree t.T.sum_case_negation ((cond.T.tree, negation) :: t.T.implications)
let filter t d = filter_tree t.T.tree d
let implies a b =
assert (IntMap.is_empty b.T.sum_case_negation) ;
assert (b.T.implications = []) ;
implies_tree a.T.tree b.T.tree
let implies_decision t d = implies_decision_tree t.T.tree d
end
module SumEnv =
struct
module M = ListMap.Make ( Order.StringList )
type 'ident t = ( SumCondition.t * 'ident ) list M.t
let empty = M.empty
let add_dot ~rev_path condition ident t =
let () =
#<If:JS_MATCH_COMPILATION $contains "SumEnv">
OManager.printf (
"@{<bright>SumEnv.add@} ~rev_path condition@\n"^^
"@[<2>where @{<bright>rev_path@} is:@\n%a@]@\n"^^
"@[<2>and @{<bright>condition@} is:@\n%a@]@\n@."
)
Common.pp_path rev_path
SumCondition.pp condition
#<End>
in
M.append rev_path (condition, ident) t
let find_dot ~rev_path condition t =
let ident =
match M.find_opt rev_path t with
| None -> None
| Some list ->
let find (cond, ident) = if SumCondition.implies condition cond then Some ident else None in
List.find_map find list
in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumEnv">
OManager.printf (
"@{<bright>SumEnv.find@} ~rev_path condition@\n"^^
"@[<2>where @{<bright>rev_path@} is:@\n%a@]@\n"^^
"@[<2>and @{<bright>condition@} is:@\n%a@]@\n@[<2>returns:@\n%a@]@\n@."
)
Common.pp_path rev_path
SumCondition.pp condition
(Option.pp_meta (DebugPrint.pp ~depth:max_int)) ident
#<End>
in
ident
end
(*
assert or skip the prefix of the sum prefix path
*)
let assert_sum_prefix sum_rev_prefix_path rev_prefix_path =
match sum_rev_prefix_path with
| [] -> ()
| _ ->
let rec aux a b =
match a, b with
| [], _ -> ()
| hda :: tla, hdb :: tlb when hda = hdb -> aux tla tlb
| _ ->
OManager.printf "assert_rev_prefix_path: %a@\nrev_prefix_path: %a@."
Common.pp_path sum_rev_prefix_path
Common.pp_path rev_prefix_path
;
assert false
in
aux (List.rev sum_rev_prefix_path) (List.rev rev_prefix_path)
let rev_skip_sum_prefix sum_rev_prefix_path rev_prefix_path =
match sum_rev_prefix_path with
| [] -> List.rev rev_prefix_path
| _ ->
let rec aux a b =
match a, b with
| [], _ -> b
| hda :: tla, hdb :: tlb when hda = hdb -> aux tla tlb
| _ ->
OManager.printf "sum_rev_prefix_path: %a@\nrev_prefix_path: %a@."
Common.pp_path sum_rev_prefix_path
Common.pp_path rev_prefix_path
;
assert false
in
aux (List.rev sum_rev_prefix_path) (List.rev rev_prefix_path)
module SumAnalysis =
struct
module SC = SumCondition
type sum_case = FieldSet.t
type sum_content = {
ty : QmlAst.ty ;
sum_rev_prefix_path : rev_prefix_path ;
colvar : PatternAnalysis.colvar ;
fields : FieldSet.t ;
cases : FieldSet.t array ;
cache_ty : (field list, QmlAst.ty) Hashtbl.t ;
cache_sum : (field list, sum) Hashtbl.t ;
ident : sum_ident ;
}
and sum = sum_content option
let pp_sum_content fmt sum =
Format.fprintf fmt (
"@[<2>{@\nty: %a@\nrev_prefix_path: %a@\ncolvar: %a@\nfields: [ %a ]@\ncases: %a@\nident: %d@\n}@]"
)
QmlPrint.pp#ty sum.ty
Common.pp_path sum.sum_rev_prefix_path
PatternAnalysis.pp_flag sum.colvar
Common.pp_fieldset sum.fields
(Format.pp_list " / " Common.pp_fieldset) (Array.to_list sum.cases)
sum.ident
let pp_sum fmt sum = Option.pp_none pp_sum_content fmt sum
let ident =
let i = ref (-1) in
(fun () -> incr(i) ; !i)
type t = {
sum : sum ;
rev_prefix_path : rev_prefix_path ;
(*
The prefix path for this sum case, only if this sum was in a nested pattern.
If the pattern is not nested, this is the empty list
*)
rowvar : PatternAnalysis.rowvar ;
(*
the rowvar is specific to each sum case.
the colvar is the same for all cases, and is in the field [sum]
*)
index : sum_index option ;
(*
in case of a closed colvar, we know statically
the index in sum.cases of this analysed case.
*)
absent : FieldSet.t ;
(*
In case of an open colvar, the set is empty.
In other cases, it contains all field not present in any possible case
containing at least the present field (and exactly them, if rowvar is closed)
*)
present : FieldSet.t ;
(*
The field constituting this case. May be incomplete in case of open rowvar,
if we wasn't able to guess the complete case.
*)
}
let pp fmt t =
Format.fprintf fmt "@[<2>{@\nsum: %a@\nprefix: %a@\nrowvar: %a@\nindex: %a@\nabsent: %a@\npresent: %a@\n}@]"
pp_sum t.sum
(Format.pp_list "." Format.pp_print_string) (List.rev t.rev_prefix_path)
PatternAnalysis.pp_flag t.rowvar
(Option.pp_none Format.pp_print_int) t.index
Common.pp_fieldset t.absent
Common.pp_fieldset t.present
let check_set ~rev_prefix_path check set =
FieldSet.fold (fun field acc -> (SC.Check (SC.Field check, field :: rev_prefix_path))::acc) set []
let cases = function
| None -> None
| Some sum -> Some sum.cases
let add t cond =
let rev_prefix_path = t.rev_prefix_path in
let fold check set cond = FieldSet.fold
(fun field cond -> SumCondition.add (SC.Check (SC.Field check, field :: rev_prefix_path)) cond) set cond
in
let cond = fold false t.absent cond in
let cond = fold true t.present cond in
cond
let field_compare = String.compare
let sort_field_list fields = List.sort field_compare fields
let sort_cases a = List.sort (Array.compare String.compare) a
let from_ty ~rev_path gamma ty =
let sum =
Return.set_checkpoint_none (
fun label ->
let cache_ty = Hashtbl.create 10 in
let cache_sum = Hashtbl.create 10 in
let ty = QmlTypesUtils.Inspect.follow_alias_noopt_private gamma ty in
let () = Hashtbl.add cache_ty [] ty in
let cases, colvar =
match ty with
| Q.TypeRecord (Q.TyRow (cases, _)) ->
[ cases ], `closed
| Q.TypeSum (Q.TyCol (cases, colvar)) ->
let colvar = if Option.is_none colvar then `closed else `open_ in
cases, colvar
| Q.TypeVar _ ->
Return.return label ()
| _ ->
OManager.printf "Imp_SumCase.SumAnalysis.from_ty: internal error on type %a@." QmlPrint.pp#ty ty ;
assert false
in
let fields = List.fold_left
(List.fold_left (fun acc (field, _) -> FieldSet.add field acc)) FieldSet.empty cases in
let cases = Array.of_list
(List.map (List.fold_left (fun acc (field, _) -> FieldSet.add field acc) FieldSet.empty) cases) in
let ident = ident () in
let sum =
{
ty ;
sum_rev_prefix_path = rev_path ;
colvar ;
fields ;
cases ;
cache_ty ;
cache_sum ;
ident ;
} in
let () = Hashtbl.add cache_sum rev_path (Some sum) in
sum
)
in
let () =
#<If:JS_MATCH_COMPILATION $contains "SumAnalysis.from_ty">
OManager.printf (
"@{<bright>from_ty@} gamma ty@\n@[<2>where @{<bright>ty@} is:@\n%a@]@\n@[<2>returns:@\n%a@]@\n@."
)
QmlPrint.pp#ty ty
pp_sum sum
#<End>
in
sum
let make sum ~rowvar ~colvar ~rev_prefix_path present =
let t =
match sum with
| None ->
{
sum ;
rev_prefix_path ;
rowvar ;
index = None ;
absent = FieldSet.empty ;
present ;
}
| Some sum -> (
assert_sum_prefix sum.sum_rev_prefix_path rev_prefix_path ;
if sum.colvar <> colvar
then
(*
Internal inconsitency between the typer and PatternAnalysis.
The pattern analysis should build pattern in which, for a given level,
all colvar should be the same. It uses the typer for guessing the correct
value of colvar. The sum was also optained using the typer. If the two
disagree, we have a problem somewhere.
*)
assert false
else
(*
Resolution:
if we are in an closed colvar case, we may try to close the rowvar,
by inspecting how many cases of the sum match the given fields
*)
let rowvar, present, absent, index =
if colvar = `closed
then
let rowvar, present, overlap, index =
if rowvar = `open_
then
(*
[good_cases] is the list (case * index) of all cases where we could be.
Example:
if we are in a sum (a|a;b|a;c|d) and the present fiels are [ a ],
the good_case is [ (|a|, 0) ; (|a;b|, 1) ; (|a,c|, 2) ]
*)
let good_cases =
Array.fold_left_i (
fun acc case index ->
if FieldSet.subset present case
then (case, index) :: acc
else acc
) [] sum.cases
in
match good_cases with
| [] ->
(*
inconsistency, this should have not type
this case means that we are trying to build an analysed sum case
with a pattern which is included in not any cases corresponding to
the type of the pattern
*)
OManager.printf (
"ASSERT FALSE:@\n@[<2>sum is:@\n%a@]@\n@[<2>present is:@\n%a@]@."
)
pp_sum_content sum
Common.pp_fieldset present
;
assert false
| [ hd, index ] ->
(*
We can complete the pattern, and close the rowvar.