aboutsummaryrefslogtreecommitdiffhomepage
path: root/tactics/tauto.ml4
blob: 7e6334bc9a4cd17455399bc0e859fa597e61bdc0 (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
(***********************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
(*   \VV/  *************************************************************)
(*    //   *      This file is distributed under the terms of the      *)
(*         *       GNU Lesser General Public License Version 2.1       *)
(***********************************************************************)

(*i camlp4deps: "parsing/grammar.cma" i*)

(*i $Id$ i*)

open Ast
open Coqast
open Hipattern
open Names
open Libnames
open Pp
open Proof_type
open Tacticals
open Tacinterp
open Tactics
open Util

let is_empty ist =
  if (is_empty_type (List.assoc 1 ist.lmatch)) then
    <:tactic<ElimType ?1;Assumption>>
  else
    <:tactic<Fail>>

let is_unit ist =
  if (is_unit_type (List.assoc 1 ist.lmatch)) then
    <:tactic<Idtac>>
  else
    <:tactic<Fail>>

let is_conj ist =
  let ind=(List.assoc 1 ist.lmatch) in
  if (is_conjunction ind) && (is_nodep_ind ind) then
     <:tactic<Idtac>>
  else
    <:tactic<Fail>>

let is_disj ist =
  if (is_disjunction (List.assoc 1 ist.lmatch)) then
     <:tactic<Idtac>>
  else
    <:tactic<Fail>>

let not_dep_intros ist =
  <:tactic<
    Repeat
      Match Context With
      | [|- ?1 -> ?2 ] -> Intro>>

let axioms ist =
  let t_is_unit = tacticIn is_unit
  and t_is_empty = tacticIn is_empty in
  <:tactic<
    Match Reverse Context With
    |[|- ?1] -> $t_is_unit;Constructor 1
    |[_:?1 |- ?] -> $t_is_empty
    |[_:?1 |- ?1] -> Assumption>>


let simplif t_reduce ist =
  let t_is_unit = tacticIn is_unit
  and t_is_conj = tacticIn is_conj
  and t_is_disj = tacticIn is_disj
  and t_not_dep_intros = tacticIn not_dep_intros in
  <:tactic<
    $t_not_dep_intros;
    Repeat
      ((Match Reverse Context With
        | [id: (?1 ? ?) |- ?] ->
            $t_is_conj;Elim id;Do 2 Intro;Clear id;$t_reduce
        | [id: (?1 ? ?) |- ?] -> $t_is_disj;Elim id;Intro;Clear id;$t_reduce
        | [id0: ?1-> ?2; id1: ?1|- ?] -> Generalize (id0 id1);Intro;Clear id0
        | [id: ?1 -> ?2|- ?] ->
          $t_is_unit;Cut ?2;
	    [Intro;Clear id
	    | (* id : ?1 -> ?2 |- ?2 *)
	      Cut ?1;[Exact id|Constructor 1;Fail]
	    ]
        | [id: (?1 ?2 ?3) -> ?4|- ?] ->
          $t_is_conj;Cut ?2-> ?3-> ?4;
	    [Intro;Clear id;$t_reduce
	    | (* id: (?1 ?2 ?3) -> ?4 |- ?2 -> ?3 -> ?4 *)
	      Intro;Intro; Cut (?1 ?2 ?3);[Exact id|Split;Assumption]
	    ]
        | [id: (?1 ?2 ?3) -> ?4|- ?] ->
          $t_is_disj;
	    Cut ?3-> ?4;
	      [Cut ?2-> ?4;
	        [Intro;Intro;Clear id;$t_reduce
		| (* id: (?1 ?2 ?3) -> ?4 |- ?2 -> ?4 *)
		  Intro; Cut (?1 ?2 ?3);[Exact id|Left;Assumption]
		]
	      | (* id: (?1 ?2 ?3) -> ?4 |- ?3 -> ?4 *)
		Intro; Cut (?1 ?2 ?3);[Exact id|Right;Assumption]
	      ]
        | [|- (?1 ? ?)] -> $t_is_conj;Split;$t_reduce);
       $t_not_dep_intros)>>

let rec tauto_intuit t_reduce t_solver ist =
  let t_axioms = tacticIn axioms
  and t_simplif = tacticIn (simplif t_reduce)
  and t_is_disj = tacticIn is_disj
  and t_tauto_intuit = tacticIn (tauto_intuit t_reduce t_solver) in
  <:tactic<
   $t_reduce;
   ($t_simplif;$t_axioms
    Orelse
      (Match Reverse Context With
      | [id:(?1-> ?2)-> ?3|- ?] ->
	  Cut ?3;
	    [Intro;Clear id
	    | Cut ?1 -> ?2;
	       [Exact id|Generalize [y:?2](id [x:?1]y);Intro;Clear id]
	    ]; Solve [ $t_tauto_intuit ]
      | [|- (?1 ? ?)] ->
        $t_is_disj;Solve [Left;$t_tauto_intuit | Right;$t_tauto_intuit]
      )
    Orelse
      (* NB: [|- ? -> ?] matches any product *)
      (Match Context With |[ |- ? -> ? ] -> Intro;$t_tauto_intuit)
    Orelse
      $t_solver
   ) >>

let unfold_not_iff = function
  | None -> interp <:tactic<Unfold not iff>>
  | Some id -> let id = (dummy_loc,id) in interp <:tactic<Unfold not iff in $id>>

let reduction_not_iff =
  Tacticals.onAllClauses (fun ido -> unfold_not_iff ido)

let t_reduction_not_iff = Tacexpr.TacArg (valueIn (VTactic reduction_not_iff))

let intuition_gen tac =
  interp (tacticIn (tauto_intuit t_reduction_not_iff tac))

let simplif_gen = interp (tacticIn (simplif t_reduction_not_iff))

let tauto g =
  try intuition_gen <:tactic<Fail>> g
  with
    Refiner.FailError _ | UserError _ ->
      errorlabstrm "tauto" [< str "Tauto failed" >]

let default_intuition_tac = <:tactic< Auto with * >>

TACTIC EXTEND Tauto
| [ "Tauto" ] -> [ tauto ]
END

TACTIC EXTEND TSimplif
| [ "Simplif" ] -> [ simplif_gen ]
END

TACTIC EXTEND Intuition
| [ "Intuition" ] -> [ intuition_gen default_intuition_tac ]
| [ "Intuition" tactic(t) ] -> [ intuition_gen t ]
END