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