src/Pure/Isar/generic_target.ML
author haftmann
Tue, 10 Aug 2010 15:09:39 +0200
changeset 38315 fa3f90cf6d9c
parent 38313 e7e84919392b
child 38341 72dba5bd5f63
permissions -rw-r--r--
basic renumbering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Isar/theory_target.ML
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     2
    Author:     Makarius
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     4
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     5
Common target infrastructure.
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     6
*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     7
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     8
signature GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     9
sig
38313
e7e84919392b separated type from term parameters
haftmann
parents: 38312
diff changeset
    10
  val define: (((binding * typ) * mixfix) * (binding * term)
e7e84919392b separated type from term parameters
haftmann
parents: 38312
diff changeset
    11
    -> term list * term list -> local_theory -> (term * thm) * local_theory)
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    12
    -> (binding * mixfix) * (Attrib.binding * term) -> local_theory
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    13
    -> (term * (string * thm)) * local_theory
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    14
  val notes: (string
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    15
    -> (Attrib.binding * (thm list * Args.src list) list) list
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    16
    -> (Attrib.binding * (thm list * Args.src list) list) list
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    17
    -> local_theory -> local_theory)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    18
    -> string -> (Attrib.binding * (thm list * Args.src list) list) list
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    19
    -> local_theory -> (string * thm list) list * local_theory
38310
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
    20
  val abbrev: (string * bool -> binding * mixfix -> term * term
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    21
    -> term list -> local_theory -> local_theory)
38310
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
    22
    -> string * bool -> (binding * mixfix) * term -> local_theory
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
    23
    -> (term * term) * local_theory
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    24
end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    25
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    26
structure Generic_Target: GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    27
struct
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    28
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    29
(* mixfix syntax *)
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    30
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    31
fun check_mixfix ctxt (b, extra_tfrees) mx =
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    32
  if null extra_tfrees then mx
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    33
  else
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    34
    (warning
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    35
      ("Additional type variable(s) in specification of " ^ Binding.str_of b ^ ": " ^
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    36
        commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    37
        (if mx = NoSyn then ""
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    38
         else "\nDropping mixfix syntax " ^ Pretty.string_of (Syntax.pretty_mixfix mx)));
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    39
      NoSyn);
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    40
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    41
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    42
(* define *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    43
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    44
fun define foundation ((b, mx), ((proto_b_def, atts), rhs)) lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    45
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    46
    val thy = ProofContext.theory_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    47
    val thy_ctxt = ProofContext.init_global thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    48
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    49
    val b_def = Thm.def_binding_optional b proto_b_def;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    50
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    51
    (*term and type parameters*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    52
    val crhs = Thm.cterm_of thy rhs;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    53
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    54
    val rhs_conv = MetaSimplifier.rewrite true defs crhs;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    55
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    56
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    57
    val T = Term.fastype_of rhs;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    58
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    59
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    60
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    61
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    62
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    63
    val term_params =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    64
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    65
      |> map_filter (fn (_, x) =>
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    66
        (case AList.lookup (op =) xs x of
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    67
          SOME T => SOME (Free (x, T))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    68
        | NONE => NONE));
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    69
    val params = type_params @ term_params;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    70
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    71
    val U = map Term.fastype_of params ---> T;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    72
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    73
    (*foundation*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    74
    val ((lhs', global_def), lthy2) = foundation
38313
e7e84919392b separated type from term parameters
haftmann
parents: 38312
diff changeset
    75
      (((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    76
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    77
    (*local definition*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    78
    val ((lhs, local_def), lthy3) = lthy2
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    79
      |> Local_Defs.add_def ((b, NoSyn), lhs');
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    80
    val def = Local_Defs.trans_terms lthy3
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    81
      [(*c == global.c xs*)     local_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    82
       (*global.c xs == rhs'*)  global_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    83
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    84
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    85
    (*note*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    86
    val ([(res_name, [res])], lthy4) = lthy3
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    87
      |> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    88
  in ((lhs, (res_name, res)), lthy4) end;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    89
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    90
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    91
(* notes *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    92
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    93
fun import_export_proof ctxt (name, raw_th) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    94
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    95
    val thy = ProofContext.theory_of ctxt;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    96
    val thy_ctxt = ProofContext.init_global thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    97
    val certT = Thm.ctyp_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    98
    val cert = Thm.cterm_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    99
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   100
    (*export assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   101
    val th = Goal.norm_result raw_th;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   102
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
38310
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
   103
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume)
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
   104
      (Assumption.all_assms_of ctxt);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   105
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   106
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   107
    (*export fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   108
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   109
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   110
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   111
      |> Variable.export ctxt thy_ctxt
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   112
      |> Drule.zero_var_indexes_list;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   113
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   114
    (*thm definition*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   115
    val result = PureThy.name_thm true true name th'';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   116
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   117
    (*import fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   118
    val (tvars, vars) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   119
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   120
      |>> map Logic.dest_type;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   121
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   122
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   123
    val inst = filter (is_Var o fst) (vars ~~ frees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   124
    val cinstT = map (pairself certT o apfst TVar) instT;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   125
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   126
    val result' = Thm.instantiate (cinstT, cinst) result;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   127
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   128
    (*import assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   129
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   130
    val result'' =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   131
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   132
        NONE => raise THM ("Failed to re-import result", 0, [result'])
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   133
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   134
      |> Goal.norm_result
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   135
      |> PureThy.name_thm false false name;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   136
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   137
  in (result'', result) end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   138
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   139
fun notes target_notes kind facts lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   140
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   141
    val thy = ProofContext.theory_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   142
    val facts' = facts
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   143
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   144
          (Local_Theory.full_name lthy (fst a))) bs))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   145
      |> PureThy.map_facts (import_export_proof lthy);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   146
    val local_facts = PureThy.map_facts #1 facts';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   147
    val global_facts = PureThy.map_facts #2 facts';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   148
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   149
    lthy
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   150
    |> target_notes kind global_facts local_facts
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   151
    |> ProofContext.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   152
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   153
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   154
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   155
(* abbrev *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   156
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   157
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   158
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   159
    val thy_ctxt = ProofContext.init_global (ProofContext.theory_of lthy);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   160
    val target_ctxt = Local_Theory.target_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   161
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   162
    val t' = Assumption.export_term lthy target_ctxt t;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   163
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   164
    val u = fold_rev lambda xs t';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   165
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   166
    val extra_tfrees =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   167
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   168
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   169
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   170
    val global_rhs =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   171
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   172
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   173
    lthy
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   174
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   175
    |> ProofContext.add_abbrev Print_Mode.internal (b, t) |> snd
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   176
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   177
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   178
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   179
end;