src/Pure/Isar/local_defs.ML
author wenzelm
Tue Jul 03 17:17:15 2007 +0200 (2007-07-03 ago)
changeset 23541 f8c5e218e4d8
parent 22900 f8a7c10e1bd0
child 24039 273698405054
permissions -rw-r--r--
exported meta_rewrite_conv;
CONVERSION tactical;
wenzelm@18830
     1
(*  Title:      Pure/Isar/local_defs.ML
wenzelm@18830
     2
    ID:         $Id$
wenzelm@18830
     3
    Author:     Makarius
wenzelm@18830
     4
wenzelm@18840
     5
Local definitions.
wenzelm@18830
     6
*)
wenzelm@18830
     7
wenzelm@18830
     8
signature LOCAL_DEFS =
wenzelm@18830
     9
sig
wenzelm@20306
    10
  val cert_def: Proof.context -> term -> (string * typ) * term
wenzelm@18830
    11
  val abs_def: term -> (string * typ) * term
wenzelm@20306
    12
  val mk_def: Proof.context -> (string * term) list -> term list
wenzelm@21684
    13
  val expand: cterm list -> thm -> thm
wenzelm@20224
    14
  val def_export: Assumption.export
wenzelm@20887
    15
  val add_defs: ((string * mixfix) * ((bstring * attribute list) * term)) list -> Proof.context ->
wenzelm@20887
    16
    (term * (bstring * thm)) list * Proof.context
wenzelm@21591
    17
  val export: Proof.context -> Proof.context -> thm -> thm list * thm
wenzelm@21844
    18
  val trans_terms: Proof.context -> thm list -> thm
wenzelm@21844
    19
  val trans_props: Proof.context -> thm list -> thm
wenzelm@21506
    20
  val print_rules: Proof.context -> unit
wenzelm@18840
    21
  val defn_add: attribute
wenzelm@18840
    22
  val defn_del: attribute
wenzelm@23541
    23
  val meta_rewrite_conv: Proof.context -> conv
wenzelm@21506
    24
  val meta_rewrite_rule: Proof.context -> thm -> thm
wenzelm@20306
    25
  val unfold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    26
  val unfold_goals: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    27
  val unfold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    28
  val fold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    29
  val fold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    30
  val derived_def: Proof.context -> bool -> term ->
wenzelm@20909
    31
    ((string * typ) * term) * (Proof.context -> thm -> thm)
wenzelm@18830
    32
end;
wenzelm@18830
    33
wenzelm@18830
    34
structure LocalDefs: LOCAL_DEFS =
wenzelm@18830
    35
struct
wenzelm@18830
    36
wenzelm@18840
    37
(** primitive definitions **)
wenzelm@18840
    38
wenzelm@18830
    39
(* prepare defs *)
wenzelm@18830
    40
wenzelm@18830
    41
fun cert_def ctxt eq =
wenzelm@18830
    42
  let
wenzelm@18950
    43
    val pp = ProofContext.pp ctxt;
wenzelm@18950
    44
    val display_term = quote o Pretty.string_of_term pp;
wenzelm@18950
    45
    fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ display_term eq);
wenzelm@18950
    46
    val ((lhs, _), eq') = eq
wenzelm@18950
    47
      |> Sign.no_vars pp
wenzelm@19897
    48
      |> Logic.dest_def pp Term.is_Free (Variable.is_fixed ctxt) (K true)
wenzelm@18950
    49
      handle TERM (msg, _) => err msg | ERROR msg => err msg;
wenzelm@18950
    50
  in (Term.dest_Free (Term.head_of lhs), eq') end;
wenzelm@18830
    51
wenzelm@21699
    52
val abs_def = Logic.abs_def #>> Term.dest_Free;
wenzelm@18830
    53
wenzelm@18875
    54
fun mk_def ctxt args =
wenzelm@18875
    55
  let
wenzelm@18875
    56
    val (xs, rhss) = split_list args;
wenzelm@18875
    57
    val (bind, _) = ProofContext.bind_fixes xs ctxt;
wenzelm@18875
    58
    val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
wenzelm@18875
    59
  in map Logic.mk_equals (lhss ~~ rhss) end;
wenzelm@18875
    60
wenzelm@18830
    61
wenzelm@18830
    62
(* export defs *)
wenzelm@18830
    63
wenzelm@20021
    64
val head_of_def =
wenzelm@20887
    65
  #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
wenzelm@20021
    66
wenzelm@18830
    67
wenzelm@18875
    68
(*
wenzelm@20887
    69
  [x, x == a]
wenzelm@18896
    70
       :
wenzelm@18896
    71
      B x
wenzelm@18896
    72
  -----------
wenzelm@20887
    73
      B a
wenzelm@18875
    74
*)
wenzelm@21684
    75
fun expand defs =
wenzelm@21684
    76
  Drule.implies_intr_list defs
wenzelm@21684
    77
  #> Drule.generalize ([], map (head_of_def o Thm.term_of) defs)
wenzelm@21684
    78
  #> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
wenzelm@21684
    79
wenzelm@21801
    80
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of);
wenzelm@21684
    81
wenzelm@21684
    82
fun def_export _ defs = (expand defs, expand_term defs);
wenzelm@18830
    83
wenzelm@18830
    84
wenzelm@18830
    85
(* add defs *)
wenzelm@18830
    86
wenzelm@20887
    87
fun add_defs defs ctxt =
wenzelm@18830
    88
  let
wenzelm@20887
    89
    val ((xs, mxs), specs) = defs |> split_list |>> split_list;
wenzelm@20887
    90
    val ((names, atts), rhss) = specs |> split_list |>> split_list;
wenzelm@20887
    91
    val names' = map2 Thm.def_name_optional xs names;
wenzelm@20887
    92
    val eqs = mk_def ctxt (xs ~~ rhss);
wenzelm@20887
    93
    val lhss = map (fst o Logic.dest_equals) eqs;
wenzelm@18830
    94
  in
wenzelm@18830
    95
    ctxt
wenzelm@20887
    96
    |> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
wenzelm@20980
    97
    |> fold Variable.declare_term eqs
wenzelm@20887
    98
    |> ProofContext.add_assms_i def_export
wenzelm@20887
    99
      (map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs)
wenzelm@20887
   100
    |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
wenzelm@18830
   101
  end;
wenzelm@18830
   102
wenzelm@18840
   103
wenzelm@22671
   104
(* specific export -- result based on educated guessing (beware of closure sizes) *)
wenzelm@21568
   105
wenzelm@22671
   106
fun export inner outer =
wenzelm@21568
   107
  let
wenzelm@22671
   108
    val exp = Assumption.export false inner outer;
wenzelm@22671
   109
    val prems = Assumption.prems_of inner;
wenzelm@22671
   110
  in fn th =>
wenzelm@22671
   111
    let
wenzelm@22671
   112
      val th' = exp th;
wenzelm@22691
   113
      val still_fixed = map #1 (Thm.fold_terms Term.add_frees th' []);
wenzelm@22671
   114
      val defs = prems |> filter_out (fn prem =>
wenzelm@22671
   115
        (case try (head_of_def o Thm.prop_of) prem of
wenzelm@22671
   116
          SOME x => member (op =) still_fixed x
wenzelm@22671
   117
        | NONE => true));
wenzelm@22671
   118
    in (map Drule.abs_def defs, th') end
wenzelm@22671
   119
  end;
wenzelm@21568
   120
wenzelm@21568
   121
wenzelm@21844
   122
(* basic transitivity reasoning -- modulo beta-eta *)
wenzelm@21844
   123
wenzelm@21844
   124
local
wenzelm@21844
   125
wenzelm@21844
   126
val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of;
wenzelm@21844
   127
wenzelm@21844
   128
fun trans_list _ _ [] = raise Empty
wenzelm@21844
   129
  | trans_list trans ctxt (th :: raw_eqs) =
wenzelm@21844
   130
      (case filter_out is_trivial raw_eqs of
wenzelm@21844
   131
        [] => th
wenzelm@21844
   132
      | eqs =>
wenzelm@22568
   133
          let val ((_, th' :: eqs'), ctxt') = Variable.import_thms true (th :: eqs) ctxt
wenzelm@21844
   134
          in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end);
wenzelm@21844
   135
wenzelm@21844
   136
in
wenzelm@21844
   137
wenzelm@21844
   138
val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm));
wenzelm@21844
   139
val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1));
wenzelm@21844
   140
wenzelm@21844
   141
end;
wenzelm@21844
   142
wenzelm@21844
   143
wenzelm@18840
   144
wenzelm@18840
   145
(** defived definitions **)
wenzelm@18840
   146
wenzelm@18840
   147
(* transformation rules *)
wenzelm@18840
   148
wenzelm@18859
   149
structure Rules = GenericDataFun
wenzelm@18840
   150
(
wenzelm@18840
   151
  type T = thm list;
wenzelm@18840
   152
  val empty = []
wenzelm@18840
   153
  val extend = I;
wenzelm@18840
   154
  fun merge _ = Drule.merge_rules;
wenzelm@18840
   155
);
wenzelm@18840
   156
wenzelm@22846
   157
fun print_rules ctxt =
wenzelm@22846
   158
  Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@22846
   159
    (map (ProofContext.pretty_thm ctxt) (Rules.get (Context.Proof ctxt))));
wenzelm@18840
   160
wenzelm@18859
   161
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
wenzelm@18859
   162
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
wenzelm@18840
   163
wenzelm@18840
   164
wenzelm@18875
   165
(* meta rewrite rules *)
wenzelm@18840
   166
wenzelm@18840
   167
val equals_ss =
wenzelm@18840
   168
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18840
   169
    addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
wenzelm@18840
   170
wenzelm@23541
   171
fun meta_rewrite_conv ctxt =
wenzelm@18840
   172
  MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@21506
   173
    (equals_ss addsimps (Rules.get (Context.Proof ctxt)));
wenzelm@18840
   174
wenzelm@23541
   175
val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv;
wenzelm@18840
   176
wenzelm@18875
   177
wenzelm@18875
   178
(* rewriting with object-level rules *)
wenzelm@18875
   179
wenzelm@21506
   180
fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
wenzelm@18840
   181
wenzelm@21708
   182
val unfold       = meta MetaSimplifier.rewrite_rule;
wenzelm@21708
   183
val unfold_goals = meta MetaSimplifier.rewrite_goals_rule;
wenzelm@21708
   184
val unfold_tac   = meta MetaSimplifier.rewrite_goals_tac;
wenzelm@21708
   185
val fold         = meta MetaSimplifier.fold_rule;
wenzelm@21708
   186
val fold_tac     = meta MetaSimplifier.fold_goals_tac;
wenzelm@18840
   187
wenzelm@18840
   188
wenzelm@18840
   189
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   190
wenzelm@18950
   191
fun derived_def ctxt conditional prop =
wenzelm@18840
   192
  let
wenzelm@18840
   193
    val ((c, T), rhs) = prop
wenzelm@20049
   194
      |> Thm.cterm_of (ProofContext.theory_of ctxt)
wenzelm@23541
   195
      |> meta_rewrite_conv ctxt
wenzelm@18840
   196
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@21568
   197
      |> conditional ? Logic.strip_imp_concl
wenzelm@18950
   198
      |> (abs_def o #2 o cert_def ctxt);
wenzelm@20909
   199
    fun prove ctxt' def =
wenzelm@18840
   200
      let
wenzelm@18840
   201
        val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@20909
   202
          if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
wenzelm@18840
   203
      in
wenzelm@20909
   204
        Goal.prove ctxt' frees [] prop (K (ALLGOALS
wenzelm@23541
   205
          (CONVERSION (meta_rewrite_conv ctxt') THEN'
wenzelm@23541
   206
            MetaSimplifier.rewrite_goal_tac [def] THEN'
wenzelm@21708
   207
            resolve_tac [Drule.reflexive_thm])))
wenzelm@18840
   208
        handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18840
   209
      end;
wenzelm@18840
   210
  in (((c, T), rhs), prove) end;
wenzelm@18840
   211
wenzelm@18830
   212
end;