src/Pure/Thy/export_theory.ML
author wenzelm
Fri, 11 May 2018 22:59:00 +0200
changeset 68154 42d63ea39161
child 68165 a7a2174ac014
permissions -rw-r--r--
some export of foundational theory content;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/export_theory.ML
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     3
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     4
Export foundational theory content.
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     5
*)
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     6
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     7
signature EXPORT_THEORY =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     8
sig
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
     9
  val export_table_diff: ('a -> XML.tree list option) ->
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    10
    'a Name_Space.table list -> 'a Name_Space.table -> XML.tree list
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    11
end;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    12
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    13
structure Export_Theory: EXPORT_THEORY =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    14
struct
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    15
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    16
(* export namespace table *)
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    17
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    18
fun export_table_diff export_decl prev_tables table =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    19
  (table, []) |-> Name_Space.fold_table (fn (name, decl) =>
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    20
    if exists (fn prev => Name_Space.defined prev name) prev_tables then I
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    21
    else
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    22
      (case export_decl decl of
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    23
        NONE => I
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    24
      | SOME body =>
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    25
          let val markup = Name_Space.markup_def (Name_Space.space_of_table table) name
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    26
          in cons (name, XML.Elem (markup, body)) end))
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    27
  |> sort_by #1 |> map #2;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    28
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    29
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    30
(* present *)
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    31
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    32
fun present get export name thy =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    33
  if Options.default_bool "export_theory" then
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    34
    (case export (map get (Theory.parents_of thy)) (get thy) of
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    35
      [] => ()
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    36
    | body => Export.export thy ("theory/" ^ name) [YXML.string_of_body body])
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    37
  else ();
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    38
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    39
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    40
(* types *)
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    41
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    42
val present_types = present (#types o Type.rep_tsig o Sign.tsig_of) o export_table_diff;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    43
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    44
fun export_logical_type (Type.LogicalType n) = SOME (XML.Encode.int n)
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    45
  | export_logical_type _ = NONE;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    46
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    47
fun export_abbreviation (Type.Abbreviation (vs, U, _)) =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    48
      let open XML.Encode Term_XML.Encode
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    49
      in SOME (pair (list string) typ (vs, U)) end
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    50
  | export_abbreviation _ = NONE;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    51
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    52
fun export_nonterminal Type.Nonterminal = SOME []
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    53
  | export_nonterminal _ = NONE;
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    54
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    55
val _ =
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    56
  Theory.setup
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    57
    (Thy_Info.add_presentation (present_types export_logical_type "types") #>
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    58
     Thy_Info.add_presentation (present_types export_abbreviation "type_synonyms") #>
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    59
     Thy_Info.add_presentation (present_types export_nonterminal "nonterminals"));
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    60
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    61
end;