src/Pure/Isar/generic_target.ML
author wenzelm
Tue, 03 Apr 2012 09:47:20 +0200
changeset 48168 0e65b6a016dc
parent 48167 d344f6d9cc85
child 48169 ca4cf5de366c
permissions -rw-r--r--
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm@42830
     1
(*  Title:      Pure/Isar/generic_target.ML
haftmann@38533
     2
    Author:     Makarius
haftmann@38533
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@38533
     4
haftmann@38533
     5
Common target infrastructure.
haftmann@38533
     6
*)
haftmann@38533
     7
haftmann@38533
     8
signature GENERIC_TARGET =
haftmann@38533
     9
sig
wenzelm@41032
    10
  val define: (((binding * typ) * mixfix) * (binding * term) ->
wenzelm@41032
    11
      term list * term list -> local_theory -> (term * thm) * local_theory) ->
wenzelm@47865
    12
    bool -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@41032
    13
    (term * (string * thm)) * local_theory
wenzelm@48126
    14
  val notes:
wenzelm@48126
    15
    (string -> (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@41032
    16
      (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) ->
wenzelm@41032
    17
    string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory ->
wenzelm@41032
    18
    (string * thm list) list * local_theory
wenzelm@48126
    19
  val locale_notes: string -> string ->
wenzelm@48126
    20
    (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@48126
    21
    (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@48126
    22
    local_theory -> local_theory
wenzelm@41032
    23
  val abbrev: (string * bool -> binding * mixfix -> term * term ->
wenzelm@41032
    24
      term list -> local_theory -> local_theory) ->
wenzelm@48126
    25
    string * bool -> (binding * mixfix) * term -> local_theory -> (term * term) * local_theory
wenzelm@47957
    26
  val background_declaration: declaration -> local_theory -> local_theory
wenzelm@48164
    27
  val locale_declaration: string -> bool -> declaration -> local_theory -> local_theory
wenzelm@48166
    28
  val standard_declaration: (int -> bool) -> declaration -> local_theory -> local_theory
wenzelm@48166
    29
  val generic_const: bool -> Syntax.mode -> (binding * mixfix) * term ->
wenzelm@48166
    30
    Context.generic -> Context.generic
wenzelm@48166
    31
  val const_declaration: (int -> bool) -> Syntax.mode -> (binding * mixfix) * term ->
wenzelm@48166
    32
    local_theory -> local_theory
wenzelm@48168
    33
  val background_foundation: ((binding * typ) * mixfix) * (binding * term) ->
wenzelm@48168
    34
    term list * term list -> local_theory -> (term * thm) * local_theory
wenzelm@41032
    35
  val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
wenzelm@41032
    36
    term list * term list -> local_theory -> (term * thm) * local_theory
wenzelm@48126
    37
  val theory_notes: string ->
wenzelm@48126
    38
    (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@48126
    39
    (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@41032
    40
    local_theory -> local_theory
wenzelm@41032
    41
  val theory_abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory
wenzelm@48163
    42
  val theory_declaration: declaration -> local_theory -> local_theory
wenzelm@46224
    43
end
haftmann@38533
    44
haftmann@38533
    45
structure Generic_Target: GENERIC_TARGET =
haftmann@38533
    46
struct
haftmann@38533
    47
haftmann@38577
    48
(** lifting primitive to target operations **)
haftmann@38577
    49
haftmann@38536
    50
(* mixfix syntax *)
haftmann@38536
    51
haftmann@38536
    52
fun check_mixfix ctxt (b, extra_tfrees) mx =
haftmann@38536
    53
  if null extra_tfrees then mx
haftmann@38536
    54
  else
wenzelm@39077
    55
    (Context_Position.if_visible ctxt warning
wenzelm@43252
    56
      ("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^
haftmann@38536
    57
        commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
haftmann@38536
    58
        (if mx = NoSyn then ""
wenzelm@43159
    59
         else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)));
haftmann@38536
    60
      NoSyn);
haftmann@38536
    61
haftmann@38536
    62
haftmann@38533
    63
(* define *)
haftmann@38533
    64
wenzelm@47865
    65
fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy =
haftmann@38533
    66
  let
wenzelm@43231
    67
    val thy = Proof_Context.theory_of lthy;
wenzelm@43231
    68
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@38533
    69
haftmann@38533
    70
    (*term and type parameters*)
wenzelm@48160
    71
    val ((defs, _), rhs') = Thm.cterm_of thy rhs
wenzelm@48160
    72
      |> Local_Defs.export_cterm lthy thy_ctxt ||> Thm.term_of;
haftmann@38533
    73
wenzelm@48156
    74
    val xs = Variable.add_fixed lthy rhs' [];
haftmann@38533
    75
    val T = Term.fastype_of rhs;
haftmann@38533
    76
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
haftmann@38533
    77
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
haftmann@38536
    78
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
haftmann@38533
    79
haftmann@38533
    80
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
wenzelm@48156
    81
    val term_params = map Free (sort (Variable.fixed_ord lthy o pairself #1) xs);
haftmann@38533
    82
    val params = type_params @ term_params;
haftmann@38533
    83
haftmann@38533
    84
    val U = map Term.fastype_of params ---> T;
haftmann@38533
    85
haftmann@38533
    86
    (*foundation*)
wenzelm@46224
    87
    val ((lhs', global_def), lthy2) = lthy
wenzelm@46224
    88
      |> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params);
haftmann@38533
    89
haftmann@38533
    90
    (*local definition*)
haftmann@38539
    91
    val ((lhs, local_def), lthy3) = lthy2
haftmann@38533
    92
      |> Local_Defs.add_def ((b, NoSyn), lhs');
wenzelm@48111
    93
wenzelm@48111
    94
    (*result*)
wenzelm@48109
    95
    val def =
wenzelm@48111
    96
      Thm.transitive local_def global_def
wenzelm@48111
    97
      |> Local_Defs.contract defs
wenzelm@48111
    98
          (Thm.cterm_of (Proof_Context.theory_of lthy3) (Logic.mk_equals (lhs, rhs)));
haftmann@38539
    99
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@47956
   100
      |> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])];
haftmann@38539
   101
  in ((lhs, (res_name, res)), lthy4) end;
haftmann@38533
   102
haftmann@38533
   103
haftmann@38533
   104
(* notes *)
haftmann@38533
   105
wenzelm@48126
   106
local
wenzelm@48126
   107
haftmann@38533
   108
fun import_export_proof ctxt (name, raw_th) =
haftmann@38533
   109
  let
wenzelm@43231
   110
    val thy = Proof_Context.theory_of ctxt;
wenzelm@43231
   111
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@38533
   112
    val certT = Thm.ctyp_of thy;
haftmann@38533
   113
    val cert = Thm.cterm_of thy;
haftmann@38533
   114
haftmann@38533
   115
    (*export assumes/defines*)
haftmann@38533
   116
    val th = Goal.norm_result raw_th;
wenzelm@46281
   117
    val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th;
wenzelm@46281
   118
    val asms' = map (Raw_Simplifier.rewrite_rule defs) asms;
haftmann@38533
   119
haftmann@38533
   120
    (*export fixes*)
haftmann@38533
   121
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
haftmann@38533
   122
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@46223
   123
    val (th'' :: vs) =
wenzelm@46223
   124
      (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
haftmann@38533
   125
      |> Variable.export ctxt thy_ctxt
haftmann@38533
   126
      |> Drule.zero_var_indexes_list;
haftmann@38533
   127
haftmann@38533
   128
    (*thm definition*)
wenzelm@46284
   129
    val result = Global_Theory.name_thm true true name th'';
haftmann@38533
   130
haftmann@38533
   131
    (*import fixes*)
haftmann@38533
   132
    val (tvars, vars) =
haftmann@38533
   133
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
haftmann@38533
   134
      |>> map Logic.dest_type;
haftmann@38533
   135
haftmann@38533
   136
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
haftmann@38533
   137
    val inst = filter (is_Var o fst) (vars ~~ frees);
haftmann@38533
   138
    val cinstT = map (pairself certT o apfst TVar) instT;
haftmann@38533
   139
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
haftmann@38533
   140
    val result' = Thm.instantiate (cinstT, cinst) result;
haftmann@38533
   141
haftmann@38533
   142
    (*import assumes/defines*)
haftmann@38533
   143
    val result'' =
wenzelm@46281
   144
      (fold (curry op COMP) asms' result'
wenzelm@46281
   145
        handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms'))
wenzelm@48109
   146
      |> Local_Defs.contract defs (Thm.cprop_of th)
haftmann@38533
   147
      |> Goal.norm_result
wenzelm@39814
   148
      |> Global_Theory.name_thm false false name;
haftmann@38533
   149
haftmann@38533
   150
  in (result'', result) end;
haftmann@38533
   151
wenzelm@48126
   152
fun standard_facts lthy ctxt =
wenzelm@48126
   153
  Element.transform_facts (Local_Theory.standard_morphism lthy ctxt);
wenzelm@48126
   154
wenzelm@48126
   155
in
wenzelm@48126
   156
haftmann@38533
   157
fun notes target_notes kind facts lthy =
haftmann@38533
   158
  let
haftmann@38533
   159
    val facts' = facts
wenzelm@39814
   160
      |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
haftmann@38533
   161
          (Local_Theory.full_name lthy (fst a))) bs))
wenzelm@39814
   162
      |> Global_Theory.map_facts (import_export_proof lthy);
wenzelm@39814
   163
    val local_facts = Global_Theory.map_facts #1 facts';
wenzelm@39814
   164
    val global_facts = Global_Theory.map_facts #2 facts';
haftmann@38533
   165
  in
haftmann@38533
   166
    lthy
wenzelm@48126
   167
    |> target_notes kind global_facts (Attrib.partial_evaluation lthy local_facts)
wenzelm@48125
   168
    |> Attrib.local_notes kind local_facts
haftmann@38533
   169
  end;
haftmann@38533
   170
wenzelm@48126
   171
fun locale_notes locale kind global_facts local_facts =
wenzelm@48126
   172
  Local_Theory.background_theory
wenzelm@48126
   173
    (Attrib.global_notes kind (Attrib.map_facts (K []) global_facts) #> snd) #>
wenzelm@48126
   174
  (fn lthy => lthy |>
wenzelm@48126
   175
    Local_Theory.target (fn ctxt => ctxt |>
wenzelm@48126
   176
      Locale.add_thmss locale kind (standard_facts lthy ctxt local_facts))) #>
wenzelm@48126
   177
  (fn lthy => lthy |>
wenzelm@48126
   178
    Local_Theory.map_contexts (fn level => fn ctxt =>
wenzelm@48126
   179
      if level = 0 orelse level = Local_Theory.level lthy then ctxt
wenzelm@48126
   180
      else ctxt |> Attrib.local_notes kind (standard_facts lthy ctxt local_facts) |> snd));
wenzelm@48126
   181
wenzelm@48126
   182
end;
wenzelm@48126
   183
haftmann@38533
   184
haftmann@38533
   185
(* abbrev *)
haftmann@38533
   186
haftmann@38533
   187
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
haftmann@38533
   188
  let
wenzelm@43231
   189
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
haftmann@38533
   190
    val target_ctxt = Local_Theory.target_of lthy;
haftmann@38533
   191
haftmann@38533
   192
    val t' = Assumption.export_term lthy target_ctxt t;
wenzelm@48156
   193
    val xs = map Free (rev (Variable.add_fixed lthy t' []));
haftmann@38533
   194
    val u = fold_rev lambda xs t';
haftmann@38533
   195
haftmann@38533
   196
    val extra_tfrees =
haftmann@38533
   197
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
haftmann@38536
   198
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
haftmann@38533
   199
haftmann@38533
   200
    val global_rhs =
haftmann@38533
   201
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
haftmann@38533
   202
  in
haftmann@38533
   203
    lthy
haftmann@38536
   204
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
wenzelm@43231
   205
    |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
haftmann@38533
   206
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
haftmann@38533
   207
  end;
haftmann@38533
   208
haftmann@38577
   209
wenzelm@47957
   210
(* declaration *)
wenzelm@47957
   211
wenzelm@47957
   212
fun background_declaration decl lthy =
wenzelm@47957
   213
  let
wenzelm@47957
   214
    val theory_decl =
wenzelm@47957
   215
      Local_Theory.standard_form lthy
wenzelm@47957
   216
        (Proof_Context.init_global (Proof_Context.theory_of lthy)) decl;
wenzelm@47957
   217
  in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end;
wenzelm@47957
   218
wenzelm@48122
   219
fun locale_declaration locale syntax decl lthy = lthy
wenzelm@48122
   220
  |> Local_Theory.target (fn ctxt => ctxt |>
wenzelm@48122
   221
    Locale.add_declaration locale syntax
wenzelm@48122
   222
      (Morphism.transform (Local_Theory.standard_morphism lthy ctxt) decl));
wenzelm@48122
   223
wenzelm@48166
   224
fun standard_declaration pred decl lthy =
wenzelm@48166
   225
  Local_Theory.map_contexts (fn level => fn ctxt =>
wenzelm@48166
   226
    if pred level then Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt
wenzelm@48166
   227
    else ctxt) lthy;
wenzelm@48166
   228
wenzelm@48166
   229
wenzelm@48166
   230
(* const declaration *)
wenzelm@48166
   231
wenzelm@48166
   232
fun generic_const same_shape prmode ((b, mx), t) context =
wenzelm@48166
   233
  let
wenzelm@48166
   234
    val const_alias =
wenzelm@48166
   235
      if same_shape then
wenzelm@48166
   236
        (case t of
wenzelm@48166
   237
          Const (c, T) =>
wenzelm@48166
   238
            let
wenzelm@48166
   239
              val thy = Context.theory_of context;
wenzelm@48166
   240
              val ctxt = Context.proof_of context;
wenzelm@48166
   241
              val t' = Syntax.check_term ctxt (Const (c, dummyT))
wenzelm@48166
   242
                |> singleton (Variable.polymorphic ctxt);
wenzelm@48166
   243
            in
wenzelm@48166
   244
              (case t' of
wenzelm@48166
   245
                Const (c', T') =>
wenzelm@48166
   246
                  if c = c' andalso Sign.typ_equiv thy (T, T') then SOME c else NONE
wenzelm@48166
   247
              | _ => NONE)
wenzelm@48166
   248
            end
wenzelm@48166
   249
        | _ => NONE)
wenzelm@48166
   250
      else NONE;
wenzelm@48166
   251
  in
wenzelm@48166
   252
    (case const_alias of
wenzelm@48166
   253
      SOME c =>
wenzelm@48166
   254
        context
wenzelm@48166
   255
        |> Context.mapping (Sign.const_alias b c) (Proof_Context.const_alias b c)
wenzelm@48166
   256
        |> Morphism.form (Proof_Context.generic_notation true prmode [(t, mx)])
wenzelm@48166
   257
    | NONE =>
wenzelm@48166
   258
        context
wenzelm@48166
   259
        |> Proof_Context.generic_add_abbrev Print_Mode.internal (b, t)
wenzelm@48166
   260
        |-> (fn (const as Const (c, _), _) => same_shape ?
wenzelm@48166
   261
              (Proof_Context.generic_revert_abbrev (#1 prmode) c #>
wenzelm@48166
   262
               Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)]))))
wenzelm@48166
   263
  end;
wenzelm@48166
   264
wenzelm@48166
   265
fun const_declaration pred prmode ((b, mx), rhs) =
wenzelm@48166
   266
  standard_declaration pred (fn phi =>
wenzelm@48166
   267
    let
wenzelm@48166
   268
      val b' = Morphism.binding phi b;
wenzelm@48166
   269
      val rhs' = Morphism.term phi rhs;
wenzelm@48166
   270
      val same_shape = Term.aconv_untyped (rhs, rhs');
wenzelm@48167
   271
    in generic_const same_shape prmode ((b', mx), Term.close_schematic_term rhs') end);
wenzelm@48164
   272
wenzelm@47957
   273
wenzelm@46223
   274
haftmann@38577
   275
(** primitive theory operations **)
haftmann@38577
   276
wenzelm@48168
   277
fun background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38577
   278
  let
wenzelm@43246
   279
    val (const, lthy2) = lthy
wenzelm@43246
   280
      |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
haftmann@38577
   281
    val lhs = list_comb (const, type_params @ term_params);
wenzelm@43246
   282
    val ((_, def), lthy3) = lthy2
wenzelm@43246
   283
      |> Local_Theory.background_theory_result
wenzelm@47798
   284
        (Thm.add_def lthy2 false false
wenzelm@47798
   285
          (Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs)));
haftmann@38577
   286
  in ((lhs, def), lthy3) end;
haftmann@38577
   287
wenzelm@48168
   288
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
wenzelm@48168
   289
  background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)
wenzelm@48168
   290
  #-> (fn (lhs, def) => const_declaration (K true) Syntax.mode_default ((b, mx), lhs)
wenzelm@48168
   291
    #> pair (lhs, def));
wenzelm@48168
   292
wenzelm@48126
   293
fun theory_notes kind global_facts local_facts =
wenzelm@48125
   294
  Local_Theory.background_theory (Attrib.global_notes kind global_facts #> snd) #>
wenzelm@48126
   295
  (fn lthy => lthy |> Local_Theory.map_contexts (fn level => fn ctxt =>
wenzelm@48126
   296
    if level = Local_Theory.level lthy then ctxt
wenzelm@48126
   297
    else
wenzelm@48126
   298
      ctxt |> Attrib.local_notes kind
wenzelm@48126
   299
        (Element.transform_facts (Local_Theory.standard_morphism lthy ctxt) local_facts) |> snd));
haftmann@38577
   300
wenzelm@39032
   301
fun theory_abbrev prmode ((b, mx), t) =
wenzelm@39032
   302
  Local_Theory.background_theory
wenzelm@39032
   303
    (Sign.add_abbrev (#1 prmode) (b, t) #->
wenzelm@39032
   304
      (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
haftmann@38577
   305
wenzelm@48163
   306
fun theory_declaration decl =
wenzelm@48166
   307
  background_declaration decl #> standard_declaration (K true) decl;
wenzelm@48163
   308
haftmann@38533
   309
end;