summaryrefslogtreecommitdiff
path: root/plugins/firstorder/rules.ml
blob: e676a8a9365fb7687e36e0e16aa7c9ba95cb42e0 (plain)
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
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2016     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

open Errors
open Util
open Names
open Term
open Vars
open Tacmach
open Tactics
open Tacticals
open Termops
open Formula
open Sequent
open Globnames
open Locus

type seqtac= (Sequent.t -> tactic) -> Sequent.t -> tactic

type lseqtac= global_reference -> seqtac

type 'a with_backtracking = tactic -> 'a

let wrap n b continue seq gls=
  Control.check_for_interrupt ();
  let nc=pf_hyps gls in
  let env=pf_env gls in
  let rec aux i nc ctx=
    if i<=0 then seq else
      match nc with
	  []->anomaly (Pp.str "Not the expected number of hyps")
	| ((id,_,typ) as nd)::q->
	    if occur_var env id (pf_concl gls) ||
	      List.exists (occur_var_in_decl env id) ctx then
		(aux (i-1) q (nd::ctx))
	    else
	      add_formula Hyp (VarRef id) typ (aux (i-1) q (nd::ctx)) gls in
  let seq1=aux n nc [] in
  let seq2=if b then
    add_formula Concl dummy_id (pf_concl gls) seq1 gls else seq1 in
    continue seq2 gls

let basename_of_global=function
    VarRef id->id
  | _->assert false

let clear_global=function
    VarRef id->clear [id]
  | _->tclIDTAC

(* connection rules *)

let axiom_tac t seq=
  try pf_constr_of_global (find_left t seq) exact_no_check
  with Not_found->tclFAIL 0 (Pp.str "No axiom link")

let ll_atom_tac a backtrack id continue seq=
  tclIFTHENELSE
    (try
      tclTHENLIST
	[pf_constr_of_global (find_left a seq) (fun left ->
	  pf_constr_of_global id (fun id -> 
	    generalize [mkApp(id, [|left|])]));
	 clear_global id;
	 Proofview.V82.of_tactic intro]
    with Not_found->tclFAIL 0 (Pp.str "No link"))
    (wrap 1 false continue seq) backtrack

(* right connectives rules *)

let and_tac backtrack continue seq=
  tclIFTHENELSE (Proofview.V82.of_tactic simplest_split) (wrap 0 true continue seq) backtrack

let or_tac backtrack continue seq=
  tclORELSE
    (Proofview.V82.of_tactic (any_constructor false (Some (Proofview.V82.tactic (tclCOMPLETE (wrap 0 true continue seq))))))
    backtrack

let arrow_tac backtrack continue seq=
  tclIFTHENELSE (Proofview.V82.of_tactic intro) (wrap 1 true continue seq)
    (tclORELSE
       (tclTHEN (Proofview.V82.of_tactic introf) (tclCOMPLETE (wrap 1 true continue seq)))
       backtrack)
(* left connectives rules *)

let left_and_tac ind backtrack id continue seq gls=
 let n=(construct_nhyps ind gls).(0) in
   tclIFTHENELSE
     (tclTHENLIST
      [Proofview.V82.of_tactic (Tacticals.New.pf_constr_of_global id simplest_elim);
       clear_global id;
       tclDO n (Proofview.V82.of_tactic intro)])
     (wrap n false continue seq)
     backtrack gls

let left_or_tac ind backtrack id continue seq gls=
  let v=construct_nhyps ind gls in
  let f n=
    tclTHENLIST
      [clear_global id;
       tclDO n (Proofview.V82.of_tactic intro);
       wrap n false continue seq] in
    tclIFTHENSVELSE
      (Proofview.V82.of_tactic (Tacticals.New.pf_constr_of_global id simplest_elim))
      (Array.map f v)
      backtrack gls

let left_false_tac id=
  Proofview.V82.of_tactic (Tacticals.New.pf_constr_of_global id simplest_elim)

(* left arrow connective rules *)

(* We use this function for false, and, or, exists *)

let ll_ind_tac (ind,u as indu) largs backtrack id continue seq gl=
     let rcs=ind_hyps 0 indu largs gl in
     let vargs=Array.of_list largs in
             (* construire le terme  H->B, le generaliser etc *)
     let myterm idc i=
       let rc=rcs.(i) in
       let p=List.length rc in
       let cstr=mkApp ((mkConstructU ((ind,(i+1)),u)),vargs) in
       let vars=Array.init p (fun j->mkRel (p-j)) in
       let capply=mkApp ((lift p cstr),vars) in
       let head=mkApp ((lift p idc),[|capply|]) in
         it_mkLambda_or_LetIn head rc in
       let lp=Array.length rcs in
       let newhyps idc =List.init lp (myterm idc) in
	 tclIFTHENELSE
	   (tclTHENLIST
	      [pf_constr_of_global id (fun idc -> generalize (newhyps idc));
	       clear_global id;
	       tclDO lp (Proofview.V82.of_tactic intro)])
	   (wrap lp false continue seq) backtrack gl

let ll_arrow_tac a b c backtrack id continue seq=
  let cc=mkProd(Anonymous,a,(lift 1 b)) in
  let d idc =mkLambda (Anonymous,b,
		  mkApp (idc, [|mkLambda (Anonymous,(lift 1 a),(mkRel 2))|])) in
    tclORELSE
      (tclTHENS (Proofview.V82.of_tactic (cut c))
	 [tclTHENLIST
	    [Proofview.V82.of_tactic introf;
	     clear_global id;
	     wrap 1 false continue seq];
	  tclTHENS (Proofview.V82.of_tactic (cut cc))
            [pf_constr_of_global id exact_no_check;
	     tclTHENLIST
	       [pf_constr_of_global id (fun idc -> generalize [d idc]);
		clear_global id;
		Proofview.V82.of_tactic introf;
		Proofview.V82.of_tactic introf;
		tclCOMPLETE (wrap 2 true continue seq)]]])
      backtrack

(* quantifier rules (easy side) *)

let forall_tac backtrack continue seq=
  tclORELSE
    (tclIFTHENELSE (Proofview.V82.of_tactic intro) (wrap 0 true continue seq)
       (tclORELSE
	  (tclTHEN (Proofview.V82.of_tactic introf) (tclCOMPLETE (wrap 0 true continue seq)))
	  backtrack))
    (if !qflag then
       tclFAIL 0 (Pp.str "reversible in 1st order mode")
     else
       backtrack)

let left_exists_tac ind backtrack id continue seq gls=
  let n=(construct_nhyps ind gls).(0) in
    tclIFTHENELSE
      (Proofview.V82.of_tactic (Tacticals.New.pf_constr_of_global id simplest_elim))
      (tclTHENLIST [clear_global id;
                    tclDO n (Proofview.V82.of_tactic intro);
                    (wrap (n-1) false continue seq)])
      backtrack
      gls

let ll_forall_tac prod backtrack id continue seq=
  tclORELSE
    (tclTHENS (Proofview.V82.of_tactic (cut prod))
       [tclTHENLIST
	  [Proofview.V82.of_tactic intro;
           pf_constr_of_global id (fun idc ->
	   (fun gls->
	      let id0=pf_nth_hyp_id gls 1 in
              let term=mkApp(idc,[|mkVar(id0)|]) in
		tclTHEN (generalize [term]) (clear [id0]) gls));
	   clear_global id;
	   Proofview.V82.of_tactic intro;
	   tclCOMPLETE (wrap 1 false continue (deepen seq))];
	tclCOMPLETE (wrap 0 true continue (deepen seq))])
    backtrack

(* rules for instantiation with unification moved to instances.ml *)

(* special for compatibility with old Intuition *)

let constant str = Coqlib.gen_constant "User" ["Init";"Logic"] str

let defined_connectives=lazy
  [AllOccurrences,EvalConstRef (fst (destConst (constant "not")));
   AllOccurrences,EvalConstRef (fst (destConst (constant "iff")))]

let normalize_evaluables=
  onAllHypsAndConcl
    (function
	 None->unfold_in_concl (Lazy.force defined_connectives)
       | Some id ->
	   unfold_in_hyp (Lazy.force defined_connectives) (id,InHypTypeOnly))