src/Pure/Isar/theory_target.ML
author wenzelm
Thu Nov 30 17:42:23 2006 +0100 (2006-11-30)
changeset 21615 1bd558879c44
parent 21613 ae3bb930b50f
child 21644 5154a7213d3c
permissions -rw-r--r--
notes: proper naming of thm proof, activated import_export_proof;
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@21570
    75
    |> is_loc ? LocalTheory.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@21570
    95
  in (Thm.term_of ct', Tactic.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@21594
   174
    val concl_conv = Tactic.rewrite true defs (Thm.cprop_of th);
wenzelm@21611
   175
    val assms = map (Tactic.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@21613
   186
    val result = PureThy.name_thm true (name, Goal.close_result th'');
wenzelm@21611
   187
wenzelm@21611
   188
    (*import fixes*)
wenzelm@21611
   189
    val (tvars, vars) =
wenzelm@21611
   190
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   191
      |>> map Logic.dest_type;
wenzelm@21611
   192
wenzelm@21611
   193
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   194
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   195
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   196
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   197
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   198
wenzelm@21611
   199
    (*import assumes/defines*)
wenzelm@21594
   200
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   201
    val result'' =
wenzelm@21611
   202
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   203
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21611
   204
      | SOME res => res) COMP (concl_conv COMP_INCR Drule.equal_elim_rule2)
wenzelm@21613
   205
      |> Goal.norm_result;
wenzelm@21611
   206
wenzelm@21611
   207
  in (result'', result) end;
wenzelm@21611
   208
wenzelm@21594
   209
fun notes loc kind facts lthy =
wenzelm@21585
   210
  let
wenzelm@21594
   211
    val is_loc = loc <> "";
wenzelm@21594
   212
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   213
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   214
wenzelm@21594
   215
    val facts' = facts
wenzelm@21615
   216
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   217
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   218
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   219
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   220
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   221
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   222
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   223
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   224
  in
wenzelm@21611
   225
    lthy |> LocalTheory.theory
wenzelm@21611
   226
      (Sign.qualified_names
wenzelm@21611
   227
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   228
        #> Sign.restore_naming thy)
wenzelm@21585
   229
wenzelm@21611
   230
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   231
wenzelm@21433
   232
    |> ProofContext.set_stmt true
wenzelm@20894
   233
    |> ProofContext.qualified_names
wenzelm@21611
   234
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   235
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   236
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   237
  end;
wenzelm@20894
   238
wenzelm@20894
   239
wenzelm@21533
   240
(* target declarations *)
wenzelm@20894
   241
wenzelm@21611
   242
fun target_decl _ "" f =
wenzelm@21533
   243
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
wenzelm@21533
   244
      LocalTheory.target (Context.proof_map (f Morphism.identity))
wenzelm@21611
   245
  | target_decl add loc f =
wenzelm@21533
   246
      LocalTheory.target (add loc (Context.proof_map o f));
wenzelm@20894
   247
wenzelm@21611
   248
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   249
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   250
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   251
wenzelm@21533
   252
fun target_morphism loc lthy =
wenzelm@21594
   253
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   254
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   255
wenzelm@21533
   256
fun target_name "" lthy = Sign.full_name (ProofContext.theory_of lthy)
wenzelm@21533
   257
  | target_name _ lthy = ProofContext.full_name (LocalTheory.target_of lthy);
wenzelm@21533
   258
wenzelm@20894
   259
wenzelm@20894
   260
(* init and exit *)
wenzelm@20894
   261
wenzelm@21293
   262
fun begin loc =
wenzelm@21293
   263
  Data.put (if loc = "" then NONE else SOME loc) #>
wenzelm@21533
   264
  LocalTheory.init (NameSpace.base loc)
wenzelm@21293
   265
   {pretty = pretty loc,
wenzelm@21594
   266
    consts = consts (loc <> ""),
wenzelm@21293
   267
    axioms = axioms,
wenzelm@21293
   268
    defs = defs,
wenzelm@21293
   269
    notes = notes loc,
wenzelm@21498
   270
    type_syntax = type_syntax loc,
wenzelm@21293
   271
    term_syntax = term_syntax loc,
wenzelm@21293
   272
    declaration = declaration loc,
wenzelm@21533
   273
    target_morphism = target_morphism loc,
wenzelm@21533
   274
    target_name = target_name loc,
wenzelm@21293
   275
    reinit = fn _ => begin loc o (if loc = "" then ProofContext.init else Locale.init loc),
wenzelm@21533
   276
    exit = LocalTheory.target_of};
wenzelm@20894
   277
wenzelm@20962
   278
fun init_i NONE thy = begin "" (ProofContext.init thy)
wenzelm@20962
   279
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   280
wenzelm@21276
   281
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   282
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   283
wenzelm@20894
   284
end;