src/HOL/Tools/Function/function_elims.ML
author wenzelm
Tue, 10 Feb 2015 14:48:26 +0100
changeset 59498 50b60f501b05
parent 59454 588b81d19823
child 59582 0fbed69ff081
permissions -rw-r--r--
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.; occasionally clarified use of context;
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
53667
0aefb31e27e0 distinguish Proof.context vs. local_theory semantically, with corresponding naming conventions;
wenzelm
parents: 53666
diff changeset
    17
  val mk_partial_elim_rules : Proof.context ->
53666
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
53669
6ede465b5be8 more antiquotations -- avoid unchecked string literals;
wenzelm
parents: 53667
diff changeset
    27
(* Extract a function and its arguments from a proposition that is
53603
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" *)
53669
6ede465b5be8 more antiquotations -- avoid unchecked string literals;
wenzelm
parents: 53667
diff changeset
    30
fun dest_funprop (Const (@{const_name HOL.eq}, _) $ lhs $ rhs) = (strip_comb lhs, rhs)
6ede465b5be8 more antiquotations -- avoid unchecked string literals;
wenzelm
parents: 53667
diff changeset
    31
  | dest_funprop (Const (@{const_name Not}, _) $ trm) = (strip_comb trm, @{term "False"})
53603
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
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
    36
fun propagate_tac ctxt i =
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    37
  let
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    38
    fun inspect eq =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    39
      (case eq of
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    40
        Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ Free x $ t) =>
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    41
          if Logic.occs (Free x, t) then raise Match else true
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    42
      | Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ t $ Free x) =>
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    43
          if Logic.occs (Free x, t) then raise Match else false
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    44
      | _ => raise Match);
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    45
    fun mk_eq thm =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    46
      (if inspect (prop_of thm) then [thm RS eq_reflection]
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    47
       else [Thm.symmetric (thm RS eq_reflection)])
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    48
      handle Match => [];
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
    49
    val simpset =
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
    50
      empty_simpset ctxt
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    51
      |> Simplifier.set_mksimps (K mk_eq);
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    52
  in
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
    53
    asm_lr_simp_tac simpset i
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    54
  end;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    55
54737
wenzelm
parents: 54736
diff changeset
    56
val eq_boolI = @{lemma "!!P. P ==> P = True" "!!P. ~P ==> P = False" by iprover+};
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    57
val boolE = @{thms HOL.TrueE HOL.FalseE};
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    58
val boolD = @{lemma "!!P. True = P ==> P" "!!P. False = P ==> ~P" by iprover+};
54737
wenzelm
parents: 54736
diff changeset
    59
val eq_bool = @{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
    60
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    61
fun bool_subst_tac ctxt i =
54737
wenzelm
parents: 54736
diff changeset
    62
  REPEAT (EqSubst.eqsubst_asm_tac ctxt [1] eq_bool i)
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59454
diff changeset
    63
  THEN REPEAT (dresolve_tac ctxt boolD i)
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59454
diff changeset
    64
  THEN REPEAT (eresolve_tac ctxt boolE i)
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    65
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    66
fun mk_bool_elims ctxt elim =
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    67
  let
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    68
    val tac = ALLGOALS (bool_subst_tac ctxt);
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    69
    fun mk_bool_elim b =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    70
      elim
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    71
      |> Thm.forall_elim b
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59454
diff changeset
    72
      |> Tactic.rule_by_tactic ctxt (TRY (resolve_tac ctxt eq_boolI 1))
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    73
      |> Tactic.rule_by_tactic ctxt tac;
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    74
  in
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    75
    map mk_bool_elim [@{cterm True}, @{cterm False}]
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
    76
  end;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    77
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    78
in
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    79
53667
0aefb31e27e0 distinguish Proof.context vs. local_theory semantically, with corresponding naming conventions;
wenzelm
parents: 53666
diff changeset
    80
fun mk_partial_elim_rules ctxt result =
0aefb31e27e0 distinguish Proof.context vs. local_theory semantically, with corresponding naming conventions;
wenzelm
parents: 53666
diff changeset
    81
  let
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    82
    val thy = Proof_Context.theory_of ctxt;
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
    83
    val cert = cterm_of thy;
53667
0aefb31e27e0 distinguish Proof.context vs. local_theory semantically, with corresponding naming conventions;
wenzelm
parents: 53666
diff changeset
    84
54737
wenzelm
parents: 54736
diff changeset
    85
    val FunctionResult {fs, R, dom, psimps, cases, ...} = result;
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    86
    val n_fs = length fs;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    87
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    88
    fun mk_partial_elim_rule (idx, f) =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    89
      let
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    90
        fun mk_var x T ctxt = case Name.variant x ctxt of (x, ctxt) => (Free (x, T), ctxt)
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    91
        fun mk_funeq 0 T ctxt (acc_vars, acc_lhs) =
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    92
              let val (y, ctxt) = mk_var "y" T ctxt
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    93
              in  (y :: acc_vars, (HOLogic.mk_Trueprop (HOLogic.mk_eq (acc_lhs, y))), T, ctxt) end
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    94
          | mk_funeq n (Type (@{type_name "fun"}, [S, T])) ctxt (acc_vars, acc_lhs) =
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    95
              let val (xn, ctxt) = mk_var "x" S ctxt
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    96
              in  mk_funeq (n - 1) T ctxt (xn :: acc_vars, acc_lhs $ xn) end
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
    97
          | mk_funeq _ _ _ _ = raise TERM ("Not a function.", [f]);
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
    98
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
    99
        val f_simps =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   100
          filter (fn r =>
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   101
            (prop_of r |> Logic.strip_assums_concl
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   102
              |> HOLogic.dest_Trueprop
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   103
              |> dest_funprop |> fst |> fst) = f)
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   104
            psimps;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   105
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   106
        val arity =
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   107
          hd f_simps
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   108
          |> prop_of
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   109
          |> Logic.strip_assums_concl
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   110
          |> HOLogic.dest_Trueprop
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   111
          |> snd o fst o dest_funprop
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   112
          |> length;
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   113
        val name_ctxt = Variable.names_of ctxt
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   114
        val (free_vars, prop, ranT, name_ctxt) = mk_funeq arity (fastype_of f) name_ctxt ([], f);
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   115
        val (rhs_var, arg_vars) = (case free_vars of x :: xs => (x, rev xs));
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   116
        val args = HOLogic.mk_tuple arg_vars;
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   117
        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
   118
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   119
        val P = mk_var "P" @{typ "bool"} name_ctxt |> fst |> cert
55968
blanchet
parents: 54739
diff changeset
   120
        val sumtree_inj = Sum_Tree.mk_inj domT n_fs (idx+1) args;
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   121
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
   122
        val cprop = cert prop;
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   123
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
   124
        val asms = [cprop, cert (HOLogic.mk_Trueprop (dom $ sumtree_inj))];
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   125
        val asms_thms = map Thm.assume asms;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   126
54737
wenzelm
parents: 54736
diff changeset
   127
        fun prep_subgoal_tac i =
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59454
diff changeset
   128
          REPEAT (eresolve_tac ctxt @{thms Pair_inject} i)
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   129
          THEN Method.insert_tac (case asms_thms of thm :: thms => (thm RS sym) :: thms) i
54738
ddada9ed12f6 proper simplifier context;
wenzelm
parents: 54737
diff changeset
   130
          THEN propagate_tac ctxt i
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 55968
diff changeset
   131
          THEN TRY ((EqSubst.eqsubst_asm_tac ctxt [1] psimps i) THEN assume_tac ctxt i)
54736
ba66830fae4c tuned whitespace;
wenzelm
parents: 53669
diff changeset
   132
          THEN bool_subst_tac ctxt i;
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   133
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   134
        val elim_stripped =
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   135
          nth cases idx
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   136
          |> Thm.forall_elim P
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   137
          |> Thm.forall_elim (cert args)
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   138
          |> Tactic.rule_by_tactic ctxt (ALLGOALS prep_subgoal_tac)
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   139
          |> fold_rev Thm.implies_intr asms
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   140
          |> Thm.forall_intr (cert rhs_var);
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   141
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   142
        val bool_elims =
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   143
          (case ranT of
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   144
            Type (@{type_name bool}, []) => mk_bool_elims ctxt elim_stripped
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   145
          | _ => []);
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   146
59454
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   147
        fun unstrip rl =
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   148
          rl
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   149
          |> fold_rev (Thm.forall_intr o cert) arg_vars
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   150
          |> Thm.forall_intr P
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   151
      in
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   152
        map unstrip (elim_stripped :: bool_elims)
588b81d19823 Fixed variable naming bug in function package
eberlm
parents: 58963
diff changeset
   153
      end;
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   154
  in
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   155
    map_index mk_partial_elim_rule fs
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   156
  end;
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   157
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   158
end;
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents:
diff changeset
   159
53666
52a0a526e677 tuned white space;
wenzelm
parents: 53664
diff changeset
   160
end;