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