src/Pure/Isar/local_defs.ML
author wenzelm
Tue Jan 31 00:39:43 2006 +0100 (2006-01-31 ago)
changeset 18859 75248f8badc9
parent 18840 ce16e2bad548
child 18875 853fa34047a4
permissions -rw-r--r--
export meta_rewrite_rule;
tuned;
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@18830
    10
  val mk_def: ProofContext.context -> (string * term) list -> term list
wenzelm@18830
    11
  val cert_def: ProofContext.context -> term -> string * term
wenzelm@18830
    12
  val abs_def: term -> (string * typ) * term
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@18840
    20
  val unfold: ProofContext.context -> bool -> thm list -> thm -> thm
wenzelm@18840
    21
  val unfold_goals: ProofContext.context -> bool -> thm list -> thm -> thm
wenzelm@18840
    22
  val unfold_tac: ProofContext.context -> bool -> thm list -> tactic
wenzelm@18840
    23
  val fold: ProofContext.context -> bool -> thm list -> thm -> thm
wenzelm@18840
    24
  val fold_tac: ProofContext.context -> bool -> thm list -> tactic
wenzelm@18840
    25
  val derived_def: ProofContext.context -> 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 mk_def ctxt args =
wenzelm@18830
    37
  let
wenzelm@18830
    38
    val (xs, rhss) = split_list args;
wenzelm@18830
    39
    val (bind, _) = ProofContext.bind_fixes xs ctxt;
wenzelm@18830
    40
    val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
wenzelm@18830
    41
  in map Logic.mk_equals (lhss ~~ rhss) end;
wenzelm@18830
    42
wenzelm@18830
    43
fun cert_def ctxt eq =
wenzelm@18830
    44
  let
wenzelm@18830
    45
    fun err msg = cat_error msg
wenzelm@18830
    46
      ("The error(s) above occurred in definition: " ^ ProofContext.string_of_term ctxt eq);
wenzelm@18830
    47
wenzelm@18830
    48
    val (lhs, rhs) = Logic.dest_equals (Term.strip_all_body eq)
wenzelm@18830
    49
      handle TERM _ => err "Not a meta-equality (==)";
wenzelm@18830
    50
    val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
wenzelm@18830
    51
    val (c, _) = Term.dest_Free f handle TERM _ =>
wenzelm@18830
    52
      err "Head of lhs must be a free/fixed variable";
wenzelm@18830
    53
wenzelm@18830
    54
    fun check_arg (Bound _) = true
wenzelm@18830
    55
      | check_arg (Free (x, _)) = not (ProofContext.is_fixed ctxt x)
wenzelm@18830
    56
      | check_arg t = (case try Logic.dest_type t of SOME (TFree _) => true | _ => false);
wenzelm@18830
    57
    fun close_arg (Free (x, T)) t = Term.all T $ Term.absfree (x, T, t)
wenzelm@18830
    58
      | close_arg (Const ("TYPE", T)) t = Term.all T $ Term.absdummy (T, t)
wenzelm@18830
    59
      | close_arg _ t = t;
wenzelm@18830
    60
wenzelm@18830
    61
    val extra_frees = Term.fold_aterms (fn v as Free (x, _) =>
wenzelm@18830
    62
      if ProofContext.is_fixed ctxt x orelse member (op aconv) xs v then I
wenzelm@18830
    63
      else insert (op =) x | _ => I) rhs [];
wenzelm@18830
    64
  in
wenzelm@18830
    65
    if not (forall check_arg xs) orelse has_duplicates (op =) xs then
wenzelm@18830
    66
      err "Arguments of lhs must be distinct free/bound variables"
wenzelm@18830
    67
    else if not (null extra_frees) then
wenzelm@18830
    68
      err ("Extra free variables on rhs: " ^ commas_quote extra_frees)
wenzelm@18830
    69
    else if Term.exists_subterm (fn t => t = f) rhs then
wenzelm@18830
    70
      err "Element to be defined occurs on rhs"
wenzelm@18830
    71
    else (c, fold_rev close_arg xs eq)
wenzelm@18830
    72
  end;
wenzelm@18830
    73
wenzelm@18830
    74
fun abs_def eq =
wenzelm@18830
    75
  let
wenzelm@18830
    76
    val body = Term.strip_all_body eq;
wenzelm@18830
    77
    val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
wenzelm@18830
    78
    val (lhs, rhs) = Logic.dest_equals (Term.subst_bounds (vars, body));
wenzelm@18830
    79
    val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
wenzelm@18830
    80
    val eq' = Term.list_abs_free (map Term.dest_Free xs, rhs);
wenzelm@18830
    81
  in (Term.dest_Free f, eq') end;
wenzelm@18830
    82
wenzelm@18830
    83
wenzelm@18830
    84
(* export defs *)
wenzelm@18830
    85
wenzelm@18830
    86
fun head_of_def cprop =
wenzelm@18830
    87
  #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
wenzelm@18830
    88
  |> Thm.cterm_of (Thm.theory_of_cterm cprop);
wenzelm@18830
    89
wenzelm@18830
    90
fun def_export _ cprops thm =
wenzelm@18830
    91
  thm
wenzelm@18830
    92
  |> Drule.implies_intr_list cprops
wenzelm@18830
    93
  |> Drule.forall_intr_list (map head_of_def cprops)
wenzelm@18830
    94
  |> Drule.forall_elim_vars 0
wenzelm@18830
    95
  |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
wenzelm@18830
    96
wenzelm@18830
    97
wenzelm@18830
    98
(* add defs *)
wenzelm@18830
    99
wenzelm@18830
   100
fun add_def (x, t) ctxt =
wenzelm@18830
   101
  let
wenzelm@18830
   102
    val [eq] = mk_def ctxt [(x, t)];
wenzelm@18830
   103
    val x' = Term.dest_Free (fst (Logic.dest_equals eq));
wenzelm@18830
   104
  in
wenzelm@18830
   105
    ctxt
wenzelm@18830
   106
    |> ProofContext.add_fixes_i [(x, NONE, NoSyn)] |> snd
wenzelm@18830
   107
    |> ProofContext.add_assms_i def_export [(("", []), [(eq, ([], []))])]
wenzelm@18830
   108
    |>> (fn [(_, [th])] => (x', th))
wenzelm@18830
   109
  end;
wenzelm@18830
   110
wenzelm@18840
   111
wenzelm@18840
   112
wenzelm@18840
   113
(** defived definitions **)
wenzelm@18840
   114
wenzelm@18840
   115
(* transformation rules *)
wenzelm@18840
   116
wenzelm@18859
   117
structure Rules = GenericDataFun
wenzelm@18840
   118
(
wenzelm@18840
   119
  val name = "Pure/derived_defs";
wenzelm@18840
   120
  type T = thm list;
wenzelm@18840
   121
  val empty = []
wenzelm@18840
   122
  val extend = I;
wenzelm@18840
   123
  fun merge _ = Drule.merge_rules;
wenzelm@18840
   124
  fun print context rules =
wenzelm@18840
   125
    Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@18840
   126
      (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
wenzelm@18840
   127
);
wenzelm@18840
   128
wenzelm@18859
   129
val _ = Context.add_setup Rules.init;
wenzelm@18840
   130
wenzelm@18859
   131
val print_rules = Rules.print;
wenzelm@18840
   132
wenzelm@18859
   133
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
wenzelm@18859
   134
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
wenzelm@18840
   135
wenzelm@18840
   136
wenzelm@18840
   137
(* transform rewrite rules *)
wenzelm@18840
   138
wenzelm@18840
   139
val equals_ss =
wenzelm@18840
   140
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18840
   141
    addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
wenzelm@18840
   142
wenzelm@18859
   143
fun meta_rewrite context =
wenzelm@18840
   144
  MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@18859
   145
    (equals_ss addsimps (Rules.get context));
wenzelm@18840
   146
wenzelm@18859
   147
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
wenzelm@18840
   148
wenzelm@18840
   149
fun meta_rewrite_tac ctxt i =
wenzelm@18859
   150
  PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite (Context.Proof ctxt))));
wenzelm@18840
   151
wenzelm@18840
   152
fun transformed f _ false = f
wenzelm@18859
   153
  | transformed f ctxt true = f o map (meta_rewrite_rule (Context.Proof ctxt));
wenzelm@18840
   154
wenzelm@18840
   155
val unfold       = transformed Tactic.rewrite_rule;
wenzelm@18840
   156
val unfold_goals = transformed Tactic.rewrite_goals_rule;
wenzelm@18840
   157
val unfold_tac   = transformed Tactic.rewrite_goals_tac;
wenzelm@18840
   158
val fold         = transformed Tactic.fold_rule;
wenzelm@18840
   159
val fold_tac     = transformed Tactic.fold_goals_tac;
wenzelm@18840
   160
wenzelm@18840
   161
wenzelm@18840
   162
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   163
wenzelm@18840
   164
fun derived_def ctxt prop =
wenzelm@18840
   165
  let
wenzelm@18840
   166
    val thy = ProofContext.theory_of ctxt;
wenzelm@18840
   167
    val ((c, T), rhs) = prop
wenzelm@18840
   168
      |> Thm.cterm_of thy
wenzelm@18859
   169
      |> meta_rewrite (Context.Proof ctxt)
wenzelm@18840
   170
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@18840
   171
      |> Logic.strip_imp_concl
wenzelm@18840
   172
      |> (snd o cert_def ctxt)
wenzelm@18840
   173
      |> abs_def;
wenzelm@18840
   174
    fun prove ctxt' t def =
wenzelm@18840
   175
      let
wenzelm@18840
   176
        val thy' = ProofContext.theory_of ctxt';
wenzelm@18840
   177
        val prop' = Term.subst_atomic [(Free (c, T), t)] prop;
wenzelm@18840
   178
        val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@18840
   179
          if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
wenzelm@18840
   180
      in
wenzelm@18840
   181
        Goal.prove thy' frees [] prop' (K (ALLGOALS
wenzelm@18840
   182
          (meta_rewrite_tac ctxt' THEN'
wenzelm@18840
   183
            Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18840
   184
            Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18840
   185
        handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18840
   186
      end;
wenzelm@18840
   187
  in (((c, T), rhs), prove) end;
wenzelm@18840
   188
wenzelm@18840
   189
wenzelm@18830
   190
end;