src/Sequents/modal.ML
author wenzelm
Sat Jun 02 22:40:03 2018 +0200 (17 months ago)
changeset 68358 e761afd35baa
parent 59582 0fbed69ff081
child 69593 3dda49e08b9d
permissions -rw-r--r--
tuned proofs;
haftmann@24584
     1
(*  Title:      Sequents/modal.ML
paulson@7096
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@7096
     3
    Copyright   1992  University of Cambridge
paulson@7096
     4
wenzelm@29269
     5
Simple modal reasoner.
paulson@7096
     6
*)
paulson@7096
     7
paulson@7096
     8
signature MODAL_PROVER_RULE =
paulson@7096
     9
sig
wenzelm@59164
    10
  val rewrite_rls      : thm list
wenzelm@59164
    11
  val safe_rls         : thm list
wenzelm@59164
    12
  val unsafe_rls       : thm list
wenzelm@59164
    13
  val bound_rls        : thm list
wenzelm@59164
    14
  val aside_rls        : thm list
paulson@7096
    15
end;
paulson@7096
    16
wenzelm@59164
    17
signature MODAL_PROVER =
paulson@7096
    18
sig
wenzelm@59164
    19
  val rule_tac   : Proof.context -> thm list -> int ->tactic
wenzelm@59164
    20
  val step_tac   : Proof.context -> int -> tactic
wenzelm@59164
    21
  val solven_tac : Proof.context -> int -> int -> tactic
wenzelm@59164
    22
  val solve_tac  : Proof.context -> int -> tactic
paulson@7096
    23
end;
paulson@7096
    24
wenzelm@59164
    25
functor Modal_ProverFun (Modal_Rule: MODAL_PROVER_RULE) : MODAL_PROVER =
paulson@7096
    26
struct
paulson@7096
    27
paulson@7096
    28
(*Returns the list of all formulas in the sequent*)
haftmann@38500
    29
fun forms_of_seq (Const(@{const_name SeqO'},_) $ P $ u) = P :: forms_of_seq u
paulson@7096
    30
  | forms_of_seq (H $ u) = forms_of_seq u
paulson@7096
    31
  | forms_of_seq _ = [];
paulson@7096
    32
paulson@7096
    33
(*Tests whether two sequences (left or right sides) could be resolved.
paulson@7096
    34
  seqp is a premise (subgoal), seqc is a conclusion of an object-rule.
paulson@7096
    35
  Assumes each formula in seqc is surrounded by sequence variables
paulson@7096
    36
  -- checks that each concl formula looks like some subgoal formula.*)
paulson@7096
    37
fun could_res (seqp,seqc) =
wenzelm@59164
    38
      forall (fn Qc => exists (fn Qp => Term.could_unify (Qp,Qc))
paulson@7096
    39
                              (forms_of_seq seqp))
paulson@7096
    40
             (forms_of_seq seqc);
paulson@7096
    41
paulson@7096
    42
(*Tests whether two sequents G|-H could be resolved, comparing each side.*)
paulson@7096
    43
fun could_resolve_seq (prem,conc) =
paulson@7096
    44
  case (prem,conc) of
paulson@7096
    45
      (_ $ Abs(_,_,leftp) $ Abs(_,_,rightp),
paulson@7096
    46
       _ $ Abs(_,_,leftc) $ Abs(_,_,rightc)) =>
paulson@7096
    47
          could_res (leftp,leftc)  andalso  could_res (rightp,rightc)
paulson@7096
    48
    | _ => false;
paulson@7096
    49
paulson@7096
    50
(*Like filt_resolve_tac, using could_resolve_seq
paulson@7096
    51
  Much faster than resolve_tac when there are many rules.
paulson@7096
    52
  Resolve subgoal i using the rules, unless more than maxr are compatible. *)
wenzelm@59498
    53
fun filseq_resolve_tac ctxt rules maxr = SUBGOAL(fn (prem,i) =>
paulson@7096
    54
  let val rls = filter_thms could_resolve_seq (maxr+1, prem, rules)
wenzelm@59498
    55
  in  if length rls > maxr  then  no_tac  else resolve_tac ctxt rls i
paulson@7096
    56
  end);
paulson@7096
    57
wenzelm@59498
    58
fun fresolve_tac ctxt rls n = filseq_resolve_tac ctxt rls 999 n;
paulson@7096
    59
paulson@7096
    60
(* NB No back tracking possible with aside rules *)
paulson@7096
    61
wenzelm@59164
    62
val aside_net = Tactic.build_net Modal_Rule.aside_rls;
wenzelm@59164
    63
fun aside_tac ctxt n = DETERM (REPEAT (filt_resolve_from_net_tac ctxt 999 aside_net n));
wenzelm@59498
    64
fun rule_tac ctxt rls n = fresolve_tac ctxt rls n THEN aside_tac ctxt n;
paulson@7096
    65
wenzelm@59498
    66
fun fres_safe_tac ctxt = fresolve_tac ctxt Modal_Rule.safe_rls;
wenzelm@59498
    67
fun fres_unsafe_tac ctxt = fresolve_tac ctxt Modal_Rule.unsafe_rls THEN' aside_tac ctxt;
wenzelm@59498
    68
fun fres_bound_tac ctxt = fresolve_tac ctxt Modal_Rule.bound_rls;
paulson@7096
    69
paulson@7096
    70
fun UPTOGOAL n tf = let fun tac i = if i<n then all_tac
paulson@7096
    71
                                    else tf(i) THEN tac(i-1)
wenzelm@59582
    72
                    in fn st => tac (Thm.nprems_of st) st end;
paulson@7096
    73
paulson@7096
    74
(* Depth first search bounded by d *)
wenzelm@59164
    75
fun solven_tac ctxt d n st = st |>
wenzelm@59164
    76
 (if d < 0 then no_tac
wenzelm@59582
    77
  else if Thm.nprems_of st = 0 then all_tac
wenzelm@59498
    78
  else (DETERM(fres_safe_tac ctxt n) THEN UPTOGOAL n (solven_tac ctxt d)) ORELSE
wenzelm@59164
    79
           ((fres_unsafe_tac ctxt n  THEN UPTOGOAL n (solven_tac ctxt d)) APPEND
wenzelm@59498
    80
             (fres_bound_tac ctxt n  THEN UPTOGOAL n (solven_tac ctxt (d - 1)))));
paulson@7096
    81
wenzelm@59164
    82
fun solve_tac ctxt d =
wenzelm@59164
    83
  rewrite_goals_tac ctxt Modal_Rule.rewrite_rls THEN solven_tac ctxt d 1;
paulson@7096
    84
wenzelm@59164
    85
fun step_tac ctxt n =
wenzelm@59164
    86
  COND (has_fewer_prems 1) all_tac
wenzelm@59498
    87
       (DETERM(fres_safe_tac ctxt n) ORELSE
wenzelm@59498
    88
        (fres_unsafe_tac ctxt n APPEND fres_bound_tac ctxt n));
paulson@7096
    89
paulson@7096
    90
end;