src/Pure/Isar/theory_target.ML
author wenzelm
Sun Nov 11 14:00:11 2007 +0100 (2007-11-11)
changeset 25392 bb8d225dcf05
parent 25381 c100bf5bd6b8
child 25395 e83bef45e6a7
permissions -rw-r--r--
abbrev: PrintMode.input instead of PrintMode.internal for global version!
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}
haftmann@25269
    11
  val init: string option -> theory -> local_theory
wenzelm@24935
    12
  val begin: string -> Proof.context -> local_theory
wenzelm@25291
    13
  val context: xstring -> 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@25291
    23
fun make_target target is_locale is_class =
wenzelm@25291
    24
  Target {target = target, is_locale = is_locale, is_class = is_class};
wenzelm@25291
    25
wenzelm@25291
    26
val global_target = make_target "" false false;
wenzelm@25012
    27
wenzelm@21006
    28
structure Data = ProofDataFun
wenzelm@21006
    29
(
wenzelm@25012
    30
  type T = target;
wenzelm@25291
    31
  fun init _ = global_target;
wenzelm@21006
    32
);
wenzelm@21006
    33
wenzelm@25012
    34
val peek = (fn Target args => args) o Data.get;
wenzelm@21006
    35
wenzelm@21006
    36
wenzelm@20894
    37
(* pretty *)
wenzelm@20894
    38
wenzelm@25012
    39
fun pretty (Target {target, is_locale, is_class}) ctxt =
wenzelm@20894
    40
  let
wenzelm@20894
    41
    val thy = ProofContext.theory_of ctxt;
wenzelm@25012
    42
    val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
wenzelm@20894
    43
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    44
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    45
    val elems =
wenzelm@20894
    46
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    47
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    48
  in
wenzelm@24939
    49
    Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
wenzelm@25012
    50
     (if target = "" then []
wenzelm@25012
    51
      else if null elems then [Pretty.str target_name]
wenzelm@25012
    52
      else [Pretty.big_list (target_name ^ " =")
wenzelm@24939
    53
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
wenzelm@20894
    54
  end;
wenzelm@20894
    55
wenzelm@20894
    56
haftmann@24838
    57
(* target declarations *)
haftmann@24838
    58
wenzelm@25012
    59
fun target_decl add (Target {target, ...}) d lthy =
haftmann@24838
    60
  let
wenzelm@24935
    61
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
haftmann@24838
    62
    val d0 = Morphism.form d';
haftmann@24838
    63
  in
wenzelm@25012
    64
    if target = "" then
haftmann@24838
    65
      lthy
haftmann@24838
    66
      |> LocalTheory.theory (Context.theory_map d0)
haftmann@24838
    67
      |> LocalTheory.target (Context.proof_map d0)
haftmann@24838
    68
    else
haftmann@24838
    69
      lthy
wenzelm@25012
    70
      |> LocalTheory.target (add target d')
haftmann@24838
    71
  end;
haftmann@24838
    72
haftmann@24838
    73
val type_syntax = target_decl Locale.add_type_syntax;
haftmann@24838
    74
val term_syntax = target_decl Locale.add_term_syntax;
haftmann@24838
    75
val declaration = target_decl Locale.add_declaration;
haftmann@24838
    76
wenzelm@25105
    77
fun class_target (Target {target, ...}) f =
wenzelm@25105
    78
  LocalTheory.raw_theory f #>
wenzelm@25105
    79
  LocalTheory.target (Class.refresh_syntax target);
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@25105
   162
(* declare_const *)
wenzelm@24939
   163
wenzelm@25105
   164
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   165
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   166
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   167
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   168
wenzelm@25212
   169
fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) pos ((c, mx), rhs) phi =
wenzelm@24939
   170
  let
wenzelm@24939
   171
    val c' = Morphism.name phi c;
wenzelm@24939
   172
    val rhs' = Morphism.term phi rhs;
wenzelm@25053
   173
    val legacy_arg = (c', Term.close_schematic_term (Logic.legacy_varify rhs'));
wenzelm@25053
   174
    val arg = (c', Term.close_schematic_term rhs');
wenzelm@25372
   175
    val similar_body = Type.similar_types (rhs, rhs');
wenzelm@25212
   176
    (* FIXME workaround based on educated guess *)
wenzelm@25372
   177
    val class_global = c' = NameSpace.qualified (Class.class_prefix target) c;
wenzelm@24939
   178
  in
wenzelm@25372
   179
    not (is_class andalso (similar_body orelse class_global)) ?
wenzelm@25212
   180
      (Context.mapping_result
wenzelm@25212
   181
        (Sign.add_abbrev PrintMode.internal pos legacy_arg)
wenzelm@25212
   182
        (ProofContext.add_abbrev PrintMode.internal pos arg)
wenzelm@25212
   183
      #-> (fn (lhs' as Const (d, _), _) =>
wenzelm@25372
   184
          similar_body ?
wenzelm@25212
   185
            (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
wenzelm@25212
   186
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
wenzelm@24939
   187
  end;
wenzelm@24939
   188
wenzelm@25105
   189
fun declare_const (ta as Target {target, is_locale, is_class}) depends ((c, T), mx) lthy =
wenzelm@24939
   190
  let
wenzelm@25053
   191
    val pos = ContextPosition.properties_of lthy;
wenzelm@24939
   192
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@25105
   193
    val U = map #2 xs ---> T;
wenzelm@25105
   194
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@25105
   195
    val (const, lthy') = lthy |> LocalTheory.theory_result (Sign.declare_const pos (c, U, mx3));
wenzelm@25105
   196
    val t = Term.list_comb (const, map Free xs);
wenzelm@24939
   197
  in
wenzelm@24939
   198
    lthy'
wenzelm@25212
   199
    |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default pos ((c, mx2), t))
wenzelm@25105
   200
    |> is_class ? class_target ta (Class.add_logical_const target pos ((c, mx1), t))
wenzelm@25105
   201
    |> LocalDefs.add_def ((c, NoSyn), t)
wenzelm@24939
   202
  end;
wenzelm@24939
   203
wenzelm@24939
   204
wenzelm@24939
   205
(* abbrev *)
wenzelm@24939
   206
wenzelm@25105
   207
fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((c, mx), t) lthy =
wenzelm@24939
   208
  let
wenzelm@25053
   209
    val pos = ContextPosition.properties_of lthy;
wenzelm@25053
   210
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@25012
   211
    val target_ctxt = LocalTheory.target_of lthy;
wenzelm@25105
   212
wenzelm@25105
   213
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@25105
   214
    val t' = Assumption.export_term lthy target_ctxt t;
wenzelm@25105
   215
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
wenzelm@25105
   216
    val u = fold_rev lambda xs t';
wenzelm@25105
   217
    val global_rhs =
wenzelm@25105
   218
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
wenzelm@25121
   219
  in
wenzelm@25121
   220
    lthy |>
wenzelm@25121
   221
     (if is_locale then
wenzelm@25392
   222
        LocalTheory.theory_result (Sign.add_abbrev PrintMode.input pos (c, global_rhs))
wenzelm@25121
   223
        #-> (fn (lhs, _) =>
wenzelm@25105
   224
          let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
wenzelm@25212
   225
            term_syntax ta (locale_const ta prmode pos ((c, mx2), lhs')) #>
wenzelm@25150
   226
            is_class ? class_target ta (Class.add_syntactic_const target prmode pos ((c, mx1), t'))
wenzelm@25105
   227
          end)
wenzelm@25105
   228
      else
wenzelm@25121
   229
        LocalTheory.theory
wenzelm@25105
   230
          (Sign.add_abbrev (#1 prmode) pos (c, global_rhs) #-> (fn (lhs, _) =>
wenzelm@25121
   231
           Sign.notation true prmode [(lhs, mx3)])))
wenzelm@25121
   232
    |> ProofContext.add_abbrev PrintMode.internal pos (c, t) |> snd
wenzelm@25121
   233
    |> LocalDefs.fixed_abbrev ((c, NoSyn), t)
wenzelm@24939
   234
  end;
wenzelm@24939
   235
wenzelm@24939
   236
wenzelm@25022
   237
(* define *)
wenzelm@24939
   238
wenzelm@25022
   239
fun define (ta as Target {target, is_locale, is_class})
wenzelm@25012
   240
    kind ((c, mx), ((name, atts), rhs)) lthy =
wenzelm@24939
   241
  let
wenzelm@24987
   242
    val thy = ProofContext.theory_of lthy;
wenzelm@24939
   243
    val thy_ctxt = ProofContext.init thy;
wenzelm@24939
   244
wenzelm@25022
   245
    val name' = Thm.def_name_optional c name;
wenzelm@24987
   246
    val (rhs', rhs_conv) =
wenzelm@24987
   247
      LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
wenzelm@24987
   248
    val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
wenzelm@24939
   249
    val T = Term.fastype_of rhs;
wenzelm@24939
   250
wenzelm@25105
   251
    (*const*)
wenzelm@25105
   252
    val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((c, T), mx);
wenzelm@25022
   253
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@24939
   254
wenzelm@24939
   255
    (*def*)
wenzelm@25022
   256
    val (global_def, lthy3) = lthy2
wenzelm@24983
   257
      |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
wenzelm@25022
   258
    val def = LocalDefs.trans_terms lthy3
wenzelm@25022
   259
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   260
       (*global.c xs == rhs'*)  global_def,
wenzelm@25022
   261
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   262
wenzelm@25105
   263
    (*note*)
wenzelm@24939
   264
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@25022
   265
      |> notes ta kind [((name', atts), [([def], [])])];
wenzelm@24939
   266
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   267
wenzelm@24939
   268
wenzelm@24939
   269
(* axioms *)
wenzelm@24939
   270
wenzelm@25022
   271
fun axioms ta kind (vars, specs) lthy =
wenzelm@24939
   272
  let
wenzelm@25022
   273
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@25022
   274
    val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
wenzelm@25022
   275
    val xs = fold Term.add_frees expanded_props [];
wenzelm@24989
   276
wenzelm@25022
   277
    (*consts*)
wenzelm@25105
   278
    val (consts, lthy') = fold_map (declare_const ta (member (op =) xs)) vars lthy;
wenzelm@25022
   279
    val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
wenzelm@25022
   280
wenzelm@25022
   281
    (*axioms*)
wenzelm@24989
   282
    val hyps = map Thm.term_of (Assumption.assms_of lthy');
wenzelm@24939
   283
    fun axiom ((name, atts), props) thy = thy
wenzelm@24983
   284
      |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
wenzelm@24939
   285
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@24939
   286
  in
wenzelm@24987
   287
    lthy'
wenzelm@25022
   288
    |> fold Variable.declare_term expanded_props
wenzelm@25022
   289
    |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
wenzelm@24939
   290
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@25022
   291
    |-> notes ta kind
wenzelm@24987
   292
    |>> pair (map #1 consts)
wenzelm@24939
   293
  end;
wenzelm@24939
   294
wenzelm@24939
   295
wenzelm@25291
   296
(* init *)
wenzelm@25291
   297
wenzelm@25291
   298
local
wenzelm@20894
   299
wenzelm@25291
   300
fun init_target _ NONE = global_target
wenzelm@25291
   301
  | init_target thy (SOME target) = make_target target true (Class.is_class thy target);
wenzelm@25291
   302
wenzelm@25291
   303
fun init_ctxt (Target {target, is_locale, is_class}) =
wenzelm@25320
   304
  if not is_locale then ProofContext.init
wenzelm@25320
   305
  else if not is_class then Locale.init target
wenzelm@25320
   306
  else Class.init target;
haftmann@25269
   307
wenzelm@25291
   308
fun init_lthy (ta as Target {target, ...}) =
wenzelm@25291
   309
  Data.put ta #>
wenzelm@25291
   310
  LocalTheory.init (NameSpace.base target)
wenzelm@25291
   311
   {pretty = pretty ta,
wenzelm@25291
   312
    axioms = axioms ta,
wenzelm@25291
   313
    abbrev = abbrev ta,
wenzelm@25291
   314
    define = define ta,
wenzelm@25291
   315
    notes = notes ta,
wenzelm@25291
   316
    type_syntax = type_syntax ta,
wenzelm@25291
   317
    term_syntax = term_syntax ta,
wenzelm@25291
   318
    declaration = declaration ta,
wenzelm@25291
   319
    reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
wenzelm@25291
   320
    exit = LocalTheory.target_of}
wenzelm@25291
   321
and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
wenzelm@20894
   322
wenzelm@25291
   323
in
wenzelm@20894
   324
wenzelm@25291
   325
fun init target thy = init_lthy_ctxt (init_target thy target) thy;
wenzelm@25291
   326
fun begin target ctxt = init_lthy (init_target (ProofContext.theory_of ctxt) (SOME target)) ctxt;
haftmann@25269
   327
wenzelm@25291
   328
fun context "-" thy = init NONE thy
wenzelm@25291
   329
  | context target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@20894
   330
wenzelm@20894
   331
end;
wenzelm@25291
   332
wenzelm@25291
   333
end;
wenzelm@25291
   334