src/Provers/classical.ML
author paulson
Sat Nov 01 13:03:00 1997 +0100 (1997-11-01 ago)
changeset 4066 7b508ac609f7
parent 3727 ed63c05d7992
child 4079 9df5e4f22d96
permissions -rw-r--r--
Fixed comments
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
paulson@2868
    17
(*Should be a type abbreviation in signature CLASSICAL*)
paulson@2868
    18
type netpair = (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net;
paulson@2868
    19
clasohm@0
    20
signature CLASSICAL_DATA =
clasohm@0
    21
  sig
lcp@681
    22
  val mp	: thm    	(* [| P-->Q;  P |] ==> Q *)
lcp@681
    23
  val not_elim	: thm		(* [| ~P;  P |] ==> R *)
lcp@681
    24
  val classical	: thm		(* (~P ==> P) ==> P *)
lcp@681
    25
  val sizef 	: thm -> int	(* size function for BEST_FIRST *)
clasohm@0
    26
  val hyp_subst_tacs: (int -> tactic) list
clasohm@0
    27
  end;
clasohm@0
    28
clasohm@0
    29
(*Higher precedence than := facilitates use of references*)
paulson@1800
    30
infix 4 addSIs addSEs addSDs addIs addEs addDs delrules
oheimb@2630
    31
        setSWrapper compSWrapper setWrapper compWrapper 
oheimb@2630
    32
	addSbefore addSaltern addbefore addaltern;
clasohm@0
    33
clasohm@0
    34
clasohm@0
    35
signature CLASSICAL =
clasohm@0
    36
  sig
clasohm@0
    37
  type claset
lcp@681
    38
  val empty_cs		: claset
paulson@1711
    39
  val merge_cs		: claset * claset -> claset
lcp@681
    40
  val addDs 		: claset * thm list -> claset
lcp@681
    41
  val addEs 		: claset * thm list -> claset
lcp@681
    42
  val addIs 		: claset * thm list -> claset
lcp@681
    43
  val addSDs		: claset * thm list -> claset
lcp@681
    44
  val addSEs		: claset * thm list -> claset
lcp@681
    45
  val addSIs		: claset * thm list -> claset
paulson@1800
    46
  val delrules		: claset * thm list -> claset
oheimb@2630
    47
  val setSWrapper 	: claset * ((int -> tactic) -> (int -> tactic)) ->claset
oheimb@2630
    48
  val compSWrapper 	: claset * ((int -> tactic) -> (int -> tactic)) ->claset
oheimb@2630
    49
  val setWrapper 	: claset * ((int -> tactic) -> (int -> tactic)) ->claset
oheimb@2630
    50
  val compWrapper 	: claset * ((int -> tactic) -> (int -> tactic)) ->claset
oheimb@2630
    51
  val addSbefore 	: claset * (int -> tactic) -> claset
oheimb@2630
    52
  val addSaltern 	: claset * (int -> tactic) -> claset
oheimb@2630
    53
  val addbefore 	: claset * (int -> tactic) -> claset
oheimb@2630
    54
  val addaltern	 	: claset * (int -> tactic) -> claset
lcp@982
    55
lcp@681
    56
  val print_cs		: claset -> unit
lcp@1073
    57
  val rep_claset	: 
lcp@1073
    58
      claset -> {safeIs: thm list, safeEs: thm list, 
lcp@1073
    59
		 hazIs: thm list, hazEs: thm list,
oheimb@2630
    60
		 uwrapper: (int -> tactic) -> (int -> tactic),
oheimb@2630
    61
		 swrapper: (int -> tactic) -> (int -> tactic),
lcp@1073
    62
		 safe0_netpair: netpair, safep_netpair: netpair,
lcp@1073
    63
		 haz_netpair: netpair, dup_netpair: netpair}
oheimb@2630
    64
  val getWrapper	: claset -> (int -> tactic) -> (int -> tactic)
oheimb@2630
    65
  val getSWrapper	: claset -> (int -> tactic) -> (int -> tactic)
lcp@982
    66
paulson@1587
    67
  val fast_tac 		: claset -> int -> tactic
paulson@1587
    68
  val slow_tac 		: claset -> int -> tactic
paulson@1587
    69
  val weight_ASTAR	: int ref
paulson@1587
    70
  val astar_tac		: claset -> int -> tactic
paulson@1587
    71
  val slow_astar_tac 	: claset -> int -> tactic
lcp@681
    72
  val best_tac 		: claset -> int -> tactic
paulson@1587
    73
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
    74
  val depth_tac		: claset -> int -> int -> tactic
lcp@681
    75
  val deepen_tac	: claset -> int -> int -> tactic
paulson@1587
    76
paulson@1587
    77
  val contr_tac 	: int -> tactic
lcp@681
    78
  val dup_elim		: thm -> thm
lcp@681
    79
  val dup_intr		: thm -> thm
lcp@681
    80
  val dup_step_tac	: claset -> int -> tactic
lcp@681
    81
  val eq_mp_tac		: int -> tactic
lcp@681
    82
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
    83
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
    84
  val mp_tac		: int -> tactic
lcp@681
    85
  val safe_tac 		: claset -> tactic
lcp@681
    86
  val safe_step_tac 	: claset -> int -> tactic
paulson@3705
    87
  val clarify_tac 	: claset -> int -> tactic
paulson@3705
    88
  val clarify_step_tac 	: claset -> int -> tactic
lcp@681
    89
  val step_tac 		: claset -> int -> tactic
oheimb@2630
    90
  val slow_step_tac	: claset -> int -> tactic
lcp@681
    91
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
    92
  val swapify 		: thm list -> thm list
lcp@681
    93
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
    94
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
    95
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
    96
  val instp_step_tac 	: claset -> int -> tactic
berghofe@1724
    97
berghofe@1724
    98
  val claset : claset ref
berghofe@1724
    99
  val AddDs 		: thm list -> unit
berghofe@1724
   100
  val AddEs 		: thm list -> unit
berghofe@1724
   101
  val AddIs 		: thm list -> unit
berghofe@1724
   102
  val AddSDs		: thm list -> unit
berghofe@1724
   103
  val AddSEs		: thm list -> unit
berghofe@1724
   104
  val AddSIs		: thm list -> unit
paulson@1807
   105
  val Delrules		: thm list -> unit
paulson@3727
   106
  val Safe_tac         	: tactic
paulson@1814
   107
  val Safe_step_tac	: int -> tactic
paulson@3705
   108
  val Clarify_tac 	: int -> tactic
paulson@3705
   109
  val Clarify_step_tac 	: int -> tactic
paulson@1800
   110
  val Step_tac 		: int -> tactic
berghofe@1724
   111
  val Fast_tac 		: int -> tactic
paulson@1800
   112
  val Best_tac 		: int -> tactic
paulson@2066
   113
  val Slow_tac 		: int -> tactic
paulson@2066
   114
  val Slow_best_tac     : int -> tactic
paulson@1800
   115
  val Deepen_tac	: int -> int -> tactic
berghofe@1724
   116
clasohm@0
   117
  end;
clasohm@0
   118
clasohm@0
   119
clasohm@0
   120
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
   121
struct
clasohm@0
   122
clasohm@0
   123
local open Data in
clasohm@0
   124
paulson@1800
   125
(*** Useful tactics for classical reasoning ***)
clasohm@0
   126
paulson@1524
   127
val imp_elim = (*cannot use bind_thm within a structure!*)
paulson@1524
   128
  store_thm ("imp_elim", make_elim mp);
clasohm@0
   129
clasohm@0
   130
(*Solve goal that assumes both P and ~P. *)
clasohm@0
   131
val contr_tac = eresolve_tac [not_elim]  THEN'  assume_tac;
clasohm@0
   132
lcp@681
   133
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   134
  Could do the same thing for P<->Q and P... *)
lcp@681
   135
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
   136
clasohm@0
   137
(*Like mp_tac but instantiates no variables*)
lcp@681
   138
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
   139
paulson@1524
   140
val swap =
paulson@1524
   141
  store_thm ("swap", rule_by_tactic (etac thin_rl 1) (not_elim RS classical));
clasohm@0
   142
clasohm@0
   143
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
   144
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
   145
clasohm@0
   146
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   147
  trying rules in order. *)
clasohm@0
   148
fun swap_res_tac rls = 
lcp@54
   149
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
   150
    in  assume_tac 	ORELSE' 
lcp@54
   151
	contr_tac 	ORELSE' 
lcp@54
   152
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   153
    end;
clasohm@0
   154
lcp@681
   155
(*Duplication of hazardous rules, for complete provers*)
paulson@2689
   156
fun dup_intr th = zero_var_indexes (th RS classical);
lcp@681
   157
lcp@681
   158
fun dup_elim th = th RSN (2, revcut_rl) |> assumption 2 |> Sequence.hd |> 
lcp@681
   159
                  rule_by_tactic (TRYALL (etac revcut_rl));
clasohm@0
   160
lcp@1073
   161
paulson@1800
   162
(**** Classical rule sets ****)
clasohm@0
   163
clasohm@0
   164
datatype claset =
lcp@982
   165
  CS of {safeIs		: thm list,		(*safe introduction rules*)
lcp@982
   166
	 safeEs		: thm list,		(*safe elimination rules*)
lcp@982
   167
	 hazIs		: thm list,		(*unsafe introduction rules*)
lcp@982
   168
	 hazEs		: thm list,		(*unsafe elimination rules*)
oheimb@2630
   169
	 uwrapper	: (int -> tactic) ->
oheimb@2630
   170
			  (int -> tactic),	(*for transforming step_tac*)
oheimb@2630
   171
	 swrapper	: (int -> tactic) ->
oheimb@2630
   172
			  (int -> tactic),	(*for transform. safe_step_tac*)
lcp@982
   173
	 safe0_netpair	: netpair,		(*nets for trivial cases*)
lcp@982
   174
	 safep_netpair	: netpair,		(*nets for >0 subgoals*)
lcp@982
   175
	 haz_netpair  	: netpair,		(*nets for unsafe rules*)
lcp@982
   176
	 dup_netpair	: netpair};		(*nets for duplication*)
clasohm@0
   177
lcp@1073
   178
(*Desired invariants are
lcp@681
   179
	safe0_netpair = build safe0_brls,
lcp@681
   180
	safep_netpair = build safep_brls,
lcp@681
   181
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   182
	dup_netpair = build (joinrules(map dup_intr hazIs, 
lcp@681
   183
				       map dup_elim hazEs))}
lcp@1073
   184
lcp@1073
   185
where build = build_netpair(Net.empty,Net.empty), 
lcp@1073
   186
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   187
      safep_brls contains all brules that generate 1 or more new subgoals.
paulson@1800
   188
The theorem lists are largely comments, though they are used in merge_cs.
lcp@1073
   189
Nets must be built incrementally, to save space and time.
lcp@1073
   190
*)
clasohm@0
   191
lcp@1073
   192
val empty_cs = 
lcp@1073
   193
  CS{safeIs	= [],
lcp@1073
   194
     safeEs	= [],
lcp@1073
   195
     hazIs	= [],
lcp@1073
   196
     hazEs	= [],
oheimb@2630
   197
     uwrapper   = I,
oheimb@2630
   198
     swrapper   = I,
lcp@1073
   199
     safe0_netpair = (Net.empty,Net.empty),
lcp@1073
   200
     safep_netpair = (Net.empty,Net.empty),
lcp@1073
   201
     haz_netpair   = (Net.empty,Net.empty),
lcp@1073
   202
     dup_netpair   = (Net.empty,Net.empty)};
clasohm@0
   203
wenzelm@3546
   204
fun print_cs (CS {safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@3546
   205
  let val pretty_thms = map Display.pretty_thm in
wenzelm@3546
   206
    Pretty.writeln (Pretty.big_list "introduction rules:" (pretty_thms hazIs));
wenzelm@3546
   207
    Pretty.writeln (Pretty.big_list "safe introduction rules:" (pretty_thms safeIs));
wenzelm@3546
   208
    Pretty.writeln (Pretty.big_list "elimination rules:" (pretty_thms hazEs));
wenzelm@3546
   209
    Pretty.writeln (Pretty.big_list "safe elimination rules:" (pretty_thms safeEs))
wenzelm@3546
   210
  end;
clasohm@0
   211
lcp@1073
   212
fun rep_claset (CS args) = args;
lcp@1073
   213
oheimb@2630
   214
fun getWrapper  (CS{uwrapper,...}) = uwrapper;
oheimb@2630
   215
oheimb@2630
   216
fun getSWrapper (CS{swrapper,...}) = swrapper;
lcp@1073
   217
lcp@1073
   218
paulson@1800
   219
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   220
lcp@1073
   221
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   222
***)
clasohm@0
   223
lcp@1073
   224
(*For use with biresolve_tac.  Combines intr rules with swap to handle negated
lcp@1073
   225
  assumptions.  Pairs elim rules with true. *)
lcp@1073
   226
fun joinrules (intrs,elims) =  
lcp@1073
   227
    (map (pair true) (elims @ swapify intrs)  @
lcp@1073
   228
     map (pair false) intrs);
lcp@1073
   229
lcp@1073
   230
(*Priority: prefer rules with fewest subgoals, 
paulson@1231
   231
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   232
fun tag_brls k [] = []
lcp@1073
   233
  | tag_brls k (brl::brls) =
lcp@1073
   234
      (1000000*subgoals_of_brl brl + k, brl) :: 
lcp@1073
   235
      tag_brls (k+1) brls;
lcp@1073
   236
paulson@1800
   237
fun insert_tagged_list kbrls netpr = foldr insert_tagged_brl (kbrls, netpr);
lcp@1073
   238
lcp@1073
   239
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   240
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   241
  new insertions the lowest priority.*)
lcp@1073
   242
fun insert (nI,nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
lcp@1073
   243
paulson@1800
   244
fun delete_tagged_list brls netpr = foldr delete_tagged_brl (brls, netpr);
lcp@1073
   245
paulson@1800
   246
val delete = delete_tagged_list o joinrules;
paulson@1800
   247
paulson@2813
   248
val mem_thm = gen_mem eq_thm
paulson@2813
   249
and rem_thm = gen_rem eq_thm;
paulson@2813
   250
paulson@1927
   251
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   252
  is still allowed.*)
paulson@1927
   253
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) = 
paulson@2813
   254
       if mem_thm (th, safeIs) then 
paulson@1927
   255
	 warning ("rule already in claset as Safe Intr\n" ^ string_of_thm th)
paulson@2813
   256
  else if mem_thm (th, safeEs) then
paulson@1927
   257
         warning ("rule already in claset as Safe Elim\n" ^ string_of_thm th)
paulson@2813
   258
  else if mem_thm (th, hazIs) then 
paulson@1927
   259
         warning ("rule already in claset as unsafe Intr\n" ^ string_of_thm th)
paulson@2813
   260
  else if mem_thm (th, hazEs) then 
paulson@1927
   261
         warning ("rule already in claset as unsafe Elim\n" ^ string_of_thm th)
paulson@1927
   262
  else ();
paulson@1927
   263
paulson@1800
   264
(*** Safe rules ***)
lcp@982
   265
oheimb@2630
   266
fun addSI (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   267
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   268
	   th)  =
paulson@2813
   269
  if mem_thm (th, safeIs) then 
paulson@1927
   270
	 (warning ("ignoring duplicate Safe Intr\n" ^ string_of_thm th);
paulson@1927
   271
	  cs)
paulson@1927
   272
  else
lcp@1073
   273
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   274
          partition (fn rl => nprems_of rl=0) [th]
paulson@1927
   275
      val nI = length safeIs + 1
lcp@1073
   276
      and nE = length safeEs
paulson@1927
   277
  in warn_dup th cs;
paulson@1927
   278
     CS{safeIs	= th::safeIs,
lcp@1073
   279
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   280
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   281
	safeEs	= safeEs,
lcp@1073
   282
	hazIs	= hazIs,
lcp@1073
   283
	hazEs	= hazEs,
oheimb@2630
   284
	uwrapper     = uwrapper,
oheimb@2630
   285
	swrapper     = swrapper,
oheimb@2630
   286
	haz_netpair  = haz_netpair,
oheimb@2630
   287
	dup_netpair  = dup_netpair}
lcp@1073
   288
  end;
lcp@1073
   289
oheimb@2630
   290
fun addSE (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   291
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   292
	   th)  =
paulson@2813
   293
  if mem_thm (th, safeEs) then 
paulson@1927
   294
	 (warning ("ignoring duplicate Safe Elim\n" ^ string_of_thm th);
paulson@1927
   295
	  cs)
paulson@1927
   296
  else
lcp@1073
   297
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   298
          partition (fn rl => nprems_of rl=1) [th]
lcp@1073
   299
      val nI = length safeIs
paulson@1927
   300
      and nE = length safeEs + 1
paulson@1927
   301
  in warn_dup th cs;
paulson@1927
   302
     CS{safeEs	= th::safeEs,
lcp@1073
   303
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   304
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   305
	safeIs	= safeIs,
lcp@1073
   306
	hazIs	= hazIs,
lcp@1073
   307
	hazEs	= hazEs,
oheimb@2630
   308
	uwrapper     = uwrapper,
oheimb@2630
   309
	swrapper     = swrapper,
oheimb@2630
   310
	haz_netpair  = haz_netpair,
oheimb@2630
   311
	dup_netpair  = dup_netpair}
lcp@1073
   312
  end;
clasohm@0
   313
paulson@1927
   314
fun rev_foldl f (e, l) = foldl f (e, rev l);
paulson@1927
   315
paulson@1927
   316
val op addSIs = rev_foldl addSI;
paulson@1927
   317
val op addSEs = rev_foldl addSE;
paulson@1927
   318
clasohm@0
   319
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   320
lcp@1073
   321
paulson@1800
   322
(*** Hazardous (unsafe) rules ***)
clasohm@0
   323
oheimb@2630
   324
fun addI (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   325
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   326
	  th)=
paulson@2813
   327
  if mem_thm (th, hazIs) then 
paulson@1927
   328
	 (warning ("ignoring duplicate unsafe Intr\n" ^ string_of_thm th);
paulson@1927
   329
	  cs)
paulson@1927
   330
  else
paulson@1927
   331
  let val nI = length hazIs + 1
lcp@1073
   332
      and nE = length hazEs
paulson@1927
   333
  in warn_dup th cs;
paulson@1927
   334
     CS{hazIs	= th::hazIs,
paulson@1927
   335
	haz_netpair = insert (nI,nE) ([th], []) haz_netpair,
paulson@1927
   336
	dup_netpair = insert (nI,nE) (map dup_intr [th], []) dup_netpair,
lcp@1073
   337
	safeIs 	= safeIs, 
lcp@1073
   338
	safeEs	= safeEs,
lcp@1073
   339
	hazEs	= hazEs,
oheimb@2630
   340
	uwrapper      = uwrapper,
oheimb@2630
   341
	swrapper      = swrapper,
lcp@1073
   342
	safe0_netpair = safe0_netpair,
lcp@1073
   343
	safep_netpair = safep_netpair}
lcp@1073
   344
  end;
lcp@1073
   345
oheimb@2630
   346
fun addE (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   347
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   348
	  th) =
paulson@2813
   349
  if mem_thm (th, hazEs) then 
paulson@1927
   350
	 (warning ("ignoring duplicate unsafe Elim\n" ^ string_of_thm th);
paulson@1927
   351
	  cs)
paulson@1927
   352
  else
lcp@1073
   353
  let val nI = length hazIs 
paulson@1927
   354
      and nE = length hazEs + 1
paulson@1927
   355
  in warn_dup th cs;
paulson@1927
   356
     CS{hazEs	= th::hazEs,
paulson@1927
   357
	haz_netpair = insert (nI,nE) ([], [th]) haz_netpair,
paulson@1927
   358
	dup_netpair = insert (nI,nE) ([], map dup_elim [th]) dup_netpair,
lcp@1073
   359
	safeIs	= safeIs, 
lcp@1073
   360
	safeEs	= safeEs,
lcp@1073
   361
	hazIs	= hazIs,
oheimb@2630
   362
	uwrapper      = uwrapper,
oheimb@2630
   363
	swrapper      = swrapper,
lcp@1073
   364
	safe0_netpair = safe0_netpair,
lcp@1073
   365
	safep_netpair = safep_netpair}
lcp@1073
   366
  end;
clasohm@0
   367
paulson@1927
   368
val op addIs = rev_foldl addI;
paulson@1927
   369
val op addEs = rev_foldl addE;
paulson@1927
   370
clasohm@0
   371
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   372
lcp@1073
   373
paulson@1800
   374
(*** Deletion of rules 
paulson@1800
   375
     Working out what to delete, requires repeating much of the code used
paulson@1800
   376
	to insert.
paulson@1927
   377
     Separate functions delSI, etc., are not exported; instead delrules
paulson@2813
   378
        searches in all the lists and chooses the relevant delXX functions.
paulson@1800
   379
***)
paulson@1800
   380
paulson@2813
   381
fun delSI th 
paulson@2813
   382
          (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@2813
   383
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   384
 if mem_thm (th, safeIs) then
paulson@2813
   385
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=0) [th]
paulson@2813
   386
   in CS{safe0_netpair = delete (safe0_rls, []) safe0_netpair,
paulson@2813
   387
	 safep_netpair = delete (safep_rls, []) safep_netpair,
paulson@2813
   388
	 safeIs	= rem_thm (safeIs,th),
paulson@2813
   389
	 safeEs	= safeEs,
paulson@2813
   390
	 hazIs	= hazIs,
paulson@2813
   391
	 hazEs	= hazEs,
paulson@2813
   392
	 uwrapper     = uwrapper,
paulson@2813
   393
	 swrapper     = swrapper,
paulson@2813
   394
	 haz_netpair  = haz_netpair,
paulson@2813
   395
	 dup_netpair  = dup_netpair}
paulson@2813
   396
   end
paulson@2813
   397
 else cs;
paulson@1800
   398
paulson@2813
   399
fun delSE th
paulson@2813
   400
          (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@2813
   401
	            safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   402
 if mem_thm (th, safeEs) then
paulson@2813
   403
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=1) [th]
paulson@2813
   404
   in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
paulson@2813
   405
	 safep_netpair = delete ([], safep_rls) safep_netpair,
paulson@2813
   406
	 safeIs	= safeIs,
paulson@2813
   407
	 safeEs	= rem_thm (safeEs,th),
paulson@2813
   408
	 hazIs	= hazIs,
paulson@2813
   409
	 hazEs	= hazEs,
paulson@2813
   410
	 uwrapper     = uwrapper,
paulson@2813
   411
	 swrapper     = swrapper,
paulson@2813
   412
	 haz_netpair  = haz_netpair,
paulson@2813
   413
	 dup_netpair  = dup_netpair}
paulson@2813
   414
   end
paulson@2813
   415
 else cs;
paulson@1800
   416
paulson@1800
   417
paulson@2813
   418
fun delI th
paulson@2813
   419
         (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@2813
   420
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   421
 if mem_thm (th, hazIs) then
paulson@2813
   422
     CS{haz_netpair = delete ([th], []) haz_netpair,
paulson@1800
   423
	dup_netpair = delete ([dup_intr th], []) dup_netpair,
paulson@1800
   424
	safeIs 	= safeIs, 
paulson@1800
   425
	safeEs	= safeEs,
paulson@2813
   426
	hazIs	= rem_thm (hazIs,th),
paulson@1800
   427
	hazEs	= hazEs,
oheimb@2630
   428
	uwrapper      = uwrapper,
oheimb@2630
   429
	swrapper      = swrapper,
paulson@1800
   430
	safe0_netpair = safe0_netpair,
paulson@2813
   431
	safep_netpair = safep_netpair}
paulson@2813
   432
 else cs;
paulson@1800
   433
paulson@2813
   434
fun delE th
paulson@2813
   435
	 (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@2813
   436
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   437
 if mem_thm (th, hazEs) then
paulson@2813
   438
     CS{haz_netpair = delete ([], [th]) haz_netpair,
paulson@1800
   439
	dup_netpair = delete ([], [dup_elim th]) dup_netpair,
paulson@1800
   440
	safeIs	= safeIs, 
paulson@1800
   441
	safeEs	= safeEs,
paulson@1800
   442
	hazIs	= hazIs,
paulson@2813
   443
	hazEs	= rem_thm (hazEs,th),
oheimb@2630
   444
	uwrapper      = uwrapper,
oheimb@2630
   445
	swrapper      = swrapper,
paulson@1800
   446
	safe0_netpair = safe0_netpair,
paulson@2813
   447
	safep_netpair = safep_netpair}
paulson@2813
   448
 else cs;
paulson@1800
   449
paulson@2813
   450
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
paulson@1800
   451
fun delrule (cs as CS{safeIs, safeEs, hazIs, hazEs, ...}, th) =
paulson@2813
   452
       if mem_thm (th, safeIs) orelse mem_thm (th, safeEs) orelse
paulson@2813
   453
	  mem_thm (th, hazIs)  orelse mem_thm (th, hazEs) 
paulson@2813
   454
       then delSI th (delSE th (delI th (delE th cs)))
paulson@2813
   455
       else (warning ("rule not in claset\n" ^ (string_of_thm th)); 
paulson@2813
   456
	     cs);
paulson@1800
   457
paulson@1800
   458
val op delrules = foldl delrule;
paulson@1800
   459
paulson@1800
   460
oheimb@2630
   461
(*** Setting or modifying the wrapper tacticals ***)
lcp@982
   462
oheimb@2630
   463
(*Set a new uwrapper*)
oheimb@2630
   464
fun (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper,
lcp@1073
   465
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
oheimb@2630
   466
    setWrapper new_uwrapper  =
oheimb@2630
   467
  CS{safeIs	= safeIs,
lcp@1073
   468
     safeEs	= safeEs,
lcp@1073
   469
     hazIs	= hazIs,
lcp@1073
   470
     hazEs	= hazEs,
oheimb@2630
   471
     uwrapper 	= new_uwrapper,
oheimb@2630
   472
     swrapper   = swrapper,
lcp@1073
   473
     safe0_netpair = safe0_netpair,
lcp@1073
   474
     safep_netpair = safep_netpair,
lcp@1073
   475
     haz_netpair = haz_netpair,
lcp@1073
   476
     dup_netpair = dup_netpair};
lcp@982
   477
oheimb@2630
   478
(*Set a new swrapper*)
oheimb@2630
   479
fun (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper,
oheimb@2630
   480
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
oheimb@2630
   481
    setSWrapper new_swrapper  =
oheimb@2630
   482
  CS{safeIs	= safeIs,
oheimb@2630
   483
     safeEs	= safeEs,
oheimb@2630
   484
     hazIs	= hazIs,
oheimb@2630
   485
     hazEs	= hazEs,
oheimb@2630
   486
     uwrapper   = uwrapper,
oheimb@2630
   487
     swrapper   = new_swrapper,
oheimb@2630
   488
     safe0_netpair = safe0_netpair,
oheimb@2630
   489
     safep_netpair = safep_netpair,
oheimb@2630
   490
     haz_netpair = haz_netpair,
oheimb@2630
   491
     dup_netpair = dup_netpair};
lcp@982
   492
oheimb@2630
   493
(*Compose a tactical with the existing uwrapper*)
oheimb@2630
   494
fun cs compWrapper  uwrapper' = cs setWrapper  (uwrapper' o getWrapper cs);
oheimb@2630
   495
oheimb@2630
   496
(*Compose a tactical with the existing swrapper*)
oheimb@2630
   497
fun cs compSWrapper swrapper' = cs setSWrapper (swrapper' o getSWrapper cs);
lcp@982
   498
oheimb@2630
   499
(*compose a safe tactic sequentially before/alternatively after safe_step_tac*)
oheimb@2630
   500
fun cs addSbefore tac1 = cs compSWrapper (fn tac2 => tac1 THEN_MAYBE' tac2);
oheimb@2630
   501
fun cs addSaltern tac2 = cs compSWrapper (fn tac1 => tac1 ORELSE'     tac2);
lcp@982
   502
oheimb@2630
   503
(*compose a tactic sequentially before/alternatively after the step tactic*)
oheimb@2630
   504
fun cs addbefore  tac1 = cs compWrapper  (fn tac2 => tac1 THEN_MAYBE' tac2);
oheimb@2630
   505
fun cs addaltern  tac2 = cs compWrapper  (fn tac1 => tac1 APPEND'     tac2);
lcp@982
   506
paulson@1711
   507
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   508
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   509
  treatment of priority might get muddled up.*)
paulson@1711
   510
fun merge_cs
oheimb@2630
   511
    (cs as CS{safeIs, safeEs, hazIs, hazEs, ...},
paulson@1711
   512
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,...}) =
paulson@1711
   513
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   514
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
oheimb@2630
   515
      val  hazIs' = gen_rems eq_thm ( hazIs2, hazIs)
oheimb@2630
   516
      val  hazEs' = gen_rems eq_thm ( hazEs2, hazEs)
paulson@1711
   517
  in cs addSIs safeIs'
paulson@1711
   518
        addSEs safeEs'
paulson@1711
   519
        addIs  hazIs'
paulson@1711
   520
        addEs  hazEs'
paulson@1711
   521
  end;
paulson@1711
   522
lcp@982
   523
paulson@1800
   524
(**** Simple tactics for theorem proving ****)
clasohm@0
   525
clasohm@0
   526
(*Attack subgoals using safe inferences -- matching, not resolution*)
oheimb@2630
   527
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@2630
   528
  getSWrapper cs (FIRST' [
oheimb@2630
   529
	eq_assume_tac,
oheimb@2630
   530
	eq_mp_tac,
oheimb@2630
   531
	bimatch_from_nets_tac safe0_netpair,
oheimb@2630
   532
	FIRST' hyp_subst_tacs,
oheimb@2630
   533
	bimatch_from_nets_tac safep_netpair]);
clasohm@0
   534
clasohm@0
   535
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@2630
   536
fun safe_tac cs = REPEAT_DETERM_FIRST 
oheimb@2630
   537
	(fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
lcp@747
   538
paulson@3705
   539
paulson@3705
   540
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   541
paulson@3705
   542
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   543
paulson@3705
   544
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   545
  create precisely n subgoals.*)
paulson@3705
   546
fun n_bimatch_from_nets_tac n = 
paulson@3705
   547
    biresolution_from_nets_tac (orderlist o filter (nsubgoalsP n)) true;
paulson@3705
   548
paulson@3705
   549
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   550
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   551
paulson@3705
   552
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   553
fun bimatch2_tac netpair i =
paulson@3705
   554
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   555
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   556
paulson@3705
   557
(*Attack subgoals using safe inferences -- matching, not resolution*)
paulson@3705
   558
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
paulson@3705
   559
  getSWrapper cs (FIRST' [
paulson@3705
   560
	eq_assume_contr_tac,
paulson@3705
   561
	bimatch_from_nets_tac safe0_netpair,
paulson@3705
   562
	FIRST' hyp_subst_tacs,
paulson@3705
   563
	n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   564
        bimatch2_tac safep_netpair]);
paulson@3705
   565
paulson@3705
   566
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   567
paulson@3705
   568
paulson@3705
   569
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   570
paulson@4066
   571
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   572
  proving a subgoal.  *)
lcp@747
   573
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   574
  assume_tac 			  APPEND' 
lcp@747
   575
  contr_tac 			  APPEND' 
lcp@747
   576
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   577
paulson@4066
   578
(*These unsafe steps could generate more subgoals.*)
lcp@747
   579
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   580
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   581
clasohm@0
   582
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   583
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   584
lcp@982
   585
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   586
  biresolve_from_nets_tac haz_netpair;
lcp@681
   587
clasohm@0
   588
(*Single step for the prover.  FAILS unless it makes progress. *)
oheimb@2630
   589
fun step_tac cs i = getWrapper cs 
oheimb@2630
   590
	(K (safe_tac cs) ORELSE' (inst_step_tac cs ORELSE' haz_step_tac cs)) i;
clasohm@0
   591
clasohm@0
   592
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   593
  allows backtracking from "safe" rules to "unsafe" rules here.*)
oheimb@2630
   594
fun slow_step_tac cs i = getWrapper cs 
oheimb@2630
   595
	(K (safe_tac cs) ORELSE' (inst_step_tac cs APPEND' haz_step_tac cs)) i;
clasohm@0
   596
paulson@1800
   597
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   598
clasohm@0
   599
(*Dumb but fast*)
clasohm@0
   600
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   601
clasohm@0
   602
(*Slower but smarter than fast_tac*)
clasohm@0
   603
fun best_tac cs = 
clasohm@0
   604
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   605
clasohm@0
   606
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   607
clasohm@0
   608
fun slow_best_tac cs = 
clasohm@0
   609
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   610
lcp@681
   611
paulson@1800
   612
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   613
val weight_ASTAR = ref 5; 
paulson@1587
   614
paulson@1587
   615
fun astar_tac cs = 
paulson@1587
   616
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   617
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   618
	      (step_tac cs 1));
paulson@1587
   619
paulson@1587
   620
fun slow_astar_tac cs = 
paulson@1587
   621
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   622
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   623
	      (slow_step_tac cs 1));
paulson@1587
   624
paulson@1800
   625
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   626
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   627
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   628
  theorems such as 43. ****)
lcp@681
   629
lcp@747
   630
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   631
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   632
  greater search depth required.*)
lcp@681
   633
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   634
  biresolve_from_nets_tac dup_netpair;
lcp@681
   635
lcp@747
   636
(*Searching to depth m.*)
paulson@3537
   637
fun depth_tac cs m i state = 
lcp@747
   638
  SELECT_GOAL 
oheimb@2630
   639
   (getWrapper cs
oheimb@3204
   640
    (fn i => REPEAT_DETERM1 (COND (has_fewer_prems i) no_tac
oheimb@3204
   641
			     (safe_step_tac cs i)) THEN_ELSE
oheimb@2630
   642
     (DEPTH_SOLVE (depth_tac cs m i),
oheimb@2630
   643
      inst0_step_tac cs i  APPEND
lcp@747
   644
      COND (K(m=0)) no_tac
oheimb@2630
   645
        ((instp_step_tac cs i APPEND dup_step_tac cs i)
oheimb@2630
   646
	 THEN DEPTH_SOLVE (depth_tac cs (m-1) i)))) 1)
paulson@3537
   647
  i state;
lcp@747
   648
paulson@2173
   649
(*Search, with depth bound m.  
paulson@2173
   650
  This is the "entry point", which does safe inferences first.*)
lcp@747
   651
fun safe_depth_tac cs m = 
lcp@681
   652
  SUBGOAL 
lcp@681
   653
    (fn (prem,i) =>
lcp@681
   654
      let val deti =
lcp@681
   655
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   656
	  case term_vars prem of
lcp@681
   657
	      []	=> DETERM 
lcp@681
   658
	    | _::_	=> I
lcp@681
   659
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   660
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   661
      end);
lcp@681
   662
paulson@2868
   663
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   664
berghofe@1724
   665
val claset = ref empty_cs;
berghofe@1724
   666
berghofe@1724
   667
fun AddDs ts = (claset := !claset addDs ts);
berghofe@1724
   668
berghofe@1724
   669
fun AddEs ts = (claset := !claset addEs ts);
berghofe@1724
   670
berghofe@1724
   671
fun AddIs ts = (claset := !claset addIs ts);
berghofe@1724
   672
berghofe@1724
   673
fun AddSDs ts = (claset := !claset addSDs ts);
berghofe@1724
   674
berghofe@1724
   675
fun AddSEs ts = (claset := !claset addSEs ts);
berghofe@1724
   676
berghofe@1724
   677
fun AddSIs ts = (claset := !claset addSIs ts);
berghofe@1724
   678
paulson@1807
   679
fun Delrules ts = (claset := !claset delrules ts);
paulson@1807
   680
paulson@3727
   681
(** The abstraction over the proof state delays the dereferencing **)
paulson@1800
   682
paulson@3727
   683
fun Safe_tac st		= safe_tac (!claset) st;
paulson@1814
   684
paulson@3727
   685
fun Safe_step_tac i st	= safe_step_tac (!claset) i st; 
paulson@3705
   686
paulson@3727
   687
fun Clarify_step_tac i st	= clarify_step_tac (!claset) i st;
paulson@3705
   688
paulson@3727
   689
fun Clarify_tac i st	= clarify_tac (!claset) i st;
paulson@1800
   690
paulson@3727
   691
fun Step_tac i st	= step_tac (!claset) i st; 
berghofe@1724
   692
paulson@3727
   693
fun Fast_tac i st	= fast_tac (!claset) i st; 
paulson@3727
   694
paulson@3727
   695
fun Best_tac i st	= best_tac (!claset) i st; 
paulson@1800
   696
paulson@3727
   697
fun Slow_tac i st	= slow_tac (!claset) i st; 
paulson@2066
   698
paulson@3727
   699
fun Slow_best_tac i st	= slow_best_tac (!claset) i st; 
paulson@2066
   700
paulson@3727
   701
fun Deepen_tac m	= deepen_tac (!claset) m; 
paulson@1800
   702
clasohm@0
   703
end; 
clasohm@0
   704
end;
oheimb@2630
   705
oheimb@2630
   706