src/Pure/Isar/theory_target.ML
author wenzelm
Tue Oct 16 17:06:19 2007 +0200 (2007-10-16)
changeset 25053 2b86fac03ec5
parent 25040 4e54c5ae6852
child 25064 c6664770ef6c
permissions -rw-r--r--
misc cleanup of abbrev/local_const;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@24935
     5
Common theory/locale/class targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@25012
    10
  val peek: local_theory -> {target: string, is_locale: bool, is_class: bool}
wenzelm@24935
    11
  val begin: string -> Proof.context -> local_theory
wenzelm@24935
    12
  val init: string option -> theory -> local_theory
wenzelm@24935
    13
  val init_cmd: xstring 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@21006
    19
(* context data *)
wenzelm@21006
    20
wenzelm@25012
    21
datatype target = Target of {target: string, is_locale: bool, is_class: bool};
wenzelm@25012
    22
wenzelm@25012
    23
fun make_target target is_locale is_class =
wenzelm@25012
    24
  Target {target = target, is_locale = is_locale, is_class = is_class};
wenzelm@25012
    25
wenzelm@21006
    26
structure Data = ProofDataFun
wenzelm@21006
    27
(
wenzelm@25012
    28
  type T = target;
wenzelm@25012
    29
  fun init _ = make_target "" false false;
wenzelm@21006
    30
);
wenzelm@21006
    31
wenzelm@25012
    32
val peek = (fn Target args => args) o Data.get;
wenzelm@21006
    33
wenzelm@21006
    34
wenzelm@20894
    35
(* pretty *)
wenzelm@20894
    36
wenzelm@25012
    37
fun pretty (Target {target, is_locale, is_class}) ctxt =
wenzelm@20894
    38
  let
wenzelm@20894
    39
    val thy = ProofContext.theory_of ctxt;
wenzelm@25012
    40
    val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
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@24939
    47
    Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
wenzelm@25012
    48
     (if target = "" then []
wenzelm@25012
    49
      else if null elems then [Pretty.str target_name]
wenzelm@25012
    50
      else [Pretty.big_list (target_name ^ " =")
wenzelm@24939
    51
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
wenzelm@20894
    52
  end;
wenzelm@20894
    53
wenzelm@20894
    54
haftmann@24838
    55
(* target declarations *)
haftmann@24838
    56
wenzelm@25012
    57
fun target_decl add (Target {target, ...}) d lthy =
haftmann@24838
    58
  let
wenzelm@24935
    59
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
haftmann@24838
    60
    val d0 = Morphism.form d';
haftmann@24838
    61
  in
wenzelm@25012
    62
    if target = "" then
haftmann@24838
    63
      lthy
haftmann@24838
    64
      |> LocalTheory.theory (Context.theory_map d0)
haftmann@24838
    65
      |> LocalTheory.target (Context.proof_map d0)
haftmann@24838
    66
    else
haftmann@24838
    67
      lthy
wenzelm@25012
    68
      |> LocalTheory.target (add target d')
haftmann@24838
    69
  end;
haftmann@24838
    70
haftmann@24838
    71
val type_syntax = target_decl Locale.add_type_syntax;
haftmann@24838
    72
val term_syntax = target_decl Locale.add_term_syntax;
haftmann@24838
    73
val declaration = target_decl Locale.add_declaration;
haftmann@24838
    74
wenzelm@25012
    75
fun target_naming (Target {target, ...}) lthy =
wenzelm@25012
    76
  (if target = "" then Sign.naming_of (ProofContext.theory_of lthy)
haftmann@24838
    77
   else ProofContext.naming_of (LocalTheory.target_of lthy))
haftmann@24838
    78
  |> NameSpace.qualified_names;
haftmann@24838
    79
haftmann@24838
    80
wenzelm@21611
    81
wenzelm@20894
    82
(* notes *)
wenzelm@20894
    83
wenzelm@21611
    84
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
    85
  let
wenzelm@21611
    86
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
    87
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
    88
    val certT = Thm.ctyp_of thy;
wenzelm@21611
    89
    val cert = Thm.cterm_of thy;
wenzelm@21594
    90
wenzelm@21611
    91
    (*export assumes/defines*)
wenzelm@21611
    92
    val th = Goal.norm_result raw_th;
wenzelm@21611
    93
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
    94
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
    95
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
    96
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
    97
wenzelm@21611
    98
    (*export fixes*)
wenzelm@22692
    99
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   100
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   101
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   102
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   103
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   104
wenzelm@21611
   105
    (*thm definition*)
wenzelm@21644
   106
    val result = th''
wenzelm@21644
   107
      |> PureThy.name_thm true true ""
wenzelm@21644
   108
      |> Goal.close_result
wenzelm@24779
   109
      |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
wenzelm@21644
   110
      |> PureThy.name_thm true true name;
wenzelm@21611
   111
wenzelm@21611
   112
    (*import fixes*)
wenzelm@21611
   113
    val (tvars, vars) =
wenzelm@21611
   114
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   115
      |>> map Logic.dest_type;
wenzelm@21611
   116
wenzelm@21611
   117
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   118
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   119
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   120
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   121
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   122
wenzelm@21611
   123
    (*import assumes/defines*)
wenzelm@21594
   124
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   125
    val result'' =
wenzelm@21611
   126
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   127
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   128
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   129
      |> Goal.norm_result
wenzelm@21644
   130
      |> PureThy.name_thm false false name;
wenzelm@21611
   131
wenzelm@21611
   132
  in (result'', result) end;
wenzelm@21611
   133
wenzelm@25022
   134
fun notes (Target {target, is_locale, ...}) kind facts lthy =
wenzelm@21585
   135
  let
wenzelm@21594
   136
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   137
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   138
wenzelm@21594
   139
    val facts' = facts
wenzelm@21615
   140
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   141
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   142
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   143
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   144
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@25012
   145
      |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   146
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@25012
   147
      |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
wenzelm@21594
   148
  in
wenzelm@21611
   149
    lthy |> LocalTheory.theory
wenzelm@21611
   150
      (Sign.qualified_names
wenzelm@21611
   151
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   152
        #> Sign.restore_naming thy)
wenzelm@21585
   153
wenzelm@25012
   154
    |> is_locale ? LocalTheory.target (Locale.add_thmss target kind target_facts)
wenzelm@21594
   155
wenzelm@20894
   156
    |> ProofContext.qualified_names
wenzelm@21611
   157
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   158
    ||> ProofContext.restore_naming lthy
wenzelm@20894
   159
  end;
wenzelm@20894
   160
wenzelm@20894
   161
wenzelm@24939
   162
(* consts *)
wenzelm@24939
   163
wenzelm@25053
   164
fun locale_const (prmode as (mode, _)) pos ((c, mx), rhs) phi =
wenzelm@24939
   165
  let
wenzelm@24939
   166
    val c' = Morphism.name phi c;
wenzelm@24939
   167
    val rhs' = Morphism.term phi rhs;
wenzelm@25053
   168
    val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs'));
wenzelm@25053
   169
    val arg = (c', Term.close_schematic_term rhs');
wenzelm@24939
   170
  in
wenzelm@25053
   171
    Context.mapping_result
wenzelm@25053
   172
      (Sign.add_abbrev Syntax.internalM pos legacy_arg)
wenzelm@25053
   173
      (ProofContext.add_abbrev Syntax.internalM pos arg)
wenzelm@25053
   174
    #-> (fn (lhs' as Const (d, _), _) =>
wenzelm@25022
   175
        Type.similar_types (rhs, rhs') ?
wenzelm@25053
   176
          (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
wenzelm@25053
   177
           Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)])))
wenzelm@24939
   178
  end;
wenzelm@24939
   179
wenzelm@25012
   180
fun declare_consts (ta as Target {target, is_locale, is_class}) depends decls lthy =
wenzelm@24939
   181
  let
wenzelm@25053
   182
    val pos = ContextPosition.properties_of lthy;
wenzelm@24939
   183
    val thy = ProofContext.theory_of lthy;
wenzelm@24939
   184
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@24939
   185
wenzelm@24939
   186
    fun const ((c, T), mx) thy =
wenzelm@24939
   187
      let
wenzelm@24939
   188
        val U = map #2 xs ---> T;
wenzelm@25012
   189
        val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
wenzelm@25012
   190
        val mx3 = if is_locale then NoSyn else mx1;
wenzelm@25053
   191
        val (const, thy') = Sign.declare_const pos (c, U, mx3) thy;
wenzelm@24959
   192
        val t = Term.list_comb (const, map Free xs);
wenzelm@24939
   193
      in (((c, mx2), t), thy') end;
haftmann@25040
   194
    val local_const = NameSpace.full (LocalTheory.target_naming lthy);
haftmann@25040
   195
    fun class_const ((c, _), mx) (_, t) =
haftmann@25040
   196
      LocalTheory.raw_theory_result (Class.add_const_in_class target ((c, mx), (local_const c, t)))
wenzelm@25022
   197
      #-> Class.remove_constraint target;
wenzelm@24939
   198
wenzelm@24939
   199
    val (abbrs, lthy') = lthy
wenzelm@24939
   200
      |> LocalTheory.theory_result (fold_map const decls)
wenzelm@24939
   201
  in
wenzelm@24939
   202
    lthy'
wenzelm@25053
   203
    |> is_locale ? fold (term_syntax ta o locale_const Syntax.mode_default pos) abbrs
haftmann@25040
   204
    |> is_class ? fold2 class_const decls abbrs
wenzelm@25028
   205
    |> fold_map (apfst (apsnd snd) oo LocalDefs.add_def) abbrs
wenzelm@24939
   206
  end;
wenzelm@24939
   207
wenzelm@24939
   208
wenzelm@24939
   209
(* abbrev *)
wenzelm@24939
   210
wenzelm@25053
   211
local
wenzelm@25053
   212
wenzelm@25053
   213
fun context_abbrev pos (c, t) lthy = lthy
wenzelm@25053
   214
  |> ProofContext.add_abbrev Syntax.internalM pos (c, t) |> snd
wenzelm@25053
   215
  |> LocalDefs.add_def ((c, NoSyn), t);
wenzelm@25053
   216
wenzelm@25053
   217
fun class_abbrev target prmode ((c, mx), rhs) lthy = lthy   (* FIXME pos *)
wenzelm@25053
   218
  |> LocalTheory.raw_theory_result (Class.add_abbrev_in_class target prmode
wenzelm@25053
   219
      ((c, mx), (NameSpace.full (LocalTheory.target_naming lthy) c, rhs)))
wenzelm@25053
   220
  |-> Class.remove_constraint target;
wenzelm@25053
   221
wenzelm@25053
   222
in
wenzelm@25053
   223
wenzelm@25012
   224
fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
wenzelm@24939
   225
  let
wenzelm@25053
   226
    val pos = ContextPosition.properties_of lthy;
wenzelm@25053
   227
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@25012
   228
    val target_ctxt = LocalTheory.target_of lthy;
wenzelm@24939
   229
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@24939
   230
    val c = Morphism.name target_morphism raw_c;
wenzelm@24939
   231
    val t = Morphism.term target_morphism raw_t;
haftmann@25040
   232
wenzelm@25053
   233
    val xs = map Free (rev (Variable.add_fixed target_ctxt t []));
wenzelm@25053
   234
    val global_rhs =
wenzelm@25053
   235
      singleton (Variable.export_terms (Variable.declare_term t target_ctxt) thy_ctxt)
wenzelm@25053
   236
        (fold_rev lambda xs t);
wenzelm@25053
   237
    val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
wenzelm@24939
   238
  in
wenzelm@25053
   239
    if is_locale then
wenzelm@25053
   240
      lthy
wenzelm@25053
   241
      |> LocalTheory.theory_result (Sign.add_abbrev Syntax.internalM pos (c, global_rhs))
wenzelm@25053
   242
      |-> (fn (lhs, _) =>
wenzelm@25053
   243
        let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
wenzelm@25053
   244
          term_syntax ta (locale_const prmode pos ((c, mx2), lhs')) #>
wenzelm@25053
   245
          is_class ? class_abbrev target prmode ((c, mx1), lhs')
wenzelm@25053
   246
        end)
wenzelm@25053
   247
      |> context_abbrev pos (c, raw_t)
wenzelm@25053
   248
    else
wenzelm@25053
   249
      lthy
wenzelm@25053
   250
      |> LocalTheory.theory
wenzelm@25053
   251
        (Sign.add_abbrev (#1 prmode) pos (c, global_rhs)
wenzelm@25053
   252
          #-> (fn (lhs, _) => Sign.notation true prmode [(lhs, mx1)]))
wenzelm@25053
   253
      |> context_abbrev pos (c, raw_t)
wenzelm@24939
   254
  end;
wenzelm@24939
   255
wenzelm@25053
   256
end;
wenzelm@25053
   257
wenzelm@24939
   258
wenzelm@25022
   259
(* define *)
wenzelm@24939
   260
wenzelm@25022
   261
fun define (ta as Target {target, is_locale, is_class})
wenzelm@25012
   262
    kind ((c, mx), ((name, atts), rhs)) lthy =
wenzelm@24939
   263
  let
wenzelm@24987
   264
    val thy = ProofContext.theory_of lthy;
wenzelm@24939
   265
    val thy_ctxt = ProofContext.init thy;
wenzelm@24939
   266
wenzelm@25022
   267
    val name' = Thm.def_name_optional c name;
wenzelm@24987
   268
    val (rhs', rhs_conv) =
wenzelm@24987
   269
      LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
wenzelm@24987
   270
    val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
wenzelm@24939
   271
    val T = Term.fastype_of rhs;
wenzelm@24939
   272
wenzelm@24939
   273
    (*consts*)
wenzelm@25022
   274
    val ([(lhs, local_def)], lthy2) = lthy
wenzelm@25012
   275
      |> declare_consts ta (member (op =) xs) [((c, T), mx)];
wenzelm@25022
   276
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@24939
   277
wenzelm@24939
   278
    (*def*)
wenzelm@25022
   279
    val (global_def, lthy3) = lthy2
wenzelm@24983
   280
      |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
wenzelm@25022
   281
    val def = LocalDefs.trans_terms lthy3
wenzelm@25022
   282
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   283
       (*global.c xs == rhs'*)  global_def,
wenzelm@25022
   284
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   285
wenzelm@24939
   286
    (*notes*)
wenzelm@24939
   287
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@25022
   288
      |> notes ta kind [((name', atts), [([def], [])])];
wenzelm@24939
   289
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   290
wenzelm@24939
   291
wenzelm@24939
   292
(* axioms *)
wenzelm@24939
   293
wenzelm@25022
   294
fun axioms ta kind (vars, specs) lthy =
wenzelm@24939
   295
  let
wenzelm@25022
   296
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@25022
   297
    val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
wenzelm@25022
   298
    val xs = fold Term.add_frees expanded_props [];
wenzelm@24989
   299
wenzelm@25022
   300
    (*consts*)
wenzelm@25022
   301
    val (consts, lthy') = declare_consts ta (member (op =) xs) vars lthy;
wenzelm@25022
   302
    val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
wenzelm@25022
   303
wenzelm@25022
   304
    (*axioms*)
wenzelm@24989
   305
    val hyps = map Thm.term_of (Assumption.assms_of lthy');
wenzelm@24939
   306
    fun axiom ((name, atts), props) thy = thy
wenzelm@24983
   307
      |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
wenzelm@24939
   308
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@24939
   309
  in
wenzelm@24987
   310
    lthy'
wenzelm@25022
   311
    |> fold Variable.declare_term expanded_props
wenzelm@25022
   312
    |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
wenzelm@24939
   313
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@25022
   314
    |-> notes ta kind
wenzelm@24987
   315
    |>> pair (map #1 consts)
wenzelm@24939
   316
  end;
wenzelm@24939
   317
wenzelm@24939
   318
wenzelm@20894
   319
(* init and exit *)
wenzelm@20894
   320
wenzelm@25012
   321
fun begin target ctxt =
haftmann@22353
   322
  let
haftmann@22353
   323
    val thy = ProofContext.theory_of ctxt;
wenzelm@25012
   324
    val is_locale = target <> "";
wenzelm@25028
   325
    val is_class = Class.is_class thy target;
wenzelm@25012
   326
    val ta = Target {target = target, is_locale = is_locale, is_class = is_class};
haftmann@22353
   327
  in
haftmann@22353
   328
    ctxt
wenzelm@25012
   329
    |> Data.put ta
wenzelm@25012
   330
    |> is_class ? Class.init target
wenzelm@25012
   331
    |> LocalTheory.init (NameSpace.base target)
wenzelm@25012
   332
     {pretty = pretty ta,
wenzelm@25022
   333
      axioms = axioms ta,
wenzelm@25012
   334
      abbrev = abbrev ta,
wenzelm@25022
   335
      define = define ta,
wenzelm@25022
   336
      notes = notes ta,
wenzelm@25012
   337
      type_syntax = type_syntax ta,
wenzelm@25012
   338
      term_syntax = term_syntax ta,
wenzelm@25012
   339
      declaration = declaration ta,
wenzelm@25012
   340
      target_naming = target_naming ta,
wenzelm@21804
   341
      reinit = fn _ =>
wenzelm@25028
   342
        begin target o (if is_locale then Locale.init target else ProofContext.init),
wenzelm@24935
   343
      exit = LocalTheory.target_of}
wenzelm@21747
   344
  end;
wenzelm@20894
   345
wenzelm@24935
   346
fun init NONE thy = begin "" (ProofContext.init thy)
wenzelm@25012
   347
  | init (SOME target) thy = begin target (Locale.init target thy);
wenzelm@20894
   348
wenzelm@24935
   349
fun init_cmd (SOME "-") thy = init NONE thy
wenzelm@25012
   350
  | init_cmd target thy = init (Option.map (Locale.intern thy) target) thy;
wenzelm@20894
   351
wenzelm@20894
   352
end;