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