src/Sequents/prover.ML
author paulson
Wed Oct 09 13:32:33 1996 +0200 (1996-10-09)
changeset 2073 fb0655539d05
child 3538 ed9de44032e0
permissions -rw-r--r--
New unified treatment of sequent calculi by Sara Kalvala
combines the old LK and Modal with the new ILL (Int. Linear Logic)
paulson@2073
     1
(*  Title:      LK/LK.ML
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@2073
     5
*)
paulson@2073
     6
paulson@2073
     7
paulson@2073
     8
(**** Theorem Packs ****)
paulson@2073
     9
paulson@2073
    10
(* based largely on LK *)
paulson@2073
    11
paulson@2073
    12
datatype pack = Pack of thm list * thm list;
paulson@2073
    13
paulson@2073
    14
(*A theorem pack has the form  (safe rules, unsafe rules)
paulson@2073
    15
  An unsafe rule is incomplete or introduces variables in subgoals,
paulson@2073
    16
  and is tried only when the safe rules are not applicable.  *)
paulson@2073
    17
paulson@2073
    18
fun less (rl1,rl2) = (nprems_of rl1) < (nprems_of rl2);
paulson@2073
    19
paulson@2073
    20
val empty_pack = Pack([],[]);
paulson@2073
    21
paulson@2073
    22
infix 4 add_safes add_unsafes;
paulson@2073
    23
paulson@2073
    24
fun (Pack(safes,unsafes)) add_safes ths   = 
paulson@2073
    25
    Pack(sort less (ths@safes), unsafes);
paulson@2073
    26
paulson@2073
    27
fun (Pack(safes,unsafes)) add_unsafes ths = 
paulson@2073
    28
    Pack(safes, sort less (ths@unsafes));
paulson@2073
    29
paulson@2073
    30
paulson@2073
    31
(*Returns the list of all formulas in the sequent*)
paulson@2073
    32
fun forms_of_seq (Const("SeqO'",_) $ P $ u) = P :: forms_of_seq u
paulson@2073
    33
  | forms_of_seq (H $ u) = forms_of_seq u
paulson@2073
    34
  | forms_of_seq _ = [];
paulson@2073
    35
paulson@2073
    36
(*Tests whether two sequences (left or right sides) could be resolved.
paulson@2073
    37
  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
paulson@2073
    38
  Assumes each formula in seqc is surrounded by sequence variables
paulson@2073
    39
  -- checks that each concl formula looks like some subgoal formula.
paulson@2073
    40
  It SHOULD check order as well, using recursion rather than forall/exists*)
paulson@2073
    41
fun could_res (seqp,seqc) =
paulson@2073
    42
      forall (fn Qc => exists (fn Qp => could_unify (Qp,Qc)) 
paulson@2073
    43
                              (forms_of_seq seqp))
paulson@2073
    44
             (forms_of_seq seqc);
paulson@2073
    45
paulson@2073
    46
paulson@2073
    47
(*Tests whether two sequents or pairs of sequents could be resolved*)
paulson@2073
    48
fun could_resolve_seq (prem,conc) =
paulson@2073
    49
  case (prem,conc) of
paulson@2073
    50
      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
paulson@2073
    51
       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
paulson@2073
    52
	  could_res (leftp,leftc) andalso could_res (rightp,rightc)
paulson@2073
    53
    | (_ $ Abs(_,_,leftp) $ rightp,
paulson@2073
    54
       _ $ Abs(_,_,leftc) $ rightc) =>
paulson@2073
    55
	  could_res (leftp,leftc)  andalso  could_unify (rightp,rightc)
paulson@2073
    56
    | _ => false;
paulson@2073
    57
paulson@2073
    58
paulson@2073
    59
(*Like filt_resolve_tac, using could_resolve_seq
paulson@2073
    60
  Much faster than resolve_tac when there are many rules.
paulson@2073
    61
  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
paulson@2073
    62
fun filseq_resolve_tac rules maxr = SUBGOAL(fn (prem,i) =>
paulson@2073
    63
  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
paulson@2073
    64
  in  if length rls > maxr  then  no_tac
paulson@2073
    65
	  else (*((rtac derelict 1 THEN rtac impl 1
paulson@2073
    66
		 THEN (rtac identity 2 ORELSE rtac ll_mp 2)
paulson@2073
    67
		 THEN rtac context1 1)
paulson@2073
    68
		 ORELSE *) resolve_tac rls i
paulson@2073
    69
  end);
paulson@2073
    70
paulson@2073
    71
paulson@2073
    72
(*Predicate: does the rule have n premises? *)
paulson@2073
    73
fun has_prems n rule =  (nprems_of rule = n);
paulson@2073
    74
paulson@2073
    75
(*Continuation-style tactical for resolution.
paulson@2073
    76
  The list of rules is partitioned into 0, 1, 2 premises.
paulson@2073
    77
  The resulting tactic, gtac, tries to resolve with rules.
paulson@2073
    78
  If successful, it recursively applies nextac to the new subgoals only.
paulson@2073
    79
  Else fails.  (Treatment of goals due to Ph. de Groote) 
paulson@2073
    80
  Bind (RESOLVE_THEN rules) to a variable: it preprocesses the rules. *)
paulson@2073
    81
paulson@2073
    82
(*Takes rule lists separated in to 0, 1, 2, >2 premises.
paulson@2073
    83
  The abstraction over state prevents needless divergence in recursion.
paulson@2073
    84
  The 9999 should be a parameter, to delay treatment of flexible goals. *)
paulson@2073
    85
paulson@2073
    86
fun RESOLVE_THEN rules =
paulson@2073
    87
  let val [rls0,rls1,rls2] = partition_list has_prems 0 2 rules;
paulson@2073
    88
      fun tac nextac i = STATE (fn state =>  
paulson@2073
    89
	  filseq_resolve_tac rls0 9999 i 
paulson@2073
    90
	  ORELSE
paulson@2073
    91
	  (DETERM(filseq_resolve_tac rls1 9999 i) THEN  TRY(nextac i))
paulson@2073
    92
	  ORELSE
paulson@2073
    93
	  (DETERM(filseq_resolve_tac rls2 9999 i) THEN  TRY(nextac(i+1))
paulson@2073
    94
					THEN  TRY(nextac i)) )
paulson@2073
    95
  in  tac  end;
paulson@2073
    96
paulson@2073
    97
paulson@2073
    98
paulson@2073
    99
(*repeated resolution applied to the designated goal*)
paulson@2073
   100
fun reresolve_tac rules = 
paulson@2073
   101
  let val restac = RESOLVE_THEN rules;  (*preprocessing done now*)
paulson@2073
   102
      fun gtac i = restac gtac i
paulson@2073
   103
  in  gtac  end; 
paulson@2073
   104
paulson@2073
   105
(*tries the safe rules repeatedly before the unsafe rules. *)
paulson@2073
   106
fun repeat_goal_tac (Pack(safes,unsafes)) = 
paulson@2073
   107
  let val restac  =    RESOLVE_THEN safes
paulson@2073
   108
      and lastrestac = RESOLVE_THEN unsafes;
paulson@2073
   109
      fun gtac i = restac gtac i  ORELSE  (print_tac THEN lastrestac gtac i)
paulson@2073
   110
  in  gtac  end; 
paulson@2073
   111
paulson@2073
   112
paulson@2073
   113
(*Tries safe rules only*)
paulson@2073
   114
fun safe_goal_tac (Pack(safes,unsafes)) = reresolve_tac safes;
paulson@2073
   115
paulson@2073
   116
(*Tries a safe rule or else a unsafe rule.  Single-step for tracing. *)
paulson@2073
   117
fun step_tac (thm_pack as Pack(safes,unsafes)) =
paulson@2073
   118
    safe_goal_tac thm_pack  ORELSE'
paulson@2073
   119
    filseq_resolve_tac unsafes 9999;
paulson@2073
   120
paulson@2073
   121
paulson@2073
   122
(* Tactic for reducing a goal, using Predicate Calculus rules.
paulson@2073
   123
   A decision procedure for Propositional Calculus, it is incomplete
paulson@2073
   124
   for Predicate-Calculus because of allL_thin and exR_thin.  
paulson@2073
   125
   Fails if it can do nothing.      *)
paulson@2073
   126
fun pc_tac thm_pack = SELECT_GOAL (DEPTH_SOLVE (repeat_goal_tac thm_pack 1));
paulson@2073
   127
paulson@2073
   128
paulson@2073
   129
(*The following two tactics are analogous to those provided by 
paulson@2073
   130
  Provers/classical.  In fact, pc_tac is usually FASTER than fast_tac!*)
paulson@2073
   131
fun fast_tac thm_pack =
paulson@2073
   132
  SELECT_GOAL (DEPTH_SOLVE (step_tac thm_pack 1));
paulson@2073
   133
paulson@2073
   134
fun best_tac thm_pack  = 
paulson@2073
   135
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, size_of_thm) 
paulson@2073
   136
	       (step_tac thm_pack 1));
paulson@2073
   137
paulson@2073
   138
paulson@2073
   139
paulson@2073
   140
signature MODAL_PROVER_RULE =
paulson@2073
   141
sig
paulson@2073
   142
    val rewrite_rls      : thm list
paulson@2073
   143
    val safe_rls         : thm list
paulson@2073
   144
    val unsafe_rls       : thm list
paulson@2073
   145
    val bound_rls        : thm list
paulson@2073
   146
    val aside_rls        : thm list
paulson@2073
   147
end;
paulson@2073
   148
paulson@2073
   149
signature MODAL_PROVER = 
paulson@2073
   150
sig
paulson@2073
   151
    val rule_tac   : thm list -> int ->tactic
paulson@2073
   152
    val step_tac   : int -> tactic
paulson@2073
   153
    val solven_tac : int -> int -> tactic
paulson@2073
   154
    val solve_tac  : int -> tactic
paulson@2073
   155
end;
paulson@2073
   156
paulson@2073
   157
functor Modal_ProverFun (Modal_Rule: MODAL_PROVER_RULE) : MODAL_PROVER = 
paulson@2073
   158
struct
paulson@2073
   159
local open Modal_Rule
paulson@2073
   160
in 
paulson@2073
   161
paulson@2073
   162
(*Returns the list of all formulas in the sequent*)
paulson@2073
   163
fun forms_of_seq (Const("SeqO",_) $ P $ u) = P :: forms_of_seq u
paulson@2073
   164
  | forms_of_seq (H $ u) = forms_of_seq u
paulson@2073
   165
  | forms_of_seq _ = [];
paulson@2073
   166
paulson@2073
   167
(*Tests whether two sequences (left or right sides) could be resolved.
paulson@2073
   168
  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
paulson@2073
   169
  Assumes each formula in seqc is surrounded by sequence variables
paulson@2073
   170
  -- checks that each concl formula looks like some subgoal formula.*)
paulson@2073
   171
fun could_res (seqp,seqc) =
paulson@2073
   172
      forall (fn Qc => exists (fn Qp => could_unify (Qp,Qc)) 
paulson@2073
   173
                              (forms_of_seq seqp))
paulson@2073
   174
             (forms_of_seq seqc);
paulson@2073
   175
paulson@2073
   176
(*Tests whether two sequents G|-H could be resolved, comparing each side.*)
paulson@2073
   177
fun could_resolve_seq (prem,conc) =
paulson@2073
   178
  case (prem,conc) of
paulson@2073
   179
      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
paulson@2073
   180
       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
paulson@2073
   181
          could_res (leftp,leftc)  andalso  could_res (rightp,rightc)
paulson@2073
   182
    | _ => false;
paulson@2073
   183
paulson@2073
   184
(*Like filt_resolve_tac, using could_resolve_seq
paulson@2073
   185
  Much faster than resolve_tac when there are many rules.
paulson@2073
   186
  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
paulson@2073
   187
fun filseq_resolve_tac rules maxr = SUBGOAL(fn (prem,i) =>
paulson@2073
   188
  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
paulson@2073
   189
  in  if length rls > maxr  then  no_tac  else resolve_tac rls i
paulson@2073
   190
  end);
paulson@2073
   191
paulson@2073
   192
fun fresolve_tac rls n = filseq_resolve_tac rls 999 n;
paulson@2073
   193
paulson@2073
   194
(* NB No back tracking possible with aside rules *)
paulson@2073
   195
paulson@2073
   196
fun aside_tac n = DETERM(REPEAT (filt_resolve_tac aside_rls 999 n));
paulson@2073
   197
fun rule_tac rls n = fresolve_tac rls n THEN aside_tac n;
paulson@2073
   198
paulson@2073
   199
val fres_safe_tac = fresolve_tac safe_rls;
paulson@2073
   200
val fres_unsafe_tac = fresolve_tac unsafe_rls THEN' aside_tac;
paulson@2073
   201
val fres_bound_tac = fresolve_tac bound_rls;
paulson@2073
   202
paulson@2073
   203
fun UPTOGOAL n tf = let fun tac i = if i<n then all_tac
paulson@2073
   204
                                    else tf(i) THEN tac(i-1)
paulson@2073
   205
                    in STATE(fn state=> tac(nprems_of state)) end;
paulson@2073
   206
paulson@2073
   207
(* Depth first search bounded by d *)
paulson@2073
   208
fun solven_tac d n = STATE (fn state =>
paulson@2073
   209
        if d<0 then no_tac
paulson@2073
   210
        else if (nprems_of state = 0) then all_tac 
paulson@2073
   211
        else (DETERM(fres_safe_tac n) THEN UPTOGOAL n (solven_tac d)) ORELSE
paulson@2073
   212
                 ((fres_unsafe_tac n  THEN UPTOGOAL n (solven_tac d)) APPEND
paulson@2073
   213
                   (fres_bound_tac n  THEN UPTOGOAL n (solven_tac (d-1)))));
paulson@2073
   214
paulson@2073
   215
fun solve_tac d = rewrite_goals_tac rewrite_rls THEN solven_tac d 1;
paulson@2073
   216
paulson@2073
   217
fun step_tac n = STATE (fn state =>  
paulson@2073
   218
      if (nprems_of state = 0) then all_tac 
paulson@2073
   219
      else (DETERM(fres_safe_tac n)) ORELSE 
paulson@2073
   220
           (fres_unsafe_tac n APPEND fres_bound_tac n));
paulson@2073
   221
paulson@2073
   222
end;
paulson@2073
   223
end;