src/HOL/Tools/TFL/rules.ML
author wenzelm
Wed, 17 Jun 2015 17:54:09 +0200
changeset 60502 aa58872267ee
parent 60365 3e28769ba2b6
permissions -rw-r--r--
manual merge;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/TFL/rules.ML
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     Konrad Slind, Cambridge University Computer Laboratory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     3
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 26928
diff changeset
     4
Emulation of HOL inference rules for TFL.
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     5
*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     6
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     7
signature RULES =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     8
sig
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     9
  val dest_thm: thm -> term list * term
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    10
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    11
  (* Inference rules *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    12
  val REFL: cterm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    13
  val ASSUME: cterm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    14
  val MP: thm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    15
  val MATCH_MP: thm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    16
  val CONJUNCT1: thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    17
  val CONJUNCT2: thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    18
  val CONJUNCTS: thm -> thm list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    19
  val DISCH: cterm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    20
  val UNDISCH: thm  -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    21
  val SPEC: cterm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    22
  val ISPEC: cterm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    23
  val ISPECL: cterm list -> thm -> thm
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
    24
  val GEN: Proof.context -> cterm -> thm -> thm
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
    25
  val GENL: Proof.context -> cterm list -> thm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    26
  val LIST_CONJ: thm list -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    27
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    28
  val SYM: thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    29
  val DISCH_ALL: thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    30
  val FILTER_DISCH_ALL: (term -> bool) -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    31
  val SPEC_ALL: thm -> thm
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
    32
  val GEN_ALL: Proof.context -> thm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    33
  val IMP_TRANS: thm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    34
  val PROVE_HYP: thm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    35
60334
63f25a1adcfc clarified context;
wenzelm
parents: 60329
diff changeset
    36
  val CHOOSE: Proof.context -> cterm * thm -> thm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    37
  val EXISTS: cterm * cterm -> thm -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    38
  val EXISTL: cterm list -> thm -> thm
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
    39
  val IT_EXISTS: Proof.context -> (cterm * cterm) list -> thm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    40
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    41
  val EVEN_ORS: thm list -> thm list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    42
  val DISJ_CASESL: thm -> thm list -> thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    43
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    44
  val list_beta_conv: cterm -> cterm list -> thm
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
    45
  val SUBS: Proof.context -> thm list -> thm -> thm
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
    46
  val simpl_conv: Proof.context -> thm list -> cterm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    47
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    48
  val rbeta: thm -> thm
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32603
diff changeset
    49
  val tracing: bool Unsynchronized.ref
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
    50
  val CONTEXT_REWRITE_RULE: Proof.context ->
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
    51
    term * term list * thm * thm list -> thm -> thm * term list
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
    52
  val RIGHT_ASSOC: Proof.context -> thm -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    53
60335
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
    54
  val prove: Proof.context -> bool -> term * tactic -> thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    55
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    56
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    57
structure Rules: RULES =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    58
struct
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    59
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
    60
fun RULES_ERR func mesg = Utils.ERR {module = "Rules", func = func, mesg = mesg};
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    61
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    62
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
    63
fun cconcl thm = Dcterm.drop_prop (#prop (Thm.crep_thm thm));
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
    64
fun chyps thm = map Dcterm.drop_prop (#hyps (Thm.crep_thm thm));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    65
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    66
fun dest_thm thm =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    67
  let val {prop,hyps,...} = Thm.rep_thm thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    68
  in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    69
  handle TERM _ => raise RULES_ERR "dest_thm" "missing Trueprop";
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    70
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    71
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    72
(* Inference rules *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    73
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    74
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    75
 *        Equality (one step)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    76
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    77
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    78
fun REFL tm = Thm.reflexive tm RS meta_eq_to_obj_eq
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    79
  handle THM (msg, _, _) => raise RULES_ERR "REFL" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    80
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    81
fun SYM thm = thm RS sym
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    82
  handle THM (msg, _, _) => raise RULES_ERR "SYM" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    83
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    84
fun ALPHA thm ctm1 =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    85
  let
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    86
    val ctm2 = Thm.cprop_of thm;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    87
    val ctm2_eq = Thm.reflexive ctm2;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    88
    val ctm1_eq = Thm.reflexive ctm1;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    89
  in Thm.equal_elim (Thm.transitive ctm2_eq ctm1_eq) thm end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    90
  handle THM (msg, _, _) => raise RULES_ERR "ALPHA" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    91
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    92
fun rbeta th =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
    93
  (case Dcterm.strip_comb (cconcl th) of
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    94
    (_, [l, r]) => Thm.transitive th (Thm.beta_conversion false r)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    95
  | _ => raise RULES_ERR "rbeta" "");
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    96
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    97
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    98
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    99
 *        Implication and the assumption list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   100
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   101
 * Assumptions get stuck on the meta-language assumption list. Implications
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   102
 * are in the object language, so discharging an assumption "A" from theorem
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   103
 * "B" results in something that looks like "A --> B".
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   104
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   105
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   106
fun ASSUME ctm = Thm.assume (Dcterm.mk_prop ctm);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   107
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   108
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   109
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   110
 * Implication in TFL is -->. Meta-language implication (==>) is only used
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   111
 * in the implementation of some of the inference rules below.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   112
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   113
fun MP th1 th2 = th2 RS (th1 RS mp)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   114
  handle THM (msg, _, _) => raise RULES_ERR "MP" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   115
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   116
(*forces the first argument to be a proposition if necessary*)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   117
fun DISCH tm thm = Thm.implies_intr (Dcterm.mk_prop tm) thm COMP impI
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   118
  handle THM (msg, _, _) => raise RULES_ERR "DISCH" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   119
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   120
fun DISCH_ALL thm = fold_rev DISCH (#hyps (Thm.crep_thm thm)) thm;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   121
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   122
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   123
fun FILTER_DISCH_ALL P thm =
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   124
 let fun check tm = P (Thm.term_of tm)
33339
d41f77196338 eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   125
 in  fold_rev (fn tm => fn th => if check tm then DISCH tm th else th) (chyps thm) thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   126
 end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   127
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   128
fun UNDISCH thm =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   129
   let val tm = Dcterm.mk_prop (#1 (Dcterm.dest_imp (cconcl thm)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   130
   in Thm.implies_elim (thm RS mp) (ASSUME tm) end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   131
   handle Utils.ERR _ => raise RULES_ERR "UNDISCH" ""
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   132
     | THM _ => raise RULES_ERR "UNDISCH" "";
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   133
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   134
fun PROVE_HYP ath bth = MP (DISCH (cconcl ath) bth) ath;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   135
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   136
fun IMP_TRANS th1 th2 = th2 RS (th1 RS Thms.imp_trans)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   137
  handle THM (msg, _, _) => raise RULES_ERR "IMP_TRANS" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   138
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   139
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   140
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   141
 *        Conjunction
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   142
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   143
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   144
fun CONJUNCT1 thm = thm RS conjunct1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   145
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT1" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   146
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   147
fun CONJUNCT2 thm = thm RS conjunct2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   148
  handle THM (msg, _, _) => raise RULES_ERR "CONJUNCT2" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   149
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   150
fun CONJUNCTS th = CONJUNCTS (CONJUNCT1 th) @ CONJUNCTS (CONJUNCT2 th) handle Utils.ERR _ => [th];
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   151
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   152
fun LIST_CONJ [] = raise RULES_ERR "LIST_CONJ" "empty list"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   153
  | LIST_CONJ [th] = th
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   154
  | LIST_CONJ (th :: rst) = MP (MP (conjI COMP (impI RS impI)) th) (LIST_CONJ rst)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   155
      handle THM (msg, _, _) => raise RULES_ERR "LIST_CONJ" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   156
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   157
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   158
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   159
 *        Disjunction
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   160
 *---------------------------------------------------------------------------*)
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   161
local
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   162
  val prop = Thm.prop_of disjI1
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   163
  val [P,Q] = Misc_Legacy.term_vars prop
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   164
  val disj1 = Thm.forall_intr (Thm.cterm_of @{context} Q) disjI1
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   165
in
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   166
fun DISJ1 thm tm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj1)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   167
  handle THM (msg, _, _) => raise RULES_ERR "DISJ1" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   168
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   169
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   170
local
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   171
  val prop = Thm.prop_of disjI2
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   172
  val [P,Q] = Misc_Legacy.term_vars prop
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   173
  val disj2 = Thm.forall_intr (Thm.cterm_of @{context} P) disjI2
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   174
in
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   175
fun DISJ2 tm thm = thm RS (Thm.forall_elim (Dcterm.drop_prop tm) disj2)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   176
  handle THM (msg, _, _) => raise RULES_ERR "DISJ2" msg;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   177
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   178
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   179
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   180
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   181
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   182
 *                   A1 |- M1, ..., An |- Mn
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   183
 *     ---------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   184
 *     [A1 |- M1 \/ ... \/ Mn, ..., An |- M1 \/ ... \/ Mn]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   185
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   186
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   187
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   188
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   189
fun EVEN_ORS thms =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   190
  let fun blue ldisjs [] _ = []
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   191
        | blue ldisjs (th::rst) rdisjs =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   192
            let val tail = tl rdisjs
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   193
                val rdisj_tl = Dcterm.list_mk_disj tail
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   194
            in fold_rev DISJ2 ldisjs (DISJ1 th rdisj_tl)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   195
               :: blue (ldisjs @ [cconcl th]) rst tail
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   196
            end handle Utils.ERR _ => [fold_rev DISJ2 ldisjs th]
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   197
   in blue [] thms (map cconcl thms) end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   198
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   199
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   200
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   201
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   202
 *         A |- P \/ Q   B,P |- R    C,Q |- R
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   203
 *     ---------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   204
 *                     A U B U C |- R
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   205
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   206
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   207
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   208
fun DISJ_CASES th1 th2 th3 =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   209
  let
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   210
    val c = Dcterm.drop_prop (cconcl th1);
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   211
    val (disj1, disj2) = Dcterm.dest_disj c;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   212
    val th2' = DISCH disj1 th2;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   213
    val th3' = DISCH disj2 th3;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   214
  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   215
    th3' RS (th2' RS (th1 RS Thms.tfl_disjE))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   216
      handle THM (msg, _, _) => raise RULES_ERR "DISJ_CASES" msg
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   217
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   218
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   219
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   220
(*-----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   221
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   222
 *       |- A1 \/ ... \/ An     [A1 |- M, ..., An |- M]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   223
 *     ---------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   224
 *                           |- M
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   225
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   226
 * Note. The list of theorems may be all jumbled up, so we have to
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   227
 * first organize it to align with the first argument (the disjunctive
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   228
 * theorem).
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   229
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   230
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   231
fun organize eq =    (* a bit slow - analogous to insertion sort *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   232
 let fun extract a alist =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   233
     let fun ex (_,[]) = raise RULES_ERR "organize" "not a permutation.1"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   234
           | ex(left,h::t) = if (eq h a) then (h,rev left@t) else ex(h::left,t)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   235
     in ex ([],alist)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   236
     end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   237
     fun place [] [] = []
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   238
       | place (a::rst) alist =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   239
           let val (item,next) = extract a alist
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   240
           in item::place rst next
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   241
           end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   242
       | place _ _ = raise RULES_ERR "organize" "not a permutation.2"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   243
 in place
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   244
 end;
36617
ed8083930203 UNDISCH/DISJ_CASESL: eliminated slightly odd Thm.legacy_freezeT -- these rules appear to be applied to thms with fixed types only;
wenzelm
parents: 36615
diff changeset
   245
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   246
fun DISJ_CASESL disjth thl =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   247
   let val c = cconcl disjth
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59058
diff changeset
   248
       fun eq th atm =
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59058
diff changeset
   249
        exists (fn t => HOLogic.dest_Trueprop t aconv Thm.term_of atm) (Thm.hyps_of th)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   250
       val tml = Dcterm.strip_disj c
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   251
       fun DL th [] = raise RULES_ERR "DISJ_CASESL" "no cases"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   252
         | DL th [th1] = PROVE_HYP th th1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   253
         | DL th [th1,th2] = DISJ_CASES th th1 th2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   254
         | DL th (th1::rst) =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   255
            let val tm = #2 (Dcterm.dest_disj (Dcterm.drop_prop(cconcl th)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   256
             in DISJ_CASES th th1 (DL (ASSUME tm) rst) end
36617
ed8083930203 UNDISCH/DISJ_CASESL: eliminated slightly odd Thm.legacy_freezeT -- these rules appear to be applied to thms with fixed types only;
wenzelm
parents: 36615
diff changeset
   257
   in DL disjth (organize eq tml thl)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   258
   end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   259
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   260
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   261
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   262
 *        Universals
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   263
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   264
local (* this is fragile *)
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   265
  val prop = Thm.prop_of spec
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   266
  val x = hd (tl (Misc_Legacy.term_vars prop))
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   267
  val cTV = Thm.ctyp_of @{context} (type_of x)
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   268
  val gspec = Thm.forall_intr (Thm.cterm_of @{context} x) spec
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   269
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   270
fun SPEC tm thm =
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   271
   let val gspec' = Drule.instantiate_normalize ([(cTV, Thm.ctyp_of_cterm tm)], []) gspec
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36617
diff changeset
   272
   in thm RS (Thm.forall_elim tm gspec') end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   273
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   274
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   275
fun SPEC_ALL thm = fold SPEC (#1 (Dcterm.strip_forall(cconcl thm))) thm;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   276
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   277
val ISPEC = SPEC
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   278
val ISPECL = fold ISPEC;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   279
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   280
(* Not optimized! Too complicated. *)
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   281
local
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   282
  val prop = Thm.prop_of allI
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   283
  val [P] = Misc_Legacy.add_term_vars (prop, [])
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   284
  fun cty_theta ctxt = map (fn (i, (S, ty)) => apply2 (Thm.ctyp_of ctxt) (TVar (i, S), ty))
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   285
  fun ctm_theta ctxt =
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   286
    map (fn (i, (_, tm2)) =>
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   287
      let val ctm2 = Thm.cterm_of ctxt tm2
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   288
      in (Thm.cterm_of ctxt (Var (i, Thm.typ_of_cterm ctm2)), ctm2) end)
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   289
  fun certify ctxt (ty_theta,tm_theta) =
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   290
    (cty_theta ctxt (Vartab.dest ty_theta),
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   291
     ctm_theta ctxt (Vartab.dest tm_theta))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   292
in
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   293
fun GEN ctxt v th =
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36617
diff changeset
   294
   let val gth = Thm.forall_intr v th
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   295
       val thy = Proof_Context.theory_of ctxt
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   296
       val Const(@{const_name Pure.all},_)$Abs(x,ty,rst) = Thm.prop_of gth
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   297
       val P' = Abs(x,ty, HOLogic.dest_Trueprop rst)  (* get rid of trueprop *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   298
       val theta = Pattern.match thy (P,P') (Vartab.empty, Vartab.empty);
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   299
       val allI2 = Drule.instantiate_normalize (certify ctxt theta) allI
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   300
       val thm = Thm.implies_elim allI2 gth
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 23150
diff changeset
   301
       val tp $ (A $ Abs(_,_,M)) = Thm.prop_of thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   302
       val prop' = tp $ (A $ Abs(x,ty,M))
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   303
   in ALPHA thm (Thm.cterm_of ctxt prop') end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   304
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   305
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   306
fun GENL ctxt = fold_rev (GEN ctxt);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   307
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   308
fun GEN_ALL ctxt thm =
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   309
  let
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   310
    val prop = Thm.prop_of thm
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   311
    val vlist = map (Thm.cterm_of ctxt) (Misc_Legacy.add_term_vars (prop, []))
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   312
  in GENL ctxt vlist thm end;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   313
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   314
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   315
fun MATCH_MP th1 th2 =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   316
   if (Dcterm.is_forall (Dcterm.drop_prop(cconcl th1)))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   317
   then MATCH_MP (th1 RS spec) th2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   318
   else MP th1 th2;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   319
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   320
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   321
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   322
 *        Existentials
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   323
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   324
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   325
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   326
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   327
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   328
 * Existential elimination
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   329
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   330
 *      A1 |- ?x.t[x]   ,   A2, "t[v]" |- t'
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   331
 *      ------------------------------------     (variable v occurs nowhere)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   332
 *                A1 u A2 |- t'
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   333
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   334
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   335
60334
63f25a1adcfc clarified context;
wenzelm
parents: 60329
diff changeset
   336
fun CHOOSE ctxt (fvar, exth) fact =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   337
  let
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   338
    val lam = #2 (Dcterm.dest_comb (Dcterm.drop_prop (cconcl exth)))
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   339
    val redex = Dcterm.capply lam fvar
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 23150
diff changeset
   340
    val t$u = Thm.term_of redex
60334
63f25a1adcfc clarified context;
wenzelm
parents: 60329
diff changeset
   341
    val residue = Thm.cterm_of ctxt (Term.betapply (t, u))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   342
  in
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   343
    GEN ctxt fvar (DISCH residue fact) RS (exth RS Thms.choose_thm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   344
      handle THM (msg, _, _) => raise RULES_ERR "CHOOSE" msg
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   345
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   346
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   347
local
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   348
  val prop = Thm.prop_of exI
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   349
  val [P, x] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars prop)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   350
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   351
fun EXISTS (template,witness) thm =
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   352
  let val abstr = #2 (Dcterm.dest_comb template) in
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   353
    thm RS (cterm_instantiate [(P, abstr), (x, witness)] exI)
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   354
      handle THM (msg, _, _) => raise RULES_ERR "EXISTS" msg
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   355
  end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   356
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   357
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   358
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   359
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   360
 *         A |- M
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   361
 *   -------------------   [v_1,...,v_n]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   362
 *    A |- ?v1...v_n. M
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   363
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   364
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   365
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   366
fun EXISTL vlist th =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   367
  fold_rev (fn v => fn thm => EXISTS(Dcterm.mk_exists(v,cconcl thm), v) thm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   368
           vlist th;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   369
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   370
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   371
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   372
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   373
 *       A |- M[x_1,...,x_n]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   374
 *   ----------------------------   [(x |-> y)_1,...,(x |-> y)_n]
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   375
 *       A |- ?y_1...y_n. M
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   376
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   377
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   378
(* Could be improved, but needs "subst_free" for certified terms *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   379
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   380
fun IT_EXISTS ctxt blist th =
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   381
  let
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   382
    val blist' = map (apply2 Thm.term_of) blist
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   383
    fun ex v M = Thm.cterm_of ctxt (USyntax.mk_exists{Bvar=v,Body = M})
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   384
  in
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   385
    fold_rev (fn (b as (r1,r2)) => fn thm =>
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   386
        EXISTS(ex r2 (subst_free [b]
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   387
                   (HOLogic.dest_Trueprop(Thm.prop_of thm))), Thm.cterm_of ctxt r1)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   388
              thm)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   389
       blist' th
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   390
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   391
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   392
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   393
 *  Faster version, that fails for some as yet unknown reason
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   394
 * fun IT_EXISTS blist th =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   395
 *    let val {thy,...} = rep_thm th
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   396
 *        val tych = cterm_of thy
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   397
 *        fun detype (x,y) = ((#t o rep_cterm) x, (#t o rep_cterm) y)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   398
 *   in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   399
 *  fold (fn (b as (r1,r2), thm) =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   400
 *  EXISTS(D.mk_exists(r2, tych(subst_free[detype b](#t(rep_cterm(cconcl thm))))),
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   401
 *           r1) thm)  blist th
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   402
 *   end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   403
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   404
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   405
(*----------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   406
 *        Rewriting
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   407
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   408
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   409
fun SUBS ctxt thl =
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   410
  rewrite_rule ctxt (map (fn th => th RS eq_reflection handle THM _ => th) thl);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   411
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41225
diff changeset
   412
val rew_conv = Raw_Simplifier.rewrite_cterm (true, false, false) (K (K NONE));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   413
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   414
fun simpl_conv ctxt thl ctm =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   415
 rew_conv (ctxt addsimps thl) ctm RS meta_eq_to_obj_eq;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   416
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   417
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   418
fun RIGHT_ASSOC ctxt = rewrite_rule ctxt [Thms.disj_assoc];
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   419
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   420
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   421
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   422
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   423
 *                  TERMINATION CONDITION EXTRACTION
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   424
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   425
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   426
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   427
(* Object language quantifier, i.e., "!" *)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   428
fun Forall v M = USyntax.mk_forall{Bvar=v, Body=M};
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   429
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   430
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   431
(* Fragile: it's a cong if it is not "R y x ==> cut f R x y = f y" *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   432
fun is_cong thm =
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   433
  case (Thm.prop_of thm) of
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   434
    (Const(@{const_name Pure.imp},_)$(Const(@{const_name Trueprop},_)$ _) $
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   435
      (Const(@{const_name Pure.eq},_) $ (Const (@{const_name Wfrec.cut},_) $ f $ R $ a $ x) $ _)) =>
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   436
        false
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   437
  | _ => true;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   438
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   439
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   440
fun dest_equal(Const (@{const_name Pure.eq},_) $
38554
f8999e19dd49 corrected some long-overseen misperceptions in recdef
haftmann
parents: 38549
diff changeset
   441
               (Const (@{const_name Trueprop},_) $ lhs)
f8999e19dd49 corrected some long-overseen misperceptions in recdef
haftmann
parents: 38549
diff changeset
   442
               $ (Const (@{const_name Trueprop},_) $ rhs)) = {lhs=lhs, rhs=rhs}
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   443
  | dest_equal(Const (@{const_name Pure.eq},_) $ lhs $ rhs) = {lhs=lhs, rhs=rhs}
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   444
  | dest_equal tm = USyntax.dest_eq tm;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   445
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   446
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm));
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   447
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   448
fun dest_all used (Const(@{const_name Pure.all},_) $ (a as Abs _)) = USyntax.dest_abs used a
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   449
  | dest_all _ _ = raise RULES_ERR "dest_all" "not a !!";
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   450
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   451
val is_all = can (dest_all []);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   452
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   453
fun strip_all used fm =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   454
   if (is_all fm)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   455
   then let val ({Bvar, Body}, used') = dest_all used fm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   456
            val (bvs, core, used'') = strip_all used' Body
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   457
        in ((Bvar::bvs), core, used'')
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   458
        end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   459
   else ([], fm, used);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   460
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   461
fun break_all(Const(@{const_name Pure.all},_) $ Abs (_,_,body)) = body
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   462
  | break_all _ = raise RULES_ERR "break_all" "not a !!";
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   463
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   464
fun list_break_all(Const(@{const_name Pure.all},_) $ Abs (s,ty,body)) =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   465
     let val (L,core) = list_break_all body
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   466
     in ((s,ty)::L, core)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   467
     end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   468
  | list_break_all tm = ([],tm);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   469
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   470
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   471
 * Rename a term of the form
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   472
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   473
 *      !!x1 ...xn. x1=M1 ==> ... ==> xn=Mn
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   474
 *                  ==> ((%v1...vn. Q) x1 ... xn = g x1 ... xn.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   475
 * to one of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   476
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   477
 *      !!v1 ... vn. v1=M1 ==> ... ==> vn=Mn
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   478
 *      ==> ((%v1...vn. Q) v1 ... vn = g v1 ... vn.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   479
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   480
 * This prevents name problems in extraction, and helps the result to read
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   481
 * better. There is a problem with varstructs, since they can introduce more
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   482
 * than n variables, and some extra reasoning needs to be done.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   483
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   484
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   485
fun get ([],_,L) = rev L
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   486
  | get (ant::rst,n,L) =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   487
      case (list_break_all ant)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   488
        of ([],_) => get (rst, n+1,L)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   489
         | (vlist,body) =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   490
            let val eq = Logic.strip_imp_concl body
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   491
                val (f,args) = USyntax.strip_comb (get_lhs eq)
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   492
                val (vstrl,_) = USyntax.strip_abs f
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   493
                val names  =
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 44058
diff changeset
   494
                  Name.variant_list (Misc_Legacy.add_term_names(body, [])) (map (#1 o dest_Free) vstrl)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   495
            in get (rst, n+1, (names,n)::L) end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   496
            handle TERM _ => get (rst, n+1, L)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   497
              | Utils.ERR _ => get (rst, n+1, L);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   498
31945
d5f186aa0bed structure Thm: less pervasive names;
wenzelm
parents: 30190
diff changeset
   499
(* Note: Thm.rename_params_rule counts from 1, not 0 *)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   500
fun rename thm =
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   501
  let
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   502
    val ants = Logic.strip_imp_prems (Thm.prop_of thm)
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   503
    val news = get (ants,1,[])
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   504
  in fold Thm.rename_params_rule news thm end;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   505
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   506
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   507
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   508
 * Beta-conversion to the rhs of an equation (taken from hol90/drule.sml)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   509
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   510
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   511
fun list_beta_conv tm =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   512
  let fun rbeta th = Thm.transitive th (Thm.beta_conversion false (#2(Dcterm.dest_eq(cconcl th))))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   513
      fun iter [] = Thm.reflexive tm
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36617
diff changeset
   514
        | iter (v::rst) = rbeta (Thm.combination(iter rst) (Thm.reflexive v))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   515
  in iter  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   516
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   517
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   518
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   519
 * Trace information for the rewriter
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   520
 *---------------------------------------------------------------------------*)
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32603
diff changeset
   521
val tracing = Unsynchronized.ref false;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   522
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   523
fun say s = if !tracing then writeln s else ();
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   524
55236
8d61b0aa7a0d proper context for printing;
wenzelm
parents: 54895
diff changeset
   525
fun print_thms ctxt s L =
8d61b0aa7a0d proper context for printing;
wenzelm
parents: 54895
diff changeset
   526
  say (cat_lines (s :: map (Display.string_of_thm ctxt) L));
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   527
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   528
fun print_term ctxt s t =
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   529
  say (cat_lines [s, Syntax.string_of_term ctxt t]);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   530
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   531
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   532
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   533
 * General abstraction handlers, should probably go in USyntax.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   534
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   535
fun mk_aabs (vstr, body) =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   536
  USyntax.mk_abs {Bvar = vstr, Body = body}
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   537
  handle Utils.ERR _ => USyntax.mk_pabs {varstruct = vstr, body = body};
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   538
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   539
fun list_mk_aabs (vstrl,tm) =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   540
    fold_rev (fn vstr => fn tm => mk_aabs(vstr,tm)) vstrl tm;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   541
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   542
fun dest_aabs used tm =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   543
   let val ({Bvar,Body}, used') = USyntax.dest_abs used tm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   544
   in (Bvar, Body, used') end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   545
   handle Utils.ERR _ =>
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   546
     let val {varstruct, body, used} = USyntax.dest_pabs used tm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   547
     in (varstruct, body, used) end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   548
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   549
fun strip_aabs used tm =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   550
   let val (vstr, body, used') = dest_aabs used tm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   551
       val (bvs, core, used'') = strip_aabs used' body
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   552
   in (vstr::bvs, core, used'') end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   553
   handle Utils.ERR _ => ([], tm, used);
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   554
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   555
fun dest_combn tm 0 = (tm,[])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   556
  | dest_combn tm n =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   557
     let val {Rator,Rand} = USyntax.dest_comb tm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   558
         val (f,rands) = dest_combn Rator (n-1)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   559
     in (f,Rand::rands)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   560
     end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   561
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   562
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   563
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   564
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   565
local fun dest_pair M = let val {fst,snd} = USyntax.dest_pair M in (fst,snd) end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   566
      fun mk_fst tm =
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37391
diff changeset
   567
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
55411
27de2c976d90 repaired hard-coded constant names
blanchet
parents: 55236
diff changeset
   568
          in  Const (@{const_name Product_Type.fst}, ty --> fty) $ tm  end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   569
      fun mk_snd tm =
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37391
diff changeset
   570
          let val ty as Type(@{type_name Product_Type.prod}, [fty,sty]) = type_of tm
55411
27de2c976d90 repaired hard-coded constant names
blanchet
parents: 55236
diff changeset
   571
          in  Const (@{const_name Product_Type.snd}, ty --> sty) $ tm  end
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   572
in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   573
fun XFILL tych x vstruct =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   574
  let fun traverse p xocc L =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   575
        if (is_Free p)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   576
        then tych xocc::L
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   577
        else let val (p1,p2) = dest_pair p
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   578
             in traverse p1 (mk_fst xocc) (traverse p2  (mk_snd xocc) L)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   579
             end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   580
  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   581
  traverse vstruct x []
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   582
end end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   583
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   584
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   585
 * Replace a free tuple (vstr) by a universally quantified variable (a).
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   586
 * Note that the notion of "freeness" for a tuple is different than for a
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   587
 * variable: if variables in the tuple also occur in any other place than
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   588
 * an occurrences of the tuple, they aren't "free" (which is thus probably
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   589
 *  the wrong word to use).
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   590
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   591
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   592
fun VSTRUCT_ELIM ctxt tych a vstr th =
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   593
  let val L = USyntax.free_vars_lr vstr
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   594
      val bind1 = tych (HOLogic.mk_Trueprop (HOLogic.mk_eq(a,vstr)))
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   595
      val thm1 = Thm.implies_intr bind1 (SUBS ctxt [SYM(Thm.assume bind1)] th)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   596
      val thm2 = forall_intr_list (map tych L) thm1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   597
      val thm3 = forall_elim_list (XFILL tych a vstr) thm2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   598
  in refl RS
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   599
     rewrite_rule ctxt [Thm.symmetric (@{thm surjective_pairing} RS eq_reflection)] thm3
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   600
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   601
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   602
fun PGEN ctxt tych a vstr th =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   603
  let val a1 = tych a
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   604
      val vstr1 = tych vstr
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   605
  in
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36617
diff changeset
   606
  Thm.forall_intr a1
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   607
     (if (is_Free vstr)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   608
      then cterm_instantiate [(vstr1,a1)] th
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   609
      else VSTRUCT_ELIM ctxt tych a vstr th)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   610
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   611
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   612
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   613
(*---------------------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   614
 * Takes apart a paired beta-redex, looking like "(\(x,y).N) vstr", into
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   615
 *
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   616
 *     (([x,y],N),vstr)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   617
 *---------------------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   618
fun dest_pbeta_redex used M n =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   619
  let val (f,args) = dest_combn M n
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   620
      val dummy = dest_aabs used f
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   621
  in (strip_aabs used f,args)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   622
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   623
60329
e85ed7a36b2f eliminated odd C combinator -- Isabelle/ML usually has canonical argument order;
wenzelm
parents: 59621
diff changeset
   624
fun pbeta_redex M n = can (fn t => dest_pbeta_redex [] t n) M;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   625
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   626
fun dest_impl tm =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   627
  let val ants = Logic.strip_imp_prems tm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   628
      val eq = Logic.strip_imp_concl tm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   629
  in (ants,get_lhs eq)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   630
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   631
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   632
fun restricted t = is_some (USyntax.find_term
44014
88bd7d74a2c1 moved recursion combinator to HOL/Library/Wfrec.thy -- it is so fundamental and well-known that it should survive recdef
krauss
parents: 44013
diff changeset
   633
                            (fn (Const(@{const_name Wfrec.cut},_)) =>true | _ => false)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   634
                            t)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   635
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   636
fun CONTEXT_REWRITE_RULE main_ctxt (func, G, cut_lemma, congs) th =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   637
 let val globals = func::G
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   638
     val ctxt0 = empty_simpset main_ctxt
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   639
     val pbeta_reduce = simpl_conv ctxt0 [@{thm split_conv} RS eq_reflection];
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32603
diff changeset
   640
     val tc_list = Unsynchronized.ref []: term list Unsynchronized.ref
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   641
     val cut_lemma' = cut_lemma RS eq_reflection
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   642
     fun prover used ctxt thm =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   643
     let fun cong_prover ctxt thm =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   644
         let val dummy = say "cong_prover:"
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   645
             val cntxt = Simplifier.prems_of ctxt
55236
8d61b0aa7a0d proper context for printing;
wenzelm
parents: 54895
diff changeset
   646
             val dummy = print_thms ctxt "cntxt:" cntxt
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   647
             val dummy = say "cong rule:"
55236
8d61b0aa7a0d proper context for printing;
wenzelm
parents: 54895
diff changeset
   648
             val dummy = say (Display.string_of_thm ctxt thm)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   649
             (* Unquantified eliminate *)
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   650
             fun uq_eliminate (thm,imp) =
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   651
                 let val tych = Thm.cterm_of ctxt
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   652
                     val _ = print_term ctxt "To eliminate:" imp
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   653
                     val ants = map tych (Logic.strip_imp_prems imp)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   654
                     val eq = Logic.strip_imp_concl imp
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   655
                     val lhs = tych(get_lhs eq)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   656
                     val ctxt' = Simplifier.add_prems (map ASSUME ants) ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   657
                     val lhs_eq_lhs1 = Raw_Simplifier.rewrite_cterm (false,true,false) (prover used) ctxt' lhs
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   658
                       handle Utils.ERR _ => Thm.reflexive lhs
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   659
                     val _ = print_thms ctxt' "proven:" [lhs_eq_lhs1]
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   660
                     val lhs_eq_lhs2 = implies_intr_list ants lhs_eq_lhs1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   661
                     val lhs_eeq_lhs2 = lhs_eq_lhs2 RS meta_eq_to_obj_eq
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   662
                  in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   663
                  lhs_eeq_lhs2 COMP thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   664
                  end
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   665
             fun pq_eliminate (thm, vlist, imp_body, lhs_eq) =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   666
              let val ((vstrl, _, used'), args) = dest_pbeta_redex used lhs_eq (length vlist)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   667
                  val dummy = forall (op aconv) (ListPair.zip (vlist, args))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   668
                    orelse error "assertion failed in CONTEXT_REWRITE_RULE"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   669
                  val imp_body1 = subst_free (ListPair.zip (args, vstrl))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   670
                                             imp_body
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   671
                  val tych = Thm.cterm_of ctxt
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   672
                  val ants1 = map tych (Logic.strip_imp_prems imp_body1)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   673
                  val eq1 = Logic.strip_imp_concl imp_body1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   674
                  val Q = get_lhs eq1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   675
                  val QeqQ1 = pbeta_reduce (tych Q)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   676
                  val Q1 = #2(Dcterm.dest_eq(cconcl QeqQ1))
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   677
                  val ctxt' = Simplifier.add_prems (map ASSUME ants1) ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   678
                  val Q1eeqQ2 = Raw_Simplifier.rewrite_cterm (false,true,false) (prover used') ctxt' Q1
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   679
                                handle Utils.ERR _ => Thm.reflexive Q1
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   680
                  val Q2 = #2 (Logic.dest_equals (Thm.prop_of Q1eeqQ2))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   681
                  val Q3 = tych(list_comb(list_mk_aabs(vstrl,Q2),vstrl))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   682
                  val Q2eeqQ3 = Thm.symmetric(pbeta_reduce Q3 RS eq_reflection)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   683
                  val thA = Thm.transitive(QeqQ1 RS eq_reflection) Q1eeqQ2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   684
                  val QeeqQ3 = Thm.transitive thA Q2eeqQ3 handle THM _ =>
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   685
                               ((Q2eeqQ3 RS meta_eq_to_obj_eq)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   686
                                RS ((thA RS meta_eq_to_obj_eq) RS trans))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   687
                                RS eq_reflection
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   688
                  val impth = implies_intr_list ants1 QeeqQ3
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   689
                  val impth1 = impth RS meta_eq_to_obj_eq
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   690
                  (* Need to abstract *)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 51717
diff changeset
   691
                  val ant_th = Utils.itlist2 (PGEN ctxt' tych) args vstrl impth1
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   692
              in ant_th COMP thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   693
              end
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   694
             fun q_eliminate (thm, imp) =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   695
              let val (vlist, imp_body, used') = strip_all used imp
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   696
                  val (ants,Q) = dest_impl imp_body
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   697
              in if (pbeta_redex Q) (length vlist)
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   698
                 then pq_eliminate (thm, vlist, imp_body, Q)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   699
                 else
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   700
                 let val tych = Thm.cterm_of ctxt
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   701
                     val ants1 = map tych ants
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   702
                     val ctxt' = Simplifier.add_prems (map ASSUME ants1) ctxt
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 41225
diff changeset
   703
                     val Q_eeq_Q1 = Raw_Simplifier.rewrite_cterm
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   704
                        (false,true,false) (prover used') ctxt' (tych Q)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   705
                      handle Utils.ERR _ => Thm.reflexive (tych Q)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   706
                     val lhs_eeq_lhs2 = implies_intr_list ants1 Q_eeq_Q1
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   707
                     val lhs_eq_lhs2 = lhs_eeq_lhs2 RS meta_eq_to_obj_eq
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   708
                     val ant_th = forall_intr_list(map tych vlist)lhs_eq_lhs2
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   709
                 in
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   710
                 ant_th COMP thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   711
              end end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   712
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   713
             fun eliminate thm =
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   714
               case Thm.prop_of thm of
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   715
                 Const(@{const_name Pure.imp},_) $ imp $ _ =>
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   716
                   eliminate
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   717
                    (if not(is_all imp)
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   718
                     then uq_eliminate (thm, imp)
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   719
                     else q_eliminate (thm, imp))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   720
                            (* Assume that the leading constant is ==,   *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   721
                | _ => thm  (* if it is not a ==>                        *)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   722
         in SOME(eliminate (rename thm)) end
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   723
         handle Utils.ERR _ => NONE    (* FIXME handle THM as well?? *)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   724
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   725
        fun restrict_prover ctxt thm =
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   726
          let val _ = say "restrict_prover:"
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   727
              val cntxt = rev (Simplifier.prems_of ctxt)
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   728
              val _ = print_thms ctxt "cntxt:" cntxt
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   729
              val Const(@{const_name Pure.imp},_) $ (Const(@{const_name Trueprop},_) $ A) $ _ =
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55411
diff changeset
   730
                Thm.prop_of thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   731
              fun genl tm = let val vlist = subtract (op aconv) globals
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 44058
diff changeset
   732
                                           (Misc_Legacy.add_term_frees(tm,[]))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   733
                            in fold_rev Forall vlist tm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   734
                            end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   735
              (*--------------------------------------------------------------
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   736
               * This actually isn't quite right, since it will think that
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   737
               * not-fully applied occs. of "f" in the context mean that the
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   738
               * current call is nested. The real solution is to pass in a
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   739
               * term "f v1..vn" which is a pattern that any full application
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   740
               * of "f" will match.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   741
               *-------------------------------------------------------------*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   742
              val func_name = #1(dest_Const func)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   743
              fun is_func (Const (name,_)) = (name = func_name)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   744
                | is_func _                = false
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   745
              val rcontext = rev cntxt
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   746
              val cncl = HOLogic.dest_Trueprop o Thm.prop_of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   747
              val antl = case rcontext of [] => []
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   748
                         | _   => [USyntax.list_mk_conj(map cncl rcontext)]
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   749
              val TC = genl(USyntax.list_mk_imp(antl, A))
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   750
              val _ = print_term ctxt "func:" func
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   751
              val _ = print_term ctxt "TC:" (HOLogic.mk_Trueprop TC)
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   752
              val _ = tc_list := (TC :: !tc_list)
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   753
              val nestedp = is_some (USyntax.find_term is_func TC)
60365
3e28769ba2b6 clarified context;
wenzelm
parents: 60363
diff changeset
   754
              val _ = if nestedp then say "nested" else say "not_nested"
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   755
              val th' = if nestedp then raise RULES_ERR "solver" "nested function"
60363
5568b16aa477 clarified context;
wenzelm
parents: 60335
diff changeset
   756
                        else let val cTC = Thm.cterm_of ctxt (HOLogic.mk_Trueprop TC)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   757
                             in case rcontext of
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   758
                                [] => SPEC_ALL(ASSUME cTC)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   759
                               | _ => MP (SPEC_ALL (ASSUME cTC))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   760
                                         (LIST_CONJ rcontext)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   761
                             end
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   762
              val th'' = th' RS thm
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   763
          in SOME (th'')
41164
6854e9a40edc avoid ML structure aliases (especially single-letter abbreviations);
wenzelm
parents: 38801
diff changeset
   764
          end handle Utils.ERR _ => NONE    (* FIXME handle THM as well?? *)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   765
    in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   766
    (if (is_cong thm) then cong_prover else restrict_prover) ctxt thm
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   767
    end
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59058
diff changeset
   768
    val ctm = Thm.cprop_of th
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59058
diff changeset
   769
    val names = Misc_Legacy.add_term_names (Thm.term_of ctm, [])
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 44121
diff changeset
   770
    val th1 =
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 44121
diff changeset
   771
      Raw_Simplifier.rewrite_cterm (false, true, false)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   772
        (prover names) (ctxt0 addsimps [cut_lemma'] |> fold Simplifier.add_eqcong congs) ctm
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36617
diff changeset
   773
    val th2 = Thm.equal_elim th1 th
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   774
 in
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33035
diff changeset
   775
 (th2, filter_out restricted (!tc_list))
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   776
 end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   777
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   778
60335
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
   779
fun prove ctxt strict (t, tac) =
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   780
  let
60335
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
   781
    val ctxt' = Variable.auto_fixes t ctxt;
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   782
  in
60335
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
   783
    if strict
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
   784
    then Goal.prove ctxt' [] [] t (K tac)
edac62cd7bde clarified context;
wenzelm
parents: 60334
diff changeset
   785
    else Goal.prove ctxt' [] [] t (K tac)
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   786
      handle ERROR msg => (warning msg; raise RULES_ERR "prove" msg)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   787
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   788
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
   789
end;