src/Provers/classical.ML
author lcp
Fri Nov 25 10:43:50 1994 +0100 (1994-11-25)
changeset 747 bdc066781063
parent 681 9b02474744ca
child 982 4fe0b642b7d5
permissions -rw-r--r--
deepen_tac: modified due to outcome of experiments. Its
choice of unsafe rule to expand is still non-deterministic.
clasohm@0
     1
(*  Title: 	Provers/classical
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1992  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Theorem prover for classical reasoning, including predicate calculus, set
clasohm@0
     7
theory, etc.
clasohm@0
     8
clasohm@0
     9
Rules must be classified as intr, elim, safe, hazardous.
clasohm@0
    10
clasohm@0
    11
A rule is unsafe unless it can be applied blindly without harmful results.
clasohm@0
    12
For a rule to be safe, its premises and conclusion should be logically
clasohm@0
    13
equivalent.  There should be no variables in the premises that are not in
clasohm@0
    14
the conclusion.
clasohm@0
    15
*)
clasohm@0
    16
clasohm@0
    17
signature CLASSICAL_DATA =
clasohm@0
    18
  sig
lcp@681
    19
  val mp	: thm    	(* [| P-->Q;  P |] ==> Q *)
lcp@681
    20
  val not_elim	: thm		(* [| ~P;  P |] ==> R *)
lcp@681
    21
  val classical	: thm		(* (~P ==> P) ==> P *)
lcp@681
    22
  val sizef 	: thm -> int	(* size function for BEST_FIRST *)
clasohm@0
    23
  val hyp_subst_tacs: (int -> tactic) list
clasohm@0
    24
  end;
clasohm@0
    25
clasohm@0
    26
(*Higher precedence than := facilitates use of references*)
clasohm@0
    27
infix 4 addSIs addSEs addSDs addIs addEs addDs;
clasohm@0
    28
clasohm@0
    29
clasohm@0
    30
signature CLASSICAL =
clasohm@0
    31
  sig
clasohm@0
    32
  type claset
lcp@681
    33
  val empty_cs		: claset
lcp@681
    34
  val addDs 		: claset * thm list -> claset
lcp@681
    35
  val addEs 		: claset * thm list -> claset
lcp@681
    36
  val addIs 		: claset * thm list -> claset
lcp@681
    37
  val addSDs		: claset * thm list -> claset
lcp@681
    38
  val addSEs		: claset * thm list -> claset
lcp@681
    39
  val addSIs		: claset * thm list -> claset
lcp@681
    40
  val print_cs		: claset -> unit
lcp@681
    41
  val rep_claset	: claset -> {safeIs: thm list, safeEs: thm list, 
lcp@681
    42
				     hazIs: thm list, hazEs: thm list}
lcp@681
    43
  val best_tac 		: claset -> int -> tactic
lcp@681
    44
  val contr_tac 	: int -> tactic
lcp@681
    45
  val depth_tac		: claset -> int -> int -> tactic
lcp@681
    46
  val deepen_tac	: claset -> int -> int -> tactic
lcp@681
    47
  val dup_elim		: thm -> thm
lcp@681
    48
  val dup_intr		: thm -> thm
lcp@681
    49
  val dup_step_tac	: claset -> int -> tactic
lcp@681
    50
  val eq_mp_tac		: int -> tactic
lcp@681
    51
  val fast_tac 		: claset -> int -> tactic
lcp@681
    52
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
    53
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
    54
  val mp_tac		: int -> tactic
lcp@681
    55
  val safe_tac 		: claset -> tactic
lcp@681
    56
  val safe_step_tac 	: claset -> int -> tactic
lcp@681
    57
  val slow_step_tac 	: claset -> int -> tactic
lcp@681
    58
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
    59
  val slow_tac 		: claset -> int -> tactic
lcp@681
    60
  val step_tac 		: claset -> int -> tactic
lcp@681
    61
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
    62
  val swapify 		: thm list -> thm list
lcp@681
    63
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
    64
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
    65
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
    66
  val instp_step_tac 	: claset -> int -> tactic
clasohm@0
    67
  end;
clasohm@0
    68
clasohm@0
    69
clasohm@0
    70
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
    71
struct
clasohm@0
    72
clasohm@0
    73
local open Data in
clasohm@0
    74
clasohm@0
    75
(** Useful tactics for classical reasoning **)
clasohm@0
    76
clasohm@0
    77
val imp_elim = make_elim mp;
clasohm@0
    78
clasohm@0
    79
(*Solve goal that assumes both P and ~P. *)
clasohm@0
    80
val contr_tac = eresolve_tac [not_elim]  THEN'  assume_tac;
clasohm@0
    81
lcp@681
    82
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
    83
  Could do the same thing for P<->Q and P... *)
lcp@681
    84
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
    85
clasohm@0
    86
(*Like mp_tac but instantiates no variables*)
lcp@681
    87
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
    88
lcp@681
    89
val swap = rule_by_tactic (etac thin_rl 1) (not_elim RS classical);
clasohm@0
    90
clasohm@0
    91
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
    92
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
    93
clasohm@0
    94
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
    95
  trying rules in order. *)
clasohm@0
    96
fun swap_res_tac rls = 
lcp@54
    97
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
    98
    in  assume_tac 	ORELSE' 
lcp@54
    99
	contr_tac 	ORELSE' 
lcp@54
   100
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   101
    end;
clasohm@0
   102
lcp@681
   103
(*Duplication of hazardous rules, for complete provers*)
lcp@681
   104
fun dup_intr th = standard (th RS classical);
lcp@681
   105
lcp@681
   106
fun dup_elim th = th RSN (2, revcut_rl) |> assumption 2 |> Sequence.hd |> 
lcp@681
   107
                  rule_by_tactic (TRYALL (etac revcut_rl));
clasohm@0
   108
clasohm@0
   109
(*** Classical rule sets ***)
clasohm@0
   110
clasohm@0
   111
type netpair = (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net;
clasohm@0
   112
clasohm@0
   113
datatype claset =
clasohm@0
   114
  CS of {safeIs		: thm list,
clasohm@0
   115
	 safeEs		: thm list,
clasohm@0
   116
	 hazIs		: thm list,
clasohm@0
   117
	 hazEs		: thm list,
clasohm@0
   118
	 safe0_netpair	: netpair,
clasohm@0
   119
	 safep_netpair	: netpair,
lcp@681
   120
	 haz_netpair  	: netpair,
lcp@681
   121
	 dup_netpair	: netpair};
clasohm@0
   122
clasohm@0
   123
fun rep_claset (CS{safeIs,safeEs,hazIs,hazEs,...}) = 
clasohm@0
   124
    {safeIs=safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=hazEs};
clasohm@0
   125
clasohm@0
   126
(*For use with biresolve_tac.  Combines intrs with swap to catch negated
clasohm@0
   127
  assumptions; pairs elims with true; sorts. *)
clasohm@0
   128
fun joinrules (intrs,elims) =  
clasohm@0
   129
  sort lessb 
clasohm@0
   130
    (map (pair true) (elims @ swapify intrs)  @
clasohm@0
   131
     map (pair false) intrs);
clasohm@0
   132
lcp@681
   133
val build = build_netpair(Net.empty,Net.empty);
lcp@681
   134
clasohm@0
   135
(*Make a claset from the four kinds of rules*)
clasohm@0
   136
fun make_cs {safeIs,safeEs,hazIs,hazEs} =
clasohm@0
   137
  let val (safe0_brls, safep_brls) = (*0 subgoals vs 1 or more*)
clasohm@0
   138
          take_prefix (fn brl => subgoals_of_brl brl=0)
clasohm@0
   139
             (joinrules(safeIs, safeEs))
clasohm@0
   140
  in CS{safeIs = safeIs, 
clasohm@0
   141
        safeEs = safeEs,
clasohm@0
   142
	hazIs = hazIs,
clasohm@0
   143
	hazEs = hazEs,
lcp@681
   144
	safe0_netpair = build safe0_brls,
lcp@681
   145
	safep_netpair = build safep_brls,
lcp@681
   146
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   147
	dup_netpair = build (joinrules(map dup_intr hazIs, 
lcp@681
   148
				       map dup_elim hazEs))}
clasohm@0
   149
  end;
clasohm@0
   150
clasohm@0
   151
(*** Manipulation of clasets ***)
clasohm@0
   152
clasohm@0
   153
val empty_cs = make_cs{safeIs=[], safeEs=[], hazIs=[], hazEs=[]};
clasohm@0
   154
clasohm@0
   155
fun print_cs (CS{safeIs,safeEs,hazIs,hazEs,...}) =
clasohm@0
   156
 (writeln"Introduction rules";  prths hazIs;
clasohm@0
   157
  writeln"Safe introduction rules";  prths safeIs;
clasohm@0
   158
  writeln"Elimination rules";  prths hazEs;
clasohm@0
   159
  writeln"Safe elimination rules";  prths safeEs;
clasohm@0
   160
  ());
clasohm@0
   161
clasohm@0
   162
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addSIs ths =
clasohm@0
   163
  make_cs {safeIs=ths@safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=hazEs};
clasohm@0
   164
clasohm@0
   165
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addSEs ths =
clasohm@0
   166
  make_cs {safeIs=safeIs, safeEs=ths@safeEs, hazIs=hazIs, hazEs=hazEs};
clasohm@0
   167
clasohm@0
   168
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   169
clasohm@0
   170
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addIs ths =
clasohm@0
   171
  make_cs {safeIs=safeIs, safeEs=safeEs, hazIs=ths@hazIs, hazEs=hazEs};
clasohm@0
   172
clasohm@0
   173
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addEs ths =
clasohm@0
   174
  make_cs {safeIs=safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=ths@hazEs};
clasohm@0
   175
clasohm@0
   176
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   177
clasohm@0
   178
(*** Simple tactics for theorem proving ***)
clasohm@0
   179
clasohm@0
   180
(*Attack subgoals using safe inferences -- matching, not resolution*)
clasohm@0
   181
fun safe_step_tac (CS{safe0_netpair,safep_netpair,...}) = 
clasohm@0
   182
  FIRST' [eq_assume_tac,
clasohm@0
   183
	  eq_mp_tac,
clasohm@0
   184
	  bimatch_from_nets_tac safe0_netpair,
clasohm@0
   185
	  FIRST' hyp_subst_tacs,
clasohm@0
   186
	  bimatch_from_nets_tac safep_netpair] ;
clasohm@0
   187
clasohm@0
   188
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
lcp@747
   189
fun safe_tac cs = REPEAT_DETERM_FIRST (safe_step_tac cs);
lcp@747
   190
lcp@747
   191
(*But these unsafe steps at least solve a subgoal!*)
lcp@747
   192
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   193
  assume_tac 			  APPEND' 
lcp@747
   194
  contr_tac 			  APPEND' 
lcp@747
   195
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   196
lcp@747
   197
(*These are much worse since they could generate more and more subgoals*)
lcp@747
   198
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   199
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   200
clasohm@0
   201
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   202
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   203
lcp@681
   204
fun haz_step_tac (cs as (CS{haz_netpair,...})) = 
lcp@681
   205
  biresolve_from_nets_tac haz_netpair;
lcp@681
   206
clasohm@0
   207
(*Single step for the prover.  FAILS unless it makes progress. *)
lcp@681
   208
fun step_tac cs i = 
lcp@681
   209
  FIRST [safe_tac cs, inst_step_tac cs i, haz_step_tac cs i];
clasohm@0
   210
clasohm@0
   211
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   212
  allows backtracking from "safe" rules to "unsafe" rules here.*)
lcp@681
   213
fun slow_step_tac cs i = 
lcp@681
   214
    safe_tac cs ORELSE (inst_step_tac cs i APPEND haz_step_tac cs i);
clasohm@0
   215
clasohm@0
   216
(*** The following tactics all fail unless they solve one goal ***)
clasohm@0
   217
clasohm@0
   218
(*Dumb but fast*)
clasohm@0
   219
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   220
clasohm@0
   221
(*Slower but smarter than fast_tac*)
clasohm@0
   222
fun best_tac cs = 
clasohm@0
   223
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   224
clasohm@0
   225
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   226
clasohm@0
   227
fun slow_best_tac cs = 
clasohm@0
   228
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   229
lcp@681
   230
lcp@747
   231
(*** Complete tactic, loosely based upon LeanTaP This tactic is the outcome
lcp@747
   232
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   233
  easy theorems faster, but loses completeness -- and many of the harder
lcp@747
   234
  theorems such as 43. ***)
lcp@681
   235
lcp@747
   236
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   237
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   238
  greater search depth required.*)
lcp@681
   239
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   240
  biresolve_from_nets_tac dup_netpair;
lcp@681
   241
lcp@747
   242
(*Searching to depth m.*)
lcp@747
   243
fun depth_tac cs m i = STATE(fn state => 
lcp@747
   244
  SELECT_GOAL 
lcp@747
   245
    (REPEAT_DETERM1 (safe_step_tac cs 1) THEN_ELSE
lcp@747
   246
     (DEPTH_SOLVE (depth_tac cs m 1),
lcp@747
   247
      inst0_step_tac cs 1  APPEND
lcp@747
   248
      COND (K(m=0)) no_tac
lcp@747
   249
        ((instp_step_tac cs 1 APPEND dup_step_tac cs 1)
lcp@747
   250
	 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))))
lcp@747
   251
  i);
lcp@747
   252
lcp@747
   253
(*Iterative deepening tactical.  Allows us to "deepen" any search tactic*)
lcp@747
   254
fun DEEPEN tacf m i = STATE(fn state => 
lcp@747
   255
   if has_fewer_prems i state then no_tac
lcp@747
   256
   else (writeln ("Depth = " ^ string_of_int m);
lcp@747
   257
	 tacf m i  ORELSE  DEEPEN tacf (m+2) i));
lcp@747
   258
lcp@747
   259
fun safe_depth_tac cs m = 
lcp@681
   260
  SUBGOAL 
lcp@681
   261
    (fn (prem,i) =>
lcp@681
   262
      let val deti =
lcp@681
   263
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   264
	  case term_vars prem of
lcp@681
   265
	      []	=> DETERM 
lcp@681
   266
	    | _::_	=> I
lcp@681
   267
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   268
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   269
      end);
lcp@681
   270
lcp@747
   271
fun deepen_tac cs = DEEPEN (safe_depth_tac cs);
lcp@681
   272
clasohm@0
   273
end; 
clasohm@0
   274
end;