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