src/Pure/Isar/local_defs.ML
author wenzelm
Mon, 07 Nov 2011 17:00:23 +0100
changeset 45390 e29521ef9059
parent 42501 2b8c34f53388
child 45406 b24ecaa46edb
permissions -rw-r--r--
tuned signature -- avoid spurious Thm.mixed_attribute;
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
    Author:     Makarius
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
     3
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
     4
Local definitions.
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
     5
*)
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
     6
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
     7
signature LOCAL_DEFS =
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
     8
sig
20306
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
     9
  val cert_def: Proof.context -> term -> (string * typ) * term
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    10
  val abs_def: term -> (string * typ) * term
21684
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    11
  val expand: cterm list -> thm -> thm
20224
9c40a144ee0e moved basic assumption operations from structure ProofContext to Assumption;
wenzelm
parents: 20049
diff changeset
    12
  val def_export: Assumption.export
30211
556d1810cdad Thm.binding;
wenzelm
parents: 29581
diff changeset
    13
  val add_defs: ((binding * mixfix) * (Thm.binding * term)) list -> Proof.context ->
556d1810cdad Thm.binding;
wenzelm
parents: 29581
diff changeset
    14
    (term * (string * thm)) list * Proof.context
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29440
diff changeset
    15
  val add_def: (binding * mixfix) * term -> Proof.context -> (term * thm) * Proof.context
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29440
diff changeset
    16
  val fixed_abbrev: (binding * mixfix) * term -> Proof.context ->
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 26424
diff changeset
    17
    (term * term) * Proof.context
21591
5e0f2340caa7 added export;
wenzelm
parents: 21568
diff changeset
    18
  val export: Proof.context -> Proof.context -> thm -> thm list * thm
35717
1856c0172cf2 more basic Local_Defs.export_cterm;
wenzelm
parents: 35624
diff changeset
    19
  val export_cterm: Proof.context -> Proof.context -> cterm -> thm list * cterm
21844
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
    20
  val trans_terms: Proof.context -> thm list -> thm
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
    21
  val trans_props: Proof.context -> thm list -> thm
35739
35a3b3721ffb Local_Defs.contract convenience;
wenzelm
parents: 35717
diff changeset
    22
  val contract: Proof.context -> thm list -> cterm -> thm -> thm
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 20980
diff changeset
    23
  val print_rules: Proof.context -> unit
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
    24
  val defn_add: attribute
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
    25
  val defn_del: attribute
23541
f8c5e218e4d8 exported meta_rewrite_conv;
wenzelm
parents: 22900
diff changeset
    26
  val meta_rewrite_conv: Proof.context -> conv
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 20980
diff changeset
    27
  val meta_rewrite_rule: Proof.context -> thm -> thm
20306
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    28
  val unfold: Proof.context -> thm list -> thm -> thm
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    29
  val unfold_goals: Proof.context -> thm list -> thm -> thm
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    30
  val unfold_tac: Proof.context -> thm list -> tactic
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    31
  val fold: Proof.context -> thm list -> thm -> thm
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    32
  val fold_tac: Proof.context -> thm list -> tactic
614b7e6c6276 normalized Proof.context/method type aliases;
wenzelm
parents: 20224
diff changeset
    33
  val derived_def: Proof.context -> bool -> term ->
20909
7132ab2b4621 simplified derived_def;
wenzelm
parents: 20887
diff changeset
    34
    ((string * typ) * term) * (Proof.context -> thm -> thm)
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    35
end;
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    36
35624
c4e29a0bb8c1 modernized structure Local_Defs;
wenzelm
parents: 33519
diff changeset
    37
structure Local_Defs: LOCAL_DEFS =
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    38
struct
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    39
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
    40
(** primitive definitions **)
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
    41
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    42
(* prepare defs *)
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    43
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    44
fun cert_def ctxt eq =
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    45
  let
40242
bb433b0668b8 tuned messages;
wenzelm
parents: 39133
diff changeset
    46
    fun err msg =
bb433b0668b8 tuned messages;
wenzelm
parents: 39133
diff changeset
    47
      cat_error msg ("The error(s) above occurred in definition:\n" ^
bb433b0668b8 tuned messages;
wenzelm
parents: 39133
diff changeset
    48
        quote (Syntax.string_of_term ctxt eq));
18950
053e830c25ad cert_def: use Logic.dest_def;
wenzelm
parents: 18896
diff changeset
    49
    val ((lhs, _), eq') = eq
39133
70d3915c92f0 pretty printing: prefer regular Proof.context over Pretty.pp, which is mostly for special bootstrap purposes involving theory merge, for example;
wenzelm
parents: 35739
diff changeset
    50
      |> Sign.no_vars ctxt
33385
fb2358edcfb6 modernized structure Primitive_Defs;
wenzelm
parents: 32091
diff changeset
    51
      |> Primitive_Defs.dest_def ctxt Term.is_Free (Variable.is_fixed ctxt) (K true)
18950
053e830c25ad cert_def: use Logic.dest_def;
wenzelm
parents: 18896
diff changeset
    52
      handle TERM (msg, _) => err msg | ERROR msg => err msg;
053e830c25ad cert_def: use Logic.dest_def;
wenzelm
parents: 18896
diff changeset
    53
  in (Term.dest_Free (Term.head_of lhs), eq') end;
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    54
33385
fb2358edcfb6 modernized structure Primitive_Defs;
wenzelm
parents: 32091
diff changeset
    55
val abs_def = Primitive_Defs.abs_def #>> Term.dest_Free;
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    56
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    57
fun mk_def ctxt args =
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    58
  let
42501
2b8c34f53388 eliminated slightly odd Proof_Context.bind_fixes;
wenzelm
parents: 42496
diff changeset
    59
    val (bs, rhss) = split_list args;
2b8c34f53388 eliminated slightly odd Proof_Context.bind_fixes;
wenzelm
parents: 42496
diff changeset
    60
    val Ts = map Term.fastype_of rhss;
2b8c34f53388 eliminated slightly odd Proof_Context.bind_fixes;
wenzelm
parents: 42496
diff changeset
    61
    val (xs, _) = Proof_Context.add_fixes (map2 (fn b => fn T => (b, SOME T, NoSyn)) bs Ts) ctxt;
2b8c34f53388 eliminated slightly odd Proof_Context.bind_fixes;
wenzelm
parents: 42496
diff changeset
    62
    val lhss = ListPair.map Free (xs, Ts);
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    63
  in map Logic.mk_equals (lhss ~~ rhss) end;
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    64
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    65
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    66
(* export defs *)
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    67
20021
815393c02db9 def_export: Drule.generalize;
wenzelm
parents: 19897
diff changeset
    68
val head_of_def =
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    69
  #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
20021
815393c02db9 def_export: Drule.generalize;
wenzelm
parents: 19897
diff changeset
    70
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    71
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    72
(*
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    73
  [x, x == a]
18896
efd9d44a0bdb tuned comments;
wenzelm
parents: 18875
diff changeset
    74
       :
efd9d44a0bdb tuned comments;
wenzelm
parents: 18875
diff changeset
    75
      B x
efd9d44a0bdb tuned comments;
wenzelm
parents: 18875
diff changeset
    76
  -----------
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    77
      B a
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
    78
*)
21684
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    79
fun expand defs =
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    80
  Drule.implies_intr_list defs
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    81
  #> Drule.generalize ([], map (head_of_def o Thm.term_of) defs)
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    82
  #> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    83
21801
c77bc21b3eef tuned expand_term;
wenzelm
parents: 21708
diff changeset
    84
val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of);
21684
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    85
e8c135b166b3 added expand;
wenzelm
parents: 21591
diff changeset
    86
fun def_export _ defs = (expand defs, expand_term defs);
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    87
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    88
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    89
(* add defs *)
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    90
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    91
fun add_defs defs ctxt =
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    92
  let
42496
65ec88b369fd more precise positions via binding;
wenzelm
parents: 42494
diff changeset
    93
    val ((xs, mxs), specs) = defs |> split_list |>> split_list;
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 26424
diff changeset
    94
    val ((bfacts, atts), rhss) = specs |> split_list |>> split_list;
42496
65ec88b369fd more precise positions via binding;
wenzelm
parents: 42494
diff changeset
    95
    val names = map2 Thm.def_binding_optional xs bfacts;
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    96
    val eqs = mk_def ctxt (xs ~~ rhss);
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
    97
    val lhss = map (fst o Logic.dest_equals) eqs;
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    98
  in
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
    99
    ctxt
42496
65ec88b369fd more precise positions via binding;
wenzelm
parents: 42494
diff changeset
   100
    |> Proof_Context.add_fixes (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
20980
e4fd72aecd03 add_defs: declare terms;
wenzelm
parents: 20909
diff changeset
   101
    |> fold Variable.declare_term eqs
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42357
diff changeset
   102
    |> Proof_Context.add_assms_i def_export
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 26424
diff changeset
   103
      (map2 (fn a => fn eq => (a, [(eq, [])])) (names ~~ atts) eqs)
20887
ec9a1bb086da replaced add_def by more elaborate add_defs;
wenzelm
parents: 20306
diff changeset
   104
    |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
   105
  end;
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
   106
25104
26b9a7db3386 tuned interfaces;
wenzelm
parents: 25025
diff changeset
   107
fun add_def (var, rhs) ctxt =
30211
556d1810cdad Thm.binding;
wenzelm
parents: 29581
diff changeset
   108
  let val ([(lhs, (_, th))], ctxt') = add_defs [(var, (Thm.empty_binding, rhs))] ctxt
25104
26b9a7db3386 tuned interfaces;
wenzelm
parents: 25025
diff changeset
   109
  in ((lhs, th), ctxt') end;
25025
17c845095a47 added add_def;
wenzelm
parents: 24985
diff changeset
   110
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   111
25119
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   112
(* fixed_abbrev *)
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   113
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   114
fun fixed_abbrev ((x, mx), rhs) ctxt =
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   115
  let
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   116
    val T = Term.fastype_of rhs;
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   117
    val ([x'], ctxt') = ctxt
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   118
      |> Variable.declare_term rhs
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42357
diff changeset
   119
      |> Proof_Context.add_fixes [(x, SOME T, mx)];
25119
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   120
    val lhs = Free (x', T);
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   121
    val _ = cert_def ctxt' (Logic.mk_equals (lhs, rhs));
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   122
    fun abbrev_export _ _ = (I, Envir.expand_term_frees [((x', T), rhs)]);
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   123
    val (_, ctxt'') = Assumption.add_assms abbrev_export [] ctxt';
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   124
  in ((lhs, rhs), ctxt'') end;
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   125
192105867f25 added fixed_abbrev;
wenzelm
parents: 25104
diff changeset
   126
24985
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   127
(* specific export -- result based on educated guessing *)
21568
a8070c4b6d43 simplified '?' operator;
wenzelm
parents: 21506
diff changeset
   128
24985
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   129
(*
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   130
  [xs, xs == as]
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   131
        :
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   132
       B xs
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   133
  --------------
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   134
       B as
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   135
*)
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   136
fun export inner outer =    (*beware of closure sizes*)
21568
a8070c4b6d43 simplified '?' operator;
wenzelm
parents: 21506
diff changeset
   137
  let
22671
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   138
    val exp = Assumption.export false inner outer;
30473
e0b66c11e7e4 Assumption.all_prems_of, Assumption.all_assms_of;
wenzelm
parents: 30434
diff changeset
   139
    val prems = Assumption.all_prems_of inner;
22671
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   140
  in fn th =>
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   141
    let
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   142
      val th' = exp th;
22691
290454649b8c Thm.fold_terms;
wenzelm
parents: 22671
diff changeset
   143
      val still_fixed = map #1 (Thm.fold_terms Term.add_frees th' []);
22671
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   144
      val defs = prems |> filter_out (fn prem =>
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   145
        (case try (head_of_def o Thm.prop_of) prem of
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   146
          SOME x => member (op =) still_fixed x
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   147
        | NONE => true));
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   148
    in (map Drule.abs_def defs, th') end
3c62305fbee6 tuned signature;
wenzelm
parents: 22568
diff changeset
   149
  end;
21568
a8070c4b6d43 simplified '?' operator;
wenzelm
parents: 21506
diff changeset
   150
24985
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   151
(*
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   152
  [xs, xs == as]
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   153
        :
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   154
     TERM b xs
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   155
  --------------  and  --------------
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   156
     TERM b as          b xs == b as
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   157
*)
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   158
fun export_cterm inner outer ct =
35717
1856c0172cf2 more basic Local_Defs.export_cterm;
wenzelm
parents: 35624
diff changeset
   159
  export inner outer (Drule.mk_term ct) ||> Drule.dest_term;
24985
0e5177e2c076 added export_cterm;
wenzelm
parents: 24981
diff changeset
   160
21568
a8070c4b6d43 simplified '?' operator;
wenzelm
parents: 21506
diff changeset
   161
21844
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   162
(* basic transitivity reasoning -- modulo beta-eta *)
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   163
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   164
local
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   165
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   166
val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of;
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   167
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   168
fun trans_list _ _ [] = raise Empty
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   169
  | trans_list trans ctxt (th :: raw_eqs) =
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   170
      (case filter_out is_trivial raw_eqs of
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   171
        [] => th
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   172
      | eqs =>
31794
71af1fd6a5e4 renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
wenzelm
parents: 30763
diff changeset
   173
          let val ((_, th' :: eqs'), ctxt') = Variable.import true (th :: eqs) ctxt
21844
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   174
          in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end);
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   175
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   176
in
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   177
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   178
val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm));
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   179
val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1));
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   180
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   181
end;
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   182
35739
35a3b3721ffb Local_Defs.contract convenience;
wenzelm
parents: 35717
diff changeset
   183
fun contract ctxt defs ct th =
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   184
  trans_props ctxt [th, Thm.symmetric (Raw_Simplifier.rewrite true defs ct)];
21844
e10b8bd7a886 added trans_terms/props;
wenzelm
parents: 21801
diff changeset
   185
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   186
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   187
(** defived definitions **)
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   188
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   189
(* transformation rules *)
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   190
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33385
diff changeset
   191
structure Rules = Generic_Data
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   192
(
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   193
  type T = thm list;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33385
diff changeset
   194
  val empty = [];
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   195
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33385
diff changeset
   196
  val merge = Thm.merge_thms;
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   197
);
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   198
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22691
diff changeset
   199
fun print_rules ctxt =
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22691
diff changeset
   200
  Pretty.writeln (Pretty.big_list "definitional transformations:"
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31794
diff changeset
   201
    (map (Display.pretty_thm ctxt) (Rules.get (Context.Proof ctxt))));
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   202
24039
273698405054 renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents: 23541
diff changeset
   203
val defn_add = Thm.declaration_attribute (Rules.map o Thm.add_thm);
273698405054 renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents: 23541
diff changeset
   204
val defn_del = Thm.declaration_attribute (Rules.map o Thm.del_thm);
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   205
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   206
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
   207
(* meta rewrite rules *)
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   208
23541
f8c5e218e4d8 exported meta_rewrite_conv;
wenzelm
parents: 22900
diff changeset
   209
fun meta_rewrite_conv ctxt =
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   210
  Raw_Simplifier.rewrite_cterm (false, false, false) (K (K NONE))
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   211
    (Raw_Simplifier.context ctxt empty_ss
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 25119
diff changeset
   212
      addeqcongs [Drule.equals_cong]    (*protect meta-level equality*)
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 25119
diff changeset
   213
      addsimps (Rules.get (Context.Proof ctxt)));
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   214
23541
f8c5e218e4d8 exported meta_rewrite_conv;
wenzelm
parents: 22900
diff changeset
   215
val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv;
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   216
18875
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
   217
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
   218
(* rewriting with object-level rules *)
853fa34047a4 (un)fold: no raw flag;
wenzelm
parents: 18859
diff changeset
   219
21506
b2a673894ce5 prefer Proof.context over Context.generic;
wenzelm
parents: 20980
diff changeset
   220
fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   221
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   222
val unfold       = meta Raw_Simplifier.rewrite_rule;
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   223
val unfold_goals = meta Raw_Simplifier.rewrite_goals_rule;
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   224
val unfold_tac   = meta Raw_Simplifier.rewrite_goals_tac;
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   225
val fold         = meta Raw_Simplifier.fold_rule;
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40242
diff changeset
   226
val fold_tac     = meta Raw_Simplifier.fold_goals_tac;
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   227
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   228
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   229
(* derived defs -- potentially within the object-logic *)
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   230
18950
053e830c25ad cert_def: use Logic.dest_def;
wenzelm
parents: 18896
diff changeset
   231
fun derived_def ctxt conditional prop =
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   232
  let
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   233
    val ((c, T), rhs) = prop
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42357
diff changeset
   234
      |> Thm.cterm_of (Proof_Context.theory_of ctxt)
23541
f8c5e218e4d8 exported meta_rewrite_conv;
wenzelm
parents: 22900
diff changeset
   235
      |> meta_rewrite_conv ctxt
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   236
      |> (snd o Logic.dest_equals o Thm.prop_of)
21568
a8070c4b6d43 simplified '?' operator;
wenzelm
parents: 21506
diff changeset
   237
      |> conditional ? Logic.strip_imp_concl
18950
053e830c25ad cert_def: use Logic.dest_def;
wenzelm
parents: 18896
diff changeset
   238
      |> (abs_def o #2 o cert_def ctxt);
20909
7132ab2b4621 simplified derived_def;
wenzelm
parents: 20887
diff changeset
   239
    fun prove ctxt' def =
42482
42c7ef050bc3 more uniform Variable.add_frees/add_fixed etc.;
wenzelm
parents: 42360
diff changeset
   240
      Goal.prove ctxt' (Variable.add_free_names ctxt' prop []) [] prop (K (ALLGOALS
42c7ef050bc3 more uniform Variable.add_frees/add_fixed etc.;
wenzelm
parents: 42360
diff changeset
   241
        (CONVERSION (meta_rewrite_conv ctxt') THEN'
42c7ef050bc3 more uniform Variable.add_frees/add_fixed etc.;
wenzelm
parents: 42360
diff changeset
   242
          rewrite_goal_tac [def] THEN'
42c7ef050bc3 more uniform Variable.add_frees/add_fixed etc.;
wenzelm
parents: 42360
diff changeset
   243
          resolve_tac [Drule.reflexive_thm])))
42c7ef050bc3 more uniform Variable.add_frees/add_fixed etc.;
wenzelm
parents: 42360
diff changeset
   244
      handle ERROR msg => cat_error msg "Failed to prove definitional specification";
18840
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   245
  in (((c, T), rhs), prove) end;
ce16e2bad548 added attributes defn_add/del;
wenzelm
parents: 18830
diff changeset
   246
18830
34b51dcdc570 Basic operations on local definitions.
wenzelm
parents:
diff changeset
   247
end;