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