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