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