src/Pure/Isar/local_defs.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45407 a83574606719
child 45620 f2a587696afb
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
wenzelm@18830
     1
(*  Title:      Pure/Isar/local_defs.ML
wenzelm@18830
     2
    Author:     Makarius
wenzelm@18830
     3
wenzelm@18840
     4
Local definitions.
wenzelm@18830
     5
*)
wenzelm@18830
     6
wenzelm@18830
     7
signature LOCAL_DEFS =
wenzelm@18830
     8
sig
wenzelm@20306
     9
  val cert_def: Proof.context -> term -> (string * typ) * term
wenzelm@18830
    10
  val abs_def: term -> (string * typ) * term
wenzelm@21684
    11
  val expand: cterm list -> thm -> thm
wenzelm@20224
    12
  val def_export: Assumption.export
wenzelm@30211
    13
  val add_defs: ((binding * mixfix) * (Thm.binding * term)) list -> Proof.context ->
wenzelm@30211
    14
    (term * (string * thm)) list * Proof.context
haftmann@29581
    15
  val add_def: (binding * mixfix) * term -> Proof.context -> (term * thm) * Proof.context
haftmann@29581
    16
  val fixed_abbrev: (binding * mixfix) * term -> Proof.context ->
wenzelm@28083
    17
    (term * term) * Proof.context
wenzelm@45407
    18
  val export: Proof.context -> Proof.context -> thm -> (thm list * thm list) * thm
wenzelm@45407
    19
  val export_cterm: Proof.context -> Proof.context -> cterm -> (thm list * thm list) * cterm
wenzelm@21844
    20
  val trans_terms: Proof.context -> thm list -> thm
wenzelm@21844
    21
  val trans_props: Proof.context -> thm list -> thm
wenzelm@35739
    22
  val contract: Proof.context -> thm list -> cterm -> thm -> thm
wenzelm@21506
    23
  val print_rules: Proof.context -> unit
wenzelm@18840
    24
  val defn_add: attribute
wenzelm@18840
    25
  val defn_del: attribute
wenzelm@23541
    26
  val meta_rewrite_conv: Proof.context -> conv
wenzelm@21506
    27
  val meta_rewrite_rule: Proof.context -> thm -> thm
wenzelm@20306
    28
  val unfold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    29
  val unfold_goals: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    30
  val unfold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    31
  val fold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    32
  val fold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    33
  val derived_def: Proof.context -> bool -> term ->
wenzelm@20909
    34
    ((string * typ) * term) * (Proof.context -> thm -> thm)
wenzelm@18830
    35
end;
wenzelm@18830
    36
wenzelm@35624
    37
structure Local_Defs: LOCAL_DEFS =
wenzelm@18830
    38
struct
wenzelm@18830
    39
wenzelm@18840
    40
(** primitive definitions **)
wenzelm@18840
    41
wenzelm@18830
    42
(* prepare defs *)
wenzelm@18830
    43
wenzelm@18830
    44
fun cert_def ctxt eq =
wenzelm@18830
    45
  let
wenzelm@40242
    46
    fun err msg =
wenzelm@40242
    47
      cat_error msg ("The error(s) above occurred in definition:\n" ^
wenzelm@40242
    48
        quote (Syntax.string_of_term ctxt eq));
wenzelm@18950
    49
    val ((lhs, _), eq') = eq
wenzelm@39133
    50
      |> Sign.no_vars ctxt
wenzelm@33385
    51
      |> Primitive_Defs.dest_def ctxt Term.is_Free (Variable.is_fixed ctxt) (K true)
wenzelm@18950
    52
      handle TERM (msg, _) => err msg | ERROR msg => err msg;
wenzelm@18950
    53
  in (Term.dest_Free (Term.head_of lhs), eq') end;
wenzelm@18830
    54
wenzelm@33385
    55
val abs_def = Primitive_Defs.abs_def #>> Term.dest_Free;
wenzelm@18830
    56
wenzelm@18875
    57
fun mk_def ctxt args =
wenzelm@18875
    58
  let
wenzelm@42501
    59
    val (bs, rhss) = split_list args;
wenzelm@42501
    60
    val Ts = map Term.fastype_of rhss;
wenzelm@42501
    61
    val (xs, _) = Proof_Context.add_fixes (map2 (fn b => fn T => (b, SOME T, NoSyn)) bs Ts) ctxt;
wenzelm@42501
    62
    val lhss = ListPair.map Free (xs, Ts);
wenzelm@18875
    63
  in map Logic.mk_equals (lhss ~~ rhss) end;
wenzelm@18875
    64
wenzelm@18830
    65
wenzelm@18830
    66
(* export defs *)
wenzelm@18830
    67
wenzelm@20021
    68
val head_of_def =
wenzelm@45406
    69
  Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
wenzelm@20021
    70
wenzelm@18830
    71
wenzelm@18875
    72
(*
wenzelm@20887
    73
  [x, x == a]
wenzelm@18896
    74
       :
wenzelm@18896
    75
      B x
wenzelm@18896
    76
  -----------
wenzelm@20887
    77
      B a
wenzelm@18875
    78
*)
wenzelm@21684
    79
fun expand defs =
wenzelm@21684
    80
  Drule.implies_intr_list defs
wenzelm@45406
    81
  #> Drule.generalize ([], map (#1 o head_of_def o Thm.term_of) defs)
wenzelm@21684
    82
  #> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
wenzelm@21684
    83
wenzelm@21801
    84
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of);
wenzelm@21684
    85
wenzelm@21684
    86
fun def_export _ defs = (expand defs, expand_term defs);
wenzelm@18830
    87
wenzelm@18830
    88
wenzelm@18830
    89
(* add defs *)
wenzelm@18830
    90
wenzelm@20887
    91
fun add_defs defs ctxt =
wenzelm@18830
    92
  let
wenzelm@42496
    93
    val ((xs, mxs), specs) = defs |> split_list |>> split_list;
wenzelm@28083
    94
    val ((bfacts, atts), rhss) = specs |> split_list |>> split_list;
wenzelm@42496
    95
    val names = map2 Thm.def_binding_optional xs bfacts;
wenzelm@20887
    96
    val eqs = mk_def ctxt (xs ~~ rhss);
wenzelm@20887
    97
    val lhss = map (fst o Logic.dest_equals) eqs;
wenzelm@18830
    98
  in
wenzelm@18830
    99
    ctxt
wenzelm@42496
   100
    |> Proof_Context.add_fixes (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
wenzelm@20980
   101
    |> fold Variable.declare_term eqs
wenzelm@42360
   102
    |> Proof_Context.add_assms_i def_export
wenzelm@28083
   103
      (map2 (fn a => fn eq => (a, [(eq, [])])) (names ~~ atts) eqs)
wenzelm@20887
   104
    |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
wenzelm@18830
   105
  end;
wenzelm@18830
   106
wenzelm@25104
   107
fun add_def (var, rhs) ctxt =
wenzelm@30211
   108
  let val ([(lhs, (_, th))], ctxt') = add_defs [(var, (Thm.empty_binding, rhs))] ctxt
wenzelm@25104
   109
  in ((lhs, th), ctxt') end;
wenzelm@25025
   110
wenzelm@18840
   111
wenzelm@25119
   112
(* fixed_abbrev *)
wenzelm@25119
   113
wenzelm@25119
   114
fun fixed_abbrev ((x, mx), rhs) ctxt =
wenzelm@25119
   115
  let
wenzelm@25119
   116
    val T = Term.fastype_of rhs;
wenzelm@25119
   117
    val ([x'], ctxt') = ctxt
wenzelm@25119
   118
      |> Variable.declare_term rhs
wenzelm@42360
   119
      |> Proof_Context.add_fixes [(x, SOME T, mx)];
wenzelm@25119
   120
    val lhs = Free (x', T);
wenzelm@25119
   121
    val _ = cert_def ctxt' (Logic.mk_equals (lhs, rhs));
wenzelm@25119
   122
    fun abbrev_export _ _ = (I, Envir.expand_term_frees [((x', T), rhs)]);
wenzelm@25119
   123
    val (_, ctxt'') = Assumption.add_assms abbrev_export [] ctxt';
wenzelm@25119
   124
  in ((lhs, rhs), ctxt'') end;
wenzelm@25119
   125
wenzelm@25119
   126
wenzelm@24985
   127
(* specific export -- result based on educated guessing *)
wenzelm@21568
   128
wenzelm@24985
   129
(*
wenzelm@24985
   130
  [xs, xs == as]
wenzelm@24985
   131
        :
wenzelm@24985
   132
       B xs
wenzelm@24985
   133
  --------------
wenzelm@24985
   134
       B as
wenzelm@24985
   135
*)
wenzelm@24985
   136
fun export inner outer =    (*beware of closure sizes*)
wenzelm@21568
   137
  let
wenzelm@22671
   138
    val exp = Assumption.export false inner outer;
wenzelm@45406
   139
    val exp_term = Assumption.export_term inner outer;
wenzelm@30473
   140
    val prems = Assumption.all_prems_of inner;
wenzelm@45407
   141
  in
wenzelm@45407
   142
    fn th =>
wenzelm@45407
   143
      let
wenzelm@45407
   144
        val th' = exp th;
wenzelm@45407
   145
        val defs_asms = prems |> map (fn prem =>
wenzelm@45407
   146
          (case try (head_of_def o Thm.prop_of) prem of
wenzelm@45407
   147
            NONE => (prem, false)
wenzelm@45407
   148
          | SOME x =>
wenzelm@45407
   149
              let val t = Free x in
wenzelm@45407
   150
                (case try exp_term t of
wenzelm@45407
   151
                  NONE => (prem, false)
wenzelm@45407
   152
                | SOME u =>
wenzelm@45407
   153
                    if t aconv u then (prem, false)
wenzelm@45407
   154
                    else (Drule.abs_def prem, true))
wenzelm@45407
   155
              end));
wenzelm@45407
   156
      in (pairself (map #1) (List.partition #2 defs_asms), th') end
wenzelm@22671
   157
  end;
wenzelm@21568
   158
wenzelm@24985
   159
(*
wenzelm@24985
   160
  [xs, xs == as]
wenzelm@24985
   161
        :
wenzelm@24985
   162
     TERM b xs
wenzelm@24985
   163
  --------------  and  --------------
wenzelm@24985
   164
     TERM b as          b xs == b as
wenzelm@24985
   165
*)
wenzelm@24985
   166
fun export_cterm inner outer ct =
wenzelm@35717
   167
  export inner outer (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@24985
   168
wenzelm@21568
   169
wenzelm@21844
   170
(* basic transitivity reasoning -- modulo beta-eta *)
wenzelm@21844
   171
wenzelm@21844
   172
local
wenzelm@21844
   173
wenzelm@21844
   174
val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of;
wenzelm@21844
   175
wenzelm@21844
   176
fun trans_list _ _ [] = raise Empty
wenzelm@21844
   177
  | trans_list trans ctxt (th :: raw_eqs) =
wenzelm@21844
   178
      (case filter_out is_trivial raw_eqs of
wenzelm@21844
   179
        [] => th
wenzelm@21844
   180
      | eqs =>
wenzelm@31794
   181
          let val ((_, th' :: eqs'), ctxt') = Variable.import true (th :: eqs) ctxt
wenzelm@21844
   182
          in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end);
wenzelm@21844
   183
wenzelm@21844
   184
in
wenzelm@21844
   185
wenzelm@21844
   186
val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm));
wenzelm@21844
   187
val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1));
wenzelm@21844
   188
wenzelm@21844
   189
end;
wenzelm@21844
   190
wenzelm@35739
   191
fun contract ctxt defs ct th =
wenzelm@41228
   192
  trans_props ctxt [th, Thm.symmetric (Raw_Simplifier.rewrite true defs ct)];
wenzelm@21844
   193
wenzelm@18840
   194
wenzelm@18840
   195
(** defived definitions **)
wenzelm@18840
   196
wenzelm@18840
   197
(* transformation rules *)
wenzelm@18840
   198
wenzelm@33519
   199
structure Rules = Generic_Data
wenzelm@18840
   200
(
wenzelm@18840
   201
  type T = thm list;
wenzelm@33519
   202
  val empty = [];
wenzelm@18840
   203
  val extend = I;
wenzelm@33519
   204
  val merge = Thm.merge_thms;
wenzelm@18840
   205
);
wenzelm@18840
   206
wenzelm@22846
   207
fun print_rules ctxt =
wenzelm@22846
   208
  Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@32091
   209
    (map (Display.pretty_thm ctxt) (Rules.get (Context.Proof ctxt))));
wenzelm@18840
   210
wenzelm@24039
   211
val defn_add = Thm.declaration_attribute (Rules.map o Thm.add_thm);
wenzelm@24039
   212
val defn_del = Thm.declaration_attribute (Rules.map o Thm.del_thm);
wenzelm@18840
   213
wenzelm@18840
   214
wenzelm@18875
   215
(* meta rewrite rules *)
wenzelm@18840
   216
wenzelm@23541
   217
fun meta_rewrite_conv ctxt =
wenzelm@41228
   218
  Raw_Simplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@41228
   219
    (Raw_Simplifier.context ctxt empty_ss
wenzelm@26424
   220
      addeqcongs [Drule.equals_cong]    (*protect meta-level equality*)
wenzelm@26424
   221
      addsimps (Rules.get (Context.Proof ctxt)));
wenzelm@18840
   222
wenzelm@23541
   223
val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv;
wenzelm@18840
   224
wenzelm@18875
   225
wenzelm@18875
   226
(* rewriting with object-level rules *)
wenzelm@18875
   227
wenzelm@21506
   228
fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
wenzelm@18840
   229
wenzelm@41228
   230
val unfold       = meta Raw_Simplifier.rewrite_rule;
wenzelm@41228
   231
val unfold_goals = meta Raw_Simplifier.rewrite_goals_rule;
wenzelm@41228
   232
val unfold_tac   = meta Raw_Simplifier.rewrite_goals_tac;
wenzelm@41228
   233
val fold         = meta Raw_Simplifier.fold_rule;
wenzelm@41228
   234
val fold_tac     = meta Raw_Simplifier.fold_goals_tac;
wenzelm@18840
   235
wenzelm@18840
   236
wenzelm@18840
   237
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   238
wenzelm@18950
   239
fun derived_def ctxt conditional prop =
wenzelm@18840
   240
  let
wenzelm@18840
   241
    val ((c, T), rhs) = prop
wenzelm@42360
   242
      |> Thm.cterm_of (Proof_Context.theory_of ctxt)
wenzelm@23541
   243
      |> meta_rewrite_conv ctxt
wenzelm@18840
   244
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@21568
   245
      |> conditional ? Logic.strip_imp_concl
wenzelm@18950
   246
      |> (abs_def o #2 o cert_def ctxt);
wenzelm@20909
   247
    fun prove ctxt' def =
wenzelm@42482
   248
      Goal.prove ctxt' (Variable.add_free_names ctxt' prop []) [] prop (K (ALLGOALS
wenzelm@42482
   249
        (CONVERSION (meta_rewrite_conv ctxt') THEN'
wenzelm@42482
   250
          rewrite_goal_tac [def] THEN'
wenzelm@42482
   251
          resolve_tac [Drule.reflexive_thm])))
wenzelm@42482
   252
      handle ERROR msg => cat_error msg "Failed to prove definitional specification";
wenzelm@18840
   253
  in (((c, T), rhs), prove) end;
wenzelm@18840
   254
wenzelm@18830
   255
end;