src/HOL/Tools/Function/function_elims.ML
author wenzelm
Mon, 16 Sep 2013 17:04:28 +0200
changeset 53666 52a0a526e677
parent 53664 51595a047730
child 53667 0aefb31e27e0
permissions -rw-r--r--
tuned white space; avoid "object-oriented English";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Function/function_elims.ML
53609
0f472e7063af tuned headers
krauss
parents: 53603
diff changeset
     2
    Author:     Manuel Eberl, TU Muenchen
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     3
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
     4
Generate the pelims rules for a function. These are of the shape
53664
51595a047730 proper Isabelle symbols -- no UTF8 here;
wenzelm
parents: 53609
diff changeset
     5
[|f x y z = w; !!\<dots>. [|x = \<dots>; y = \<dots>; z = \<dots>; w = \<dots>|] ==> P; \<dots>|] ==> P
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     6
and are derived from the cases rule. There is at least one pelim rule for
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     7
each function (cf. mutually recursive functions)
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     8
There may be more than one pelim rule for a function in case of functions
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
     9
that return a boolean. For such a function, e.g. P x, not only the normal
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    10
elim rule with the premise P x = z is generated, but also two additional
53664
51595a047730 proper Isabelle symbols -- no UTF8 here;
wenzelm
parents: 53609
diff changeset
    11
elim rules with P x resp. \<not>P x as premises.
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    12
*)
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    13
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    14
signature FUNCTION_ELIMS =
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    15
sig
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    16
  val dest_funprop : term -> (term * term list) * term
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    17
  val mk_partial_elim_rules : local_theory ->
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    18
    Function_Common.function_result -> thm list list
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    19
end;
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    20
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    21
structure Function_Elims : FUNCTION_ELIMS =
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    22
struct
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    23
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    24
open Function_Lib
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    25
open Function_Common
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    26
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    27
(* Extracts a function and its arguments from a proposition that is
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    28
   either of the form "f x y z = ..." or, in case of function that
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    29
   returns a boolean, "f x y z" *)
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    30
fun dest_funprop (Const ("HOL.eq", _) $ lhs $ rhs) = (strip_comb lhs, rhs)
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    31
  | dest_funprop (Const ("HOL.Not", _) $ trm) = (strip_comb trm, @{term "False"})
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    32
  | dest_funprop trm = (strip_comb trm, @{term "True"});
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    33
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    34
local
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    35
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    36
fun propagate_tac i thm =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    37
  let fun inspect eq = case eq of
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    38
              Const ("HOL.Trueprop",_) $ (Const ("HOL.eq",_) $ Free x $ t) =>
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    39
                  if Logic.occs (Free x, t) then raise Match else true
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    40
            | Const ("HOL.Trueprop",_) $ (Const ("HOL.eq",_) $ t $ Free x) =>
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    41
                  if Logic.occs (Free x, t) then raise Match else false
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    42
            | _ => raise Match;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    43
      fun mk_eq thm = (if inspect (prop_of thm) then
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    44
                          [thm RS eq_reflection]
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    45
                      else
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    46
                          [Thm.symmetric (thm RS eq_reflection)])
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    47
                      handle Match => [];
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    48
      val ss = Simplifier.global_context (Thm.theory_of_thm thm) empty_ss
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    49
               |> Simplifier.set_mksimps (K mk_eq)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    50
  in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    51
    asm_lr_simp_tac ss i thm
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    52
  end;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    53
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    54
val eqBoolI = @{lemma "!!P. P ==> P = True" "!!P. ~P ==> P = False" by iprover+}
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    55
val boolE = @{thms HOL.TrueE HOL.FalseE}
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    56
val boolD = @{lemma "!!P. True = P ==> P" "!!P. False = P ==> ~P" by iprover+}
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    57
val eqBool = @{thms HOL.eq_True HOL.eq_False HOL.not_False_eq_True HOL.not_True_eq_False}
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    58
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    59
fun bool_subst_tac ctxt i =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    60
    REPEAT (EqSubst.eqsubst_asm_tac ctxt [1] eqBool i)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    61
    THEN REPEAT (dresolve_tac boolD i)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    62
    THEN REPEAT (eresolve_tac boolE i)
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    63
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    64
fun mk_bool_elims ctxt elim =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    65
  let val tac = ALLGOALS (bool_subst_tac ctxt)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    66
      fun mk_bool_elim b =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    67
        elim
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    68
        |> Thm.forall_elim b
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    69
        |> Tactic.rule_by_tactic ctxt (TRY (resolve_tac eqBoolI 1))
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    70
        |> Tactic.rule_by_tactic ctxt tac
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    71
  in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    72
      map mk_bool_elim [@{cterm True}, @{cterm False}]
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    73
  end;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    74
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    75
in
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    76
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    77
fun mk_partial_elim_rules ctxt result=
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    78
  let val FunctionResult {fs, G, R, dom, psimps, simple_pinducts, cases,
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    79
                          termination, domintros, ...} = result;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    80
      val n_fs = length fs;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    81
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    82
      fun mk_partial_elim_rule (idx,f) =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    83
        let fun mk_funeq 0 T (acc_vars, acc_lhs) =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    84
                let val y = Free("y",T) in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    85
                  (y :: acc_vars, (HOLogic.mk_Trueprop (HOLogic.mk_eq (acc_lhs, y))), T)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    86
                end
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    87
              | mk_funeq n (Type("fun",[S,T])) (acc_vars, acc_lhs) =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    88
                let val xn = Free ("x" ^ Int.toString n,S) in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    89
                  mk_funeq (n - 1) T (xn :: acc_vars, acc_lhs $ xn)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    90
                end
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    91
              | mk_funeq _ _ _ = raise (TERM ("Not a function.", [f]))
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    92
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    93
            val f_simps = filter (fn r => (prop_of r |> Logic.strip_assums_concl
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    94
                                           |> HOLogic.dest_Trueprop
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    95
                                           |> dest_funprop |> fst |> fst) = f)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    96
                                 psimps
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    97
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    98
            val arity = hd f_simps |> prop_of |> Logic.strip_assums_concl
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    99
                                   |> HOLogic.dest_Trueprop
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   100
                                   |> snd o fst o dest_funprop |> length;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   101
            val (free_vars,prop,ranT) = mk_funeq arity (fastype_of f) ([],f)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   102
            val (rhs_var, arg_vars) = case free_vars of x::xs => (x, rev xs)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   103
            val args = HOLogic.mk_tuple arg_vars;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   104
            val domT = R |> dest_Free |> snd |> hd o snd o dest_Type
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   105
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   106
            val sumtree_inj = SumTree.mk_inj domT n_fs (idx+1) args;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   107
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   108
            val thy = Proof_Context.theory_of ctxt;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   109
            val cprop = cterm_of thy prop
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   110
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   111
            val asms = [cprop, cterm_of thy (HOLogic.mk_Trueprop (dom $ sumtree_inj))];
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   112
            val asms_thms = map Thm.assume asms;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   113
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   114
            fun prep_subgoal i =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   115
              REPEAT (eresolve_tac @{thms Pair_inject} i)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   116
              THEN Method.insert_tac (case asms_thms of
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   117
                                        thm::thms => (thm RS sym) :: thms) i
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   118
              THEN propagate_tac i
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   119
              THEN TRY
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   120
                  ((EqSubst.eqsubst_asm_tac ctxt [1] psimps i) THEN atac i)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   121
              THEN bool_subst_tac ctxt i;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   122
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   123
          val tac = ALLGOALS prep_subgoal;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   124
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   125
          val elim_stripped =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   126
                nth cases idx
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   127
                |> Thm.forall_elim @{cterm "P::bool"}
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   128
                |> Thm.forall_elim (cterm_of thy args)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   129
                |> Tactic.rule_by_tactic ctxt tac
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   130
                |> fold_rev Thm.implies_intr asms
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   131
                |> Thm.forall_intr (cterm_of thy rhs_var)
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   132
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   133
          val bool_elims = (case ranT of
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   134
                              Type ("HOL.bool", []) => mk_bool_elims ctxt elim_stripped
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   135
                              | _ => []);
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   136
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   137
          fun unstrip rl =
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   138
                rl  |> (fn thm => List.foldr (uncurry Thm.forall_intr) thm
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   139
                           (map (cterm_of thy) arg_vars))
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   140
                    |> Thm.forall_intr @{cterm "P::bool"}
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   141
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   142
      in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   143
        map unstrip (elim_stripped :: bool_elims)
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   144
      end;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   145
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   146
  in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   147
    map_index mk_partial_elim_rule fs
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   148
  end;
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   149
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   150
end;
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   151
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   152
end;
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   153