src/Pure/Isar/local_defs.ML
author wenzelm
Wed Oct 11 22:55:17 2006 +0200 (2006-10-11 ago)
changeset 20980 e4fd72aecd03
parent 20909 7132ab2b4621
child 21506 b2a673894ce5
permissions -rw-r--r--
add_defs: declare terms;
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 find_def: Proof.context -> string -> thm option
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@18840
    17
  val print_rules: Context.generic -> unit
wenzelm@18840
    18
  val defn_add: attribute
wenzelm@18840
    19
  val defn_del: attribute
wenzelm@18859
    20
  val meta_rewrite_rule: Context.generic -> 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@20887
    78
(* find def *)
wenzelm@20887
    79
wenzelm@20887
    80
fun find_def ctxt x =
wenzelm@20887
    81
  Assumption.prems_of ctxt |> find_first (fn th =>
wenzelm@20887
    82
    (case try (head_of_def o Thm.prop_of) th of
wenzelm@20887
    83
      SOME y => x = y
wenzelm@20887
    84
    | NONE => false));
wenzelm@20887
    85
wenzelm@20887
    86
wenzelm@18830
    87
(* add defs *)
wenzelm@18830
    88
wenzelm@20887
    89
fun add_defs defs ctxt =
wenzelm@18830
    90
  let
wenzelm@20887
    91
    val ((xs, mxs), specs) = defs |> split_list |>> split_list;
wenzelm@20887
    92
    val ((names, atts), rhss) = specs |> split_list |>> split_list;
wenzelm@20887
    93
    val names' = map2 Thm.def_name_optional xs names;
wenzelm@20887
    94
    val eqs = mk_def ctxt (xs ~~ rhss);
wenzelm@20887
    95
    val lhss = map (fst o Logic.dest_equals) eqs;
wenzelm@18830
    96
  in
wenzelm@18830
    97
    ctxt
wenzelm@20887
    98
    |> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
wenzelm@20980
    99
    |> fold Variable.declare_term eqs
wenzelm@20887
   100
    |> ProofContext.add_assms_i def_export
wenzelm@20887
   101
      (map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs)
wenzelm@20887
   102
    |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
wenzelm@18830
   103
  end;
wenzelm@18830
   104
wenzelm@18840
   105
wenzelm@18840
   106
wenzelm@18840
   107
(** defived definitions **)
wenzelm@18840
   108
wenzelm@18840
   109
(* transformation rules *)
wenzelm@18840
   110
wenzelm@18859
   111
structure Rules = GenericDataFun
wenzelm@18840
   112
(
wenzelm@18840
   113
  val name = "Pure/derived_defs";
wenzelm@18840
   114
  type T = thm list;
wenzelm@18840
   115
  val empty = []
wenzelm@18840
   116
  val extend = I;
wenzelm@18840
   117
  fun merge _ = Drule.merge_rules;
wenzelm@18840
   118
  fun print context rules =
wenzelm@18840
   119
    Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@18840
   120
      (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
wenzelm@18840
   121
);
wenzelm@18840
   122
wenzelm@18859
   123
val _ = Context.add_setup Rules.init;
wenzelm@18840
   124
wenzelm@18859
   125
val print_rules = Rules.print;
wenzelm@18840
   126
wenzelm@18859
   127
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
wenzelm@18859
   128
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
wenzelm@18840
   129
wenzelm@18840
   130
wenzelm@18875
   131
(* meta rewrite rules *)
wenzelm@18840
   132
wenzelm@18840
   133
val equals_ss =
wenzelm@18840
   134
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18840
   135
    addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
wenzelm@18840
   136
wenzelm@18859
   137
fun meta_rewrite context =
wenzelm@18840
   138
  MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@18859
   139
    (equals_ss addsimps (Rules.get context));
wenzelm@18840
   140
wenzelm@18859
   141
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
wenzelm@18840
   142
wenzelm@18840
   143
fun meta_rewrite_tac ctxt i =
wenzelm@18859
   144
  PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite (Context.Proof ctxt))));
wenzelm@18840
   145
wenzelm@18875
   146
wenzelm@18875
   147
(* rewriting with object-level rules *)
wenzelm@18875
   148
wenzelm@18875
   149
fun meta f ctxt = f o map (meta_rewrite_rule (Context.Proof ctxt));
wenzelm@18840
   150
wenzelm@18875
   151
val unfold       = meta Tactic.rewrite_rule;
wenzelm@18875
   152
val unfold_goals = meta Tactic.rewrite_goals_rule;
wenzelm@18875
   153
val unfold_tac   = meta Tactic.rewrite_goals_tac;
wenzelm@18875
   154
val fold         = meta Tactic.fold_rule;
wenzelm@18875
   155
val fold_tac     = meta Tactic.fold_goals_tac;
wenzelm@18840
   156
wenzelm@18840
   157
wenzelm@18840
   158
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   159
wenzelm@18950
   160
fun derived_def ctxt conditional prop =
wenzelm@18840
   161
  let
wenzelm@18840
   162
    val ((c, T), rhs) = prop
wenzelm@20049
   163
      |> Thm.cterm_of (ProofContext.theory_of ctxt)
wenzelm@18859
   164
      |> meta_rewrite (Context.Proof ctxt)
wenzelm@18840
   165
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@18950
   166
      |> K conditional ? Logic.strip_imp_concl
wenzelm@18950
   167
      |> (abs_def o #2 o cert_def ctxt);
wenzelm@20909
   168
    fun prove ctxt' def =
wenzelm@18840
   169
      let
wenzelm@18840
   170
        val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@20909
   171
          if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
wenzelm@18840
   172
      in
wenzelm@20909
   173
        Goal.prove ctxt' frees [] prop (K (ALLGOALS
wenzelm@18840
   174
          (meta_rewrite_tac ctxt' THEN'
wenzelm@18840
   175
            Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18840
   176
            Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18840
   177
        handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18840
   178
      end;
wenzelm@18840
   179
  in (((c, T), rhs), prove) end;
wenzelm@18840
   180
wenzelm@18830
   181
end;