src/Pure/Isar/theory_target.ML
author haftmann
Wed Aug 11 12:24:24 2010 +0200 (2010-08-11 ago)
changeset 38341 72dba5bd5f63
parent 38339 fb8fd73827d4
child 38342 09d4a04d5c2e
permissions -rw-r--r--
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    Author:     Makarius
wenzelm@30437
     3
    Author:     Florian Haftmann, TU Muenchen
wenzelm@20894
     4
haftmann@25542
     5
Common theory/locale/class/instantiation/overloading targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
haftmann@38339
    10
  val peek: local_theory -> {target: string, is_locale: bool, is_class: bool}
haftmann@25269
    11
  val init: string option -> theory -> local_theory
wenzelm@35205
    12
  val context_cmd: xstring -> theory -> local_theory
haftmann@25864
    13
  val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
haftmann@29971
    14
  val instantiation_cmd: xstring list * xstring list * xstring -> theory -> local_theory
haftmann@25864
    15
  val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
wenzelm@26049
    16
  val overloading_cmd: (string * string * bool) list -> theory -> local_theory
wenzelm@20894
    17
end;
wenzelm@20894
    18
wenzelm@33553
    19
structure Theory_Target: THEORY_TARGET =
wenzelm@20894
    20
struct
wenzelm@20894
    21
wenzelm@21006
    22
(* context data *)
wenzelm@21006
    23
haftmann@38338
    24
datatype target = Target of {target: string, is_locale: bool, is_class: bool};
wenzelm@25012
    25
haftmann@38338
    26
fun make_target target is_locale is_class =
haftmann@38338
    27
  Target {target = target, is_locale = is_locale, is_class = is_class};
wenzelm@25291
    28
haftmann@38338
    29
val global_target = make_target "" false false;
wenzelm@25012
    30
wenzelm@33519
    31
structure Data = Proof_Data
wenzelm@21006
    32
(
wenzelm@25012
    33
  type T = target;
wenzelm@25291
    34
  fun init _ = global_target;
wenzelm@21006
    35
);
wenzelm@21006
    36
wenzelm@25012
    37
val peek = (fn Target args => args) o Data.get;
wenzelm@21006
    38
wenzelm@21006
    39
wenzelm@33456
    40
(* generic declarations *)
wenzelm@33456
    41
haftmann@38305
    42
fun locale_declaration locale { syntax, pervasive } decl lthy =
haftmann@24838
    43
  let
haftmann@38305
    44
    val add = if syntax then Locale.add_syntax_declaration else Locale.add_declaration;
haftmann@38305
    45
    val locale_decl = Morphism.transform (Local_Theory.target_morphism lthy) decl;
haftmann@24838
    46
  in
haftmann@38305
    47
    lthy
haftmann@38341
    48
    |> pervasive ? Generic_Target.theory_declaration decl
haftmann@38305
    49
    |> Local_Theory.target (add locale locale_decl)
haftmann@24838
    50
  end;
haftmann@24838
    51
haftmann@38305
    52
fun target_declaration (Target {target, ...}) { syntax, pervasive } =
haftmann@38341
    53
  if target = "" then Generic_Target.theory_declaration
haftmann@38305
    54
  else locale_declaration target { syntax = syntax, pervasive = pervasive };
haftmann@24838
    55
wenzelm@21611
    56
haftmann@38297
    57
(* consts in locales *)
haftmann@38297
    58
haftmann@38297
    59
fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
haftmann@38297
    60
  let
haftmann@38297
    61
    val b' = Morphism.binding phi b;
haftmann@38297
    62
    val rhs' = Morphism.term phi rhs;
haftmann@38297
    63
    val arg = (b', Term.close_schematic_term rhs');
haftmann@38297
    64
    val same_shape = Term.aconv_untyped (rhs, rhs');
haftmann@38297
    65
    (* FIXME workaround based on educated guess *)
haftmann@38297
    66
    val prefix' = Binding.prefix_of b';
haftmann@38297
    67
    val is_canonical_class = is_class andalso
haftmann@38297
    68
      (Binding.eq_name (b, b')
haftmann@38297
    69
        andalso not (null prefix')
haftmann@38297
    70
        andalso List.last prefix' = (Class_Target.class_prefix target, false)
haftmann@38297
    71
      orelse same_shape);
haftmann@38297
    72
  in
haftmann@38297
    73
    not is_canonical_class ?
haftmann@38297
    74
      (Context.mapping_result
haftmann@38297
    75
        (Sign.add_abbrev Print_Mode.internal arg)
haftmann@38297
    76
        (ProofContext.add_abbrev Print_Mode.internal arg)
haftmann@38297
    77
      #-> (fn (lhs' as Const (d, _), _) =>
haftmann@38297
    78
          same_shape ?
haftmann@38310
    79
            (Context.mapping
haftmann@38310
    80
              (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
haftmann@38297
    81
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
haftmann@38297
    82
  end;
haftmann@38297
    83
haftmann@38305
    84
fun locale_const_declaration (ta as Target {target, ...}) prmode arg =
haftmann@38305
    85
  locale_declaration target { syntax = true, pervasive = false } (locale_const ta prmode arg);
haftmann@38305
    86
haftmann@38305
    87
fun class_target (Target {target, ...}) f =
haftmann@38305
    88
  Local_Theory.raw_theory f #>
haftmann@38305
    89
  Local_Theory.target (Class_Target.refresh_syntax target);
haftmann@38305
    90
haftmann@38297
    91
haftmann@38318
    92
(* define *)
haftmann@38318
    93
haftmann@38318
    94
fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
haftmann@38318
    95
haftmann@38318
    96
fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
haftmann@38341
    97
  Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
haftmann@38318
    98
  #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, mx), lhs)
haftmann@38318
    99
    #> pair (lhs, def))
haftmann@38318
   100
haftmann@38318
   101
fun class_foundation (ta as Target {target, ...})
haftmann@38318
   102
    (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
haftmann@38341
   103
  Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
haftmann@38318
   104
  #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
haftmann@38318
   105
    #> class_target ta (Class_Target.declare target ((b, mx), (type_params, lhs)))
haftmann@38318
   106
    #> pair (lhs, def))
haftmann@38318
   107
haftmann@38318
   108
fun instantiation_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38318
   109
  case Class_Target.instantiation_param lthy b
haftmann@38318
   110
   of SOME c => if mx <> NoSyn then syntax_error c
haftmann@38318
   111
        else lthy |> Local_Theory.theory_result (AxClass.declare_overloaded (c, U)
haftmann@38318
   112
            ##>> AxClass.define_overloaded b_def (c, rhs))
haftmann@38318
   113
          ||> Class_Target.confirm_declaration b
haftmann@38318
   114
    | NONE => lthy |>
haftmann@38341
   115
        Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params);
haftmann@38318
   116
haftmann@38318
   117
fun overloading_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38318
   118
  case Overloading.operation lthy b
haftmann@38318
   119
   of SOME (c, checked) => if mx <> NoSyn then syntax_error c
haftmann@38318
   120
        else lthy |> Local_Theory.theory_result (Overloading.declare (c, U)
haftmann@38318
   121
            ##>> Overloading.define checked b_def (c, rhs))
haftmann@38319
   122
          ||> Overloading.confirm b
haftmann@38318
   123
    | NONE => lthy |>
haftmann@38341
   124
        Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params);
wenzelm@37314
   125
wenzelm@25105
   126
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   127
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   128
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   129
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   130
haftmann@38303
   131
fun foundation (ta as Target {target, is_locale, is_class, ...})
haftmann@38314
   132
    (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38303
   133
  let
haftmann@38312
   134
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
haftmann@38303
   135
    val (const, lthy2) = lthy |>
haftmann@38303
   136
      (case Class_Target.instantiation_param lthy b of
haftmann@38314
   137
        SOME c =>
haftmann@38314
   138
          if mx3 <> NoSyn then syntax_error c
haftmann@38314
   139
          else Local_Theory.theory_result (AxClass.declare_overloaded (c, U))
haftmann@38303
   140
            ##> Class_Target.confirm_declaration b
haftmann@38303
   141
      | NONE =>
haftmann@38303
   142
          (case Overloading.operation lthy b of
haftmann@38314
   143
            SOME (c, _) =>
haftmann@38314
   144
              if mx3 <> NoSyn then syntax_error c
haftmann@38314
   145
              else Local_Theory.theory_result (Overloading.declare (c, U))
haftmann@38303
   146
                ##> Overloading.confirm b
haftmann@38303
   147
          | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
haftmann@38303
   148
    val Const (head, _) = const;
haftmann@38314
   149
    val lhs = list_comb (const, type_params @ term_params);
haftmann@38303
   150
  in
haftmann@38303
   151
    lthy2
haftmann@38314
   152
    |> pair lhs
haftmann@38303
   153
    ||>> Local_Theory.theory_result
haftmann@38303
   154
      (case Overloading.operation lthy b of
haftmann@38314
   155
        SOME (_, checked) => Overloading.define checked b_def (head, rhs)
haftmann@38303
   156
      | NONE =>
haftmann@38303
   157
          if is_some (Class_Target.instantiation_param lthy b)
haftmann@38314
   158
          then AxClass.define_overloaded b_def (head, rhs)
haftmann@38314
   159
          else Thm.add_def false false (b_def, Logic.mk_equals (lhs, rhs)) #>> snd)
haftmann@38314
   160
    ||> is_locale ? locale_const_declaration ta Syntax.mode_default ((b, mx2), lhs)
haftmann@38310
   161
    ||> is_class ? class_target ta
haftmann@38314
   162
          (Class_Target.declare target ((b, mx1), (type_params, lhs)))
haftmann@38303
   163
  end;
haftmann@38303
   164
wenzelm@24939
   165
haftmann@38308
   166
(* notes *)
haftmann@38308
   167
haftmann@38308
   168
fun locale_notes locale kind global_facts local_facts lthy = 
haftmann@38308
   169
  let
haftmann@38308
   170
    val global_facts' = Attrib.map_facts (K I) global_facts;
haftmann@38310
   171
    val local_facts' = Element.facts_map
haftmann@38310
   172
      (Element.morph_ctxt (Local_Theory.target_morphism lthy)) local_facts;
haftmann@38308
   173
  in
haftmann@38308
   174
    lthy
haftmann@38308
   175
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
haftmann@38308
   176
    |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
haftmann@38308
   177
  end
haftmann@38308
   178
haftmann@38319
   179
fun target_notes (ta as Target {target, is_locale, ...}) =
haftmann@38319
   180
  if is_locale then locale_notes target
haftmann@38341
   181
    else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;
haftmann@38308
   182
haftmann@38308
   183
haftmann@38308
   184
(* abbrev *)
haftmann@38308
   185
haftmann@38308
   186
fun locale_abbrev ta prmode ((b, mx), t) xs = Local_Theory.theory_result
haftmann@38310
   187
  (Sign.add_abbrev Print_Mode.internal (b, t)) #->
haftmann@38310
   188
    (fn (lhs, _) => locale_const_declaration ta prmode
haftmann@38310
   189
      ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
haftmann@38308
   190
haftmann@38308
   191
fun target_abbrev (ta as Target {target, is_locale, is_class, ...})
haftmann@38312
   192
    prmode (b, mx) (global_rhs, t') xs lthy =
haftmann@38318
   193
  if is_locale
haftmann@38308
   194
    then lthy
haftmann@38318
   195
      |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
haftmann@38318
   196
      |> is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx), t'))
haftmann@38308
   197
    else lthy
haftmann@38341
   198
      |> Generic_Target.theory_abbrev prmode ((b, mx), global_rhs);
haftmann@38308
   199
haftmann@38308
   200
haftmann@38308
   201
(* pretty *)
haftmann@38308
   202
haftmann@38308
   203
fun pretty_thy ctxt target is_class =
haftmann@38308
   204
  let
haftmann@38308
   205
    val thy = ProofContext.theory_of ctxt;
haftmann@38308
   206
    val target_name =
haftmann@38308
   207
      [Pretty.command (if is_class then "class" else "locale"),
haftmann@38308
   208
        Pretty.str (" " ^ Locale.extern thy target)];
haftmann@38310
   209
    val fixes = map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
haftmann@38310
   210
      (#1 (ProofContext.inferred_fixes ctxt));
haftmann@38310
   211
    val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
haftmann@38310
   212
      (Assumption.all_assms_of ctxt);
haftmann@38308
   213
    val elems =
haftmann@38308
   214
      (if null fixes then [] else [Element.Fixes fixes]) @
haftmann@38308
   215
      (if null assumes then [] else [Element.Assumes assumes]);
haftmann@38308
   216
  in
haftmann@38308
   217
    if target = "" then []
haftmann@38308
   218
    else if null elems then [Pretty.block target_name]
haftmann@38308
   219
    else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
haftmann@38308
   220
      map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
haftmann@38308
   221
  end;
haftmann@38308
   222
haftmann@38338
   223
fun pretty (Target {target, is_class, ...}) ctxt =
haftmann@38308
   224
  Pretty.block [Pretty.command "theory", Pretty.brk 1,
haftmann@38308
   225
      Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
haftmann@38338
   226
    pretty_thy ctxt target is_class;
haftmann@38308
   227
haftmann@38308
   228
wenzelm@35205
   229
(* init various targets *)
wenzelm@25291
   230
wenzelm@25291
   231
local
wenzelm@20894
   232
haftmann@38338
   233
fun init_data (Target {target, is_locale, is_class}) =
haftmann@38338
   234
  if not is_locale then ProofContext.init_global
haftmann@29576
   235
  else if not is_class then Locale.init target
haftmann@29358
   236
  else Class_Target.init target;
haftmann@25269
   237
haftmann@38338
   238
fun init_target (ta as Target {target, ...}) thy =
haftmann@38247
   239
  thy
haftmann@38247
   240
  |> init_data ta
haftmann@38247
   241
  |> Data.put ta
haftmann@38247
   242
  |> Local_Theory.init NONE (Long_Name.base_name target)
haftmann@38319
   243
     {define = Generic_Target.define (foundation ta),
haftmann@38319
   244
      notes = Generic_Target.notes (target_notes ta),
haftmann@38319
   245
      abbrev = Generic_Target.abbrev (target_abbrev ta),
haftmann@38310
   246
      declaration = fn pervasive => target_declaration ta
haftmann@38310
   247
        { syntax = false, pervasive = pervasive },
haftmann@38310
   248
      syntax_declaration = fn pervasive => target_declaration ta
haftmann@38310
   249
        { syntax = true, pervasive = pervasive },
haftmann@38308
   250
      pretty = pretty ta,
haftmann@38247
   251
      reinit = init_target ta o ProofContext.theory_of,
haftmann@38338
   252
      exit = Local_Theory.target_of};
haftmann@38247
   253
haftmann@38247
   254
fun named_target _ NONE = global_target
haftmann@38247
   255
  | named_target thy (SOME target) =
haftmann@38247
   256
      if Locale.defined thy target
haftmann@38338
   257
      then make_target target true (Class_Target.is_class thy target)
haftmann@38247
   258
      else error ("No such locale: " ^ quote target);
wenzelm@20894
   259
wenzelm@26049
   260
fun gen_overloading prep_const raw_ops thy =
wenzelm@26049
   261
  let
wenzelm@36610
   262
    val ctxt = ProofContext.init_global thy;
wenzelm@26049
   263
    val ops = raw_ops |> map (fn (name, const, checked) =>
wenzelm@26049
   264
      (name, Term.dest_Const (prep_const ctxt const), checked));
haftmann@38319
   265
  in
haftmann@38319
   266
    thy
haftmann@38319
   267
    |> Overloading.init ops
haftmann@38319
   268
    |> Local_Theory.init NONE ""
haftmann@38319
   269
       {define = Generic_Target.define overloading_foundation,
haftmann@38319
   270
        notes = Generic_Target.notes
haftmann@38341
   271
          (fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts),
haftmann@38319
   272
        abbrev = Generic_Target.abbrev
haftmann@38319
   273
          (fn prmode => fn (b, mx) => fn (t, _) => fn _ =>
haftmann@38341
   274
            Generic_Target.theory_abbrev prmode ((b, mx), t)),
haftmann@38341
   275
        declaration = K Generic_Target.theory_declaration,
haftmann@38341
   276
        syntax_declaration = K Generic_Target.theory_declaration,
haftmann@38319
   277
        pretty = single o Overloading.pretty,
haftmann@38319
   278
        reinit = gen_overloading prep_const raw_ops o ProofContext.theory_of,
haftmann@38319
   279
        exit = Local_Theory.target_of o Overloading.conclude}
haftmann@38319
   280
  end;
wenzelm@26049
   281
wenzelm@25291
   282
in
wenzelm@20894
   283
haftmann@38247
   284
fun init target thy = init_target (named_target thy target) thy;
haftmann@25269
   285
wenzelm@35205
   286
fun context_cmd "-" thy = init NONE thy
wenzelm@35205
   287
  | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@33282
   288
haftmann@38319
   289
fun instantiation arities thy =
haftmann@38319
   290
  thy
haftmann@38319
   291
  |> Class_Target.init_instantiation arities
haftmann@38319
   292
  |> Local_Theory.init NONE ""
haftmann@38319
   293
     {define = Generic_Target.define instantiation_foundation,
haftmann@38319
   294
      notes = Generic_Target.notes
haftmann@38341
   295
        (fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts),
haftmann@38319
   296
      abbrev = Generic_Target.abbrev
haftmann@38341
   297
        (fn prmode => fn (b, mx) => fn (t, _) => fn _ => Generic_Target.theory_abbrev prmode ((b, mx), t)),
haftmann@38341
   298
      declaration = K Generic_Target.theory_declaration,
haftmann@38341
   299
      syntax_declaration = K Generic_Target.theory_declaration,
haftmann@38319
   300
      pretty = single o Class_Target.pretty_instantiation,
haftmann@38319
   301
      reinit = instantiation arities o ProofContext.theory_of,
haftmann@38319
   302
      exit = Local_Theory.target_of o Class_Target.conclude_instantiation};
haftmann@38319
   303
haftmann@38310
   304
fun instantiation_cmd arities thy =
haftmann@38310
   305
  instantiation (Class_Target.read_multi_arity thy arities) thy;
haftmann@25519
   306
wenzelm@26049
   307
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
wenzelm@26049
   308
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@25462
   309
wenzelm@20894
   310
end;
wenzelm@25291
   311
wenzelm@25291
   312
end;