src/Pure/Isar/theory_target.ML
author wenzelm
Thu Dec 07 23:16:55 2006 +0100 (2006-12-07)
changeset 21708 45e7491bea47
parent 21691 826ab43d43f5
child 21718 43b935d6fb5a
permissions -rw-r--r--
reorganized structure Tactic vs. MetaSimplifier;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@21594
     5
Common theory/locale targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@21006
    10
  val peek: local_theory -> string option
wenzelm@20962
    11
  val begin: bstring -> Proof.context -> local_theory
wenzelm@20894
    12
  val init: xstring option -> theory -> local_theory
wenzelm@20894
    13
  val init_i: string option -> theory -> local_theory
wenzelm@20894
    14
end;
wenzelm@20894
    15
wenzelm@20894
    16
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    17
struct
wenzelm@20894
    18
wenzelm@21594
    19
wenzelm@20894
    20
(** locale targets **)
wenzelm@20894
    21
wenzelm@21006
    22
(* context data *)
wenzelm@21006
    23
wenzelm@21006
    24
structure Data = ProofDataFun
wenzelm@21006
    25
(
wenzelm@21006
    26
  val name = "Isar/theory_target";
wenzelm@21006
    27
  type T = string option;
wenzelm@21006
    28
  fun init _ = NONE;
wenzelm@21006
    29
  fun print _ _ = ();
wenzelm@21006
    30
);
wenzelm@21006
    31
wenzelm@21006
    32
val _ = Context.add_setup Data.init;
wenzelm@21006
    33
val peek = Data.get;
wenzelm@21006
    34
wenzelm@21006
    35
wenzelm@20894
    36
(* pretty *)
wenzelm@20894
    37
wenzelm@20894
    38
fun pretty loc ctxt =
wenzelm@20894
    39
  let
wenzelm@20894
    40
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    41
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    42
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    43
    val elems =
wenzelm@20894
    44
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    45
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    46
  in
wenzelm@20984
    47
    if loc = "" then
wenzelm@20984
    48
      [Pretty.block
wenzelm@20984
    49
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    50
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    51
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    52
    else
wenzelm@20984
    53
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    54
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    55
  end;
wenzelm@20894
    56
wenzelm@20894
    57
wenzelm@20894
    58
(* consts *)
wenzelm@20894
    59
wenzelm@21594
    60
fun consts is_loc depends decls lthy =
wenzelm@20894
    61
  let
wenzelm@21533
    62
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@20894
    63
wenzelm@20894
    64
    fun const ((c, T), mx) thy =
wenzelm@20894
    65
      let
wenzelm@20894
    66
        val U = map #2 xs ---> T;
wenzelm@21533
    67
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
wenzelm@21533
    68
        val thy' = Sign.add_consts_authentic [(c, U, Syntax.unlocalize_mixfix is_loc mx)] thy;
wenzelm@21533
    69
      in (((c, mx), t), thy') end;
wenzelm@20894
    70
wenzelm@21533
    71
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21533
    72
    val defs = abbrs |> map (fn (x, t) => (x, (("", []), t)));
wenzelm@20894
    73
  in
wenzelm@20894
    74
    lthy'
wenzelm@21691
    75
    |> is_loc ? TermSyntax.abbrevs Syntax.default_mode abbrs
wenzelm@21611
    76
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
wenzelm@20894
    77
  end;
wenzelm@20894
    78
wenzelm@20894
    79
wenzelm@20894
    80
(* defs *)
wenzelm@20894
    81
wenzelm@21594
    82
local
wenzelm@21594
    83
wenzelm@21570
    84
infix also;
wenzelm@21611
    85
wenzelm@21570
    86
fun eq1 also eq2 =
wenzelm@21570
    87
  eq2 COMP (eq1 COMP (Drule.incr_indexes2 eq1 eq2 transitive_thm));
wenzelm@21570
    88
wenzelm@21594
    89
fun expand_term ctxt t =
wenzelm@21570
    90
  let
wenzelm@21570
    91
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
    92
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
    93
    val ct = Thm.cterm_of thy t;
wenzelm@21594
    94
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
    95
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
    96
wenzelm@20894
    97
fun add_def (name, prop) =
wenzelm@20894
    98
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
    99
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   100
wenzelm@20894
   101
in
wenzelm@20894
   102
wenzelm@21570
   103
fun defs kind args lthy0 =
wenzelm@20894
   104
  let
wenzelm@21533
   105
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   106
      let
wenzelm@21594
   107
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   108
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   109
wenzelm@21570
   110
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   111
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   112
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   113
wenzelm@21570
   114
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   115
        val (def, lthy3) = lthy2
wenzelm@20894
   116
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@20894
   117
wenzelm@21533
   118
        val eq =
wenzelm@21570
   119
          (*c == loc.c xs*) lhs_def
wenzelm@21570
   120
          (*lhs' == rhs'*)  also def
wenzelm@21570
   121
          (*rhs' == rhs*)   also Thm.symmetric rhs_conv;
wenzelm@20894
   122
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   123
wenzelm@21570
   124
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   125
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
wenzelm@20894
   126
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   127
wenzelm@20894
   128
end;
wenzelm@20894
   129
wenzelm@20894
   130
wenzelm@21611
   131
(* axioms *)
wenzelm@21611
   132
wenzelm@21611
   133
local
wenzelm@21611
   134
wenzelm@21611
   135
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   136
  let
wenzelm@21611
   137
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   138
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   139
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   140
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   141
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   142
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   143
wenzelm@21611
   144
in
wenzelm@21611
   145
wenzelm@21611
   146
fun axioms kind specs lthy =
wenzelm@21611
   147
  let
wenzelm@21611
   148
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   149
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   150
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   151
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   152
  in
wenzelm@21611
   153
    lthy
wenzelm@21611
   154
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   155
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   156
    |-> LocalTheory.notes kind
wenzelm@21611
   157
  end;
wenzelm@21611
   158
wenzelm@21611
   159
end;
wenzelm@21611
   160
wenzelm@21611
   161
wenzelm@20894
   162
(* notes *)
wenzelm@20894
   163
wenzelm@21611
   164
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   165
  let
wenzelm@21611
   166
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   167
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   168
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   169
    val cert = Thm.cterm_of thy;
wenzelm@21594
   170
wenzelm@21611
   171
    (*export assumes/defines*)
wenzelm@21611
   172
    val th = Goal.norm_result raw_th;
wenzelm@21611
   173
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   174
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   175
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   176
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   177
wenzelm@21611
   178
    (*export fixes*)
wenzelm@21611
   179
    val tfrees = map TFree (Drule.fold_terms Term.add_tfrees th' []);
wenzelm@21611
   180
    val frees = map Free (Drule.fold_terms Term.add_frees th' []);
wenzelm@21611
   181
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   182
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   183
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   184
wenzelm@21611
   185
    (*thm definition*)
wenzelm@21644
   186
    val result = th''
wenzelm@21644
   187
      |> PureThy.name_thm true true ""
wenzelm@21644
   188
      |> Goal.close_result
wenzelm@21644
   189
      |> PureThy.name_thm true true name;
wenzelm@21611
   190
wenzelm@21611
   191
    (*import fixes*)
wenzelm@21611
   192
    val (tvars, vars) =
wenzelm@21611
   193
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   194
      |>> map Logic.dest_type;
wenzelm@21611
   195
wenzelm@21611
   196
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   197
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   198
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   199
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   200
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   201
wenzelm@21611
   202
    (*import assumes/defines*)
wenzelm@21594
   203
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   204
    val result'' =
wenzelm@21611
   205
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   206
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21611
   207
      | SOME res => res) COMP (concl_conv COMP_INCR Drule.equal_elim_rule2)
wenzelm@21644
   208
      |> Goal.norm_result
wenzelm@21644
   209
      |> PureThy.name_thm false false name;
wenzelm@21611
   210
wenzelm@21611
   211
  in (result'', result) end;
wenzelm@21611
   212
wenzelm@21594
   213
fun notes loc kind facts lthy =
wenzelm@21585
   214
  let
wenzelm@21594
   215
    val is_loc = loc <> "";
wenzelm@21594
   216
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   217
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   218
wenzelm@21594
   219
    val facts' = facts
wenzelm@21615
   220
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   221
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   222
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   223
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   224
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   225
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   226
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   227
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   228
  in
wenzelm@21611
   229
    lthy |> LocalTheory.theory
wenzelm@21611
   230
      (Sign.qualified_names
wenzelm@21611
   231
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   232
        #> Sign.restore_naming thy)
wenzelm@21585
   233
wenzelm@21611
   234
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   235
wenzelm@21433
   236
    |> ProofContext.set_stmt true
wenzelm@20894
   237
    |> ProofContext.qualified_names
wenzelm@21611
   238
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   239
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   240
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   241
  end;
wenzelm@20894
   242
wenzelm@20894
   243
wenzelm@21533
   244
(* target declarations *)
wenzelm@20894
   245
wenzelm@21611
   246
fun target_decl _ "" f =
wenzelm@21533
   247
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
wenzelm@21533
   248
      LocalTheory.target (Context.proof_map (f Morphism.identity))
wenzelm@21668
   249
  | target_decl add loc f = LocalTheory.target (add loc f);
wenzelm@20894
   250
wenzelm@21611
   251
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   252
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   253
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   254
wenzelm@21533
   255
fun target_morphism loc lthy =
wenzelm@21594
   256
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   257
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   258
wenzelm@21668
   259
fun target_name loc lthy =
wenzelm@21668
   260
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   261
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@21668
   262
  |> NameSpace.qualified_names
wenzelm@21668
   263
  |> NameSpace.full;
wenzelm@21533
   264
wenzelm@20894
   265
wenzelm@20894
   266
(* init and exit *)
wenzelm@20894
   267
wenzelm@21293
   268
fun begin loc =
wenzelm@21293
   269
  Data.put (if loc = "" then NONE else SOME loc) #>
wenzelm@21533
   270
  LocalTheory.init (NameSpace.base loc)
wenzelm@21293
   271
   {pretty = pretty loc,
wenzelm@21594
   272
    consts = consts (loc <> ""),
wenzelm@21293
   273
    axioms = axioms,
wenzelm@21293
   274
    defs = defs,
wenzelm@21293
   275
    notes = notes loc,
wenzelm@21498
   276
    type_syntax = type_syntax loc,
wenzelm@21293
   277
    term_syntax = term_syntax loc,
wenzelm@21293
   278
    declaration = declaration loc,
wenzelm@21533
   279
    target_morphism = target_morphism loc,
wenzelm@21533
   280
    target_name = target_name loc,
wenzelm@21293
   281
    reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc),
wenzelm@21533
   282
    exit = LocalTheory.target_of};
wenzelm@20894
   283
wenzelm@20962
   284
fun init_i NONE thy = begin "" (ProofContext.init thy)
wenzelm@20962
   285
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   286
wenzelm@21276
   287
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   288
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   289
wenzelm@20894
   290
end;