Debian Codegraph @

plugins/btauto/refl_btauto.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 open Constr
12
13 let bt_lib_constr n = lazy (UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref n)
14
15 let decomp_term sigma (c : Constr.t) =
16 Constr.kind (EConstr.Unsafe.to_constr (Termops.strip_outer_cast sigma (EConstr.of_constr c)))
17
18 let lapp c v = Constr.mkApp (Lazy.force c, v)
19
20 let (===) = Constr.equal
21
22
23 module RocqList = struct
24 let _nil = bt_lib_constr "core.list.nil"
25 let _cons = bt_lib_constr "core.list.cons"
26
27 let cons ty h t = lapp _cons [|ty; h ; t|]
28 let nil ty = lapp _nil [|ty|]
29 let rec of_list ty = function
30 | [] -> nil ty
31 | t::q -> cons ty t (of_list ty q)
32
33 end
34
35 module RocqPositive = struct
36 let _xH = bt_lib_constr "num.pos.xH"
37 let _xO = bt_lib_constr "num.pos.xO"
38 let _xI = bt_lib_constr "num.pos.xI"
39
40 (* A Rocq nat from an int *)
41 let rec of_int n =
42 if n <= 1 then Lazy.force _xH
43 else
44 let ans = of_int (n / 2) in
45 if n mod 2 = 0 then lapp _xO [|ans|]
46 else lapp _xI [|ans|]
47
48 end
49
50 module Env = struct
51
52 module ConstrHashtbl = Hashtbl.Make (Constr)
53
54 type t = (int ConstrHashtbl.t * int ref)
55
56 let add (tbl, off) (t : Constr.t) =
57 try ConstrHashtbl.find tbl t
58 with
59 | Not_found ->
60 let i = !off in
61 let () = ConstrHashtbl.add tbl t i in
62 let () = incr off in
63 i
64
65 let empty () = (ConstrHashtbl.create 16, ref 1)
66
67 let to_list (env, _) =
68 (* we need to get an ordered list *)
69 let fold constr key accu = (key, constr) :: accu in
70 let l = ConstrHashtbl.fold fold env [] in
71 let sorted_l = List.sort (fun p1 p2 -> Int.compare (fst p1) (fst p2)) l in
72 List.map snd sorted_l
73
74 end
75
76 module Bool = struct
77
78 let ind = lazy (Globnames.destIndRef (Rocqlib.lib_ref "core.bool.type"))
79 let typ = bt_lib_constr "core.bool.type"
80 let trueb = bt_lib_constr "core.bool.true"
81 let falseb = bt_lib_constr "core.bool.false"
82 let andb = bt_lib_constr "core.bool.andb"
83 let orb = bt_lib_constr "core.bool.orb"
84 let xorb = bt_lib_constr "core.bool.xorb"
85 let negb = bt_lib_constr "core.bool.negb"
86
87 type t =
88 | Var of int
89 | Const of bool
90 | Andb of t * t
91 | Orb of t * t
92 | Xorb of t * t
93 | Negb of t
94 | Ifb of t * t * t
95
96 let quote (env : Env.t) genv sigma (c : Constr.t) : t =
97 let trueb = Lazy.force trueb in
98 let falseb = Lazy.force falseb in
99 let andb = Lazy.force andb in
100 let orb = Lazy.force orb in
101 let xorb = Lazy.force xorb in
102 let negb = Lazy.force negb in
103
104 let rec aux c = match decomp_term sigma c with
105 | App (head, args) ->
106 if head === andb && Array.length args = 2 then
107 Andb (aux args.(0), aux args.(1))
108 else if head === orb && Array.length args = 2 then
109 Orb (aux args.(0), aux args.(1))
110 else if head === xorb && Array.length args = 2 then
111 Xorb (aux args.(0), aux args.(1))
112 else if head === negb && Array.length args = 1 then
113 Negb (aux args.(0))
114 else Var (Env.add env c)
115 | Case (info, _, _, _, _, arg, pats) ->
116 let is_bool =
117 let i = info.ci_ind in
118 Environ.QInd.equal genv i (Lazy.force ind)
119 in
120 if is_bool then
121 Ifb ((aux arg), (aux (snd pats.(0))), (aux (snd pats.(1))))
122 else
123 Var (Env.add env c)
124 | _ ->
125 if c === falseb then Const false
126 else if c === trueb then Const true
127 else Var (Env.add env c)
128 in
129 aux c
130
131 end
132
133 module Btauto = struct
134
135 open Pp
136
137 let eq = bt_lib_constr "core.eq.type"
138
139 let f_var = bt_lib_constr "plugins.btauto.f_var"
140 let f_btm = bt_lib_constr "plugins.btauto.f_btm"
141 let f_top = bt_lib_constr "plugins.btauto.f_top"
142 let f_cnj = bt_lib_constr "plugins.btauto.f_cnj"
143 let f_dsj = bt_lib_constr "plugins.btauto.f_dsj"
144 let f_neg = bt_lib_constr "plugins.btauto.f_neg"
145 let f_xor = bt_lib_constr "plugins.btauto.f_xor"
146 let f_ifb = bt_lib_constr "plugins.btauto.f_ifb"
147
148 let eval = bt_lib_constr "plugins.btauto.eval"
149 let witness = bt_lib_constr "plugins.btauto.witness"
150 let soundness = bt_lib_constr "plugins.btauto.soundness"
151
152 let rec convert = function
153 | Bool.Var n -> lapp f_var [|RocqPositive.of_int n|]
154 | Bool.Const true -> Lazy.force f_top
155 | Bool.Const false -> Lazy.force f_btm
156 | Bool.Andb (b1, b2) -> lapp f_cnj [|convert b1; convert b2|]
157 | Bool.Orb (b1, b2) -> lapp f_dsj [|convert b1; convert b2|]
158 | Bool.Negb b -> lapp f_neg [|convert b|]
159 | Bool.Xorb (b1, b2) -> lapp f_xor [|convert b1; convert b2|]
160 | Bool.Ifb (b1, b2, b3) -> lapp f_ifb [|convert b1; convert b2; convert b3|]
161
162 let convert_env env : Constr.t =
163 RocqList.of_list (Lazy.force Bool.typ) env
164
165 let reify env t = lapp eval [|convert_env env; convert t|]
166
167 let print_counterexample env sigma p penv =
168 let var = lapp witness [|p|] in
169 let var = EConstr.of_constr var in
170 (* Compute an assignment that dissatisfies the goal *)
171 let redfun, _ = Redexpr.reduction_of_red_expr env Genredexpr.(CbvVm None) in
172 let _, var = redfun env sigma var in
173 let var = EConstr.Unsafe.to_constr var in
174 let rec to_list l = match decomp_term sigma l with
175 | App (c, _)
176 when c === (Lazy.force RocqList._nil) -> []
177 | App (c, [|_; h; t|])
178 when c === (Lazy.force RocqList._cons) ->
179 if h === (Lazy.force Bool.trueb) then (true :: to_list t)
180 else if h === (Lazy.force Bool.falseb) then (false :: to_list t)
181 else invalid_arg "to_list"
182 | _ -> invalid_arg "to_list"
183 in
184 let concat sep = function
185 | [] -> mt ()
186 | h :: t ->
187 let rec aux = function
188 | [] -> mt ()
189 | x :: t -> (sep ++ x ++ aux t)
190 in
191 h ++ aux t
192 in
193 let var = to_list var in
194 let len = List.length var in
195 let penv = CList.firstn len penv in
196 let assign = List.combine penv var in
197 let map_msg (key,v) =
198 let b = bool v in
199 let term = Printer.pr_constr_env env sigma key in
200 term ++ spc () ++ str ":=" ++ spc () ++ b
201 in
202 let assign = List.map map_msg assign in
203 let l = str "[" ++ (concat (str ";" ++ spc ()) assign) ++ str "]" in
204 str "Not a tautology:" ++ spc () ++ l
205
206 let print_counterexample p penv =
207 Proofview.Goal.enter begin fun gl ->
208 let env = Proofview.Goal.env gl in
209 let sigma = Proofview.Goal.sigma gl in
210 let msg = lazy (print_counterexample env sigma p penv) in
211 Proofview.tclZERO ~info:(Exninfo.reify()) (Tacticals.FailError (0, msg))
212 end
213
214 let try_unification env =
215 Proofview.Goal.enter begin fun gl ->
216 let concl = Proofview.Goal.concl gl in
217 let eq = Lazy.force eq in
218 let concl = EConstr.Unsafe.to_constr concl in
219 let t = decomp_term (Proofview.Goal.sigma gl) concl in
220 match t with
221 | App (c, [|typ; p; _|]) when c === eq ->
222 (* should be an equality [@eq poly ?p (Cst false)] *)
223 let tac = Tacticals.tclORELSE0 Tactics.reflexivity (print_counterexample p env) in
224 tac
225 | _ ->
226 let msg = str "Btauto: Internal error" in
227 Tacticals.tclFAIL msg
228 end
229
230 let tac =
231 Proofview.Goal.enter begin fun gl ->
232 let concl = Proofview.Goal.concl gl in
233 let concl = EConstr.Unsafe.to_constr concl in
234 let genv = Proofview.Goal.env gl in
235 let sigma = Proofview.Goal.sigma gl in
236 let eq = Lazy.force eq in
237 let bool = Lazy.force Bool.typ in
238 let t = decomp_term sigma concl in
239 match t with
240 | App (c, [|typ; tl; tr|])
241 when typ === bool && c === eq ->
242 let env = Env.empty () in
243 let fl = Bool.quote env genv sigma tl in
244 let fr = Bool.quote env genv sigma tr in
245 let env = Env.to_list env in
246 let fl = reify env fl in
247 let fr = reify env fr in
248 let changed_gl = Constr.mkApp (c, [|typ; fl; fr|]) in
249 let changed_gl = EConstr.of_constr changed_gl in
250 Tacticals.tclTHENLIST [
251 Tactics.change_concl changed_gl;
252 Tactics.apply (EConstr.of_constr (Lazy.force soundness));
253 Tactics.normalise_vm_in_concl;
254 try_unification env
255 ]
256 | _ ->
257 let msg = str "Cannot recognize a boolean equality" in
258 Tacticals.tclFAIL msg
259 end
260
261 end