src/FOLP/classical.ML
author paulson
Fri Feb 16 18:00:47 1996 +0100 (1996-02-16)
changeset 1512 ce37c64244c0
parent 1459 d12da312eff4
child 4440 9ed4098074bc
permissions -rw-r--r--
Elimination of fully-functorial style.
Type tactic changed to a type abbrevation (from a datatype).
Constructor tactic and function apply deleted.
clasohm@1459
     1
(*  Title:      FOLP/classical
clasohm@0
     2
    ID:         $Id$
clasohm@1459
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1992  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Like Provers/classical but modified because match_tac is unsuitable for
clasohm@0
     7
proof objects.
clasohm@0
     8
clasohm@0
     9
Theorem prover for classical reasoning, including predicate calculus, set
clasohm@0
    10
theory, etc.
clasohm@0
    11
clasohm@0
    12
Rules must be classified as intr, elim, safe, hazardous.
clasohm@0
    13
clasohm@0
    14
A rule is unsafe unless it can be applied blindly without harmful results.
clasohm@0
    15
For a rule to be safe, its premises and conclusion should be logically
clasohm@0
    16
equivalent.  There should be no variables in the premises that are not in
clasohm@0
    17
the conclusion.
clasohm@0
    18
*)
clasohm@0
    19
clasohm@0
    20
signature CLASSICAL_DATA =
clasohm@0
    21
  sig
clasohm@1459
    22
  val mp: thm                   (* [| P-->Q;  P |] ==> Q *)
clasohm@1459
    23
  val not_elim: thm             (* [| ~P;  P |] ==> R *)
clasohm@1459
    24
  val swap: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
clasohm@1459
    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*)
clasohm@0
    30
infix 4 addSIs addSEs addSDs addIs addEs addDs;
clasohm@0
    31
clasohm@0
    32
clasohm@0
    33
signature CLASSICAL =
clasohm@0
    34
  sig
clasohm@0
    35
  type claset
clasohm@0
    36
  val empty_cs: claset
clasohm@0
    37
  val addDs : claset * thm list -> claset
clasohm@0
    38
  val addEs : claset * thm list -> claset
clasohm@0
    39
  val addIs : claset * thm list -> claset
clasohm@0
    40
  val addSDs: claset * thm list -> claset
clasohm@0
    41
  val addSEs: claset * thm list -> claset
clasohm@0
    42
  val addSIs: claset * thm list -> claset
clasohm@0
    43
  val print_cs: claset -> unit
clasohm@0
    44
  val rep_claset: claset -> 
clasohm@0
    45
      {safeIs: thm list, safeEs: thm list, hazIs: thm list, hazEs: thm list, 
clasohm@0
    46
       safe0_brls:(bool*thm)list, safep_brls: (bool*thm)list,
clasohm@0
    47
       haz_brls: (bool*thm)list}
clasohm@0
    48
  val best_tac : claset -> int -> tactic
clasohm@0
    49
  val contr_tac : int -> tactic
clasohm@0
    50
  val fast_tac : claset -> int -> tactic
clasohm@0
    51
  val inst_step_tac : int -> tactic
clasohm@0
    52
  val joinrules : thm list * thm list -> (bool * thm) list
clasohm@0
    53
  val mp_tac: int -> tactic
clasohm@0
    54
  val safe_tac : claset -> tactic
clasohm@0
    55
  val safe_step_tac : claset -> int -> tactic
clasohm@0
    56
  val slow_step_tac : claset -> int -> tactic
clasohm@0
    57
  val step_tac : claset -> int -> tactic
clasohm@0
    58
  val swapify : thm list -> thm list
clasohm@0
    59
  val swap_res_tac : thm list -> int -> tactic
clasohm@0
    60
  val uniq_mp_tac: int -> tactic
clasohm@0
    61
  end;
clasohm@0
    62
clasohm@0
    63
clasohm@0
    64
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
    65
struct
clasohm@0
    66
clasohm@0
    67
local open Data in
clasohm@0
    68
clasohm@0
    69
(** Useful tactics for classical reasoning **)
clasohm@0
    70
clasohm@0
    71
val imp_elim = make_elim mp;
clasohm@0
    72
clasohm@0
    73
(*Solve goal that assumes both P and ~P. *)
clasohm@1459
    74
val contr_tac = etac not_elim THEN'  assume_tac;
clasohm@0
    75
clasohm@0
    76
(*Finds P-->Q and P in the assumptions, replaces implication by Q *)
clasohm@0
    77
fun mp_tac i = eresolve_tac ([not_elim,imp_elim]) i  THEN  assume_tac i;
clasohm@0
    78
clasohm@0
    79
(*Like mp_tac but instantiates no variables*)
clasohm@0
    80
fun uniq_mp_tac i = ematch_tac ([not_elim,imp_elim]) i  THEN  uniq_assume_tac i;
clasohm@0
    81
clasohm@0
    82
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
    83
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
    84
clasohm@0
    85
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
    86
  trying rules in order. *)
clasohm@0
    87
fun swap_res_tac rls = 
clasohm@0
    88
    let fun tacf rl = rtac rl ORELSE' etac (rl RSN (2,swap))
clasohm@0
    89
    in  assume_tac ORELSE' contr_tac ORELSE' FIRST' (map tacf rls)
clasohm@0
    90
    end;
clasohm@0
    91
clasohm@0
    92
clasohm@0
    93
(*** Classical rule sets ***)
clasohm@0
    94
clasohm@0
    95
datatype claset =
clasohm@0
    96
 CS of {safeIs: thm list,
clasohm@1459
    97
        safeEs: thm list,
clasohm@1459
    98
        hazIs: thm list,
clasohm@1459
    99
        hazEs: thm list,
clasohm@1459
   100
        (*the following are computed from the above*)
clasohm@1459
   101
        safe0_brls: (bool*thm)list,
clasohm@1459
   102
        safep_brls: (bool*thm)list,
clasohm@1459
   103
        haz_brls: (bool*thm)list};
clasohm@0
   104
  
clasohm@0
   105
fun rep_claset (CS x) = x;
clasohm@0
   106
clasohm@0
   107
(*For use with biresolve_tac.  Combines intrs with swap to catch negated
clasohm@0
   108
  assumptions.  Also pairs elims with true. *)
clasohm@0
   109
fun joinrules (intrs,elims) =  
clasohm@0
   110
  map (pair true) (elims @ swapify intrs)  @  map (pair false) intrs;
clasohm@0
   111
clasohm@0
   112
(*Note that allE precedes exI in haz_brls*)
clasohm@0
   113
fun make_cs {safeIs,safeEs,hazIs,hazEs} =
clasohm@0
   114
  let val (safe0_brls, safep_brls) = (*0 subgoals vs 1 or more*)
clasohm@0
   115
          partition (apl(0,op=) o subgoals_of_brl) 
clasohm@0
   116
             (sort lessb (joinrules(safeIs, safeEs)))
clasohm@0
   117
  in CS{safeIs=safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=hazEs,
clasohm@1459
   118
        safe0_brls=safe0_brls, safep_brls=safep_brls,
clasohm@1459
   119
        haz_brls = sort lessb (joinrules(hazIs, hazEs))}
clasohm@0
   120
  end;
clasohm@0
   121
clasohm@0
   122
(*** Manipulation of clasets ***)
clasohm@0
   123
clasohm@0
   124
val empty_cs = make_cs{safeIs=[], safeEs=[], hazIs=[], hazEs=[]};
clasohm@0
   125
clasohm@0
   126
fun print_cs (CS{safeIs,safeEs,hazIs,hazEs,...}) =
clasohm@0
   127
 (writeln"Introduction rules";  prths hazIs;
clasohm@0
   128
  writeln"Safe introduction rules";  prths safeIs;
clasohm@0
   129
  writeln"Elimination rules";  prths hazEs;
clasohm@0
   130
  writeln"Safe elimination rules";  prths safeEs;
clasohm@0
   131
  ());
clasohm@0
   132
clasohm@0
   133
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addSIs ths =
clasohm@0
   134
  make_cs {safeIs=ths@safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=hazEs};
clasohm@0
   135
clasohm@0
   136
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addSEs ths =
clasohm@0
   137
  make_cs {safeIs=safeIs, safeEs=ths@safeEs, hazIs=hazIs, hazEs=hazEs};
clasohm@0
   138
clasohm@0
   139
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   140
clasohm@0
   141
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addIs ths =
clasohm@0
   142
  make_cs {safeIs=safeIs, safeEs=safeEs, hazIs=ths@hazIs, hazEs=hazEs};
clasohm@0
   143
clasohm@0
   144
fun (CS{safeIs,safeEs,hazIs,hazEs,...}) addEs ths =
clasohm@0
   145
  make_cs {safeIs=safeIs, safeEs=safeEs, hazIs=hazIs, hazEs=ths@hazEs};
clasohm@0
   146
clasohm@0
   147
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   148
clasohm@0
   149
(*** Simple tactics for theorem proving ***)
clasohm@0
   150
clasohm@0
   151
(*Attack subgoals using safe inferences*)
clasohm@0
   152
fun safe_step_tac (CS{safe0_brls,safep_brls,...}) = 
clasohm@0
   153
  FIRST' [uniq_assume_tac,
clasohm@1459
   154
          uniq_mp_tac,
clasohm@1459
   155
          biresolve_tac safe0_brls,
clasohm@1459
   156
          FIRST' hyp_subst_tacs,
clasohm@1459
   157
          biresolve_tac safep_brls] ;
clasohm@0
   158
clasohm@0
   159
(*Repeatedly attack subgoals using safe inferences*)
clasohm@0
   160
fun safe_tac cs = DETERM (REPEAT_FIRST (safe_step_tac cs));
clasohm@0
   161
clasohm@0
   162
(*These steps could instantiate variables and are therefore unsafe.*)
clasohm@0
   163
val inst_step_tac = assume_tac APPEND' contr_tac;
clasohm@0
   164
clasohm@0
   165
(*Single step for the prover.  FAILS unless it makes progress. *)
clasohm@0
   166
fun step_tac (cs as (CS{haz_brls,...})) i = 
clasohm@0
   167
  FIRST [safe_tac cs,
clasohm@0
   168
         inst_step_tac i,
clasohm@0
   169
         biresolve_tac haz_brls i];
clasohm@0
   170
clasohm@0
   171
(*** The following tactics all fail unless they solve one goal ***)
clasohm@0
   172
clasohm@0
   173
(*Dumb but fast*)
clasohm@0
   174
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   175
clasohm@0
   176
(*Slower but smarter than fast_tac*)
clasohm@0
   177
fun best_tac cs = 
clasohm@0
   178
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   179
clasohm@0
   180
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   181
  allows backtracking from "safe" rules to "unsafe" rules here.*)
clasohm@0
   182
fun slow_step_tac (cs as (CS{haz_brls,...})) i = 
clasohm@0
   183
    safe_tac cs ORELSE (assume_tac i APPEND biresolve_tac haz_brls i);
clasohm@0
   184
clasohm@0
   185
end; 
clasohm@0
   186
end;