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