src/Tools/case_product.ML
author noschinl
Wed Dec 08 18:18:36 2010 +0100 (2010-12-08)
changeset 41826 18d4d2b60016
child 41883 392364739e5d
permissions -rw-r--r--
introduce attribute case_prod for combining case rules
* * *
[PATCH 1/9] Make tac independent of consumes

From 1a53ef4c070f924ff8e69529b0c1b13fa2844722 Mon Sep 17 00:00:00 2001
---
case_product.ML | 11 ++++++-----
1 files changed, 6 insertions(+), 5 deletions(-)
* * *
[PATCH 2/9] Replace MRS by OF

From da55d08ae287bfdc18dec554067b45a3e298c243 Mon Sep 17 00:00:00 2001
---
case_product.ML | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
* * *
[PATCH 3/9] Clean up tactic

From d26d50caaa3526c8c0625d7395467c6914f1a8d9 Mon Sep 17 00:00:00 2001
---
case_product.ML | 13 +++++--------
1 files changed, 5 insertions(+), 8 deletions(-)
* * *
[PATCH 4/9] Move out get_consumes a bit more

From 6ed5415f29cc43cea30c216edb1e74ec6c0f6c33 Mon Sep 17 00:00:00 2001
---
case_product.ML | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
* * *
[PATCH 5/9] Clean up tactic

From d301cfe6377e9f7db74b190fb085e0e66eeadfb5 Mon Sep 17 00:00:00 2001
---
case_product.ML | 3 +--
1 files changed, 1 insertions(+), 2 deletions(-)
* * *
[PATCH 6/9] Cleanup build_concl_prems

From c30889e131e74a92caac5b1e6d3d816890406ca7 Mon Sep 17 00:00:00 2001
---
case_product.ML | 18 ++++++++----------
1 files changed, 8 insertions(+), 10 deletions(-)
* * *
[PATCH 7/9] Split logic & annotation a bit

From e065606118308c96e013fad72ab9ad89b5a4b945 Mon Sep 17 00:00:00 2001
---
case_product.ML | 26 ++++++++++++++++++--------
1 files changed, 18 insertions(+), 8 deletions(-)
* * *
[PATCH 8/9] Remove dependency on consumes attribute

From e2a4de044481586d6127bbabd0ef48d0e3ab57c0 Mon Sep 17 00:00:00 2001
---
case_product.ML | 46 ++++++++++++++++++++++------------------------
1 files changed, 22 insertions(+), 24 deletions(-)
* * *
[PATCH 9/9] whitespace

From 59f5036bd8f825da4a362970292a34ec06c0f1a2 Mon Sep 17 00:00:00 2001
---
case_product.ML | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
noschinl@41826
     1
(*   Title: case_product.ML
noschinl@41826
     2
     Author: Lars Noschinski
noschinl@41826
     3
noschinl@41826
     4
Combines two case rules into a single one.
noschinl@41826
     5
noschinl@41826
     6
Assumes that the theorems are of the form
noschinl@41826
     7
  "[| C1; ...; Cm; A1 ==> P; ...; An ==> P |] ==> P"
noschinl@41826
     8
where m is given by the "consumes" attribute of the theorem.
noschinl@41826
     9
*)
noschinl@41826
    10
noschinl@41826
    11
signature CASE_PRODUCT =
noschinl@41826
    12
  sig
noschinl@41826
    13
noschinl@41826
    14
  val combine: Proof.context -> thm -> thm -> thm
noschinl@41826
    15
  val combine_annotated: Proof.context -> thm -> thm -> thm
noschinl@41826
    16
  val setup: theory -> theory
noschinl@41826
    17
end;
noschinl@41826
    18
noschinl@41826
    19
structure Case_Product: CASE_PRODUCT =
noschinl@41826
    20
struct
noschinl@41826
    21
noschinl@41826
    22
(*Instantiates the conclusion of thm2 to the one of thm1.*)
noschinl@41826
    23
fun inst_concl thm1 thm2 =
noschinl@41826
    24
  let
noschinl@41826
    25
    val cconcl_of = Drule.strip_imp_concl o Thm.cprop_of
noschinl@41826
    26
  in Thm.instantiate (Thm.match (cconcl_of thm2, cconcl_of thm1)) thm2 end
noschinl@41826
    27
noschinl@41826
    28
fun inst_thms thm1 thm2 ctxt =
noschinl@41826
    29
  let
noschinl@41826
    30
    val import = yield_singleton (apfst snd oo Variable.import true)
noschinl@41826
    31
    val (i_thm1, ctxt') = import thm1 ctxt
noschinl@41826
    32
    val (i_thm2, ctxt'') = import (inst_concl i_thm1 thm2) ctxt'
noschinl@41826
    33
  in ((i_thm1, i_thm2), ctxt'') end
noschinl@41826
    34
noschinl@41826
    35
(*
noschinl@41826
    36
Returns list of prems, where loose bounds have been replaced by frees.
noschinl@41826
    37
FIXME: Focus
noschinl@41826
    38
*)
noschinl@41826
    39
fun free_prems t ctxt =
noschinl@41826
    40
  let
noschinl@41826
    41
    val bs = Term.strip_all_vars t
noschinl@41826
    42
    val (names, ctxt') = Variable.variant_fixes (map fst bs) ctxt
noschinl@41826
    43
    val subst = map Free (names ~~ map snd bs)
noschinl@41826
    44
    val t' = map (Term.subst_bounds o pair (rev subst)) (Logic.strip_assums_hyp t)
noschinl@41826
    45
  in ((t', subst), ctxt') end
noschinl@41826
    46
noschinl@41826
    47
fun build_concl_prems thm1 thm2 ctxt =
noschinl@41826
    48
  let
noschinl@41826
    49
    val concl = Thm.concl_of thm1
noschinl@41826
    50
noschinl@41826
    51
    fun is_consumes t = not (Logic.strip_assums_concl t aconv concl)
noschinl@41826
    52
    val (p_cons1, p_cases1) = chop_while is_consumes (Thm.prems_of thm1)
noschinl@41826
    53
    val (p_cons2, p_cases2) = chop_while is_consumes (Thm.prems_of thm2)
noschinl@41826
    54
noschinl@41826
    55
    val p_cases_prod = map (fn p1 => map (fn p2 =>
noschinl@41826
    56
      let
noschinl@41826
    57
        val (((t1, subst1), (t2, subst2)), _) = ctxt
noschinl@41826
    58
          |> free_prems p1 ||>> free_prems p2
noschinl@41826
    59
      in
noschinl@41826
    60
        Logic.list_implies (t1 @ t2, concl)
noschinl@41826
    61
        |> fold_rev Logic.all (subst1 @ subst2)
noschinl@41826
    62
      end
noschinl@41826
    63
      ) p_cases2) p_cases1
noschinl@41826
    64
noschinl@41826
    65
    val prems = p_cons1 :: p_cons2 :: p_cases_prod
noschinl@41826
    66
  in
noschinl@41826
    67
    (concl, prems)
noschinl@41826
    68
  end
noschinl@41826
    69
noschinl@41826
    70
fun case_product_tac prems struc thm1 thm2 =
noschinl@41826
    71
  let
noschinl@41826
    72
    val (p_cons1 :: p_cons2 :: premss) = unflat struc prems
noschinl@41826
    73
    val thm2' = thm2 OF p_cons2
noschinl@41826
    74
  in
noschinl@41826
    75
    (Tactic.rtac (thm1 OF p_cons1)
noschinl@41826
    76
     THEN' EVERY' (map (fn p =>
noschinl@41826
    77
       Tactic.rtac thm2'
noschinl@41826
    78
       THEN' EVERY' (map (ProofContext.fact_tac o single) p)) premss)
noschinl@41826
    79
    )
noschinl@41826
    80
  end
noschinl@41826
    81
noschinl@41826
    82
fun combine ctxt thm1 thm2 =
noschinl@41826
    83
  let
noschinl@41826
    84
    val ((i_thm1, i_thm2), ctxt') = inst_thms thm1 thm2 ctxt
noschinl@41826
    85
    val (concl, prems_rich) = build_concl_prems i_thm1 i_thm2 ctxt'
noschinl@41826
    86
  in
noschinl@41826
    87
    Goal.prove ctxt' [] (flat prems_rich) concl (fn {prems, ...} =>
noschinl@41826
    88
      case_product_tac prems prems_rich i_thm1 i_thm2 1)
noschinl@41826
    89
    |> singleton (Variable.export ctxt' ctxt)
noschinl@41826
    90
  end;
noschinl@41826
    91
noschinl@41826
    92
fun annotation thm1 thm2 =
noschinl@41826
    93
  let
noschinl@41826
    94
    val (names1, cons1) = apfst (map fst) (Rule_Cases.get thm1)
noschinl@41826
    95
    val (names2, cons2) = apfst (map fst) (Rule_Cases.get thm2)
noschinl@41826
    96
    val names = map_product (fn x => fn y => x ^ "_" ^y) names1 names2
noschinl@41826
    97
  in
noschinl@41826
    98
    Rule_Cases.case_names names o Rule_Cases.consumes (cons1 + cons2)
noschinl@41826
    99
  end
noschinl@41826
   100
noschinl@41826
   101
fun combine_annotated ctxt thm1 thm2 =
noschinl@41826
   102
  combine ctxt thm1 thm2
noschinl@41826
   103
  |> snd o annotation thm1 thm2 o pair (Context.Proof ctxt)
noschinl@41826
   104
noschinl@41826
   105
(* Attribute setup *)
noschinl@41826
   106
noschinl@41826
   107
val case_prod_attr = let
noschinl@41826
   108
    fun combine_list ctxt = fold (fn x => fn y => combine_annotated ctxt y x)
noschinl@41826
   109
  in
noschinl@41826
   110
    Attrib.thms >> (fn thms => Thm.rule_attribute (fn ctxt => fn thm =>
noschinl@41826
   111
      combine_list (Context.proof_of ctxt) thms thm))
noschinl@41826
   112
  end
noschinl@41826
   113
noschinl@41826
   114
val setup =
noschinl@41826
   115
 Attrib.setup @{binding "case_product"} case_prod_attr "product with other case rules"
noschinl@41826
   116
noschinl@41826
   117
end;