src/Pure/Thy/export_theory.ML
author wenzelm
Sat, 04 Aug 2018 22:32:41 +0200
changeset 68724 7fafadbf16c7
parent 68539 6740e3611a86
child 68725 367e60d9aa1b
permissions -rw-r--r--
export in foundational order;
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
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
     9
  val setup_presentation: (Thy_Info.presentation_context -> theory -> unit) -> unit
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    10
  val export_body: theory -> string -> XML.body -> unit
68154
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
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    16
(* general setup *)
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    17
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    18
fun setup_presentation f =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    19
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    20
    if Options.bool (#options context) "export_theory" then f context thy else ()));
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    21
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    22
fun export_body thy name body =
68230
9bee37c2ac2b more scalable;
wenzelm
parents: 68208
diff changeset
    23
  Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    24
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    25
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    26
(* presentation *)
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    27
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    28
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    29
  let
68264
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
    30
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
    31
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
    32
68206
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    33
    (* parents *)
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    34
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    35
    val parents = Theory.parents_of thy;
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    36
    val _ =
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    37
      export_body thy "parents"
68231
0004e7a9fa10 clarified encoding;
wenzelm
parents: 68230
diff changeset
    38
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
68206
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    39
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    40
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    41
    (* entities *)
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    42
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    43
    fun entity_markup space name =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    44
      let
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    45
        val {serial, pos, ...} = Name_Space.the_entry space name;
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    46
        val props =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    47
          Markup.serial_properties serial @
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    48
          Position.offset_properties_of (adjust_pos pos);
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    49
      in (Markup.entityN, (Markup.nameN, name) :: props) end;
68154
42d63ea39161 some export of foundational theory content;
wenzelm
parents:
diff changeset
    50
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    51
    fun export_entities export_name export get_space decls =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    52
      let val elems =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    53
        let
68206
dedf1a70d1fa export more theory and session structure;
wenzelm
parents: 68201
diff changeset
    54
          val parent_spaces = map get_space parents;
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    55
          val space = get_space thy;
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    56
        in
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    57
          (decls, []) |-> fold (fn (name, decl) =>
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    58
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    59
            else
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    60
              (case export name decl of
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    61
                NONE => I
68724
7fafadbf16c7 export in foundational order;
wenzelm
parents: 68539
diff changeset
    62
              | SOME body =>
7fafadbf16c7 export in foundational order;
wenzelm
parents: 68539
diff changeset
    63
                  cons (#serial (Name_Space.the_entry space name),
7fafadbf16c7 export in foundational order;
wenzelm
parents: 68539
diff changeset
    64
                    XML.Elem (entity_markup space name, body))))
7fafadbf16c7 export in foundational order;
wenzelm
parents: 68539
diff changeset
    65
          |> sort (int_ord o apply2 #1) |> map #2
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    66
        end;
68724
7fafadbf16c7 export in foundational order;
wenzelm
parents: 68539
diff changeset
    67
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    68
      in if null elems then () else export_body thy export_name elems end;
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    69
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    70
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    71
    (* types *)
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    72
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    73
    val encode_type =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    74
      let open XML.Encode Term_XML.Encode
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    75
      in pair (list string) (option typ) end;
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    76
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    77
    fun export_type (Type.LogicalType n) =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    78
          SOME (encode_type (Name.invent Name.context Name.aT n, NONE))
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    79
      | export_type (Type.Abbreviation (args, U, false)) =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    80
          SOME (encode_type (args, SOME U))
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    81
      | export_type _ = NONE;
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    82
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    83
    val _ =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    84
      export_entities "types" (K export_type) Sign.type_space
68264
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
    85
        (Name_Space.dest_table (#types rep_tsig));
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    86
68173
7ed88a534bb6 more uniform types vs. consts;
wenzelm
parents: 68172
diff changeset
    87
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    88
    (* consts *)
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    89
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    90
    val encode_const =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    91
      let open XML.Encode Term_XML.Encode
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    92
      in triple (list string) typ (option term) end;
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
    93
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    94
    fun export_const c (T, abbrev) =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    95
      let
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    96
        val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    97
        val abbrev' = abbrev |> Option.map (Logic.unvarify_global #> map_types Type.strip_sorts);
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    98
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
    99
      in SOME (encode_const (args, T', abbrev')) end;
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
   100
68201
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
   101
    val _ =
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
   102
      export_entities "consts" export_const Sign.const_space
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
   103
        (#constants (Consts.dest (Sign.consts_of thy)));
dee993b88a7b misc tuning and clarification;
wenzelm
parents: 68199
diff changeset
   104
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   105
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   106
    (* axioms and facts *)
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   107
68539
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   108
    val standard_prop_of =
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   109
      Thm.transfer thy #>
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   110
      Thm.check_hyps (Context.Theory thy) #>
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   111
      Drule.sort_constraint_intr_shyps #>
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   112
      Thm.full_prop_of;
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   113
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   114
    val encode_props =
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   115
      let open XML.Encode Term_XML.Encode
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   116
      in triple (list (pair string sort)) (list (pair string typ)) (list term) end;
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   117
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   118
    fun export_props props =
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   119
      let
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   120
        val props' = map Logic.unvarify_global props;
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   121
        val typargs = rev (fold Term.add_tfrees props' []);
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   122
        val args = rev (fold Term.add_frees props' []);
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   123
      in encode_props (typargs, args, props') end;
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   124
68235
a3bd410db5b2 standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents: 68232
diff changeset
   125
    val export_axiom = export_props o single;
68539
6740e3611a86 disallow hyps in export;
wenzelm
parents: 68295
diff changeset
   126
    val export_fact = export_props o Term_Subst.zero_var_indexes_list o map standard_prop_of;
68208
d9f2cf4fc002 more exports;
wenzelm
parents: 68206
diff changeset
   127
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   128
    val _ =
68235
a3bd410db5b2 standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents: 68232
diff changeset
   129
      export_entities "axioms" (K (SOME o export_axiom)) Theory.axiom_space
a3bd410db5b2 standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents: 68232
diff changeset
   130
        (Theory.axioms_of thy);
a3bd410db5b2 standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents: 68232
diff changeset
   131
    val _ =
a3bd410db5b2 standardize implicit variables: non-zero indexes do occur occasionally, e.g. via RS;
wenzelm
parents: 68232
diff changeset
   132
      export_entities "facts" (K (SOME o export_fact)) (Facts.space_of o Global_Theory.facts_of)
68232
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   133
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
4b93573ac5b4 export facts;
wenzelm
parents: 68231
diff changeset
   134
68264
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   135
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   136
    (* type classes *)
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   137
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   138
    val encode_class =
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   139
      let open XML.Encode Term_XML.Encode
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   140
      in pair (list (pair string typ)) (list term) end;
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   141
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   142
    fun export_class name =
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   143
      (case try (Axclass.get_info thy) name of
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   144
        NONE => ([], [])
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   145
      | SOME {params, axioms, ...} =>
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   146
          (params, map (Logic.unvarify_global o Thm.full_prop_of) axioms))
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   147
      |> encode_class |> SOME;
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   148
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   149
    val _ =
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   150
      export_entities "classes" (fn name => fn () => export_class name)
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   151
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
bb9a3be6952a more exports;
wenzelm
parents: 68235
diff changeset
   152
68295
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   153
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   154
    (* sort algebra *)
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   155
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   156
    val {classrel, arities} =
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   157
      Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   158
        (#2 (#classes rep_tsig));
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   159
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   160
    val encode_classrel =
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   161
      let open XML.Encode
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   162
      in list (pair string (list string)) end;
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   163
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   164
    val encode_arities =
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   165
      let open XML.Encode Term_XML.Encode
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   166
      in list (triple string (list sort) string) end;
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   167
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   168
    val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   169
    val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   170
781a98696638 export sort algebra;
wenzelm
parents: 68264
diff changeset
   171
  in () end);
68165
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
   172
a7a2174ac014 more exports;
wenzelm
parents: 68154
diff changeset
   173
end;