src/Pure/Isar/theory_target.ML
author haftmann
Tue Aug 10 14:11:28 2010 +0200 (2010-08-10 ago)
changeset 38308 0e4649095739
parent 38307 0028571ade2d
child 38310 9d4c0c74ae7d
permissions -rw-r--r--
try to uniformly follow define/note/abbrev/declaration order as close as possible
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@38297
    97
            (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
haftmann@38297
    98
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
haftmann@38297
    99
  end;
haftmann@38297
   100
haftmann@38305
   101
fun locale_const_declaration (ta as Target {target, ...}) prmode arg =
haftmann@38305
   102
  locale_declaration target { syntax = true, pervasive = false } (locale_const ta prmode arg);
haftmann@38305
   103
haftmann@38305
   104
fun class_target (Target {target, ...}) f =
haftmann@38305
   105
  Local_Theory.raw_theory f #>
haftmann@38305
   106
  Local_Theory.target (Class_Target.refresh_syntax target);
haftmann@38305
   107
haftmann@38297
   108
wenzelm@37314
   109
(* mixfix notation *)
wenzelm@37314
   110
wenzelm@37314
   111
local
wenzelm@24939
   112
wenzelm@25105
   113
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   114
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   115
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   116
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   117
wenzelm@37314
   118
in
wenzelm@37314
   119
wenzelm@37314
   120
fun prep_mixfix ctxt ta (b, extra_tfrees) mx =
wenzelm@37314
   121
  let
wenzelm@37314
   122
    val mx' =
wenzelm@37314
   123
      if null extra_tfrees then mx
wenzelm@37314
   124
      else
wenzelm@37314
   125
        (warning
wenzelm@37314
   126
          ("Additional type variable(s) in specification of " ^ Binding.str_of b ^ ": " ^
wenzelm@37314
   127
            commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
wenzelm@37314
   128
            (if mx = NoSyn then ""
wenzelm@37314
   129
             else "\nDropping mixfix syntax " ^ Pretty.string_of (Syntax.pretty_mixfix mx)));
wenzelm@37314
   130
          NoSyn);
wenzelm@37314
   131
  in fork_mixfix ta mx' end;
wenzelm@37314
   132
wenzelm@37314
   133
end;
wenzelm@37314
   134
wenzelm@37314
   135
wenzelm@25022
   136
(* define *)
wenzelm@24939
   137
wenzelm@35764
   138
local
wenzelm@35764
   139
wenzelm@35764
   140
fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
wenzelm@35764
   141
haftmann@38303
   142
fun foundation (ta as Target {target, is_locale, is_class, ...})
haftmann@38303
   143
    (((b, U), mx), rhs') name' params (extra_tfrees, type_params) lthy =
haftmann@38303
   144
  let
haftmann@38303
   145
    val (mx1, mx2, mx3) = prep_mixfix lthy ta (b, extra_tfrees) mx;
haftmann@38303
   146
    val (const, lthy2) = lthy |>
haftmann@38303
   147
      (case Class_Target.instantiation_param lthy b of
haftmann@38303
   148
        SOME c' =>
haftmann@38303
   149
          if mx3 <> NoSyn then syntax_error c'
haftmann@38303
   150
          else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
haftmann@38303
   151
            ##> Class_Target.confirm_declaration b
haftmann@38303
   152
      | NONE =>
haftmann@38303
   153
          (case Overloading.operation lthy b of
haftmann@38303
   154
            SOME (c', _) =>
haftmann@38303
   155
              if mx3 <> NoSyn then syntax_error c'
haftmann@38303
   156
              else Local_Theory.theory_result (Overloading.declare (c', U))
haftmann@38303
   157
                ##> Overloading.confirm b
haftmann@38303
   158
          | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
haftmann@38303
   159
    val Const (head, _) = const;
haftmann@38303
   160
    val lhs' = list_comb (const, params);
haftmann@38303
   161
  in
haftmann@38303
   162
    lthy2
haftmann@38303
   163
    |> pair lhs'
haftmann@38303
   164
    ||>> Local_Theory.theory_result
haftmann@38303
   165
      (case Overloading.operation lthy b of
haftmann@38303
   166
        SOME (_, checked) => Overloading.define checked name' (head, rhs')
haftmann@38303
   167
      | NONE =>
haftmann@38303
   168
          if is_some (Class_Target.instantiation_param lthy b)
haftmann@38303
   169
          then AxClass.define_overloaded name' (head, rhs')
haftmann@38303
   170
          else Thm.add_def false false (name', Logic.mk_equals (lhs', rhs')) #>> snd)
haftmann@38305
   171
    ||> is_locale ? locale_const_declaration ta Syntax.mode_default ((b, mx2), lhs')
haftmann@38303
   172
    ||> is_class ? class_target ta (Class_Target.declare target ((b, mx1), (type_params, lhs')))
haftmann@38303
   173
  end;
haftmann@38303
   174
wenzelm@35764
   175
in
wenzelm@35764
   176
haftmann@38306
   177
fun define ta = Generic_Target.define (foundation ta);
wenzelm@24939
   178
wenzelm@35764
   179
end;
wenzelm@35764
   180
wenzelm@24939
   181
haftmann@38308
   182
(* notes *)
haftmann@38308
   183
haftmann@38308
   184
fun theory_notes kind global_facts lthy =
haftmann@38308
   185
  let
haftmann@38308
   186
    val thy = ProofContext.theory_of lthy;
haftmann@38308
   187
    val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
haftmann@38308
   188
  in
haftmann@38308
   189
    lthy
haftmann@38308
   190
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
haftmann@38308
   191
    |> Local_Theory.target (ProofContext.note_thmss kind global_facts' #> snd)
haftmann@38308
   192
  end;
haftmann@38308
   193
haftmann@38308
   194
fun locale_notes locale kind global_facts local_facts lthy = 
haftmann@38308
   195
  let
haftmann@38308
   196
    val global_facts' = Attrib.map_facts (K I) global_facts;
haftmann@38308
   197
    val local_facts' = Element.facts_map (Element.morph_ctxt (Local_Theory.target_morphism lthy)) local_facts;
haftmann@38308
   198
  in
haftmann@38308
   199
    lthy
haftmann@38308
   200
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
haftmann@38308
   201
    |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
haftmann@38308
   202
  end
haftmann@38308
   203
haftmann@38308
   204
fun notes (Target {target, is_locale, ...}) =
haftmann@38308
   205
  Generic_Target.notes (if is_locale then locale_notes target
haftmann@38308
   206
    else fn kind => fn global_facts => fn _ => theory_notes kind global_facts);
haftmann@38308
   207
haftmann@38308
   208
haftmann@38308
   209
(* abbrev *)
haftmann@38308
   210
haftmann@38308
   211
fun theory_abbrev prmode ((b, mx), t) = Local_Theory.theory
haftmann@38308
   212
  (Sign.add_abbrev (#1 prmode) (b, t) #-> (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
haftmann@38308
   213
haftmann@38308
   214
fun locale_abbrev ta prmode ((b, mx), t) xs = Local_Theory.theory_result
haftmann@38308
   215
  (Sign.add_abbrev Print_Mode.internal (b, t)) #-> (fn (lhs, _) =>
haftmann@38308
   216
    locale_const_declaration ta prmode ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
haftmann@38308
   217
haftmann@38308
   218
fun target_abbrev (ta as Target {target, is_locale, is_class, ...})
haftmann@38308
   219
    prmode (b, mx) (global_rhs, t') (extra_tfrees, xs) lthy =
haftmann@38308
   220
  let
haftmann@38308
   221
    val (mx1, mx2, mx3) = prep_mixfix lthy ta (b, extra_tfrees) mx;
haftmann@38308
   222
  in if is_locale
haftmann@38308
   223
    then lthy
haftmann@38308
   224
      |> locale_abbrev ta prmode ((b, mx2), global_rhs) xs
haftmann@38308
   225
      |> is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx1), t'))
haftmann@38308
   226
    else lthy
haftmann@38308
   227
      |> theory_abbrev prmode ((b, mx3), global_rhs)
haftmann@38308
   228
  end;
haftmann@38308
   229
haftmann@38308
   230
fun abbrev ta = Generic_Target.abbrev (target_abbrev ta);
haftmann@38308
   231
haftmann@38308
   232
haftmann@38308
   233
(* pretty *)
haftmann@38308
   234
haftmann@38308
   235
fun pretty_thy ctxt target is_class =
haftmann@38308
   236
  let
haftmann@38308
   237
    val thy = ProofContext.theory_of ctxt;
haftmann@38308
   238
    val target_name =
haftmann@38308
   239
      [Pretty.command (if is_class then "class" else "locale"),
haftmann@38308
   240
        Pretty.str (" " ^ Locale.extern thy target)];
haftmann@38308
   241
    val fixes =
haftmann@38308
   242
      map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
haftmann@38308
   243
    val assumes =
haftmann@38308
   244
      map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])])) (Assumption.all_assms_of ctxt);
haftmann@38308
   245
    val elems =
haftmann@38308
   246
      (if null fixes then [] else [Element.Fixes fixes]) @
haftmann@38308
   247
      (if null assumes then [] else [Element.Assumes assumes]);
haftmann@38308
   248
  in
haftmann@38308
   249
    if target = "" then []
haftmann@38308
   250
    else if null elems then [Pretty.block target_name]
haftmann@38308
   251
    else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
haftmann@38308
   252
      map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
haftmann@38308
   253
  end;
haftmann@38308
   254
haftmann@38308
   255
fun pretty (Target {target, is_class, instantiation, overloading, ...}) ctxt =
haftmann@38308
   256
  Pretty.block [Pretty.command "theory", Pretty.brk 1,
haftmann@38308
   257
      Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
haftmann@38308
   258
    (if not (null overloading) then [Overloading.pretty ctxt]
haftmann@38308
   259
     else if not (null (#1 instantiation)) then [Class_Target.pretty_instantiation ctxt]
haftmann@38308
   260
     else pretty_thy ctxt target is_class);
haftmann@38308
   261
haftmann@38308
   262
wenzelm@35205
   263
(* init various targets *)
wenzelm@25291
   264
wenzelm@25291
   265
local
wenzelm@20894
   266
haftmann@38247
   267
fun init_data (Target {target, is_locale, is_class, instantiation, overloading}) =
haftmann@29358
   268
  if not (null (#1 instantiation)) then Class_Target.init_instantiation instantiation
haftmann@25519
   269
  else if not (null overloading) then Overloading.init overloading
wenzelm@36610
   270
  else if not is_locale then ProofContext.init_global
haftmann@29576
   271
  else if not is_class then Locale.init target
haftmann@29358
   272
  else Class_Target.init target;
haftmann@25269
   273
haftmann@38247
   274
fun init_target (ta as Target {target, instantiation, overloading, ...}) thy =
haftmann@38247
   275
  thy
haftmann@38247
   276
  |> init_data ta
haftmann@38247
   277
  |> Data.put ta
haftmann@38247
   278
  |> Local_Theory.init NONE (Long_Name.base_name target)
haftmann@38308
   279
     {define = define ta,
haftmann@38308
   280
      notes = notes ta,
haftmann@38247
   281
      abbrev = abbrev ta,
haftmann@38305
   282
      declaration = fn pervasive => target_declaration ta { syntax = false, pervasive = pervasive },
haftmann@38305
   283
      syntax_declaration = fn pervasive => target_declaration ta { syntax = true, pervasive = pervasive },
haftmann@38308
   284
      pretty = pretty ta,
haftmann@38247
   285
      reinit = init_target ta o ProofContext.theory_of,
haftmann@38247
   286
      exit = Local_Theory.target_of o
haftmann@38247
   287
        (if not (null (#1 instantiation)) then Class_Target.conclude_instantiation
haftmann@38247
   288
         else if not (null overloading) then Overloading.conclude
haftmann@38247
   289
         else I)};
haftmann@38247
   290
haftmann@38247
   291
fun named_target _ NONE = global_target
haftmann@38247
   292
  | named_target thy (SOME target) =
haftmann@38247
   293
      if Locale.defined thy target
haftmann@38247
   294
      then make_target target true (Class_Target.is_class thy target) ([], [], []) []
haftmann@38247
   295
      else error ("No such locale: " ^ quote target);
wenzelm@20894
   296
wenzelm@26049
   297
fun gen_overloading prep_const raw_ops thy =
wenzelm@26049
   298
  let
wenzelm@36610
   299
    val ctxt = ProofContext.init_global thy;
wenzelm@26049
   300
    val ops = raw_ops |> map (fn (name, const, checked) =>
wenzelm@26049
   301
      (name, Term.dest_Const (prep_const ctxt const), checked));
haftmann@38247
   302
  in thy |> init_target (make_target "" false false ([], [], []) ops) end;
wenzelm@26049
   303
wenzelm@25291
   304
in
wenzelm@20894
   305
haftmann@38247
   306
fun init target thy = init_target (named_target thy target) thy;
haftmann@25269
   307
wenzelm@35205
   308
fun context_cmd "-" thy = init NONE thy
wenzelm@35205
   309
  | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@33282
   310
haftmann@38247
   311
fun instantiation arities = init_target (make_target "" false false arities []);
wenzelm@35205
   312
fun instantiation_cmd arities thy = instantiation (Class_Target.read_multi_arity thy arities) thy;
haftmann@25519
   313
wenzelm@26049
   314
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
wenzelm@26049
   315
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@25462
   316
wenzelm@20894
   317
end;
wenzelm@25291
   318
wenzelm@25291
   319
end;