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