forked from vouillon/coinst
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coinst.ml
1394 lines (1298 loc) · 41 KB
/
coinst.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
(* Co-installability tools
* http://coinst.irill.org/
* Copyright (C) 2010-2011 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* These programs are free software; you can redistribute them and/or
* modify them under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
(*
./check_coinstall -ignore daemontools-run /var/lib/apt/lists/ftp.fr.debian.org_debian_dists_testing_main_binary-amd64_Packages -ignore liboss-salsa-asound2 -ignore libgd2-noxpm -ignore libqt4-phonon -ignore libjack-jackd2-0 -ignore libjpeg8-dev -ignore libhdf4-dev -ignore libgl1-mesa-swx11
*)
let mark_all = ref false
let mark_reversed = ref false
let grayscale = ref false
let explain = ref false
let roots = ref []
let stats = ref false
let graph = ref "graph.dot"
let conflict_file = ref None
type output_type = Graph | Json
let output_type = ref Graph
(****)
let insert tbl x v =
let l =
try Hashtbl.find tbl x with Not_found ->
let l = ref [] in Hashtbl.add tbl x l; l
in
l := v :: !l
let get tbl x = try !(Hashtbl.find tbl x) with Not_found -> []
(****)
module F (M : Api.S) = struct
module Repository = Repository.F(M)
open Repository
module Quotient = Quotient.F (Repository)
module Graph = Graph.F (Repository)
module Json = Coinst_json.F (Repository)
(****)
let get_output_f () =
match !output_type with
| Graph -> Graph.output
| Json -> Json.output
let simplify_formula confl f =
Formula.filter
(fun d ->
Disj.for_all
(fun p ->
Conflict.exists confl (fun q -> not (Disj.implies1 q d)) p) d)
f
let filter_conflicts confl p f =
Formula.fold
(fun d nf ->
Formula.conj nf
(Formula.of_disj
(Disj.filter
(fun q ->
not (PSet.exists (fun r -> Formula.implies1 f (Disj.lit r))
(Conflict.of_package confl q)))
d)))
f Formula._true
let rec flatten_deps tbl dist deps conflicts visited l =
Formula.fold
(fun d (l, r) ->
let (l', r') =
Disj.fold
(fun i (l, r) ->
let (l', r') = flatten_dep tbl dist deps conflicts visited i in
(Formula.disj l' l, PSet.union r r')) d (Formula._false, r)
in
(Formula.conj l' l, r'))
l (Formula._true, PSet.empty)
and flatten_dep tbl dist deps conflicts visited i =
try
(Hashtbl.find tbl i, PSet.empty)
with Not_found ->
let res =
if List.mem i visited then
(Formula._true, PSet.singleton i)
else begin
let (l, r) =
flatten_deps tbl dist deps conflicts (i :: visited) (PTbl.get deps i)
in
let l = simplify_formula conflicts l in
(*???
let l = filter_conflicts conflicts i l in
*)
let r = PSet.remove i r in
if Conflict.has conflicts i then
(Formula.conj (Formula.lit i) l, r)
else
(l, r)
end
in
(* Only cache the result if it is unconditionally true *)
if PSet.is_empty (snd res) then Hashtbl.add tbl i (fst res);
res
let flatten_dependencies dist deps confl =
let tbl = Hashtbl.create 17 in
PTbl.init dist (fun p -> fst (flatten_dep tbl dist deps confl [] p))
(****)
let focus pool deps confl =
(*
- Map: i |-> (p, d)
-
*)
let large = false in
if !roots <> [] then begin
let i = ref (-1) in
let pieces = Hashtbl.create 101 in
let piece_conflicts = PTbl.create pool [] in
let package_pieces = PTbl.create pool [] in
let tbl_add tbl p v = PTbl.set tbl p (v :: PTbl.get tbl p) in
PTbl.iteri
(fun p f ->
Formula.iter f
(fun d ->
incr i;
Disj.iter d
(fun q -> tbl_add piece_conflicts q !i);
tbl_add package_pieces p !i;
Hashtbl.add pieces !i (p, d)))
deps;
let roots =
List.fold_left
(fun s nm ->
List.fold_left (fun s p -> PSet.add (Package.of_index p) s)
s (M.parse_package_name pool nm))
PSet.empty !roots
in
Format.eprintf "ROOTS: %d@." (PSet.cardinal roots);
let dp = deps in
let new_deps = PTbl.create pool Formula._true in
let new_confl = Conflict.create pool in
let visited = ref PSet.empty in
let packages = ref PSet.empty in
let rec add_conflict p =
Format.eprintf "ADD confl: %a@." (Package.print pool) p;
if not (PSet.mem p !visited) then begin
visited := PSet.add p !visited;
packages := PSet.add p !packages;
PTbl.set new_deps p
(Formula.conj (PTbl.get new_deps p) (Formula.lit p));
PSet.iter
(fun q ->
Format.eprintf "ADD confl: %a %a@." (Package.print pool) p (Package.print pool) q;
Conflict.add new_confl p q;
let l = PTbl.get piece_conflicts q in
packages := PSet.add q !packages;
List.iter (fun i -> add_piece (Some q) i) l)
(Conflict.of_package confl p)
end
and add_piece q i =
if large then begin
let (p, d) = Hashtbl.find pieces i in
let l = PTbl.get package_pieces p in
List.iter (fun i -> add_piece_2 q i) l
end else
add_piece_2 q i
and add_piece_2 q i =
let (p, d) = Hashtbl.find pieces i in
Format.eprintf "ADD piece: %a => %a@." (Package.print pool) p (Disj.print pool) d;
if
(*
true
*)
PSet.mem p roots ||
(*FIX: or any equivalent package... *)
not (Disj.exists (fun r -> PSet.mem r roots) d
||
(*FIX: or any equivalent package... *)
(Some p <> q &&
PSet.exists (fun r -> Conflict.check confl r p) roots)
)
then begin
packages := PSet.add p !packages;
PTbl.set new_deps p
(Formula.conj (PTbl.get new_deps p) (Formula.of_disj d));
Disj.iter d (fun r -> if Some r <> q then add_conflict r)
end
and add_dep p =
packages := PSet.add p !packages;
let l = PTbl.get package_pieces p in
List.iter
(fun i -> add_piece None i)
l
in
PSet.iter
(fun p ->
Format.eprintf "ADD root: %a ==> %a@." (Package.print pool) p (Formula.print pool) (PTbl.get dp p);
add_dep p;
(*
PSet.iter add_dep (Conflict.of_package confl p)
*)
)
roots;
Conflict.iter confl
(fun p q ->
if
PSet.mem p !packages &&
Formula.implies (PTbl.get new_deps p) (Formula.lit p) &&
PSet.mem q !packages &&
Formula.implies (PTbl.get new_deps q) (Formula.lit q)
then begin
Conflict.add new_confl p q;
end);
(*
*)
(Some !packages, new_deps, new_confl)
end else
(None, deps, confl)
(****)
(*
If a set of packages are not co-installable, then there is a minimal
set of dependencies and conflicts that make them incompatible. We
enumerate (an overapproximation) of all these minimal sets.
We define inductively *forced* dependencies and packages:
- a dependency d is forced when all packages p in d but at most one
are forced;
- a package p is forced when all dependencies d' such that there
exists a package p' in d' such that p conflicts wih p', either
d' is forced, or p in d'.
In a minimal set, if we have dependencies d, d' and packages p, p'
such that p in d, p' in d', p conflicts with p' and d' is forced,
then there are no other dependencies such that p'' in d'' and p''
conflicts with p.
In a minimal set, if we have dependencies d, d' and packages p, p'
such that p in d, p' in d', p conflicts with p' and p is forced,
then
1) d' is forced
2) p not in d'
3) there is no other dependency in a similar situation with respect
to p
4) p' is not forced (so all other packages in d' are forced)
XXX
*)
let debug_problems =
Debug.make "coinst_prob"
"Debug enumeration of possible co-installability issues" []
(*let _ = Debug.set "coinst_prob"*)
let debug = false
module IntSet = Util.IntSet
module ListTbl = Util.ListTbl
module PSetSet = Set.Make (PSet)
module PSetMap = Map.Make (PSet)
type st =
{ dist : M.pool; deps : Formula.t PTbl.t; confl : Conflict.t;
pieces : (int, Package.t * Disj.t) Hashtbl.t;
pieces_in_confl : (Package.t, int) ListTbl.t;
set : PSet.t; forced_deps : (int, Package.t option) Hashtbl.t;
forced_packages : PSet.t;
installed : IntSet.t; not_installed : IntSet.t;
check : PSet.t -> bool }
let print_prob st =
let space = String.make (2 * IntSet.cardinal st.installed) ' ' in
Format.eprintf "-------------------@.";
IntSet.iter
(fun i ->
let (p, d) = Hashtbl.find st.pieces i in
Format.eprintf "%s| %a => %a@." space
(Package.print_name st.dist) p
(Disj.print st.dist) d)
st.installed
let rec add_piece st i cont =
assert (not (IntSet.mem i st.installed || IntSet.mem i st.not_installed));
let (p, d) = Hashtbl.find st.pieces i in
(*
if debug(*_problems ()*) then
*)
(*
Format.printf "Considering %a => %a@."
(Package.print_name st.dist) p (Disj.print st.dist) d;
*)
if
(* We do not add a dependency if it is implied by, or implies, a
dependency currently under consideration. *)
not (IntSet.exists
(fun i' ->
let (_, d') = Hashtbl.find st.pieces i' in
Disj.implies d d' || Disj.implies d' d)
st.installed)
&&
(* When adding a package in st.set, we check that d is not implied
by any of the dependencies of a package already in st.set *)
(PSet.mem p st.set ||
not (PSet.exists
(fun p -> Formula.implies1 (PTbl.get st.deps p) d)
st.set))
&&
(* If we are adding a package, we check whether the set is still
co-installable *)
(PSet.mem p st.set || st.check (PSet.add p st.set))
then begin
if debug(*_problems ()*) then
Format.printf "Adding %a => %a@."
(Package.print_name st.dist) p (Disj.print st.dist) d;
let st =
{st with
set = PSet.add p st.set;
(*
forced =
(match Disj.to_lit d with
Some q -> PSet.union (Conflict.of_package st.confl q) st.forced
| None -> st.forced);
*)
installed = IntSet.add i st.installed}
in
(*
Format.eprintf "-";
IntSet.iter (fun i -> Format.eprintf " %d" i) st.installed;
Format.eprintf "@.";
*)
if debug(*_problems ()*) then print_prob st;
if false then print_prob st;
(* Make sure that there is at least one piece in conflict for all
dependencies, then consider all possible additions *)
Disj.fold
(fun p cont st ->
if
PSet.exists (fun q -> PSet.mem q st.forced_packages)
(Conflict.of_package st.confl p)
then
cont st
else
let st =
PSet.fold
(fun q st ->
List.fold_right
(fun j st ->
match
try
Hashtbl.find st.forced_deps j
with Not_found ->
None
with
Some q' when q' = q ->
let (_, d') = Hashtbl.find st.pieces j in
if Disj.implies1 p d' then st else
do_add_piece st q j cont
| _ ->
st)
(ListTbl.find st.pieces_in_confl q) st)
(Conflict.of_package st.confl p) st
in
PSet.fold
(fun q cont ->
List.fold_right
(fun j cont st ->
if Hashtbl.mem st.forced_deps j then
cont st
else
maybe_add_piece st j cont)
(ListTbl.find st.pieces_in_confl q) cont)
(Conflict.of_package st.confl p) cont st)
d
cont
st
end else
if debug(*_problems ()*) then
Format.printf "Could not add %a => %a@."
(Package.print_name st.dist) p (Disj.print st.dist) d;
and do_add_piece st q i cont =
if IntSet.mem i st.installed then begin
st
end else if not (IntSet.mem i st.not_installed) then begin
add_piece {st with forced_packages = PSet.add q st.forced_packages} i cont;
st(* {st with not_installed = IntSet.add i st.not_installed}*)
end else
st
and maybe_add_piece st i cont =
if
not (IntSet.mem i st.installed || IntSet.mem i st.not_installed)
then begin
add_piece st i cont;
cont {st with not_installed = IntSet.add i st.not_installed}
end else
cont st
let forced_deps confl pieces pieces_in_confl =
let forced = Hashtbl.create 101 in
while
let changed = ref false in
Hashtbl.iter
(fun i (p, d) ->
if not (Hashtbl.mem forced i) then begin
let forced_package q =
PSet.for_all
(fun q' ->
List.for_all
(fun i' ->
Hashtbl.mem forced i' ||
let (p', d') = Hashtbl.find pieces i' in
Disj.implies1 q d')
(ListTbl.find pieces_in_confl q'))
(Conflict.of_package confl q)
in
let floating_packages =
Disj.filter (fun q -> not (forced_package q)) d in
if Disj.implies floating_packages Disj._false then begin
Hashtbl.add forced i None; changed := true
end else
match Disj.to_lit floating_packages with
Some p -> Hashtbl.add forced i (Some p); changed := true
| None -> ()
end)
pieces;
!changed
do () done;
forced
(*
Hashtbl.iter
(fun i (p, d) ->
if not (Hashtbl.mem forced i) then begin
Format.eprintf "> %a => %a@."
(Package.print_name dist) p
(Disj.print dist) d;
Disj.iter d (fun q ->
PSet.iter
(fun q' ->
try
let i' =
List.find
(fun i' ->
not (Hashtbl.mem forced i' ||
let (p', d') = Hashtbl.find pieces i' in
Disj.implies1 q d'))
(ListTbl.find pieces_in_confl q')
in
let (p, d) = Hashtbl.find pieces i' in
Format.eprintf "(%a => %a)@."
(Package.print_name dist) p
(Disj.print dist) d
with Not_found -> ())
(Conflict.of_package confl q))
end)
pieces;
let deps = PTbl.create (Quotient.pool quotient) Formula._true in
Hashtbl.iter
(fun i (p, d) ->
if not (Hashtbl.mem forced i) then
PTbl.set deps p (Formula.conj (PTbl.get deps p) (Formula.of_disj d)))
pieces;
Graph.output "/tmp/floating.dot" ~mark_all:false
~grayscale:(!grayscale)
quotient deps confl
*)
let find_problems quotient deps confl check =
let dist = Quotient.pool quotient in
let pieces = Hashtbl.create 101 in
let last_piece = ref (-1) in
let pieces_in_confl = ListTbl.create 101 in
Quotient.iter
(fun p ->
let f = PTbl.get deps p in
Formula.iter f
(fun d ->
incr last_piece;
let i = !last_piece in
Hashtbl.add pieces i (p, d);
Disj.iter d (fun p -> ListTbl.add pieces_in_confl p i)))
quotient;
let forced = forced_deps confl pieces pieces_in_confl in
let st =
{ dist = dist; deps = deps; confl = confl;
pieces = pieces; pieces_in_confl = pieces_in_confl;
set = PSet.empty; forced_packages = PSet.empty;
forced_deps = forced; check = check;
installed = IntSet.empty; not_installed = IntSet.empty }
in
let st = ref st in
for i = 0 to !last_piece do
add_piece !st i (fun _ -> ());
if None = try Hashtbl.find forced i with Not_found -> None then begin
st := {!st with not_installed = IntSet.add i (!st).not_installed}
end
done
let enumerate_non_coinstallable_sets quotient deps confl st =
let dist = Quotient.pool quotient in
let results = ref PSetSet.empty in
let add_result s =
if not (PSetSet.mem s !results) then begin
if debug_problems () then begin
Format.eprintf "==>";
PSet.iter
(fun p -> Format.eprintf " %a" (Package.print_name dist) p) s;
Format.eprintf "@."
end;
results := PSetSet.add s !results
end else
if debug then Format.printf "Already considered@."
in
let installable_status = ref PSetMap.empty in
let check s =
let is_installable s =
try PSetMap.find s !installable_status with Not_found ->
let res =
M.Solver.solve_lst st (List.map Package.index (PSet.elements s)) in
M.Solver.reset st;
installable_status := PSetMap.add s res !installable_status;
res
in
if is_installable s then begin
if debug then begin
Format.printf "Still co-installable:";
List.iter (fun p -> Format.printf " %a" (Package.print_name dist) p)
(PSet.elements s);
Format.printf "@.";
end;
true
end else begin
if
PSet.exists (fun p -> not (is_installable (PSet.remove p s))) s
then begin
if debug_problems () then begin
Format.eprintf "Not minimal:";
List.iter (fun p -> Format.eprintf " %a" (Package.print_name dist) p)
(PSet.elements s);
Format.eprintf "@.";
end
end else begin
add_result s
end;
false
end
in
find_problems quotient deps confl check;
!results
(****)
let read_data ignored_packages ic =
let dist = M.new_pool () in
M.parse_packages dist ignored_packages ic;
let confl = Conflict.create dist in
let c = M.compute_conflicts dist in
Array.iteri
(fun p1 l ->
List.iter
(fun p2 ->
Conflict.add confl
(Package.of_index p1) (Package.of_index p2))
l)
c;
let deps =
let d = M.compute_deps dist in
PTbl.init dist
(fun p ->
Formula.conjl
(List.map (fun l' -> Formula.lit_disj (Package.of_index_list l'))
d.(Package.index p)))
in
(dist, deps, confl)
let print_problem quotient deps confl =
let dist = Quotient.pool quotient in
Quotient.iter
(fun p ->
let f = PTbl.get deps p in
Format.eprintf "%a => %a@."
(Package.print dist) p (Formula.print dist) f)
quotient;
Conflict.iter confl
(fun p1 p2 ->
Format.eprintf "%a ## %a@."
(Package.print dist) p1 (Package.print dist) p2)
let generate_rules quotient deps confl =
let dist = Quotient.pool quotient in
let st =
M.Solver.initialize_problem
~print_var:(M.print_pack dist) (M.pool_size dist) in
Conflict.iter confl
(fun p1 p2 ->
let p1 = M.Solver.lit_of_var (Package.index p1) false in
let p2 = M.Solver.lit_of_var (Package.index p2) false in
M.Solver.add_rule st [|p1; p2|] []);
Quotient.iter
(fun p ->
let f = PTbl.get deps p in
Formula.iter f
(fun d ->
let l = Disj.to_lits d in
if not (PSet.mem p l) then begin
let l = List.map (fun p -> Package.index p) (PSet.elements l) in
M.Solver.add_rule st
(Array.of_list
(M.Solver.lit_of_var (Package.index p) false ::
List.map (fun p -> M.Solver.lit_of_var p true) l))
[];
match l with
[] | [_] ->
()
| _ ->
M.Solver.associate_vars st
(M.Solver.lit_of_var (Package.index p) true) l
end))
quotient;
st
(****)
let remove_redundant_conflicts dist deps confl =
let conj_deps p =
let f = PTbl.get deps p in
Formula.fold
(fun d s -> match Disj.to_lit d with Some p -> PSet.add p s | None -> s)
f PSet.empty
in
Conflict.iter confl
(fun p1 p2 ->
let d1 = conj_deps p1 in
let d2 = conj_deps p2 in
if
PSet.exists
(fun q1 ->
PSet.exists
(fun q2 ->
(p1 <> q1 || p2 <> q2) &&
(p1 <> q2 || p2 <> q1) &&
Conflict.check confl q1 q2)
d2)
d1
then begin
(*
Format.eprintf "%a ## %a@."
(Package.print dist) p1 (Package.print dist) p2;
*)
Conflict.remove confl p1 p2
end);
let try_remove_conflict p1 p2 =
let f1 = PTbl.get deps p1 in
let d2 = conj_deps p2 in
if
Formula.exists
(fun d1 ->
Disj.for_all
(fun q1 ->
PSet.exists
(fun q2 ->
(p1 <> q1 || p2 <> q2) &&
(p1 <> q2 || p2 <> q1) &&
Conflict.check confl q1 q2)
d2)
d1)
f1
then begin
(*
Format.eprintf "%a ## %a@."
(Package.print dist) p1 (Package.print dist) p2;
*)
Conflict.remove confl p1 p2
end
in
Conflict.iter confl try_remove_conflict;
Conflict.iter confl (fun p1 p2 -> try_remove_conflict p2 p1);
(* We may now be able to remove some dependencies *)
PTbl.map (simplify_formula confl) deps
(****)
let remove_self_conflicts dist deps confl =
let s = ref PSet.empty in
PTbl.iteri
(fun p f ->
if Formula.exists (fun d -> match Disj.to_lit d with Some q -> Conflict.check confl p q | None -> false) f then
s := PSet.add p !s)
deps;
(*
PSet.iter (fun p -> Format.eprintf "SELF CONFLICT: %a@." (Package.print dist) p) !s;
*)
PTbl.map
(fun f ->
Formula.fold
(fun d f ->
let d = Disj.filter (fun q -> not (PSet.mem q !s)) d in
Formula.conj (Formula.of_disj d) f)
f Formula._true)
deps
(****)
module DepMap = Map.Make (Disj)
module DepSet = Set.Make (Disj)
let coinstallability_kernel quotient deps confl =
let deps_of_confl = PTbl.create (Quotient.pool quotient) DepSet.empty in
let all_deps = ref DepSet.empty in
PTbl.iteri
(fun p f ->
Formula.iter f
(fun d ->
all_deps := DepSet.add d !all_deps;
Disj.iter d
(fun p ->
PTbl.set deps_of_confl p
(DepSet.add d (PTbl.get deps_of_confl p)))))
deps;
let forced_deps = ref DepSet.empty in
let updated = ref false in
let update_dep d =
if not (DepSet.mem d !forced_deps) then begin
(*
Format.eprintf "Checking %a@." (Disj.print (Quotient.pool quotient)) d;
*)
let c =
Disj.fold
(fun p n ->
if n > 1 then n else begin
if
Conflict.for_all confl
(fun p' ->
(*
DepSet.iter (fun d ->
let pool = Quotient.pool quotient in
Format.eprintf "%a # %a => %a %b@." (Package.print pool) p (Package.print pool) p' (Disj.print pool) d (DepSet.mem d !forced_deps)) (PTbl.get deps_of_confl p');
*)
Disj.implies1 p' d
||
DepSet.for_all
(fun d' ->
Disj.equiv d' d (*XXX implies?*)
||
Disj.implies1 p d'
||
DepSet.mem d' !forced_deps)
(PTbl.get deps_of_confl p')
(*
DepSet.subset (DepSet.remove d (PTbl.get deps_of_confl p')) !forced_deps
*)
)
p
then
n
else
1 + n
end)
d 0
in
Format.eprintf "Checked (%d) %a@." c (Disj.print (Quotient.pool quotient)) d;
if c <= 1 then begin
updated := true;
Format.eprintf "Forced: %a@." (Disj.print (Quotient.pool quotient)) d;
forced_deps := DepSet.add d !forced_deps
end
end
in
prerr_endline "========================================";
while
updated := false;
DepSet.iter update_dep !all_deps;
prerr_endline "========================================";
!updated
do () done;
DepSet.iter
(fun d ->
if not (DepSet.mem d !forced_deps) then
Format.eprintf "Not forced: %a@."
(Disj.print (Quotient.pool quotient)) d)
!all_deps;
let deps' = PTbl.create (Quotient.pool quotient) Formula._true in
Quotient.iter
(fun p ->
PTbl.set deps' p
(Formula.filter
(fun d -> not (DepSet.mem d !forced_deps))
(PTbl.get deps p)))
quotient;
let targets = ref PSet.empty in
DepSet.iter
(fun d ->
if not (DepSet.mem d !forced_deps) then
Disj.iter d (fun p -> targets := PSet.add p !targets))
!all_deps;
let confl' = Conflict.create (Quotient.pool quotient) in
Conflict.iter confl
(fun p p' ->
if PSet.mem p !targets || PSet.mem p' !targets then
Conflict.add confl' p p');
(deps', confl')
(****)
let array_mean a =
let c = ref 0 in
let s = ref 0 in
for i = 0 to Array.length a - 1 do
let (u, v) = a.(i) in
c := !c + u;
s := !s + u * v
done;
float !s /. float !c
let array_median a =
let c = ref 0 in
for i = 0 to Array.length a - 1 do
let (u, v) = a.(i) in
c := !c + u
done;
let i = ref 0 in
let s = ref 0 in
while !s < (!c + 1) / 2 do
let (u, v) = a.(!i) in
s := !s + u;
incr i
done;
snd a.(!i - 1)
let rec cone deps s p =
if PSet.mem p s then s else
let s = PSet.add p s in
Formula.fold (fun d s -> Disj.fold (fun q s -> cone deps s q) d s)
(PTbl.get deps p) s
let conflicts_count confl s =
let count = ref 0 in
PSet.iter (fun p -> if Conflict.has confl p then incr count) s;
!count
let output_cone f pool deps confl p s =
(*
Format.eprintf "== %a ==@." (Package.print pool) p;
*)
let quotient = Quotient.subset pool s in
let cfl = Conflict.create pool in
Conflict.iter
confl
(fun p q -> if PSet.mem p s && PSet.mem q s then Conflict.add cfl p q);
let output_f = get_output_f () in
output_f f ~mark_all:true
~package_weight:(fun p -> if Conflict.has confl p then 1000. else 1.)
~edge_color:(fun _ _ _ -> Some "blue") quotient deps cfl
let print_stats f txt quotient deps confl =
let pkgs = ref 0 in
let dps = ref 0 in
let cones = ref [] in
let confls = ref 0 in
Quotient.iter
(fun p ->
incr pkgs;
let c = cone deps PSet.empty p in
let sz = PSet.cardinal c in
let ncfl = conflicts_count confl c in
if sz <= 38 && sz > 20 && ncfl = 3 then
output_cone "/tmp/cone.dot" (Quotient.pool quotient) deps confl p c;
cones :=
(1 (*Quotient.class_size quotient p*),
sz, ncfl) ::
!cones;
Formula.iter (PTbl.get deps p) (fun d -> if not (Disj.implies1 p d) then incr dps))
quotient;
Conflict.iter confl (fun _ _ -> incr confls);
let cones = Array.of_list !cones in
Array.sort (fun (_, x, _) (_, y, _) -> compare y x) cones;
let ch = open_out (Format.sprintf "/tmp/%s.txt" f) in
let s = ref 0 in
for i = 0 to Array.length cones - 1 do
let (w, sz, ncfl) = cones.(i) in
Printf.fprintf ch "%d %d %d\n" !s sz ncfl;
s := !s + w;
if i = Array.length cones - 1 then
Printf.fprintf ch "%d %d\n" !s sz
done;
close_out ch;
let cones = Array.map (fun (w, sz, _) -> (w, sz)) cones in
Format.eprintf "%s: %d package, %d dependencies, %d conflicts; cone size: %d / %f / %d@."
txt !pkgs !dps !confls (snd cones.(0))
(array_mean cones) (array_median cones)
(****)
let f ignored_packages ic =
let (dist, deps, confl) = read_data ignored_packages ic in
if !stats then
print_stats "initial" "Initial repository"
(Quotient.trivial dist) deps confl;
let flatten_deps = flatten_dependencies dist deps confl in
(*XXX FIX: should iterate... *)
let flatten_deps = remove_self_conflicts dist flatten_deps confl in
let flatten_deps = remove_redundant_conflicts dist flatten_deps confl in
let flatten_deps = flatten_dependencies dist flatten_deps confl in
let maybe_remove fd2 p f d =
Disj.exists (fun q ->
Conflict.for_all confl (fun r ->
Formula.exists (fun d' -> Disj.implies d' d && not (Disj.implies1 q d')) (PTbl.get fd2 r)) q
(*
&& (
Format.eprintf "%a =>(%a) %a@." (Package.print dist) p (Package.print dist) q (Disj.print dist) d;
true)
*)
) d
in
let is_composition fd2 p f d =
Formula.exists (fun d' ->
not (Disj.equiv d d') && not (Disj.equiv (Disj.lit p) d') &&
(
let f =
Disj.fold (fun p f -> Formula.disj (PTbl.get fd2 p) f) d' Formula._false
in
let res1 =
Formula.exists (fun d'' -> Disj.implies d d'') f
in
(*
let res2 =
not (Formula.implies (Formula.filter (fun d' -> not (Disj.equiv d d')) f) f)
in
let res =
Disj.exists (fun q ->
Formula.exists (fun d'' ->
Disj.implies d (Disj.cut d' q d'')) (PTbl.get fd2 q)) d'
in
if res1 <> res2 then begin
Format.eprintf "??? %b %b@." res1 res2
end;
*)
(*
if res <> res1 then begin
(*
Format.eprintf "%a : %a => %a@." (Package.print dist) p
(Disj.print dist) d (Formula.print dist) f;
Disj.iter d' (fun q ->
if not (Formula.exists (fun d'' -> Disj.equiv (Disj.lit q) d'') (PTbl.get fd2 q)) then
Format.eprintf "!!! %a => %a@." (Package.print dist) q (Formula.print dist) (PTbl.get fd2 q);
Formula.iter (PTbl.get fd2 q) (fun d'' ->
if Disj.implies d (Disj.cut d' q d'')
then Format.eprintf "%a <= %a / %a / %a@." (Disj.print dist) d (Disj.print dist) d' (Package.print dist) q (Disj.print dist) d''
));
*)
Format.eprintf "!!! %b %b %b@." res res1 res2
end;
*)
res1
)
) f
in
(*
PTbl.iteri (fun p f ->
Formula.iter f (fun d -> if Conflict.exists confl (fun q -> Disj.implies1 q d) p then Format.eprintf "YYY %a ==> %a@." (Package.print dist) p (Disj.print dist) d
)) fd2;
*)
let rec remove_deps deps =
let changed = ref false in
let deps =