src/Pure/Isar/local_defs.ML
author wenzelm
Wed Dec 06 21:19:01 2006 +0100 (2006-12-06 ago)
changeset 21684 e8c135b166b3
parent 21591 5e0f2340caa7
child 21687 f689f729afab
permissions -rw-r--r--
added expand;
export: added explicit term operation;
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@21506
    18
  val print_rules: Proof.context -> unit
wenzelm@18840
    19
  val defn_add: attribute
wenzelm@18840
    20
  val defn_del: attribute
wenzelm@21506
    21
  val meta_rewrite_rule: Proof.context -> thm -> thm
wenzelm@20306
    22
  val unfold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    23
  val unfold_goals: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    24
  val unfold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    25
  val fold: Proof.context -> thm list -> thm -> thm
wenzelm@20306
    26
  val fold_tac: Proof.context -> thm list -> tactic
wenzelm@20306
    27
  val derived_def: Proof.context -> bool -> term ->
wenzelm@20909
    28
    ((string * typ) * term) * (Proof.context -> thm -> thm)
wenzelm@18830
    29
end;
wenzelm@18830
    30
wenzelm@18830
    31
structure LocalDefs: LOCAL_DEFS =
wenzelm@18830
    32
struct
wenzelm@18830
    33
wenzelm@18840
    34
(** primitive definitions **)
wenzelm@18840
    35
wenzelm@18830
    36
(* prepare defs *)
wenzelm@18830
    37
wenzelm@18830
    38
fun cert_def ctxt eq =
wenzelm@18830
    39
  let
wenzelm@18950
    40
    val pp = ProofContext.pp ctxt;
wenzelm@18950
    41
    val display_term = quote o Pretty.string_of_term pp;
wenzelm@18950
    42
    fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ display_term eq);
wenzelm@18950
    43
    val ((lhs, _), eq') = eq
wenzelm@18950
    44
      |> Sign.no_vars pp
wenzelm@19897
    45
      |> Logic.dest_def pp Term.is_Free (Variable.is_fixed ctxt) (K true)
wenzelm@18950
    46
      handle TERM (msg, _) => err msg | ERROR msg => err msg;
wenzelm@18950
    47
  in (Term.dest_Free (Term.head_of lhs), eq') end;
wenzelm@18830
    48
wenzelm@18950
    49
val abs_def = Logic.abs_def #> apfst Term.dest_Free;
wenzelm@18830
    50
wenzelm@18875
    51
fun mk_def ctxt args =
wenzelm@18875
    52
  let
wenzelm@18875
    53
    val (xs, rhss) = split_list args;
wenzelm@18875
    54
    val (bind, _) = ProofContext.bind_fixes xs ctxt;
wenzelm@18875
    55
    val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
wenzelm@18875
    56
  in map Logic.mk_equals (lhss ~~ rhss) end;
wenzelm@18875
    57
wenzelm@18830
    58
wenzelm@18830
    59
(* export defs *)
wenzelm@18830
    60
wenzelm@20021
    61
val head_of_def =
wenzelm@20887
    62
  #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
wenzelm@20021
    63
wenzelm@18830
    64
wenzelm@18875
    65
(*
wenzelm@20887
    66
  [x, x == a]
wenzelm@18896
    67
       :
wenzelm@18896
    68
      B x
wenzelm@18896
    69
  -----------
wenzelm@20887
    70
      B a
wenzelm@18875
    71
*)
wenzelm@21684
    72
fun expand defs =
wenzelm@21684
    73
  Drule.implies_intr_list defs
wenzelm@21684
    74
  #> Drule.generalize ([], map (head_of_def o Thm.term_of) defs)
wenzelm@21684
    75
  #> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
wenzelm@21684
    76
wenzelm@21684
    77
fun expand_term [] = I
wenzelm@21684
    78
  | expand_term defs = Pattern.rewrite_term
wenzelm@21684
    79
      (Theory.merge_list (map Thm.theory_of_cterm defs))
wenzelm@21684
    80
      (map (Logic.dest_equals o Thm.prop_of o Assumption.assume) defs) [];
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@21684
   104
(* specific export -- result based on educated guessing *)
wenzelm@21568
   105
wenzelm@21591
   106
fun export inner outer th =
wenzelm@21568
   107
  let
wenzelm@21568
   108
    val th' = Assumption.export false inner outer th;
wenzelm@21591
   109
    val still_fixed = map #1 (Drule.fold_terms Term.add_frees th' []);
wenzelm@21591
   110
    val defs = Assumption.prems_of inner |> filter_out (fn prem =>
wenzelm@21591
   111
      (case try (head_of_def o Thm.prop_of) prem of
wenzelm@21591
   112
        SOME x => member (op =) still_fixed x
wenzelm@21591
   113
      | NONE => true));
wenzelm@21591
   114
  in (map Drule.abs_def defs, th') end;
wenzelm@21568
   115
wenzelm@21568
   116
wenzelm@18840
   117
wenzelm@18840
   118
(** defived definitions **)
wenzelm@18840
   119
wenzelm@18840
   120
(* transformation rules *)
wenzelm@18840
   121
wenzelm@18859
   122
structure Rules = GenericDataFun
wenzelm@18840
   123
(
wenzelm@18840
   124
  val name = "Pure/derived_defs";
wenzelm@18840
   125
  type T = thm list;
wenzelm@18840
   126
  val empty = []
wenzelm@18840
   127
  val extend = I;
wenzelm@18840
   128
  fun merge _ = Drule.merge_rules;
wenzelm@18840
   129
  fun print context rules =
wenzelm@18840
   130
    Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@18840
   131
      (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
wenzelm@18840
   132
);
wenzelm@18840
   133
wenzelm@18859
   134
val _ = Context.add_setup Rules.init;
wenzelm@18840
   135
wenzelm@21506
   136
val print_rules = Rules.print o Context.Proof;
wenzelm@18840
   137
wenzelm@18859
   138
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
wenzelm@18859
   139
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
wenzelm@18840
   140
wenzelm@18840
   141
wenzelm@18875
   142
(* meta rewrite rules *)
wenzelm@18840
   143
wenzelm@18840
   144
val equals_ss =
wenzelm@18840
   145
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18840
   146
    addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
wenzelm@18840
   147
wenzelm@21506
   148
fun meta_rewrite ctxt =
wenzelm@18840
   149
  MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@21506
   150
    (equals_ss addsimps (Rules.get (Context.Proof ctxt)));
wenzelm@18840
   151
wenzelm@18859
   152
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
wenzelm@18840
   153
wenzelm@18840
   154
fun meta_rewrite_tac ctxt i =
wenzelm@21506
   155
  PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite ctxt)));
wenzelm@18840
   156
wenzelm@18875
   157
wenzelm@18875
   158
(* rewriting with object-level rules *)
wenzelm@18875
   159
wenzelm@21506
   160
fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
wenzelm@18840
   161
wenzelm@18875
   162
val unfold       = meta Tactic.rewrite_rule;
wenzelm@18875
   163
val unfold_goals = meta Tactic.rewrite_goals_rule;
wenzelm@18875
   164
val unfold_tac   = meta Tactic.rewrite_goals_tac;
wenzelm@18875
   165
val fold         = meta Tactic.fold_rule;
wenzelm@18875
   166
val fold_tac     = meta Tactic.fold_goals_tac;
wenzelm@18840
   167
wenzelm@18840
   168
wenzelm@18840
   169
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   170
wenzelm@18950
   171
fun derived_def ctxt conditional prop =
wenzelm@18840
   172
  let
wenzelm@18840
   173
    val ((c, T), rhs) = prop
wenzelm@20049
   174
      |> Thm.cterm_of (ProofContext.theory_of ctxt)
wenzelm@21506
   175
      |> meta_rewrite ctxt
wenzelm@18840
   176
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@21568
   177
      |> conditional ? Logic.strip_imp_concl
wenzelm@18950
   178
      |> (abs_def o #2 o cert_def ctxt);
wenzelm@20909
   179
    fun prove ctxt' def =
wenzelm@18840
   180
      let
wenzelm@18840
   181
        val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@20909
   182
          if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
wenzelm@18840
   183
      in
wenzelm@20909
   184
        Goal.prove ctxt' frees [] prop (K (ALLGOALS
wenzelm@18840
   185
          (meta_rewrite_tac ctxt' THEN'
wenzelm@18840
   186
            Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18840
   187
            Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18840
   188
        handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18840
   189
      end;
wenzelm@18840
   190
  in (((c, T), rhs), prove) end;
wenzelm@18840
   191
wenzelm@18830
   192
end;