src/Pure/Isar/bundle.ML
author wenzelm
Tue, 20 Mar 2012 20:00:13 +0100
changeset 47057 12423b36fcc4
child 47066 8a6124d09ff5
permissions -rw-r--r--
basic support for bundled declarations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47057
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/bundle.ML
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     3
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     4
Bundled declarations (notes etc.).
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     5
*)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     6
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     7
signature BUNDLE =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     8
sig
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
     9
  type bundle = (thm list * Args.src list) list
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    10
  val the_bundle: Proof.context -> string -> bundle
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    11
  val check: Proof.context -> xstring * Position.T -> string
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    12
  val bundle: binding * (thm list * Args.src list) list ->
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    13
    (binding * typ option * mixfix) list -> local_theory -> local_theory
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    14
  val bundle_cmd: binding * (Facts.ref * Args.src list) list ->
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    15
    (binding * string option * mixfix) list -> local_theory -> local_theory
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    16
  val include_: string -> Proof.state -> Proof.state
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    17
  val include_cmd: xstring * Position.T -> Proof.state -> Proof.state
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    18
  val including: string -> Proof.state -> Proof.state
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    19
  val including_cmd: xstring * Position.T -> Proof.state -> Proof.state
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    20
  val print_bundles: Proof.context -> unit
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    21
end;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    22
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    23
structure Bundle: BUNDLE =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    24
struct
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    25
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    26
(* maintain bundles *)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    27
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    28
type bundle = (thm list * Args.src list) list;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    29
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    30
fun transform_bundle phi : bundle -> bundle =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    31
  map (fn (fact, atts) => (Morphism.fact phi fact, map (Args.transform_values phi) atts));
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    32
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    33
structure Data = Generic_Data
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    34
(
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    35
  type T = bundle Name_Space.table;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    36
  val empty : T = Name_Space.empty_table "bundle";
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    37
  val extend = I;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    38
  val merge = Name_Space.merge_tables;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    39
);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    40
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    41
val get_bundles = Data.get o Context.Proof;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    42
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    43
fun the_bundle ctxt name =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    44
  (case Symtab.lookup (#2 (get_bundles ctxt)) name of
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    45
    SOME bundle => bundle
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    46
  | NONE => error ("Unknown bundle " ^ quote name));
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    47
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    48
fun check ctxt = #1 o Name_Space.check (Context.Proof ctxt) (get_bundles ctxt);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    49
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    50
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    51
(* define bundle *)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    52
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    53
local
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    54
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    55
fun gen_bundle prep_fact prep_att prep_vars (binding, raw_bundle) fixes lthy =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    56
  let
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    57
    val (_, ctxt') = lthy |> prep_vars fixes |-> Proof_Context.add_fixes;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    58
    val bundle0 = raw_bundle
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    59
      |> map (fn (fact, atts) => (prep_fact lthy fact, map (prep_att lthy) atts));
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    60
    val bundle =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    61
      Attrib.partial_evaluation ctxt' [(Attrib.empty_binding, bundle0)] |> map snd |> flat
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    62
      |> transform_bundle (Proof_Context.export_morphism ctxt' lthy);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    63
  in
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    64
    lthy |> Local_Theory.declaration {syntax = false, pervasive = true}
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    65
      (fn phi => fn context =>
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    66
        context |> Data.map
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    67
          (#2 o Name_Space.define context true
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    68
            (Morphism.binding phi binding, transform_bundle phi bundle)))
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    69
  end;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    70
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    71
in
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    72
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    73
val bundle = gen_bundle (K I) (K I) Proof_Context.cert_vars;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    74
val bundle_cmd =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    75
  gen_bundle Proof_Context.get_fact (Attrib.intern_src o Proof_Context.theory_of)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    76
    Proof_Context.read_vars;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    77
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    78
end;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    79
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    80
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    81
(* include bundle *)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    82
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    83
fun gen_include prep raw_name =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    84
  Proof.map_context (fn ctxt =>
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    85
    let
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    86
      val bundle = the_bundle ctxt (prep ctxt raw_name);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    87
      val attrib = Attrib.attribute_i (Proof_Context.theory_of ctxt);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    88
      val note = ((Binding.empty, []), map (apsnd (map attrib)) bundle);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    89
    in #2 (Proof_Context.note_thmss "" [note] ctxt) end);
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    90
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    91
fun include_ name = Proof.assert_forward #> gen_include (K I) name #> Proof.put_facts NONE;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    92
fun include_cmd name = Proof.assert_forward #> gen_include check name #> Proof.put_facts NONE;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    93
fun including name = Proof.assert_backward #> gen_include (K I) name;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    94
fun including_cmd name = Proof.assert_backward #> gen_include check name;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    95
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    96
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    97
(* print_bundles *)
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    98
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
    99
fun print_bundles ctxt =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   100
  let
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   101
    val prt_thm = Pretty.backquote o Display.pretty_thm ctxt;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   102
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   103
    fun prt_fact (ths, []) = map prt_thm ths
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   104
      | prt_fact (ths, atts) = Pretty.enclose "(" ")"
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   105
          (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   106
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   107
    fun prt_bundle (name, bundle) =
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   108
      Pretty.block (Pretty.command "bundle" :: Pretty.str " " :: Pretty.str name ::
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   109
        Pretty.breaks (Pretty.str " =" :: maps prt_fact bundle));
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   110
  in
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   111
    map prt_bundle (Name_Space.extern_table ctxt (get_bundles ctxt))
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   112
  end |> Pretty.chunks |> Pretty.writeln;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   113
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   114
end;
12423b36fcc4 basic support for bundled declarations;
wenzelm
parents:
diff changeset
   115