src/Sequents/prover.ML
author paulson
Wed Jul 28 13:55:02 1999 +0200 (1999-07-28)
changeset 7122 87b233b31889
parent 7097 5ab37ed3d53c
child 7150 d203e2282789
permissions -rw-r--r--
renamed ...thm_pack... to ...pack...
paulson@7122
     1
(*  Title:      Sequents/prover
paulson@2073
     2
    ID:         $Id$
paulson@2073
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@2073
     4
    Copyright   1992  University of Cambridge
paulson@7097
     5
paulson@7097
     6
Simple classical reasoner for the sequent calculus, based on "theorem packs"
paulson@2073
     7
*)
paulson@2073
     8
paulson@2073
     9
paulson@7097
    10
(*Higher precedence than := facilitates use of references*)
paulson@7097
    11
infix 4 add_safes add_unsafes;
paulson@2073
    12
paulson@7122
    13
structure Cla =
paulson@7122
    14
paulson@7122
    15
struct
paulson@7122
    16
paulson@2073
    17
datatype pack = Pack of thm list * thm list;
paulson@2073
    18
paulson@7122
    19
val trace = ref false;
paulson@7122
    20
paulson@2073
    21
(*A theorem pack has the form  (safe rules, unsafe rules)
paulson@2073
    22
  An unsafe rule is incomplete or introduces variables in subgoals,
paulson@2073
    23
  and is tried only when the safe rules are not applicable.  *)
paulson@2073
    24
paulson@2073
    25
fun less (rl1,rl2) = (nprems_of rl1) < (nprems_of rl2);
paulson@2073
    26
paulson@2073
    27
val empty_pack = Pack([],[]);
paulson@2073
    28
paulson@7097
    29
fun warn_duplicates [] = []
paulson@7097
    30
  | warn_duplicates dups =
paulson@7097
    31
      (warning (String.concat ("Ignoring duplicate theorems:\n"::
paulson@7097
    32
			       map (suffix "\n" o string_of_thm) dups));
paulson@7097
    33
       dups);
paulson@2073
    34
paulson@2073
    35
fun (Pack(safes,unsafes)) add_safes ths   = 
paulson@7097
    36
    let val dups = warn_duplicates (gen_inter eq_thm (ths,safes))
paulson@7097
    37
	val ths' = gen_rems eq_thm (ths,dups)
paulson@7097
    38
    in
paulson@7097
    39
        Pack(sort (make_ord less) (ths'@safes), unsafes)
paulson@7097
    40
    end;
paulson@2073
    41
paulson@2073
    42
fun (Pack(safes,unsafes)) add_unsafes ths = 
paulson@7097
    43
    let val dups = warn_duplicates (gen_inter eq_thm (ths,unsafes))
paulson@7097
    44
	val ths' = gen_rems eq_thm (ths,dups)
paulson@7097
    45
    in
paulson@7097
    46
	Pack(safes, sort (make_ord less) (ths'@unsafes))
paulson@7097
    47
    end;
paulson@7097
    48
paulson@7097
    49
fun merge_pack (Pack(safes,unsafes), Pack(safes',unsafes')) =
paulson@7097
    50
        Pack(sort (make_ord less) (safes@safes'), 
paulson@7097
    51
	     sort (make_ord less) (unsafes@unsafes'));
paulson@2073
    52
paulson@2073
    53
paulson@7097
    54
fun print_pack (Pack(safes,unsafes)) =
paulson@7097
    55
    (writeln "Safe rules:";  print_thms safes;
paulson@7097
    56
     writeln "Unsafe rules:"; print_thms unsafes);
paulson@7097
    57
paulson@2073
    58
(*Returns the list of all formulas in the sequent*)
paulson@7097
    59
fun forms_of_seq (Const("SeqO'",_) $ P $ u) = P :: forms_of_seq u
paulson@2073
    60
  | forms_of_seq (H $ u) = forms_of_seq u
paulson@2073
    61
  | forms_of_seq _ = [];
paulson@2073
    62
paulson@2073
    63
(*Tests whether two sequences (left or right sides) could be resolved.
paulson@2073
    64
  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
paulson@2073
    65
  Assumes each formula in seqc is surrounded by sequence variables
paulson@2073
    66
  -- checks that each concl formula looks like some subgoal formula.
paulson@2073
    67
  It SHOULD check order as well, using recursion rather than forall/exists*)
paulson@2073
    68
fun could_res (seqp,seqc) =
paulson@2073
    69
      forall (fn Qc => exists (fn Qp => could_unify (Qp,Qc)) 
paulson@2073
    70
                              (forms_of_seq seqp))
paulson@2073
    71
             (forms_of_seq seqc);
paulson@2073
    72
paulson@2073
    73
paulson@2073
    74
(*Tests whether two sequents or pairs of sequents could be resolved*)
paulson@2073
    75
fun could_resolve_seq (prem,conc) =
paulson@2073
    76
  case (prem,conc) of
paulson@2073
    77
      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
paulson@2073
    78
       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
paulson@2073
    79
	  could_res (leftp,leftc) andalso could_res (rightp,rightc)
paulson@2073
    80
    | (_ $ Abs(_,_,leftp) $ rightp,
paulson@2073
    81
       _ $ Abs(_,_,leftc) $ rightc) =>
paulson@2073
    82
	  could_res (leftp,leftc)  andalso  could_unify (rightp,rightc)
paulson@2073
    83
    | _ => false;
paulson@2073
    84
paulson@2073
    85
paulson@2073
    86
(*Like filt_resolve_tac, using could_resolve_seq
paulson@2073
    87
  Much faster than resolve_tac when there are many rules.
paulson@2073
    88
  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
paulson@2073
    89
fun filseq_resolve_tac rules maxr = SUBGOAL(fn (prem,i) =>
paulson@2073
    90
  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
paulson@2073
    91
  in  if length rls > maxr  then  no_tac
paulson@2073
    92
	  else (*((rtac derelict 1 THEN rtac impl 1
paulson@2073
    93
		 THEN (rtac identity 2 ORELSE rtac ll_mp 2)
paulson@2073
    94
		 THEN rtac context1 1)
paulson@2073
    95
		 ORELSE *) resolve_tac rls i
paulson@2073
    96
  end);
paulson@2073
    97
paulson@2073
    98
paulson@2073
    99
(*Predicate: does the rule have n premises? *)
paulson@2073
   100
fun has_prems n rule =  (nprems_of rule = n);
paulson@2073
   101
paulson@2073
   102
(*Continuation-style tactical for resolution.
paulson@2073
   103
  The list of rules is partitioned into 0, 1, 2 premises.
paulson@2073
   104
  The resulting tactic, gtac, tries to resolve with rules.
paulson@2073
   105
  If successful, it recursively applies nextac to the new subgoals only.
paulson@2073
   106
  Else fails.  (Treatment of goals due to Ph. de Groote) 
paulson@2073
   107
  Bind (RESOLVE_THEN rules) to a variable: it preprocesses the rules. *)
paulson@2073
   108
paulson@2073
   109
(*Takes rule lists separated in to 0, 1, 2, >2 premises.
paulson@2073
   110
  The abstraction over state prevents needless divergence in recursion.
paulson@2073
   111
  The 9999 should be a parameter, to delay treatment of flexible goals. *)
paulson@2073
   112
paulson@2073
   113
fun RESOLVE_THEN rules =
paulson@2073
   114
  let val [rls0,rls1,rls2] = partition_list has_prems 0 2 rules;
paulson@3538
   115
      fun tac nextac i state = state |>
paulson@3538
   116
	     (filseq_resolve_tac rls0 9999 i 
paulson@3538
   117
	      ORELSE
paulson@3538
   118
	      (DETERM(filseq_resolve_tac rls1 9999 i) THEN  TRY(nextac i))
paulson@3538
   119
	      ORELSE
paulson@3538
   120
	      (DETERM(filseq_resolve_tac rls2 9999 i) THEN  TRY(nextac(i+1))
paulson@3538
   121
					    THEN  TRY(nextac i)))
paulson@2073
   122
  in  tac  end;
paulson@2073
   123
paulson@2073
   124
paulson@2073
   125
paulson@2073
   126
(*repeated resolution applied to the designated goal*)
paulson@2073
   127
fun reresolve_tac rules = 
paulson@2073
   128
  let val restac = RESOLVE_THEN rules;  (*preprocessing done now*)
paulson@2073
   129
      fun gtac i = restac gtac i
paulson@2073
   130
  in  gtac  end; 
paulson@2073
   131
paulson@2073
   132
(*tries the safe rules repeatedly before the unsafe rules. *)
paulson@2073
   133
fun repeat_goal_tac (Pack(safes,unsafes)) = 
paulson@2073
   134
  let val restac  =    RESOLVE_THEN safes
paulson@2073
   135
      and lastrestac = RESOLVE_THEN unsafes;
paulson@6054
   136
      fun gtac i = restac gtac i  
paulson@7122
   137
	           ORELSE  (if !trace then
paulson@7122
   138
				(print_tac "" THEN lastrestac gtac i)
paulson@7122
   139
			    else lastrestac gtac i)
paulson@2073
   140
  in  gtac  end; 
paulson@2073
   141
paulson@2073
   142
paulson@2073
   143
(*Tries safe rules only*)
paulson@7097
   144
fun safe_tac (Pack(safes,unsafes)) = reresolve_tac safes;
paulson@7097
   145
paulson@7097
   146
val safe_goal_tac = safe_tac;   (*backwards compatibility*)
paulson@2073
   147
paulson@2073
   148
(*Tries a safe rule or else a unsafe rule.  Single-step for tracing. *)
paulson@7122
   149
fun step_tac (pack as Pack(safes,unsafes)) =
paulson@7122
   150
    safe_tac pack  ORELSE'
paulson@2073
   151
    filseq_resolve_tac unsafes 9999;
paulson@2073
   152
paulson@2073
   153
paulson@2073
   154
(* Tactic for reducing a goal, using Predicate Calculus rules.
paulson@2073
   155
   A decision procedure for Propositional Calculus, it is incomplete
paulson@2073
   156
   for Predicate-Calculus because of allL_thin and exR_thin.  
paulson@2073
   157
   Fails if it can do nothing.      *)
paulson@7122
   158
fun pc_tac pack = SELECT_GOAL (DEPTH_SOLVE (repeat_goal_tac pack 1));
paulson@2073
   159
paulson@2073
   160
paulson@2073
   161
(*The following two tactics are analogous to those provided by 
paulson@2073
   162
  Provers/classical.  In fact, pc_tac is usually FASTER than fast_tac!*)
paulson@7122
   163
fun fast_tac pack =
paulson@7122
   164
  SELECT_GOAL (DEPTH_SOLVE (step_tac pack 1));
paulson@2073
   165
paulson@7122
   166
fun best_tac pack  = 
paulson@2073
   167
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, size_of_thm) 
paulson@7122
   168
	       (step_tac pack 1));
paulson@2073
   169
paulson@2073
   170
paulson@2073
   171
paulson@7097
   172
structure ProverArgs =
paulson@7097
   173
  struct
paulson@7097
   174
  val name = "Sequents/prover";
paulson@7097
   175
  type T = pack ref;
paulson@7097
   176
  val empty = ref empty_pack
paulson@7097
   177
  fun copy (ref pack) = ref pack;
paulson@7097
   178
  val prep_ext = copy;
paulson@7097
   179
  fun merge (ref pack1, ref pack2) = ref (merge_pack (pack1, pack2));
paulson@7097
   180
  fun print _ (ref pack) = print_pack pack;
paulson@7097
   181
  end;
paulson@2073
   182
paulson@7097
   183
structure ProverData = TheoryDataFun(ProverArgs);
paulson@2073
   184
paulson@7097
   185
val prover_setup = [ProverData.init];
paulson@2073
   186
paulson@7122
   187
val print_pack = ProverData.print;
paulson@7122
   188
val pack_ref_of_sg = ProverData.get_sg;
paulson@7122
   189
val pack_ref_of = ProverData.get;
paulson@2073
   190
paulson@7122
   191
(* access global pack *)
paulson@2073
   192
paulson@7122
   193
val pack_of_sg = ! o pack_ref_of_sg;
paulson@7122
   194
val pack_of = pack_of_sg o sign_of;
paulson@2073
   195
paulson@7122
   196
val pack = pack_of o Context.the_context;
paulson@7122
   197
val pack_ref = pack_ref_of_sg o sign_of o Context.the_context;
paulson@2073
   198
paulson@7097
   199
paulson@7122
   200
(* change global pack *)
paulson@7122
   201
paulson@7122
   202
fun change_pack f x = pack_ref () := (f (pack (), x));
paulson@2073
   203
paulson@7122
   204
val Add_safes = change_pack (op add_safes);
paulson@7122
   205
val Add_unsafes = change_pack (op add_unsafes);
paulson@7122
   206
paulson@2073
   207
paulson@7122
   208
fun Fast_tac st = fast_tac (pack()) st;
paulson@7122
   209
fun Step_tac st = step_tac (pack()) st;
paulson@7122
   210
fun Safe_tac st = safe_tac (pack()) st;
paulson@7122
   211
fun Pc_tac st   = pc_tac (pack()) st;
paulson@2073
   212
paulson@7122
   213
end;
paulson@7122
   214
paulson@7122
   215
paulson@7122
   216
open Cla;