Debian Codegraph @

kernel/conv_oracle.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Bruno Barras as part of the rewriting of the conversion
12 algorithm, Nov 2001 *)
13
14 open Names
15
16 (* Priority for the expansion of constant in the conversion test.
17 * Higher levels means that the expansion is less prioritary.
18 * (And Expand stands for -oo, and Opaque +oo.)
19 * The default value is [Level 100].
20 *)
21 type level = Expand | Level of int | Opaque
22 let pr_level = function
23 | Expand -> Pp.str "expand"
24 | Level 0 -> Pp.str "transparent"
25 | Level n -> Pp.int n
26 | Opaque -> Pp.str "opaque"
27
28 let default = Level 0
29 let is_default = function
30 | Level 0 -> true
31 | _ -> false
32 let transparent = default
33 let is_transparent = function
34 | Level 0 -> true
35 | _ -> false
36
37 type oracle = {
38 var_opacity : level Id.Map.t;
39 cst_opacity : level Cmap.t;
40 prj_opacity : level PRmap.t;
41 var_trstate : Id.Pred.t;
42 cst_trstate : Cpred.t;
43 prj_trstate : PRpred.t;
44 }
45
46 let empty = {
47 var_opacity = Id.Map.empty;
48 cst_opacity = Cmap.empty;
49 prj_opacity = PRmap.empty;
50 var_trstate = Id.Pred.full;
51 cst_trstate = Cpred.full;
52 prj_trstate = PRpred.full;
53 }
54
55 type evaluable =
56 | EvalVarRef of Id.t
57 | EvalConstRef of Constant.t
58 | EvalProjectionRef of Projection.Repr.t
59
60 let get_strategy { var_opacity; cst_opacity; prj_opacity; _ } = function
61 | EvalVarRef id ->
62 (try Id.Map.find id var_opacity
63 with Not_found -> default)
64 | EvalConstRef c ->
65 (try Cmap.find c cst_opacity
66 with Not_found -> default)
67 | EvalProjectionRef p ->
68 (try PRmap.find p prj_opacity
69 with Not_found -> default)
70
71 let set_strategy ({var_opacity; cst_opacity; prj_opacity; _} as oracle) k l =
72 match k with
73 | EvalVarRef id ->
74 let var_opacity =
75 if is_default l then Id.Map.remove id var_opacity
76 else Id.Map.add id l var_opacity
77 in
78 let var_trstate = match l with
79 | Opaque -> Id.Pred.remove id oracle.var_trstate
80 | _ -> Id.Pred.add id oracle.var_trstate
81 in
82 { oracle with var_opacity; var_trstate; }
83 | EvalConstRef c ->
84 let cst_opacity =
85 if is_default l then Cmap.remove c cst_opacity
86 else Cmap.add c l cst_opacity
87 in
88 let cst_trstate = match l with
89 | Opaque -> Cpred.remove c oracle.cst_trstate
90 | _ -> Cpred.add c oracle.cst_trstate
91 in
92 { oracle with cst_opacity; cst_trstate; }
93 | EvalProjectionRef p ->
94 let prj_opacity =
95 if is_default l then PRmap.remove p prj_opacity
96 else PRmap.add p l prj_opacity
97 in
98 let prj_trstate = match l with
99 | Opaque -> PRpred.remove p oracle.prj_trstate
100 | _ -> PRpred.add p oracle.prj_trstate
101 in
102 {oracle with prj_opacity; prj_trstate}
103
104 let fold_strategy f {var_opacity; cst_opacity; prj_opacity; _} accu =
105 let fvar id lvl accu = f (EvalVarRef id) lvl accu in
106 let fcst cst lvl accu = f (EvalConstRef cst) lvl accu in
107 let fprj p lvl accu = f (EvalProjectionRef p) lvl accu in
108 let accu = Id.Map.fold fvar var_opacity accu in
109 let accu = Cmap.fold fcst cst_opacity accu in
110 PRmap.fold fprj prj_opacity accu
111
112 let get_transp_state { var_trstate; cst_trstate; prj_trstate; _ } =
113 let open TransparentState in
114 { tr_var = var_trstate; tr_cst = cst_trstate ; tr_prj = prj_trstate }
115
116 let dep_order l2r k1 k2 =
117 match k1, k2 with
118 | None, None -> l2r
119 | None, _ -> true
120 | Some _, None -> false
121 | Some k1, Some k2 ->
122 match k1, k2 with
123 | EvalVarRef _, EvalVarRef _ -> l2r
124 | EvalVarRef _, (EvalConstRef _ | EvalProjectionRef _) -> true
125 | EvalConstRef _, EvalVarRef _ -> false
126 | EvalConstRef _, EvalProjectionRef _ -> l2r
127 | EvalConstRef _, EvalConstRef _ -> l2r
128 | EvalProjectionRef _, EvalVarRef _ -> false
129 | EvalProjectionRef _, EvalConstRef _ -> l2r
130 | EvalProjectionRef _, EvalProjectionRef _ -> l2r
131
132 (* Unfold the first constant only if it is "more transparent" than the
133 second one. In case of tie, use the recommended default. *)
134 let oracle_order o l2r k1 k2 =
135 let s1 = match k1 with None -> Expand | Some k1 -> get_strategy o k1 in
136 let s2 = match k2 with None -> Expand | Some k2 -> get_strategy o k2 in
137 match s1, s2 with
138 | Expand, Expand -> dep_order l2r k1 k2
139 | Expand, (Opaque | Level _) -> true
140 | (Opaque | Level _), Expand -> false
141 | Opaque, Opaque -> dep_order l2r k1 k2
142 | Level _, Opaque -> true
143 | Opaque, Level _ -> false
144 | Level n1, Level n2 ->
145 if Int.equal n1 n2 then dep_order l2r k1 k2
146 else n1 < n2