src/Pure/Isar/theory_target.ML
author haftmann
Fri Feb 23 08:39:26 2007 +0100 (2007-02-23)
changeset 22353 c818c6b836f5
parent 22301 1435d027e453
child 22380 7635e59e3125
permissions -rw-r--r--
tuned
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
haftmann@22353
    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@20894
    19
(** locale targets **)
wenzelm@20894
    20
wenzelm@21006
    21
(* context data *)
wenzelm@21006
    22
wenzelm@21006
    23
structure Data = ProofDataFun
wenzelm@21006
    24
(
wenzelm@21006
    25
  val name = "Isar/theory_target";
wenzelm@21006
    26
  type T = string option;
wenzelm@21006
    27
  fun init _ = NONE;
wenzelm@21006
    28
  fun print _ _ = ();
wenzelm@21006
    29
);
wenzelm@21006
    30
wenzelm@21006
    31
val _ = Context.add_setup Data.init;
wenzelm@21006
    32
val peek = Data.get;
wenzelm@21006
    33
wenzelm@21006
    34
wenzelm@20894
    35
(* pretty *)
wenzelm@20894
    36
wenzelm@20894
    37
fun pretty loc ctxt =
wenzelm@20894
    38
  let
wenzelm@20894
    39
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    40
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    41
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    42
    val elems =
wenzelm@20894
    43
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    44
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    45
  in
wenzelm@20984
    46
    if loc = "" then
wenzelm@20984
    47
      [Pretty.block
wenzelm@20984
    48
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    49
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    50
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    51
    else
wenzelm@20984
    52
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    53
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    54
  end;
wenzelm@20894
    55
wenzelm@20894
    56
wenzelm@21804
    57
(* consts *)
wenzelm@21804
    58
wenzelm@21825
    59
fun internal_abbrev prmode ((c, mx), t) =
wenzelm@21825
    60
  LocalTheory.term_syntax (ProofContext.target_abbrev prmode ((c, mx), t)) #>
wenzelm@21825
    61
  ProofContext.add_abbrev Syntax.internalM (c, t) #> snd;
wenzelm@21804
    62
haftmann@22353
    63
fun consts is_loc some_class depends decls lthy =
wenzelm@21804
    64
  let
wenzelm@21804
    65
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@21747
    66
wenzelm@21804
    67
    fun const ((c, T), mx) thy =
wenzelm@21804
    68
      let
wenzelm@21804
    69
        val U = map #2 xs ---> T;
wenzelm@21804
    70
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
haftmann@22353
    71
        val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21804
    72
        val thy' = Sign.add_consts_authentic [(c, U, mx1)] thy;
wenzelm@21804
    73
      in (((c, mx2), t), thy') end;
wenzelm@21804
    74
wenzelm@21804
    75
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21804
    76
    val defs = map (apsnd (pair ("", []))) abbrs;
wenzelm@21804
    77
  in
wenzelm@21804
    78
    lthy'
wenzelm@21825
    79
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
wenzelm@21804
    80
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
wenzelm@21804
    81
  end;
wenzelm@21804
    82
wenzelm@21804
    83
wenzelm@21804
    84
(* abbrev *)
wenzelm@21747
    85
wenzelm@21747
    86
fun occ_params ctxt ts =
wenzelm@21747
    87
  #1 (ProofContext.inferred_fixes ctxt)
wenzelm@21747
    88
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
wenzelm@21747
    89
haftmann@22353
    90
fun abbrev is_loc some_class  prmode ((raw_c, mx), raw_t) lthy =
wenzelm@21747
    91
  let
wenzelm@21747
    92
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@21747
    93
    val target = LocalTheory.target_of lthy;
wenzelm@21747
    94
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@21747
    95
wenzelm@21747
    96
    val c = Morphism.name target_morphism raw_c;
wenzelm@21747
    97
    val t = Morphism.term target_morphism raw_t;
wenzelm@21747
    98
    val xs = map Free (occ_params target [t]);
wenzelm@21747
    99
    val u = fold_rev Term.lambda xs t;
wenzelm@21804
   100
    val U = Term.fastype_of u;
wenzelm@21747
   101
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
wenzelm@21747
   102
wenzelm@21804
   103
    val ((lhs as Const (full_c, _), rhs), lthy1) = lthy
wenzelm@21747
   104
      |> LocalTheory.theory_result (Sign.add_abbrev (#1 prmode) (c, u'));
haftmann@22353
   105
    val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21747
   106
  in
wenzelm@21747
   107
    lthy1
wenzelm@21804
   108
    |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
wenzelm@21825
   109
    |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
wenzelm@21804
   110
    |> ProofContext.local_abbrev (c, rhs)
wenzelm@21747
   111
  end;
wenzelm@21747
   112
wenzelm@21747
   113
wenzelm@20894
   114
(* defs *)
wenzelm@20894
   115
wenzelm@21845
   116
local
wenzelm@21570
   117
wenzelm@21594
   118
fun expand_term ctxt t =
wenzelm@21570
   119
  let
wenzelm@21570
   120
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   121
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   122
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   123
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
   124
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
   125
wenzelm@20894
   126
fun add_def (name, prop) =
wenzelm@20894
   127
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   128
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   129
wenzelm@20894
   130
in
wenzelm@20894
   131
haftmann@22353
   132
fun defs loc some_class kind args lthy0 =
wenzelm@20894
   133
  let
wenzelm@21533
   134
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   135
      let
wenzelm@21594
   136
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   137
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   138
wenzelm@21570
   139
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   140
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   141
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   142
wenzelm@21570
   143
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   144
        val (def, lthy3) = lthy2
wenzelm@20894
   145
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@21845
   146
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   147
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   148
           (*lhs' == rhs'*)  def,
wenzelm@21845
   149
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
haftmann@22353
   150
        val lthy4 = case some_class
haftmann@22353
   151
         of SOME class => 
haftmann@22353
   152
              LocalTheory.raw_theory
haftmann@22353
   153
                (ClassPackage.add_def_in_class lthy3 class
haftmann@22353
   154
                  ((c, mx), ((name, atts), (rhs, eq)))) lthy3
haftmann@22353
   155
          | _ => lthy3;
haftmann@22301
   156
      in ((lhs, ((name', atts), [([eq], [])])), lthy4) end;
wenzelm@20894
   157
wenzelm@21570
   158
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   159
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
wenzelm@20894
   160
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   161
wenzelm@20894
   162
end;
wenzelm@20894
   163
wenzelm@20894
   164
wenzelm@21611
   165
(* axioms *)
wenzelm@21611
   166
wenzelm@21611
   167
local
wenzelm@21611
   168
wenzelm@21611
   169
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   170
  let
wenzelm@21611
   171
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   172
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   173
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   174
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   175
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   176
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   177
wenzelm@21611
   178
in
wenzelm@21611
   179
wenzelm@21611
   180
fun axioms kind specs lthy =
wenzelm@21611
   181
  let
wenzelm@21611
   182
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   183
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   184
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   185
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   186
  in
wenzelm@21611
   187
    lthy
wenzelm@21611
   188
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   189
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   190
    |-> LocalTheory.notes kind
wenzelm@21611
   191
  end;
wenzelm@21611
   192
wenzelm@21611
   193
end;
wenzelm@21611
   194
wenzelm@21611
   195
wenzelm@20894
   196
(* notes *)
wenzelm@20894
   197
wenzelm@21611
   198
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   199
  let
wenzelm@21611
   200
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   201
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   202
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   203
    val cert = Thm.cterm_of thy;
wenzelm@21594
   204
wenzelm@21611
   205
    (*export assumes/defines*)
wenzelm@21611
   206
    val th = Goal.norm_result raw_th;
wenzelm@21611
   207
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   208
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   209
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   210
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   211
wenzelm@21611
   212
    (*export fixes*)
wenzelm@21611
   213
    val tfrees = map TFree (Drule.fold_terms Term.add_tfrees th' []);
wenzelm@21611
   214
    val frees = map Free (Drule.fold_terms Term.add_frees th' []);
wenzelm@21611
   215
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   216
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   217
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   218
wenzelm@21611
   219
    (*thm definition*)
wenzelm@21644
   220
    val result = th''
wenzelm@21644
   221
      |> PureThy.name_thm true true ""
wenzelm@21644
   222
      |> Goal.close_result
wenzelm@21644
   223
      |> PureThy.name_thm true true name;
wenzelm@21611
   224
wenzelm@21611
   225
    (*import fixes*)
wenzelm@21611
   226
    val (tvars, vars) =
wenzelm@21611
   227
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   228
      |>> map Logic.dest_type;
wenzelm@21611
   229
wenzelm@21611
   230
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   231
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   232
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   233
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   234
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   235
wenzelm@21611
   236
    (*import assumes/defines*)
wenzelm@21594
   237
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   238
    val result'' =
wenzelm@21611
   239
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   240
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   241
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   242
      |> Goal.norm_result
wenzelm@21644
   243
      |> PureThy.name_thm false false name;
wenzelm@21611
   244
wenzelm@21611
   245
  in (result'', result) end;
wenzelm@21611
   246
wenzelm@21594
   247
fun notes loc kind facts lthy =
wenzelm@21585
   248
  let
wenzelm@21594
   249
    val is_loc = loc <> "";
wenzelm@21594
   250
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   251
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   252
wenzelm@21594
   253
    val facts' = facts
wenzelm@21615
   254
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   255
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   256
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   257
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   258
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   259
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   260
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   261
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   262
  in
wenzelm@21611
   263
    lthy |> LocalTheory.theory
wenzelm@21611
   264
      (Sign.qualified_names
wenzelm@21611
   265
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   266
        #> Sign.restore_naming thy)
wenzelm@21585
   267
wenzelm@21611
   268
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   269
wenzelm@21433
   270
    |> ProofContext.set_stmt true
wenzelm@20894
   271
    |> ProofContext.qualified_names
wenzelm@21611
   272
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   273
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   274
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   275
  end;
wenzelm@20894
   276
wenzelm@20894
   277
wenzelm@21533
   278
(* target declarations *)
wenzelm@20894
   279
wenzelm@21611
   280
fun target_decl _ "" f =
wenzelm@21533
   281
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
wenzelm@21533
   282
      LocalTheory.target (Context.proof_map (f Morphism.identity))
wenzelm@21668
   283
  | target_decl add loc f = LocalTheory.target (add loc f);
wenzelm@20894
   284
wenzelm@21611
   285
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   286
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   287
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   288
wenzelm@21533
   289
fun target_morphism loc lthy =
wenzelm@21594
   290
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   291
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   292
wenzelm@22203
   293
fun target_naming loc lthy =
wenzelm@21668
   294
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   295
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@22203
   296
  |> NameSpace.qualified_names;
wenzelm@21533
   297
wenzelm@20894
   298
wenzelm@20894
   299
(* init and exit *)
wenzelm@20894
   300
haftmann@22353
   301
fun begin loc ctxt =
haftmann@22353
   302
  let
haftmann@22353
   303
    val thy = ProofContext.theory_of ctxt;
haftmann@22353
   304
    val is_loc = loc <> "";
haftmann@22353
   305
    val some_class = ClassPackage.class_of_locale thy loc;
haftmann@22353
   306
  in
haftmann@22353
   307
    ctxt
haftmann@22353
   308
    |> Data.put (if is_loc then SOME loc else NONE)
haftmann@22353
   309
    |> LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   310
     {pretty = pretty loc,
haftmann@22353
   311
      consts = consts is_loc some_class,
wenzelm@21747
   312
      axioms = axioms,
haftmann@22353
   313
      abbrev = abbrev is_loc some_class,
haftmann@22353
   314
      defs = defs loc some_class,
wenzelm@21747
   315
      notes = notes loc,
wenzelm@21747
   316
      type_syntax = type_syntax loc,
wenzelm@21747
   317
      term_syntax = term_syntax loc,
wenzelm@21747
   318
      declaration = declaration loc,
wenzelm@21747
   319
      target_morphism = target_morphism loc,
wenzelm@22203
   320
      target_naming = target_naming loc,
wenzelm@21804
   321
      reinit = fn _ =>
haftmann@22353
   322
        begin loc o (if is_loc then Locale.init loc else ProofContext.init),
wenzelm@21747
   323
      exit = LocalTheory.target_of}
wenzelm@21747
   324
  end;
wenzelm@20894
   325
haftmann@22353
   326
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann@22353
   327
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   328
wenzelm@21276
   329
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   330
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   331
wenzelm@20894
   332
end;