src/HOL/Tools/coinduction.ML
author blanchet
Wed, 24 Sep 2014 15:45:55 +0200
changeset 58425 246985c6b20b
parent 58002 0ed1e999a0fb
child 58634 9f10d82e8188
permissions -rw-r--r--
simpler proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54366
diff changeset
     1
(*  Title:      HOL/Tools/coinduction.ML
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     2
    Author:     Johannes Hölzl, TU Muenchen
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     3
    Author:     Dmitriy Traytel, TU Muenchen
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     4
    Copyright   2013
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     5
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     6
Coinduction method that avoids some boilerplate compared to coinduct.
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     7
*)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     8
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
     9
signature COINDUCTION =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    10
sig
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    11
  val coinduction_tac: Proof.context -> term list -> thm option -> thm list -> cases_tactic
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    12
  val setup: theory -> theory
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    13
end;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    14
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    15
structure Coinduction : COINDUCTION =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    16
struct
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    17
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54366
diff changeset
    18
open Ctr_Sugar_Util
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54366
diff changeset
    19
open Ctr_Sugar_General_Tactics
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    20
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    21
fun filter_in_out _ [] = ([], [])
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    22
  | filter_in_out P (x :: xs) = (let
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    23
      val (ins, outs) = filter_in_out P xs;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    24
    in
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    25
      if P x then (x :: ins, outs) else (ins, x :: outs)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    26
    end);
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    27
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    28
fun ALLGOALS_SKIP skip tac st =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    29
  let fun doall n = if n = skip then all_tac else tac n THEN doall (n - 1)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    30
  in doall (nprems_of st) st  end;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    31
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    32
fun THEN_ALL_NEW_SKIP skip tac1 tac2 i st =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    33
  st |> (tac1 i THEN (fn st' =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    34
    Seq.INTERVAL tac2 (i + skip) (i + nprems_of st' - nprems_of st) st'));
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    35
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    36
fun DELETE_PREMS_AFTER skip tac i st =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    37
  let
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    38
    val n = nth (prems_of st) (i - 1) |> Logic.strip_assums_hyp |> length;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    39
  in
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    40
    (THEN_ALL_NEW_SKIP skip tac (REPEAT_DETERM_N n o etac thin_rl)) i st
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    41
  end;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    42
56231
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    43
fun coinduction_tac ctxt raw_vars opt_raw_thm prems = HEADGOAL (SUBGOAL_CASES (fn (goal, _, _) =>
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    44
  let
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    45
    val lhs_of_eq = HOLogic.dest_Trueprop #> HOLogic.dest_eq #> fst;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    46
    fun find_coinduct t = 
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    47
      Induct.find_coinductP ctxt t @
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    48
      (try (Induct.find_coinductT ctxt o fastype_of o lhs_of_eq) t |> the_default [])
56231
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    49
    val raw_thm =
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    50
      (case opt_raw_thm of
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    51
        SOME raw_thm => raw_thm
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    52
      | NONE => goal |> Logic.strip_assums_concl |> find_coinduct |> hd);
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    53
    val skip = Integer.max 1 (Rule_Cases.get_consumes raw_thm) - 1
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    54
    val cases = Rule_Cases.get raw_thm |> fst
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    55
  in
56231
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
    56
    HEADGOAL (
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54540
diff changeset
    57
      Object_Logic.rulify_tac ctxt THEN'
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    58
      Method.insert_tac prems THEN'
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54540
diff changeset
    59
      Object_Logic.atomize_prems_tac ctxt THEN'
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    60
      DELETE_PREMS_AFTER skip (Subgoal.FOCUS (fn {concl, context = ctxt, params, prems, ...} =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    61
        let
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    62
          val vars = raw_vars @ map (term_of o snd) params;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    63
          val names_ctxt = ctxt
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    64
            |> fold Variable.declare_names vars
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    65
            |> fold Variable.declare_thm (raw_thm :: prems);
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    66
          val thm_concl = Thm.cprop_of raw_thm |> strip_imp_concl;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    67
          val (rhoTs, rhots) = Thm.match (thm_concl, concl)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    68
            |>> map (pairself typ_of)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    69
            ||> map (pairself term_of);
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    70
          val xs = hd (Thm.prems_of raw_thm) |> HOLogic.dest_Trueprop |> strip_comb |> snd
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    71
            |> map (subst_atomic_types rhoTs);
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    72
          val raw_eqs = map (fn x => (x, AList.lookup op aconv rhots x |> the)) xs;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    73
          val ((names, ctxt), Ts) = map_split (apfst fst o dest_Var o fst) raw_eqs
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    74
            |>> (fn names => Variable.variant_fixes names names_ctxt) ;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    75
          val eqs =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    76
            map3 (fn name => fn T => fn (_, rhs) =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    77
              HOLogic.mk_eq (Free (name, T), rhs))
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    78
            names Ts raw_eqs;
54366
13bfdbcfbbfb swap equations and premises in the coinductive step for better proof automation
Andreas Lochbihler
parents: 54026
diff changeset
    79
          val phi = eqs @ map (HOLogic.dest_Trueprop o prop_of) prems
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    80
            |> try (Library.foldr1 HOLogic.mk_conj)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    81
            |> the_default @{term True}
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    82
            |> list_exists_free vars
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    83
            |> Term.map_abs_vars (Variable.revert_fixed ctxt)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    84
            |> fold_rev Term.absfree (names ~~ Ts)
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    85
            |> certify ctxt;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    86
          val thm = cterm_instantiate_pos [SOME phi] raw_thm;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    87
          val e = length eqs;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    88
          val p = length prems;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    89
        in
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    90
          HEADGOAL (EVERY' [rtac thm,
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    91
            EVERY' (map (fn var =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    92
              rtac (cterm_instantiate_pos [NONE, SOME (certify ctxt var)] exI)) vars),
54366
13bfdbcfbbfb swap equations and premises in the coinductive step for better proof automation
Andreas Lochbihler
parents: 54026
diff changeset
    93
            if p = 0 then CONJ_WRAP' (K (rtac refl)) eqs
13bfdbcfbbfb swap equations and premises in the coinductive step for better proof automation
Andreas Lochbihler
parents: 54026
diff changeset
    94
            else REPEAT_DETERM_N e o (rtac conjI THEN' rtac refl) THEN' CONJ_WRAP' rtac prems,
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    95
            K (ALLGOALS_SKIP skip
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    96
               (REPEAT_DETERM_N (length vars) o (etac exE THEN' rotate_tac ~1) THEN'
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    97
               DELETE_PREMS_AFTER 0 (Subgoal.FOCUS (fn {prems, params, context = ctxt, ...} =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    98
                 (case prems of
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
    99
                   [] => all_tac
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   100
                 | inv::case_prems =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   101
                     let
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   102
                       val (init, last) = funpow_yield (p + e - 1) HOLogic.conj_elim inv;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   103
                       val inv_thms = init @ [last];
54366
13bfdbcfbbfb swap equations and premises in the coinductive step for better proof automation
Andreas Lochbihler
parents: 54026
diff changeset
   104
                       val eqs = take e inv_thms;
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   105
                       fun is_local_var t = 
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   106
                         member (fn (t, (_, t')) => t aconv (term_of t')) params t;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   107
                        val (eqs, assms') = filter_in_out (is_local_var o lhs_of_eq o prop_of) eqs;
54366
13bfdbcfbbfb swap equations and premises in the coinductive step for better proof automation
Andreas Lochbihler
parents: 54026
diff changeset
   108
                        val assms = assms' @ drop e inv_thms
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   109
                      in
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   110
                        HEADGOAL (Method.insert_tac (assms @ case_prems)) THEN
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   111
                        unfold_thms_tac ctxt eqs
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   112
                      end)) ctxt)))])
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   113
        end) ctxt) THEN'
56231
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
   114
      K (prune_params_tac ctxt))
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
   115
    #> Seq.maps (fn st =>
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   116
      CASES (Rule_Cases.make_common (Proof_Context.theory_of ctxt, prop_of st) cases) all_tac st)
56231
b98813774a63 enforce subgoal boundaries via SUBGOAL/SUBGOAL_CASES -- clean tactical failure if out-of-range;
wenzelm
parents: 55954
diff changeset
   117
  end));
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   118
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   119
local
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   120
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   121
val ruleN = "rule"
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   122
val arbitraryN = "arbitrary"
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   123
fun single_rule [rule] = rule
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   124
  | single_rule _ = error "Single rule expected";
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   125
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   126
fun named_rule k arg get =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   127
  Scan.lift (Args.$$$ k -- Args.colon) |-- Scan.repeat arg :|--
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   128
    (fn names => Scan.peek (fn context => Scan.succeed (names |> map (fn name =>
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   129
      (case get (Context.proof_of context) name of SOME x => x
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   130
      | NONE => error ("No rule for " ^ k ^ " " ^ quote name))))));
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   131
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   132
fun rule get_type get_pred =
55951
c07d184aebe9 tuned signature -- more uniform check_type_name/read_type_name;
wenzelm
parents: 54742
diff changeset
   133
  named_rule Induct.typeN (Args.type_name {proper = false, strict = false}) get_type ||
55954
a29aefc88c8d more uniform check_const/read_const;
wenzelm
parents: 55951
diff changeset
   134
  named_rule Induct.predN (Args.const {proper = false, strict = false}) get_pred ||
a29aefc88c8d more uniform check_const/read_const;
wenzelm
parents: 55951
diff changeset
   135
  named_rule Induct.setN (Args.const {proper = false, strict = false}) get_pred ||
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   136
  Scan.lift (Args.$$$ ruleN -- Args.colon) |-- Attrib.thms;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   137
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   138
val coinduct_rule = rule Induct.lookup_coinductT Induct.lookup_coinductP >> single_rule;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   139
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   140
fun unless_more_args scan = Scan.unless (Scan.lift
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   141
  ((Args.$$$ arbitraryN || Args.$$$ Induct.typeN ||
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   142
    Args.$$$ Induct.predN || Args.$$$ Induct.setN || Args.$$$ ruleN) -- Args.colon)) scan;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   143
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   144
val arbitrary = Scan.optional (Scan.lift (Args.$$$ arbitraryN -- Args.colon) |--
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   145
  Scan.repeat1 (unless_more_args Args.term)) [];
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   146
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   147
in
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   148
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   149
val setup =
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   150
  Method.setup @{binding coinduction}
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   151
    (arbitrary -- Scan.option coinduct_rule >>
58002
0ed1e999a0fb simplified type Proof.method;
wenzelm
parents: 56231
diff changeset
   152
      (fn (arbitrary, opt_rule) => fn ctxt => fn facts =>
0ed1e999a0fb simplified type Proof.method;
wenzelm
parents: 56231
diff changeset
   153
        Seq.DETERM (coinduction_tac ctxt arbitrary opt_rule facts)))
54026
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   154
    "coinduction on types or predicates/sets";
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   155
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   156
end;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   157
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   158
end;
82d9b2701a03 new coinduction method
traytel
parents:
diff changeset
   159