src/Pure/Thy/export_theory.ML
changeset 74261 d28a51dd9da6
parent 74235 dbaed92fd8af
child 74264 04214caeb9ac
equal deleted inserted replaced
74260:bb37fb85d82c 74261:d28a51dd9da6
     4 Export foundational theory content and locale/class structure.
     4 Export foundational theory content and locale/class structure.
     5 *)
     5 *)
     6 
     6 
     7 signature EXPORT_THEORY =
     7 signature EXPORT_THEORY =
     8 sig
     8 sig
       
     9   val other_name_space: (theory -> Name_Space.T) -> theory -> theory
     9   val export_enabled: Thy_Info.presentation_context -> bool
    10   val export_enabled: Thy_Info.presentation_context -> bool
    10   val export_body: theory -> string -> XML.body -> unit
    11   val export_body: theory -> string -> XML.body -> unit
    11 end;
    12 end;
    12 
    13 
    13 structure Export_Theory: EXPORT_THEORY =
    14 structure Export_Theory: EXPORT_THEORY =
    14 struct
    15 struct
       
    16 
       
    17 (* other name spaces *)
       
    18 
       
    19 fun err_dup_kind kind = error ("Duplicate name space kind " ^ quote kind);
       
    20 
       
    21 structure Data = Theory_Data
       
    22 (
       
    23   type T = ((theory -> Name_Space.T) * serial) Symtab.table;
       
    24   val empty = Symtab.empty;
       
    25   val extend = I;
       
    26   fun merge data =
       
    27     Symtab.merge (eq_snd op =) data
       
    28       handle Symtab.DUP dup => err_dup_kind dup;
       
    29 );
       
    30 
       
    31 val other_name_spaces = map (#1 o #2) o Symtab.dest o Data.get;
       
    32 
       
    33 fun other_name_space get_space thy =
       
    34   let
       
    35     val kind = Name_Space.kind_of (get_space thy);
       
    36     val entry = (get_space, serial ());
       
    37   in
       
    38     Data.map (Symtab.update_new (kind, entry)) thy
       
    39       handle Symtab.DUP dup => err_dup_kind dup
       
    40   end;
       
    41 
       
    42 val _ = Theory.setup
       
    43  (other_name_space Thm.oracle_space #>
       
    44   other_name_space Global_Theory.fact_space #>
       
    45   other_name_space (Bundle.bundle_space o Context.Theory) #>
       
    46   other_name_space (Attrib.attribute_space o Context.Theory) #>
       
    47   other_name_space (Method.method_space o Context.Theory));
       
    48 
    15 
    49 
    16 (* approximative syntax *)
    50 (* approximative syntax *)
    17 
    51 
    18 val get_syntax = Syntax.get_approx o Proof_Context.syn_of;
    52 val get_syntax = Syntax.get_approx o Proof_Context.syn_of;
    19 fun get_syntax_type ctxt = get_syntax ctxt o Lexicon.mark_type;
    53 fun get_syntax_type ctxt = get_syntax ctxt o Lexicon.mark_type;
   113 fun export_enabled (context: Thy_Info.presentation_context) =
   147 fun export_enabled (context: Thy_Info.presentation_context) =
   114   Options.bool (#options context) "export_theory";
   148   Options.bool (#options context) "export_theory";
   115 
   149 
   116 fun export_body thy name body =
   150 fun export_body thy name body =
   117   if XML.is_empty_body body then ()
   151   if XML.is_empty_body body then ()
   118   else Export.export thy (Path.binding0 (Path.make ["theory", name])) body;
   152   else Export.export thy (Path.binding0 (Path.make ("theory" :: space_explode "/" name))) body;
   119 
   153 
   120 val _ = (Theory.setup o Thy_Info.add_presentation) (fn context => fn thy =>
   154 val _ = (Theory.setup o Thy_Info.add_presentation) (fn context => fn thy =>
   121   let
   155   let
   122     val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
   156     val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
   123     val consts = Sign.consts_of thy;
   157     val consts = Sign.consts_of thy;
   269           NONE => ""
   303           NONE => ""
   270         | SOME thm_name => Thm_Name.print thm_name);
   304         | SOME thm_name => Thm_Name.print thm_name);
   271 
   305 
   272     fun entity_markup_thm (serial, (name, i)) =
   306     fun entity_markup_thm (serial, (name, i)) =
   273       let
   307       let
   274         val space = Facts.space_of (Global_Theory.facts_of thy);
   308         val space = Global_Theory.fact_space thy;
   275         val xname = Name_Space.extern_shortest thy_ctxt space name;
   309         val xname = Name_Space.extern_shortest thy_ctxt space name;
   276         val {pos, ...} = Name_Space.the_entry space name;
   310         val {pos, ...} = Name_Space.the_entry space name;
   277       in make_entity_markup (Thm_Name.print (name, i)) (Thm_Name.print (xname, i)) pos serial end;
   311       in make_entity_markup (Thm_Name.print (name, i)) (Thm_Name.print (xname, i)) pos serial end;
   278 
   312 
   279     fun encode_thm thm_id raw_thm =
   313     fun encode_thm thm_id raw_thm =
   431           [] => ()
   465           [] => ()
   432         | spec_rules =>
   466         | spec_rules =>
   433             export_body thy "spec_rules" (encode_specs (map spec_rule_content spec_rules)))
   467             export_body thy "spec_rules" (encode_specs (map spec_rule_content spec_rules)))
   434       else ();
   468       else ();
   435 
   469 
       
   470 
       
   471     (* other entities *)
       
   472 
       
   473     fun export_other get_space =
       
   474       let
       
   475         val space = get_space thy;
       
   476         val export_name = "other/" ^ Name_Space.kind_of space;
       
   477         val decls = Name_Space.get_names space |> map (rpair ());
       
   478       in export_entities export_name get_space decls (fn _ => fn () => SOME (K [])) end;
       
   479 
       
   480     val other_spaces = other_name_spaces thy;
       
   481     val other_kinds = map (fn get_space => Name_Space.kind_of (get_space thy)) other_spaces;
       
   482     val _ =
       
   483       if null other_kinds then ()
       
   484       else
       
   485         Export.export thy \<^path_binding>\<open>theory/other_kinds\<close>
       
   486           (XML.Encode.string (cat_lines other_kinds));
       
   487     val _ = List.app export_other other_spaces;
       
   488 
   436   in () end);
   489   in () end);
   437 
   490 
   438 end;
   491 end;