src/Sequents/prover.ML
changeset 2073 fb0655539d05
child 3538 ed9de44032e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Sequents/prover.ML	Wed Oct 09 13:32:33 1996 +0200
     1.3 @@ -0,0 +1,223 @@
     1.4 +(*  Title:      LK/LK.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1992  University of Cambridge
     1.8 +*)
     1.9 +
    1.10 +
    1.11 +(**** Theorem Packs ****)
    1.12 +
    1.13 +(* based largely on LK *)
    1.14 +
    1.15 +datatype pack = Pack of thm list * thm list;
    1.16 +
    1.17 +(*A theorem pack has the form  (safe rules, unsafe rules)
    1.18 +  An unsafe rule is incomplete or introduces variables in subgoals,
    1.19 +  and is tried only when the safe rules are not applicable.  *)
    1.20 +
    1.21 +fun less (rl1,rl2) = (nprems_of rl1) < (nprems_of rl2);
    1.22 +
    1.23 +val empty_pack = Pack([],[]);
    1.24 +
    1.25 +infix 4 add_safes add_unsafes;
    1.26 +
    1.27 +fun (Pack(safes,unsafes)) add_safes ths   = 
    1.28 +    Pack(sort less (ths@safes), unsafes);
    1.29 +
    1.30 +fun (Pack(safes,unsafes)) add_unsafes ths = 
    1.31 +    Pack(safes, sort less (ths@unsafes));
    1.32 +
    1.33 +
    1.34 +(*Returns the list of all formulas in the sequent*)
    1.35 +fun forms_of_seq (Const("SeqO'",_) $ P $ u) = P :: forms_of_seq u
    1.36 +  | forms_of_seq (H $ u) = forms_of_seq u
    1.37 +  | forms_of_seq _ = [];
    1.38 +
    1.39 +(*Tests whether two sequences (left or right sides) could be resolved.
    1.40 +  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
    1.41 +  Assumes each formula in seqc is surrounded by sequence variables
    1.42 +  -- checks that each concl formula looks like some subgoal formula.
    1.43 +  It SHOULD check order as well, using recursion rather than forall/exists*)
    1.44 +fun could_res (seqp,seqc) =
    1.45 +      forall (fn Qc => exists (fn Qp => could_unify (Qp,Qc)) 
    1.46 +                              (forms_of_seq seqp))
    1.47 +             (forms_of_seq seqc);
    1.48 +
    1.49 +
    1.50 +(*Tests whether two sequents or pairs of sequents could be resolved*)
    1.51 +fun could_resolve_seq (prem,conc) =
    1.52 +  case (prem,conc) of
    1.53 +      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
    1.54 +       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
    1.55 +	  could_res (leftp,leftc) andalso could_res (rightp,rightc)
    1.56 +    | (_ $ Abs(_,_,leftp) $ rightp,
    1.57 +       _ $ Abs(_,_,leftc) $ rightc) =>
    1.58 +	  could_res (leftp,leftc)  andalso  could_unify (rightp,rightc)
    1.59 +    | _ => false;
    1.60 +
    1.61 +
    1.62 +(*Like filt_resolve_tac, using could_resolve_seq
    1.63 +  Much faster than resolve_tac when there are many rules.
    1.64 +  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
    1.65 +fun filseq_resolve_tac rules maxr = SUBGOAL(fn (prem,i) =>
    1.66 +  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
    1.67 +  in  if length rls > maxr  then  no_tac
    1.68 +	  else (*((rtac derelict 1 THEN rtac impl 1
    1.69 +		 THEN (rtac identity 2 ORELSE rtac ll_mp 2)
    1.70 +		 THEN rtac context1 1)
    1.71 +		 ORELSE *) resolve_tac rls i
    1.72 +  end);
    1.73 +
    1.74 +
    1.75 +(*Predicate: does the rule have n premises? *)
    1.76 +fun has_prems n rule =  (nprems_of rule = n);
    1.77 +
    1.78 +(*Continuation-style tactical for resolution.
    1.79 +  The list of rules is partitioned into 0, 1, 2 premises.
    1.80 +  The resulting tactic, gtac, tries to resolve with rules.
    1.81 +  If successful, it recursively applies nextac to the new subgoals only.
    1.82 +  Else fails.  (Treatment of goals due to Ph. de Groote) 
    1.83 +  Bind (RESOLVE_THEN rules) to a variable: it preprocesses the rules. *)
    1.84 +
    1.85 +(*Takes rule lists separated in to 0, 1, 2, >2 premises.
    1.86 +  The abstraction over state prevents needless divergence in recursion.
    1.87 +  The 9999 should be a parameter, to delay treatment of flexible goals. *)
    1.88 +
    1.89 +fun RESOLVE_THEN rules =
    1.90 +  let val [rls0,rls1,rls2] = partition_list has_prems 0 2 rules;
    1.91 +      fun tac nextac i = STATE (fn state =>  
    1.92 +	  filseq_resolve_tac rls0 9999 i 
    1.93 +	  ORELSE
    1.94 +	  (DETERM(filseq_resolve_tac rls1 9999 i) THEN  TRY(nextac i))
    1.95 +	  ORELSE
    1.96 +	  (DETERM(filseq_resolve_tac rls2 9999 i) THEN  TRY(nextac(i+1))
    1.97 +					THEN  TRY(nextac i)) )
    1.98 +  in  tac  end;
    1.99 +
   1.100 +
   1.101 +
   1.102 +(*repeated resolution applied to the designated goal*)
   1.103 +fun reresolve_tac rules = 
   1.104 +  let val restac = RESOLVE_THEN rules;  (*preprocessing done now*)
   1.105 +      fun gtac i = restac gtac i
   1.106 +  in  gtac  end; 
   1.107 +
   1.108 +(*tries the safe rules repeatedly before the unsafe rules. *)
   1.109 +fun repeat_goal_tac (Pack(safes,unsafes)) = 
   1.110 +  let val restac  =    RESOLVE_THEN safes
   1.111 +      and lastrestac = RESOLVE_THEN unsafes;
   1.112 +      fun gtac i = restac gtac i  ORELSE  (print_tac THEN lastrestac gtac i)
   1.113 +  in  gtac  end; 
   1.114 +
   1.115 +
   1.116 +(*Tries safe rules only*)
   1.117 +fun safe_goal_tac (Pack(safes,unsafes)) = reresolve_tac safes;
   1.118 +
   1.119 +(*Tries a safe rule or else a unsafe rule.  Single-step for tracing. *)
   1.120 +fun step_tac (thm_pack as Pack(safes,unsafes)) =
   1.121 +    safe_goal_tac thm_pack  ORELSE'
   1.122 +    filseq_resolve_tac unsafes 9999;
   1.123 +
   1.124 +
   1.125 +(* Tactic for reducing a goal, using Predicate Calculus rules.
   1.126 +   A decision procedure for Propositional Calculus, it is incomplete
   1.127 +   for Predicate-Calculus because of allL_thin and exR_thin.  
   1.128 +   Fails if it can do nothing.      *)
   1.129 +fun pc_tac thm_pack = SELECT_GOAL (DEPTH_SOLVE (repeat_goal_tac thm_pack 1));
   1.130 +
   1.131 +
   1.132 +(*The following two tactics are analogous to those provided by 
   1.133 +  Provers/classical.  In fact, pc_tac is usually FASTER than fast_tac!*)
   1.134 +fun fast_tac thm_pack =
   1.135 +  SELECT_GOAL (DEPTH_SOLVE (step_tac thm_pack 1));
   1.136 +
   1.137 +fun best_tac thm_pack  = 
   1.138 +  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, size_of_thm) 
   1.139 +	       (step_tac thm_pack 1));
   1.140 +
   1.141 +
   1.142 +
   1.143 +signature MODAL_PROVER_RULE =
   1.144 +sig
   1.145 +    val rewrite_rls      : thm list
   1.146 +    val safe_rls         : thm list
   1.147 +    val unsafe_rls       : thm list
   1.148 +    val bound_rls        : thm list
   1.149 +    val aside_rls        : thm list
   1.150 +end;
   1.151 +
   1.152 +signature MODAL_PROVER = 
   1.153 +sig
   1.154 +    val rule_tac   : thm list -> int ->tactic
   1.155 +    val step_tac   : int -> tactic
   1.156 +    val solven_tac : int -> int -> tactic
   1.157 +    val solve_tac  : int -> tactic
   1.158 +end;
   1.159 +
   1.160 +functor Modal_ProverFun (Modal_Rule: MODAL_PROVER_RULE) : MODAL_PROVER = 
   1.161 +struct
   1.162 +local open Modal_Rule
   1.163 +in 
   1.164 +
   1.165 +(*Returns the list of all formulas in the sequent*)
   1.166 +fun forms_of_seq (Const("SeqO",_) $ P $ u) = P :: forms_of_seq u
   1.167 +  | forms_of_seq (H $ u) = forms_of_seq u
   1.168 +  | forms_of_seq _ = [];
   1.169 +
   1.170 +(*Tests whether two sequences (left or right sides) could be resolved.
   1.171 +  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
   1.172 +  Assumes each formula in seqc is surrounded by sequence variables
   1.173 +  -- checks that each concl formula looks like some subgoal formula.*)
   1.174 +fun could_res (seqp,seqc) =
   1.175 +      forall (fn Qc => exists (fn Qp => could_unify (Qp,Qc)) 
   1.176 +                              (forms_of_seq seqp))
   1.177 +             (forms_of_seq seqc);
   1.178 +
   1.179 +(*Tests whether two sequents G|-H could be resolved, comparing each side.*)
   1.180 +fun could_resolve_seq (prem,conc) =
   1.181 +  case (prem,conc) of
   1.182 +      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
   1.183 +       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
   1.184 +          could_res (leftp,leftc)  andalso  could_res (rightp,rightc)
   1.185 +    | _ => false;
   1.186 +
   1.187 +(*Like filt_resolve_tac, using could_resolve_seq
   1.188 +  Much faster than resolve_tac when there are many rules.
   1.189 +  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
   1.190 +fun filseq_resolve_tac rules maxr = SUBGOAL(fn (prem,i) =>
   1.191 +  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
   1.192 +  in  if length rls > maxr  then  no_tac  else resolve_tac rls i
   1.193 +  end);
   1.194 +
   1.195 +fun fresolve_tac rls n = filseq_resolve_tac rls 999 n;
   1.196 +
   1.197 +(* NB No back tracking possible with aside rules *)
   1.198 +
   1.199 +fun aside_tac n = DETERM(REPEAT (filt_resolve_tac aside_rls 999 n));
   1.200 +fun rule_tac rls n = fresolve_tac rls n THEN aside_tac n;
   1.201 +
   1.202 +val fres_safe_tac = fresolve_tac safe_rls;
   1.203 +val fres_unsafe_tac = fresolve_tac unsafe_rls THEN' aside_tac;
   1.204 +val fres_bound_tac = fresolve_tac bound_rls;
   1.205 +
   1.206 +fun UPTOGOAL n tf = let fun tac i = if i<n then all_tac
   1.207 +                                    else tf(i) THEN tac(i-1)
   1.208 +                    in STATE(fn state=> tac(nprems_of state)) end;
   1.209 +
   1.210 +(* Depth first search bounded by d *)
   1.211 +fun solven_tac d n = STATE (fn state =>
   1.212 +        if d<0 then no_tac
   1.213 +        else if (nprems_of state = 0) then all_tac 
   1.214 +        else (DETERM(fres_safe_tac n) THEN UPTOGOAL n (solven_tac d)) ORELSE
   1.215 +                 ((fres_unsafe_tac n  THEN UPTOGOAL n (solven_tac d)) APPEND
   1.216 +                   (fres_bound_tac n  THEN UPTOGOAL n (solven_tac (d-1)))));
   1.217 +
   1.218 +fun solve_tac d = rewrite_goals_tac rewrite_rls THEN solven_tac d 1;
   1.219 +
   1.220 +fun step_tac n = STATE (fn state =>  
   1.221 +      if (nprems_of state = 0) then all_tac 
   1.222 +      else (DETERM(fres_safe_tac n)) ORELSE 
   1.223 +           (fres_unsafe_tac n APPEND fres_bound_tac n));
   1.224 +
   1.225 +end;
   1.226 +end;