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