src/Pure/Isar/local_defs.ML
author wenzelm
Thu Jul 27 13:43:01 2006 +0200 (2006-07-27 ago)
changeset 20224 9c40a144ee0e
parent 20049 f48c4a3a34bc
child 20306 614b7e6c6276
permissions -rw-r--r--
moved basic assumption operations from structure ProofContext to Assumption;
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@20224
    13
  val def_export: Assumption.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@19897
    43
      |> Logic.dest_def pp Term.is_Free (Variable.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@20021
    59
val head_of_def =
wenzelm@20021
    60
  #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body o Thm.term_of;
wenzelm@20021
    61
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@20021
    73
  |> Drule.generalize ([], map head_of_def cprops)
wenzelm@18830
    74
  |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
wenzelm@18830
    75
wenzelm@18830
    76
wenzelm@18830
    77
(* add defs *)
wenzelm@18830
    78
wenzelm@18830
    79
fun add_def (x, t) ctxt =
wenzelm@18830
    80
  let
wenzelm@18830
    81
    val [eq] = mk_def ctxt [(x, t)];
wenzelm@18830
    82
    val x' = Term.dest_Free (fst (Logic.dest_equals eq));
wenzelm@18830
    83
  in
wenzelm@18830
    84
    ctxt
wenzelm@18830
    85
    |> ProofContext.add_fixes_i [(x, NONE, NoSyn)] |> snd
wenzelm@19585
    86
    |> ProofContext.add_assms_i def_export [(("", []), [(eq, [])])]
wenzelm@18830
    87
    |>> (fn [(_, [th])] => (x', th))
wenzelm@18830
    88
  end;
wenzelm@18830
    89
wenzelm@18840
    90
wenzelm@18840
    91
wenzelm@18840
    92
(** defived definitions **)
wenzelm@18840
    93
wenzelm@18840
    94
(* transformation rules *)
wenzelm@18840
    95
wenzelm@18859
    96
structure Rules = GenericDataFun
wenzelm@18840
    97
(
wenzelm@18840
    98
  val name = "Pure/derived_defs";
wenzelm@18840
    99
  type T = thm list;
wenzelm@18840
   100
  val empty = []
wenzelm@18840
   101
  val extend = I;
wenzelm@18840
   102
  fun merge _ = Drule.merge_rules;
wenzelm@18840
   103
  fun print context rules =
wenzelm@18840
   104
    Pretty.writeln (Pretty.big_list "definitional transformations:"
wenzelm@18840
   105
      (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
wenzelm@18840
   106
);
wenzelm@18840
   107
wenzelm@18859
   108
val _ = Context.add_setup Rules.init;
wenzelm@18840
   109
wenzelm@18859
   110
val print_rules = Rules.print;
wenzelm@18840
   111
wenzelm@18859
   112
val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
wenzelm@18859
   113
val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
wenzelm@18840
   114
wenzelm@18840
   115
wenzelm@18875
   116
(* meta rewrite rules *)
wenzelm@18840
   117
wenzelm@18840
   118
val equals_ss =
wenzelm@18840
   119
  MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
wenzelm@18840
   120
    addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
wenzelm@18840
   121
wenzelm@18859
   122
fun meta_rewrite context =
wenzelm@18840
   123
  MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
wenzelm@18859
   124
    (equals_ss addsimps (Rules.get context));
wenzelm@18840
   125
wenzelm@18859
   126
val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
wenzelm@18840
   127
wenzelm@18840
   128
fun meta_rewrite_tac ctxt i =
wenzelm@18859
   129
  PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite (Context.Proof ctxt))));
wenzelm@18840
   130
wenzelm@18875
   131
wenzelm@18875
   132
(* rewriting with object-level rules *)
wenzelm@18875
   133
wenzelm@18875
   134
fun meta f ctxt = f o map (meta_rewrite_rule (Context.Proof ctxt));
wenzelm@18840
   135
wenzelm@18875
   136
val unfold       = meta Tactic.rewrite_rule;
wenzelm@18875
   137
val unfold_goals = meta Tactic.rewrite_goals_rule;
wenzelm@18875
   138
val unfold_tac   = meta Tactic.rewrite_goals_tac;
wenzelm@18875
   139
val fold         = meta Tactic.fold_rule;
wenzelm@18875
   140
val fold_tac     = meta Tactic.fold_goals_tac;
wenzelm@18840
   141
wenzelm@18840
   142
wenzelm@18840
   143
(* derived defs -- potentially within the object-logic *)
wenzelm@18840
   144
wenzelm@18950
   145
fun derived_def ctxt conditional prop =
wenzelm@18840
   146
  let
wenzelm@18840
   147
    val ((c, T), rhs) = prop
wenzelm@20049
   148
      |> Thm.cterm_of (ProofContext.theory_of ctxt)
wenzelm@18859
   149
      |> meta_rewrite (Context.Proof ctxt)
wenzelm@18840
   150
      |> (snd o Logic.dest_equals o Thm.prop_of)
wenzelm@18950
   151
      |> K conditional ? Logic.strip_imp_concl
wenzelm@18950
   152
      |> (abs_def o #2 o cert_def ctxt);
wenzelm@18840
   153
    fun prove ctxt' t def =
wenzelm@18840
   154
      let
wenzelm@18840
   155
        val prop' = Term.subst_atomic [(Free (c, T), t)] prop;
wenzelm@18840
   156
        val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@19897
   157
          if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
wenzelm@18840
   158
      in
wenzelm@20049
   159
        Goal.prove ctxt' frees [] prop' (K (ALLGOALS
wenzelm@18840
   160
          (meta_rewrite_tac ctxt' THEN'
wenzelm@18840
   161
            Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18840
   162
            Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18840
   163
        handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18840
   164
      end;
wenzelm@18840
   165
  in (((c, T), rhs), prove) end;
wenzelm@18840
   166
wenzelm@18830
   167
end;