package codex

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file condition_map.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
(**************************************************************************)
(*  This file is part of the Codex semantics library.                     *)
(*                                                                        *)
(*  Copyright (C) 2013-2025                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It 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.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file LICENSE).                      *)
(*                                                                        *)
(**************************************************************************)

[@@@warning "-32-34"]

exception Never_refined

module type L = sig
  type t
  val equal: t -> t -> bool
  val compare: t -> t -> int
  val hash: t -> int
end  

module type CONDITION = sig
  type t
  val pretty: Format.formatter -> t -> unit
  val all: t
  val equal: t -> t -> bool
  val empty: t
  val is_empty: t -> bool
  val inter: t -> t -> t
  val (&&~): t -> t -> t
  val union: t -> t -> t
  val (||~): t -> t -> t    
  val disjoint: t -> t -> bool
  val is_included: t -> t -> bool
  val complement: t -> t
  val var: unit -> t
  val hash: t -> int
end

module type LConditionMapCommon = sig
  module L:L
  module Cond:CONDITION
  type t
  val pretty: (Format.formatter -> L.t -> unit) -> Format.formatter -> t -> unit
  val find: join:(L.t -> L.t -> L.t) -> bottom:L.t -> t -> Cond.t -> L.t
  val refine: inter:(L.t -> L.t -> L.t) -> t -> cond:Cond.t -> ?notcond:Cond.t -> L.t -> t
end


module type LConditionMapNoPartial = sig
  include LConditionMapCommon
end

module type LConditionMap = sig
  include LConditionMapCommon
  val create_partial: t
end

module type LConditionMapFold = sig
  include LConditionMap
  val create_partial: t
  val fold_with_cond: t -> Cond.t -> 'a -> (L.t -> Cond.t -> 'a -> 'a) -> 'a
end


module FakeTop(L:L)(* :L with type t = L.t option *) = struct
  type t = L.t option
  let join ~join a b = match (a,b) with
    | None, x | x, None -> raise Never_refined
    | Some a, Some b -> Some(join a b)

  let inter ~inter a b = match (a,b) with
    | None, x | x, None -> x
    | Some a, Some b -> Some(inter a b)

  let bottom ~bottom = Some bottom

  let pretty pp fmt = function
    | None -> Format.fprintf fmt "<none>"
    | Some x -> pp fmt x

  let equal a b = match a,b with
    | None,None -> true
    | Some a, Some b -> L.equal a b
    | _ ,_-> false

  let compare a b = match a,b with
    | None, None -> 0
    | None, Some _ -> 1
    | Some _, None -> -1
    | Some a, Some b -> L.compare a b

  let hash = function
    | None -> 0
    | Some x -> 1 + L.hash x
  
end

module type TRANSFER_FUNCTIONS = sig
  module Cond:CONDITION

  type 'a t

  val ar0: (module LConditionMap with type t = 'a t and type L.t = 'a and type Cond.t = Cond.t) -> interres:('a -> 'a -> 'a) ->
    Cond.t -> 'a -> 'a t -> 'a t

  val ar1:
    (module LConditionMap with type t = 'a t and type L.t = 'a and type Cond.t = Cond.t) -> joina:('a -> 'a -> 'a) -> bottoma:'a ->
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) -> interres:('res -> 'res -> 'res) ->   
    Cond.t -> ('a -> 'res) -> 'a t -> 'res t -> 'res t

  val ar2:
    (module LConditionMap with type t = 'a t and type L.t = 'a and type Cond.t = Cond.t) -> joina:('a -> 'a -> 'a) -> bottoma:'a ->
    (module LConditionMap with type t = 'b t and type L.t = 'b and type Cond.t = Cond.t) -> joinb:('b -> 'b -> 'b) -> bottomb:'b ->  
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) ->  interres:('res -> 'res -> 'res) ->   
    Cond.t -> ('a -> 'b -> 'res) -> 'a t -> 'b t -> 'res t -> 'res t

  val ar1_bwd:
    (module LConditionMap with type t = 'a t and type L.t = 'a and type Cond.t = Cond.t) -> joina:('a -> 'a -> 'a) -> bottoma:'a -> intera:('a -> 'a -> 'a) -> 
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) -> joinres:('res -> 'res -> 'res) ->  bottomres:'res -> 
    Cond.t -> ('a -> 'res -> 'a option) ->
    'a t -> 'res t -> (Cond.t * 'a t)

  val ar2_bwd:
    (module LConditionMap with type t = 'a t and type L.t = 'a and type Cond.t = Cond.t) -> joina:('a -> 'a -> 'a) -> bottoma:'a -> intera:('a -> 'a -> 'a) -> 
    (module LConditionMap with type t = 'b t and type L.t = 'b and type Cond.t = Cond.t) -> joinb:('b -> 'b -> 'b) -> bottomb:'b -> interb:('b -> 'b -> 'b) ->   
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) -> joinres:('res -> 'res -> 'res) -> bottomres:'res ->  
    Cond.t -> ('a -> 'b -> 'res -> 'a option * 'b option) ->
    'a t -> 'b t -> 'res t -> (Cond.t * 'a t) * (Cond.t * 'b t)

  val nondet_disjoint:
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) ->
    conda:Cond.t -> notconda:Cond.t -> cma:'res t ->
    condb:Cond.t -> notcondb:Cond.t -> cmb:'res t ->
    join:('res -> 'res -> 'res) -> bottom:'res -> inter:('res -> 'res -> 'res) -> 
    old:'res t  -> 'res t

  val nondet_non_disjoint:
    (module LConditionMap with type t = 'res t and type L.t = 'res and type Cond.t = Cond.t) ->
    conda:Cond.t -> cma:'res t ->
    condb:Cond.t -> cmb:'res t ->
    condaorb:Cond.t -> notcondaorb:Cond.t ->
    join:('res -> 'res -> 'res) -> bottom:'res -> inter:('res -> 'res -> 'res) ->
    old:'res t  -> 'res t

end

module ConditionMapTree(BDD:CONDITION) = struct

  type 'a node_ =
    | Interior of 'a i * 'a i
    | Leaf of 'a

  and 'a i = { key: BDD.t; node: 'a node_}

  type 'a t = 'a option i;;

  module Make(L:L)
  = struct

    (* To handle partial information, we add a top on the lattice.  *)
    module FL = FakeTop(L)
    type t = L.t option i

    module Cond = BDD
    module L = L

    
    module Orig_(L:L) = struct
      let rec pretty pp fmt {key;node} =
        Format.fprintf fmt "@[<hv>%a -> %a@]" BDD.pretty key (pretty_node pp) node

      and pretty_node pp  fmt = function
        | Leaf a -> pp fmt a
        | Interior(n1,n2) -> Format.fprintf fmt "@[<v>%a@\n%a@]" (pretty pp) n1 (pretty pp) n2
      ;;

      let create top = {key=BDD.all; node=Leaf top}

      let rec find ~join ~bottom = fun {key;node} cond ->
        if BDD.disjoint key cond then bottom
        else
          match node with
          | Leaf(v) -> v
          | Interior(c1,c2) -> join (find ~join ~bottom c1 cond) (find ~join ~bottom c2 cond)
      ;;

      let refine ~inter =
        let rec refine_whole {key;node} value =
          let newnode = 
            match node with
            | Leaf(v) -> Leaf(inter v value)
            | Interior(n1,n2) -> Interior(refine_whole n1 value,refine_whole n2 value)
          in {key;node=newnode}
        in

        let rec f ({key;node} as obj) ~cond ?(notcond=(BDD.complement cond)) value = 
          let binter = BDD.inter cond key in
          if BDD.is_empty binter then obj
          else if BDD.equal binter key
          then refine_whole obj value
          else
            let newnode = match node with
              | Leaf v ->
                let n1 = {key = binter; node = Leaf(inter v value)} in
                let n2 = {key = BDD.inter key notcond; node = Leaf(v)} in
                Interior(n1,n2)
              | Interior(n1,n2) -> Interior(f n1 ~cond ~notcond value, f n2 ~cond ~notcond value)
            in {key;node=newnode}
        in f
      ;;
    end

    (**************** Handle partial. ****************)

    module Orig = Orig_(FL);;
    
    let pretty pp fmt x = Orig.pretty (FL.pretty pp) fmt x;;
    
    let create_partial = Orig.create None
    
    let find ~join ~bottom n c =
      match Orig.find ~join:(FL.join ~join) ~bottom:(FL.bottom ~bottom) n c with
      | None -> assert false
      | Some x -> x
    ;;
    
    let refine ~inter  n ~cond ?notcond v =
      Orig.refine ~inter:(FL.inter ~inter) n ~cond ?notcond (Some v)
    ;;

  end

end

(* Similar to ConditionMap below, but do not regroup partitions based
   on their values. In some case this leads to a huge number of
   cases. Merging partitions using lists is also not very
   efficient. So this structure should be used only for testing
   purpose, or expereminting changes in the interface. *)
module ConditionMapPartitionList(BDD:CONDITION) = struct

  type 'a i = ('a * BDD.t) list
  (* type 'a i = Cons of ('a * BDD.t * 'a i option) *)

  type 'a t = 'a option i

  module Make(L:L)
  = struct

    (* To handle partial information, we add a top on the lattice.  *)
    module FL = FakeTop(L)
    type t = L.t option i

    module Cond = BDD
    module L = L

    
    module Orig_(L:L) = struct
      let pretty fmt = assert false

      let create top = [(top,BDD.all)]

      let find ~join ~bottom n cond =
        List.fold_left (fun acc (v,c) ->
            if BDD.disjoint c cond then acc
            else FL.join ~join acc v) (FL.bottom ~bottom) n
      ;;

      (* One advantage of the alist is that we do not need to have
         that two different partitions have different values. This
         could allow to avoid computing a or if the BDD operation is
         too costly (e.g. CUDD allows to limit the time spent doing
         some operations). *)
      let rec remove_assoc x (accbdd,acclist) = function
        | [] -> (accbdd,acclist)
        | (v,c)::rest when L.equal x v -> remove_assoc x (BDD.union accbdd c,acclist) rest
        | a::rest -> remove_assoc x (accbdd,a::acclist) rest
      ;;
      
      let rec merge_partitions = function
        | [] -> []
        | (v1,c1)::rest ->
          let rest = merge_partitions rest in
          let (newc,rest) = remove_assoc v1 (c1,[]) rest in
          (v1,newc)::rest
      ;;

      (* We could join partitions with same BDD together. But it is also OK not to do it. *)
      let refine ~inter  n ~cond ?(notcond = BDD.complement cond) newv =
        let rec loop = function
          | [] -> []
          | (v,c)::rest ->
            let c1 = BDD.inter cond c in
            let res = loop rest in
            if BDD.is_empty c1 then (v,c)::res
            else if BDD.equal c1 c then (inter newv v,c)::res
            else
              let c2 = BDD.inter notcond c in
              (v,c2)::(inter newv v,c1)::res
        in
        let res = loop n in
        let length = List.length res in
        (* This is optional. *)
        let newres = merge_partitions res in
        let newlength = List.length newres in
        if length > 10 then
          Codex_log.warning "I have a list of length %d newlength %d" length newlength;
        newres
      ;;

    end

    (**************** Handle partial. ****************)

    (* Same as above *)
    (* module Orig = Orig_(FL);;
     * 
     * let pretty fmt x = Orig.pretty fmt x;;
     * let create _ = assert false
     * 
     * let create_partial = Orig.create None
     * 
     * let find  n c =
     *   match Orig.find n c with
     *   | None -> assert false
     *   | Some x -> x
     * ;;
     * 
     * let refine  n ~cond ?notcond v =
     *   Orig.refine n ~cond ?notcond (Some v)
     * ;; *)
  end
end

(* Old version of ConditionMap where we use a fake top on the lattice. *)
module ConditionMapPartitionOld(BDD:CONDITION) = struct

  module MakeReal(L:L):LConditionMapFold
    with module L = L
     and module Cond = BDD
  = struct

    (* To handle partial information, we add a top on the lattice.  *)
    module FL = FakeTop(L)
    (* type 'a tmp = 'a t *)

    module Cond = BDD
    module L = L

    module Map = Map.Make(FL)
    type t = BDD.t Map.t

    module Orig = struct
      let pretty pp fmt x =
        let l = Map.bindings x in
        let print_node fmt (v,c) = Format.fprintf fmt "%a -> %a" (FL.pretty pp) v BDD.pretty c in
        (Format.pp_print_list print_node) fmt l


      (* let create top = [(top,BDD.all)] *)

      let find ~join ~bottom n cond =
        (* Codex_log.feedback "find: %a %a" (pretty pp) n BDD.pretty cond; *)
        Map.fold (fun v c acc ->
            if BDD.disjoint c cond then acc
            else FL.join ~join acc v) n (FL.bottom ~bottom)
      ;;

      let refine ~inter n ~cond ?(notcond = BDD.complement cond) newv =
        let add v c map =
          match Map.find v map with
          | exception Not_found -> Map.add v c map
          | c2 -> Map.add v (BDD.union c c2) map
        in

        let f v c acc = 
          let c1 = BDD.inter cond c in
          if BDD.is_empty c1 then (add v c acc) 
          else if BDD.equal c1 c then
            (add (FL.inter ~inter newv v) c acc)
          else
            let c2 = BDD.inter notcond c in
            let acc = add (FL.inter ~inter newv v) c1 acc in
            let acc = add v c2 acc in
            acc
        in
        Map.fold f n Map.empty
      ;;

    end
    
    (* Same as above *)

    let pretty pp fmt x = Orig.pretty pp fmt x;;
    let create _ = assert false

    let create_partial = Map.singleton None BDD.all

    let find ~join ~bottom  n c =
      match Orig.find ~join ~bottom n c with
      | None -> assert false
      | Some x -> x
    ;;
    
    let refine ~inter  n ~cond ?notcond v =
      (* Kernel.feedback "refine %a %a with %a" L.pretty v pretty n BDD.pretty cond; *)
      let res = Orig.refine ~inter n ~cond ?notcond (Some v) in
      (* Kernel.feedback "refine %a %a with %a res %a" L.pretty v
         pretty n BDD.pretty cond pretty res; *)
      res
    ;;

    let fold_with_cond x cond acc g  =
      let f v c acc = match v with
        | None -> if not (BDD.disjoint cond c) then raise Never_refined else acc
        | Some v ->
          let inter = BDD.inter cond c in
          if BDD.is_empty inter then acc
          else g v inter(* inter *) acc
      in
      Map.fold f x acc
    ;;
    
  end

  (* I think that this is still safe to use, but the implementation is
     using Obj. *)
  module Unsafe = struct

    type 'a t = Obj.t

    module Make(L:L) = struct

      module CM = MakeReal(L)
      module Cond = BDD
      module L = L

      type t = Obj.t

      let pretty pp fmt x = CM.pretty pp fmt (Obj.obj x)
      let refine ~inter x ~cond ?notcond v =
        Obj.repr @@
        CM.refine ~inter (Obj.obj x) ~cond ?notcond v
      let find ~join ~bottom x cond = CM.find ~join ~bottom (Obj.obj x) cond
      let create_partial = Obj.repr @@ CM.create_partial
      let fold_with_cond = Obj.magic CM.fold_with_cond
      
    end

  end

  (* This is less performant, because we allocate an extra record, and
     we need to retrieve the function pointer from an element in the
     record (virtual call). *)
  module Safe = struct

    module type T = sig
      type key
      module CM:LConditionMapFold with type L.t = key and module Cond = BDD
      val value: CM.t
    end

    type 'a t = (module T with type key = 'a)

    module Make(L:L) = struct

      module CM = MakeReal(L)
      module Cond = BDD
      module L = L

      type 'a tmp = 'a t
      type t = L.t tmp

      let pretty pp fmt (module T:T with type key = L.t) = T.CM.pretty pp fmt T.value
      let refine ~inter (module T:T with type key = L.t) ~cond ?notcond v =
        let v = T.CM.refine ~inter T.value ~cond ?notcond v in
        let module Res = struct
          type key = T.key
          module CM = T.CM
          let value = v
        end in
        (module Res:T with type key = L.t)
      ;;

      let find ~join ~bottom (module T:T with type key = L.t) cond  =
        T.CM.find ~join ~bottom T.value cond
      ;;

      let fold_with_cond (module T:T with type key = L.t) cond acc f  =
        T.CM.fold_with_cond T.value cond acc f
      ;;

      
      let create_partial =
        let module Res = struct
          type key = L.t
          module CM = CM
          let value = CM.create_partial
        end in
        (module Res:T with type key = L.t)

    end
  end

  (* include Safe *)
  include Unsafe

end

module ConditionMapPartition(BDD:CONDITION) = struct

  module MakeReal(L:L):LConditionMapFold
    with module L = L
     and module Cond = BDD
  = struct

    module Cond = BDD
    module L = L

    module Orig = struct

      (* module Map = Map.Make(L) *)                 
      module Map = Smallmap.Make(L)
      type t = BDD.t Map.t

      (* Can be used only for explicit. *)
      let map_fold_explicit f map init = Map.fold f map init, BDD.empty

      (* For implicit: also return the union of the keys, which is the
         conditions for which the map is defined. *)
      let map_fold_implicit f map init =
        let f' key value (acc,bdds) =
          let acc = f key value acc in
          let bdds = BDD.union bdds value in
          acc,bdds
        in Map.fold f' map (init,BDD.empty)
      ;;

      let map_fold = map_fold_explicit
      
             
      let pretty pp fmt x =
        let l = Map.bindings x in
        let print_node fmt (v,c) = Format.fprintf fmt "%a -> %a" pp v BDD.pretty c in
        (Format.pp_print_list print_node) fmt l


      (* let create top = [(top,BDD.all)] *)

      let find ~join ~bottom n cond =
        (* Codex_log.feedback "find: %a" (\* (pretty pp) n *\)  BDD.pretty cond; *)
        map_fold (fun v c acc ->
            if BDD.disjoint c cond then acc
            else join acc v) n bottom
      ;;

      let add v c map =
        match Map.find v map with
        | exception Not_found -> Map.add v c map
        | c2 -> Map.add v (BDD.union c c2) map
      ;;

      let refine ~inter n ~cond ?(notcond = BDD.complement cond) newv =
        let f v c acc = 
          let c1 = BDD.inter cond c in
          if BDD.is_empty c1 then (add v c acc) 
          else if BDD.equal c1 c then
            (add (inter newv v) c acc)
          else
            let c2 = BDD.inter notcond c in
            let acc = add (inter newv v) c1 acc in
            let acc = add v c2 acc in
            acc
        in
        map_fold f n Map.empty
      ;;

    end


    (* Add the special case where the BDD is undefined on top. *)
    module Explicit(Arg:sig end) = struct

      assert(Orig.map_fold == Orig.map_fold_explicit);
      
      (* Explicit the condition for which the BDD is not defined;
         remove the need for the fake top. *)
      type t =
        { orig:Orig.t;
          undefined: BDD.t}

      let pretty pp fmt x = Orig.pretty pp fmt x.orig;;
      let create _ = assert false

      let create_partial =
        { orig = Orig.Map.empty; undefined = BDD.all }

      let find ~join ~bottom n c =
        if(not @@ BDD.disjoint n.undefined c)
        then raise Never_refined;
        fst @@ Orig.find ~join ~bottom n.orig c 


      let refine ~inter n ~cond ?(notcond = BDD.complement cond) v =
        (* Codex_log.feedback "refining condition %a oldundef %a" BDD.pretty cond BDD.pretty n.undefined; *)
        let newundefined = BDD.inter n.undefined notcond in
        if BDD.equal newundefined n.undefined
        then
          (* Refine already existing values. *)
          { orig = fst @@ Orig.refine ~inter n.orig ~cond ~notcond v;
            undefined = newundefined;
          }
        else
          (* We define new values whose values we previously not knew; 
             we make as if these old values were "top". *)
          let newlydefined  = BDD.inter cond n.undefined in
          assert(not @@ BDD.is_empty newlydefined);
          let orig = n.orig in
          let orig = fst @@ Orig.refine ~inter orig ~cond ~notcond v in          
          let orig = Orig.add v newlydefined orig in
          { orig; undefined = newundefined }

      let fold_with_cond x cond acc g = assert false
    (* let fold_with_cond x cond acc g  =
     *   let f v c acc = match v with
     *     | None -> if not (BDD.disjoint cond c) then raise Never_refined else acc
     *     | Some v ->
     *       let inter = BDD.inter cond c in
     *       if BDD.is_empty inter then acc
     *       else g v inter(\* inter *\) acc
     *   in
     *   Map.fold f x acc
     * ;; *)
      
    end


    module Implicit(Arg:sig end) = struct

      assert(Orig.map_fold == Orig.map_fold_implicit);
      
      type t = Orig.t
      let pretty pp fmt x = Orig.pretty pp fmt x;;

      let create_partial = Orig.Map.empty;;

      let find ~join ~bottom orig c =
        let res,defined = Orig.find ~join ~bottom orig c in
        if(not @@ BDD.is_included c defined)
        then raise Never_refined
        else res

      let refine ~inter n ~cond ?(notcond = BDD.complement cond) v =
        let orig,defined = Orig.refine ~inter n ~cond ~notcond v in
        if BDD.is_included cond defined
        (* Refine already existing values only. *)         
        then orig
        else
          (* We define new values whose values we previously not knew; 
             we make as if these old values were "top". *)
          let newlydefined  = BDD.inter cond @@ BDD.complement defined in
          assert(not @@ BDD.is_empty newlydefined);
          let orig = Orig.add v newlydefined orig in
          orig
      let fold_with_cond x cond acc g = assert false
                                      
    end

    (* Explicit requires more storage, but implicit requires
       additional BDD computation; so which is faster or more
       efficient depends on the benchmark. Maybe a good solution
       would be to store the value in the smallmap. *)
    include Explicit(struct end)
    (* include Implicit(struct end) *)          

          
  end

  (* I think that this is still safe to use, but the implementation is
     using Obj. *)
  module Unsafe = struct

    type 'a t = Obj.t

    module Make(L:L) = struct

      module CM = MakeReal(L)
      module Cond = BDD
      module L = L

      type t = Obj.t

      let pretty pp fmt x = CM.pretty pp fmt (Obj.obj x)
      let refine ~inter x ~cond ?notcond v =
        Obj.repr @@
        CM.refine ~inter (Obj.obj x) ~cond ?notcond v
      let find ~join ~bottom x cond = CM.find ~join ~bottom (Obj.obj x) cond
      let create_partial = Obj.repr @@ CM.create_partial
      let fold_with_cond = Obj.magic CM.fold_with_cond
      
    end

  end

  (* This is less performant, because we allocate an extra record, and
     we need to retrieve the function pointer from an element in the
     record (virtual call). *)
  module Safe = struct

    module type T = sig
      type key
      module CM:LConditionMapFold with type L.t = key and module Cond = BDD
      val value: CM.t
    end

    type 'a t = (module T with type key = 'a)

    module Make(L:L) = struct

      module CM = MakeReal(L)
      module Cond = BDD
      module L = L

      type 'a tmp = 'a t
      type t = L.t tmp

      let pretty pp fmt (module T:T with type key = L.t) = T.CM.pretty pp fmt T.value
      let refine ~inter (module T:T with type key = L.t) ~cond ?notcond v =
        let v = T.CM.refine ~inter T.value ~cond ?notcond v in
        let module Res = struct
          type key = T.key
          module CM = T.CM
          let value = v
        end in
        (module Res:T with type key = L.t)
      ;;

      let find ~join ~bottom (module T:T with type key = L.t) cond  =
        T.CM.find ~join ~bottom T.value cond
      ;;

      let fold_with_cond (module T:T with type key = L.t) cond acc f  =
        T.CM.fold_with_cond T.value cond acc f
      ;;

      
      let create_partial =
        let module Res = struct
          type key = L.t
          module CM = CM
          let value = CM.create_partial
        end in
        (module Res:T with type key = L.t)

    end
  end

  (* include Safe *)
  include Unsafe

end

(* Generic implementation. *)
module MakePathInsensitive
    (BDD:CONDITION)
    (M:sig type 'a t end)
  :TRANSFER_FUNCTIONS with module Cond = BDD and type 'a t = 'a M.t                           
= struct
  module Cond =  BDD

  type 'a t = 'a M.t

  let ar0 (type res) (module L:LConditionMap with type t = res M.t and type L.t = res and type Cond.t = Cond.t) ~interres
      cond value old =
    let notcond = BDD.complement cond in
    L.refine ~inter:interres old ~cond ~notcond value

  let ar1
      (type a) (type ma) (module La:LConditionMap with type t = ma and type L.t = a and type Cond.t = Cond.t) ~joina ~bottoma
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t) ~interres
      cond f a old =
    let av = La.find ~join:joina ~bottom:bottoma a cond in
    let notcond = BDD.complement cond in
    Lres.refine ~inter:interres old ~cond ~notcond (f av)
  ;;

  let ar2
      (type a) (type ma) (module La:LConditionMap with type t = ma and type L.t = a and type Cond.t = Cond.t) ~joina ~bottoma
      (type b) (type mb) (module Lb:LConditionMap with type t = mb and type L.t = b and type Cond.t = Cond.t) ~joinb ~bottomb        
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t) ~interres
      cond f a b old =
    let av = La.find ~join:joina ~bottom:bottoma a cond in
    let bv = Lb.find ~join:joinb ~bottom:bottomb b cond in      
    let notcond = BDD.complement cond in
    Lres.refine ~inter:interres old ~cond ~notcond (f av bv)
  ;;

  let changed (refine:'a -> cond:BDD.t -> ?notcond:BDD.t -> 'b -> 'a) cond r = function
    | None -> BDD.empty, r
    | Some x -> cond, refine r ~cond x
  ;;

  let ar1_bwd
      (type a) (type ma) (module La:LConditionMap with type t = ma and type L.t = a and type Cond.t = Cond.t) ~joina ~bottoma ~intera
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t) ~joinres ~bottomres
      cond f a res =
    let va = La.find ~join:joina ~bottom:bottoma a cond in
    let v = Lres.find ~join:joinres ~bottom:bottomres res cond in
    let newva = f va v in
    changed (La.refine ~inter:intera) cond a newva
  ;;    


  let ar2_bwd
      (type a) (type ma) (module La:LConditionMap with type t = ma and type L.t = a and type Cond.t = Cond.t) ~joina ~bottoma ~intera
      (type b) (type mb) (module Lb:LConditionMap with type t = mb and type L.t = b and type Cond.t = Cond.t) ~joinb ~bottomb ~interb      
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t) ~joinres ~bottomres
      cond f a b res =
    let va = La.find ~join:joina ~bottom:bottoma a cond in
    let vb = Lb.find ~join:joinb ~bottom:bottomb b cond in
    let v = Lres.find ~join:joinres ~bottom:bottomres res cond in
    let newva, newvb = f va vb v in
    (changed (La.refine ~inter:intera) cond a newva,
     changed (Lb.refine ~inter:interb) cond b newvb)
  ;;    

  (* Note: a simple implementation of path-sensitive analysis would
     get a list of (condition,value) leaves; and refine with the
     cartesian product. This would work, but there would be a
     quadratic, instead of linear behaviour when the elements have the
     same partitionning. *)

  let nondet_disjoint
      (type res) (type mres) (module L:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t)
      ~conda ~notconda ~cma ~condb ~notcondb ~cmb ~join ~bottom ~inter ~old =
    let av = L.find ~join ~bottom cma conda in
    let bv = L.find ~join ~bottom cmb condb in
    let res = L.refine ~inter old av ~cond:conda ~notcond:notconda in
    let res = L.refine ~inter res bv ~cond:condb ~notcond:notcondb in
    res
  ;;

  let nondet_non_disjoint
      (type res) (type mres) (module L:LConditionMap with type t = mres and type L.t = res and type Cond.t = Cond.t)
      ~conda ~cma ~condb ~cmb ~condaorb ~notcondaorb ~join ~bottom ~inter ~old =
    let av = L.find ~join ~bottom cma conda in
    let bv = L.find ~join ~bottom cmb condb in
    let abv = join av bv in
    let res = L.refine ~inter old abv ~cond:condaorb ~notcond:notcondaorb in
    res
  ;;
  
end


module MakePathSensitive
    (BDD:CONDITION)
    (M:sig type 'a t end)
  (* :TRANSFER_FUNCTIONS with module Cond = BDD and type 'a t = 'a M.t                            *)
= struct
  module Cond =  BDD

  type 'a t = 'a M.t
  
  let ar0
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = BDD.t)
      cond value old =
    Lres.refine old ~cond value
  ;;

  
  let ar1
      (type a) (type ma) (module La:LConditionMapFold with type t = ma and type L.t = a and type Cond.t = BDD.t)
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = BDD.t) ~inter
      cond f a old =
    La.fold_with_cond a cond old (fun v c acc ->
        let v = f v in
        Lres.refine ~inter acc ~cond:c v)
  ;;

  let ar2
      (type a) (type ma) (module La:LConditionMapFold with type t = ma and type L.t = a and type Cond.t = BDD.t)
      (type b) (type mb) (module Lb:LConditionMapFold with type t = mb and type L.t = b and type Cond.t = BDD.t)
      (type res) (type mres) (module Lres:LConditionMap with type t = mres and type L.t = res and type Cond.t = BDD.t) ~inter
      cond f a b old =
    La.fold_with_cond a cond old (fun va ca acc ->
        Lb.fold_with_cond b ca acc (fun vb cab acc ->
            Lres.refine ~inter acc ~cond:cab (f va vb)))
  ;;

  let ar1_bwd
      (type a) (type ma) (module La:LConditionMapFold with type t = ma and type L.t = a and type Cond.t = BDD.t) ~intera
      (type res) (type mres) (module Lres:LConditionMapFold with type t = mres and type L.t = res and type Cond.t = BDD.t)
      cond f a res =
    Lres.fold_with_cond res cond (BDD.empty,a) (fun vres cres ((acccond,acca) as acc) ->
        La.fold_with_cond a cres acc (fun va cares (acccond,acca) ->
            match f va vres with
              | None -> (acccond, acca)
              | Some v -> (BDD.union cares acccond, La.refine ~inter:intera acca ~cond:cares v)))
  ;;

  let ar2_bwd 
      (type a) (type ma) (module La:LConditionMapFold with type t = ma and type L.t = a and type Cond.t = BDD.t) ~intera
      (type b) (type mb) (module Lb:LConditionMapFold with type t = mb and type L.t = b and type Cond.t = BDD.t) ~interb
      (type res) (type mres) (module Lres:LConditionMapFold with type t = mres and type L.t = res and type Cond.t = BDD.t)
      cond f a b res =
    Lres.fold_with_cond res cond ((BDD.empty,a),(BDD.empty,b)) (fun vres cres acc ->
        La.fold_with_cond a cres acc (fun va cares acc ->
            Lb.fold_with_cond b cares acc (fun vb cabres ((accca,acca),(acccb,accb)) ->
                let newa,newb = f va vb vres in
                let (accca,acca) = match newa with
                |  None -> accca, acca
                |  Some v -> (BDD.union cabres accca, La.refine ~inter:intera acca ~cond:cabres v)
                in
                let (acccb,accb) = match newb with
                |  None -> acccb, accb
                |  Some v -> (BDD.union cabres acccb, Lb.refine ~inter:interb accb ~cond:cabres v)
                in
                ((accca,acca),(acccb,accb)))))
  
  ;;

  let nondet_disjoint
      (type res) (type mres) (module L:LConditionMapFold with type t = mres and type L.t = res and type Cond.t = Cond.t)
      ~conda ~notconda ~cma ~condb ~notcondb ~cmb ~inter ~old =
    let res = old in
    let res = L.fold_with_cond cma conda res (fun v c acc ->
        L.refine ~inter acc ~cond:c(* (BDD.inter c conda) *) v) in
    let res = L.fold_with_cond cmb condb res (fun v c acc ->
        L.refine ~inter acc ~cond:c(* (BDD.inter c condb) *) v) in
    res
  ;;

  let nondet_non_disjoint
      (type res) (type mres) (module L:LConditionMapFold with type t = mres and type L.t = res and type Cond.t = Cond.t)
      ~conda ~cma ~condb ~cmb ~condaorb ~notcondaorb ~inter ~old =
    (* Special case. *)
    if BDD.equal conda condb
    then ar2 (module L) (module L) (module L) conda (assert false) ~inter cma cmb old
    else
      (* MAYBE: pass these as an argument. *)
      let a_and_b = BDD.inter conda condb in
      let a_minus_b = BDD.inter conda @@ BDD.complement condb in
      let b_minus_a = BDD.inter condb @@ BDD.complement conda in
      (* Add the common values. *)
      let acc =
        ar2 (module L) (module L) (module L) a_and_b (* L.L.join *)(assert false) ~inter cma cmb old in
      (* Add the values in a only *)
      let acc =
        L.fold_with_cond cma a_minus_b acc (fun v c acc ->
            L.refine ~inter acc ~cond:c v) in
      (* Add the values in b only. *)
      let acc =
        L.fold_with_cond cmb b_minus_a acc (fun v c acc ->
            L.refine ~inter acc ~cond:c v) in
      acc

end