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