Debian Codegraph @

tactics/contradiction.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 open EConstr
13 open Hipattern
14 open Tactics
15 open Reductionops
16 open Proofview.Notations
17
18 module NamedDecl = Context.Named.Declaration
19
20 (* Absurd *)
21
22 let mk_absurd_proof env r t =
23 Tacticals.pf_constr_of_global (Rocqlib.(lib_ref "core.not.type")) >>= fun rocq_not ->
24 Refine.refine ~typecheck:true begin fun sigma ->
25 let sigma, negev = Evarutil.new_evar env sigma (mkApp (rocq_not, [|t|])) in
26 let sigma, ev = Evarutil.new_evar ~relevance:r env sigma t in
27 sigma, mkApp (negev, [|ev|])
28 end
29
30 let absurd c =
31 Proofview.Goal.enter begin fun gl ->
32 let sigma = Proofview.Goal.sigma gl in
33 let env = Proofview.Goal.env gl in
34 let j = Retyping.get_judgment_of env sigma c in
35 let sigma, j = Coercion.inh_coerce_to_sort env sigma j in
36 let t = nf_betaiota env sigma j.Environ.utj_val in
37 let r = ESorts.relevance_of_sort j.Environ.utj_type in
38 Proofview.Unsafe.tclEVARS sigma <*>
39 Tactics.exfalso <*> mk_absurd_proof env r t
40 end
41
42 let absurd c = absurd c
43
44 (* Contradiction *)
45
46 (** [f] does not assume its argument to be [nf_evar]-ed. *)
47 let filter_hyp f tac =
48 let rec seek = function
49 | [] ->
50 let info = Exninfo.reify () in
51 Proofview.tclZERO ~info Not_found
52 | d::rest when f (NamedDecl.get_type d) -> tac (NamedDecl.get_id d)
53 | _::rest -> seek rest in
54 Proofview.Goal.enter begin fun gl ->
55 let hyps = Proofview.Goal.hyps gl in
56 seek hyps
57 end
58
59 let contradiction_context =
60 Proofview.Goal.enter begin fun gl ->
61 let sigma = Proofview.Goal.sigma gl in
62 let env = Proofview.Goal.env gl in
63 let rec seek_neg l = match l with
64 | [] ->
65 let info = Exninfo.reify () in
66 Tacticals.tclZEROMSG ~info (Pp.str"No such contradiction")
67 | d :: rest ->
68 let id = NamedDecl.get_id d in
69 let typ = nf_evar sigma (NamedDecl.get_type d) in
70 let typ = whd_all env sigma typ in
71 if is_empty_type env sigma typ then
72 simplest_elim (mkVar id)
73 else match EConstr.kind sigma typ with
74 | Prod (na,t,u) when is_empty_type env sigma u ->
75 let is_unit_or_eq = match_with_unit_or_eq_type env sigma t in
76 Tacticals.tclORELSE
77 (match is_unit_or_eq with
78 | Some _ ->
79 let hd,args = decompose_app_list sigma t in
80 let (ind,_ as indu) = destInd sigma hd in
81 let nparams = Inductiveops.inductive_nparams env ind in
82 let params = Util.List.firstn nparams args in
83 let p = applist ((mkConstructUi (indu,1)), params) in
84 (* Checking on the fly that it type-checks *)
85 simplest_elim (mkApp (mkVar id,[|p|]))
86 | None ->
87 let info = Exninfo.reify () in
88 Tacticals.tclZEROMSG ~info (Pp.str"Not a negated unit type."))
89 (Proofview.tclORELSE
90 (Proofview.Goal.enter begin fun gl ->
91 let sigma = Proofview.Goal.sigma gl in
92 let env = Proofview.Goal.env gl in
93 filter_hyp (fun typ -> is_conv_leq env sigma typ t)
94 (fun id' -> simplest_elim (mkApp (mkVar id,[|mkVar id'|])))
95 end)
96 begin function (e, info) -> match e with
97 | Not_found -> seek_neg rest
98 | e -> Proofview.tclZERO ~info e
99 end)
100 | _ -> seek_neg rest
101 in
102 let hyps = Proofview.Goal.hyps gl in
103 seek_neg hyps
104 end
105
106 let is_negation_of env sigma typ t =
107 match EConstr.kind sigma (whd_all env sigma t) with
108 | Prod (na,t,u) ->
109 is_empty_type env sigma u && is_conv_leq env sigma typ t
110 | _ -> false
111
112 let contradiction_term (c,lbind as cl) =
113 Proofview.Goal.enter begin fun gl ->
114 let sigma = Proofview.Goal.sigma gl in
115 let env = Proofview.Goal.env gl in
116 let typ = Retyping.get_type_of env sigma c in
117 let _, ccl = whd_decompose_prod env sigma typ in
118 if is_empty_type env sigma ccl then
119 Tacticals.tclTHEN
120 (elim false None cl None)
121 (Tacticals.tclTRY assumption)
122 else
123 Proofview.tclORELSE
124 begin
125 if lbind = Tactypes.NoBindings then
126 filter_hyp (fun c -> is_negation_of env sigma typ c)
127 (fun id -> simplest_elim (mkApp (mkVar id,[|c|])))
128 else
129 let info = Exninfo.reify () in
130 Proofview.tclZERO ~info Not_found
131 end
132 begin function (e, info) -> match e with
133 | Not_found ->
134 Tacticals.tclZEROMSG ~info (Pp.str"Not a contradiction.")
135 | e -> Proofview.tclZERO ~info e
136 end
137 end
138
139 let contradiction = function
140 | None -> Tacticals.tclTHEN intros contradiction_context
141 | Some c -> contradiction_term c