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