src/Pure/Isar/theory_target.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22846 fb79144af9a3
child 23087 ad7244663431
permissions -rw-r--r--
tuned Pure/General/name_space.ML
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
  type T = string option;
wenzelm@21006
    26
  fun init _ = NONE;
wenzelm@21006
    27
);
wenzelm@21006
    28
wenzelm@21006
    29
val peek = Data.get;
wenzelm@21006
    30
wenzelm@21006
    31
wenzelm@20894
    32
(* pretty *)
wenzelm@20894
    33
wenzelm@20894
    34
fun pretty loc ctxt =
wenzelm@20894
    35
  let
wenzelm@20894
    36
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    37
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    38
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    39
    val elems =
wenzelm@20894
    40
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    41
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    42
  in
wenzelm@20984
    43
    if loc = "" then
wenzelm@20984
    44
      [Pretty.block
wenzelm@20984
    45
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    46
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    47
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    48
    else
wenzelm@20984
    49
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    50
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    51
  end;
wenzelm@20894
    52
wenzelm@20894
    53
wenzelm@21804
    54
(* consts *)
wenzelm@21804
    55
wenzelm@21825
    56
fun internal_abbrev prmode ((c, mx), t) =
wenzelm@22380
    57
  (* FIXME really permissive *)
wenzelm@22380
    58
  LocalTheory.term_syntax (perhaps o try o ProofContext.target_abbrev prmode ((c, mx), t)) #>
wenzelm@21825
    59
  ProofContext.add_abbrev Syntax.internalM (c, t) #> snd;
wenzelm@21804
    60
haftmann@22353
    61
fun consts is_loc some_class depends decls lthy =
wenzelm@21804
    62
  let
wenzelm@21804
    63
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@21747
    64
wenzelm@21804
    65
    fun const ((c, T), mx) thy =
wenzelm@21804
    66
      let
wenzelm@21804
    67
        val U = map #2 xs ---> T;
wenzelm@21804
    68
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
haftmann@22353
    69
        val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21804
    70
        val thy' = Sign.add_consts_authentic [(c, U, mx1)] thy;
wenzelm@21804
    71
      in (((c, mx2), t), thy') end;
wenzelm@21804
    72
wenzelm@21804
    73
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21804
    74
    val defs = map (apsnd (pair ("", []))) abbrs;
wenzelm@21804
    75
  in
wenzelm@21804
    76
    lthy'
wenzelm@21825
    77
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
wenzelm@21804
    78
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
wenzelm@21804
    79
  end;
wenzelm@21804
    80
wenzelm@21804
    81
wenzelm@21804
    82
(* abbrev *)
wenzelm@21747
    83
wenzelm@21747
    84
fun occ_params ctxt ts =
wenzelm@21747
    85
  #1 (ProofContext.inferred_fixes ctxt)
wenzelm@21747
    86
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
wenzelm@21747
    87
haftmann@22353
    88
fun abbrev is_loc some_class  prmode ((raw_c, mx), raw_t) lthy =
wenzelm@21747
    89
  let
wenzelm@21747
    90
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@21747
    91
    val target = LocalTheory.target_of lthy;
wenzelm@21747
    92
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@21747
    93
wenzelm@21747
    94
    val c = Morphism.name target_morphism raw_c;
wenzelm@21747
    95
    val t = Morphism.term target_morphism raw_t;
wenzelm@21747
    96
    val xs = map Free (occ_params target [t]);
wenzelm@21747
    97
    val u = fold_rev Term.lambda xs t;
wenzelm@21804
    98
    val U = Term.fastype_of u;
wenzelm@21747
    99
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
wenzelm@21747
   100
wenzelm@21804
   101
    val ((lhs as Const (full_c, _), rhs), lthy1) = lthy
wenzelm@21747
   102
      |> LocalTheory.theory_result (Sign.add_abbrev (#1 prmode) (c, u'));
haftmann@22353
   103
    val (mx1, mx2) = ClassPackage.fork_mixfix is_loc some_class mx;
wenzelm@21747
   104
  in
wenzelm@21747
   105
    lthy1
wenzelm@21804
   106
    |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
wenzelm@21825
   107
    |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
wenzelm@21804
   108
    |> ProofContext.local_abbrev (c, rhs)
wenzelm@21747
   109
  end;
wenzelm@21747
   110
wenzelm@21747
   111
wenzelm@20894
   112
(* defs *)
wenzelm@20894
   113
wenzelm@21845
   114
local
wenzelm@21570
   115
wenzelm@21594
   116
fun expand_term ctxt t =
wenzelm@21570
   117
  let
wenzelm@21570
   118
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   119
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   120
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   121
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
   122
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
   123
wenzelm@20894
   124
fun add_def (name, prop) =
wenzelm@20894
   125
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   126
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   127
wenzelm@20894
   128
in
wenzelm@20894
   129
haftmann@22353
   130
fun defs loc some_class kind args lthy0 =
wenzelm@20894
   131
  let
wenzelm@21533
   132
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   133
      let
wenzelm@21594
   134
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   135
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   136
wenzelm@21570
   137
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   138
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   139
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   140
wenzelm@21570
   141
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   142
        val (def, lthy3) = lthy2
wenzelm@20894
   143
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@21845
   144
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   145
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   146
           (*lhs' == rhs'*)  def,
wenzelm@21845
   147
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
haftmann@22353
   148
        val lthy4 = case some_class
haftmann@22353
   149
         of SOME class => 
haftmann@22424
   150
              lthy3
haftmann@22424
   151
              |> LocalTheory.raw_theory
haftmann@22353
   152
                (ClassPackage.add_def_in_class lthy3 class
haftmann@22424
   153
                  ((c, mx), ((name, atts), (rhs, eq))))
haftmann@22353
   154
          | _ => lthy3;
haftmann@22301
   155
      in ((lhs, ((name', atts), [([eq], [])])), lthy4) end;
wenzelm@20894
   156
wenzelm@21570
   157
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   158
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
haftmann@22424
   159
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@22692
   213
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   214
    val frees = map Free (Thm.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@22673
   280
fun target_decl add loc d lthy =
wenzelm@22673
   281
  let
wenzelm@22673
   282
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
wenzelm@22673
   283
    val d0 = Morphism.form d';
wenzelm@22673
   284
  in
wenzelm@22673
   285
    if loc = "" then
wenzelm@22673
   286
      lthy
wenzelm@22673
   287
      |> LocalTheory.theory (Context.theory_map d0)
wenzelm@22673
   288
      |> LocalTheory.target (Context.proof_map d0)
wenzelm@22673
   289
    else
wenzelm@22673
   290
      lthy
wenzelm@22673
   291
      |> LocalTheory.target (add loc d')
wenzelm@22673
   292
  end;
wenzelm@20894
   293
wenzelm@21611
   294
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   295
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   296
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   297
wenzelm@21533
   298
fun target_morphism loc lthy =
wenzelm@21594
   299
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   300
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   301
wenzelm@22203
   302
fun target_naming loc lthy =
wenzelm@21668
   303
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   304
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@22203
   305
  |> NameSpace.qualified_names;
wenzelm@21533
   306
wenzelm@20894
   307
wenzelm@20894
   308
(* init and exit *)
wenzelm@20894
   309
haftmann@22353
   310
fun begin loc ctxt =
haftmann@22353
   311
  let
haftmann@22353
   312
    val thy = ProofContext.theory_of ctxt;
haftmann@22353
   313
    val is_loc = loc <> "";
haftmann@22353
   314
    val some_class = ClassPackage.class_of_locale thy loc;
haftmann@22353
   315
  in
haftmann@22353
   316
    ctxt
haftmann@22353
   317
    |> Data.put (if is_loc then SOME loc else NONE)
haftmann@22353
   318
    |> LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   319
     {pretty = pretty loc,
haftmann@22353
   320
      consts = consts is_loc some_class,
wenzelm@21747
   321
      axioms = axioms,
haftmann@22353
   322
      abbrev = abbrev is_loc some_class,
haftmann@22353
   323
      defs = defs loc some_class,
wenzelm@21747
   324
      notes = notes loc,
wenzelm@21747
   325
      type_syntax = type_syntax loc,
wenzelm@21747
   326
      term_syntax = term_syntax loc,
wenzelm@21747
   327
      declaration = declaration loc,
wenzelm@21747
   328
      target_morphism = target_morphism loc,
wenzelm@22203
   329
      target_naming = target_naming loc,
wenzelm@21804
   330
      reinit = fn _ =>
haftmann@22353
   331
        begin loc o (if is_loc then Locale.init loc else ProofContext.init),
wenzelm@21747
   332
      exit = LocalTheory.target_of}
wenzelm@21747
   333
  end;
wenzelm@20894
   334
haftmann@22353
   335
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann@22353
   336
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   337
wenzelm@21276
   338
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   339
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   340
wenzelm@20894
   341
end;