src/Pure/Thy/export_theory.ML
author wenzelm
Wed Aug 14 19:21:34 2019 +0200 (2 months ago)
changeset 70529 2ecbbe6b35db
parent 70386 6af87375b95f
child 70534 fb876ebbf5a7
permissions -rw-r--r--
uniform standard_vars for terms and proof terms;
     1 (*  Title:      Pure/Thy/export_theory.ML
     2     Author:     Makarius
     3 
     4 Export foundational theory content and locale/class structure.
     5 *)
     6 
     7 signature EXPORT_THEORY =
     8 sig
     9   val setup_presentation: (Thy_Info.presentation_context -> theory -> unit) -> unit
    10   val export_body: theory -> string -> XML.body -> unit
    11 end;
    12 
    13 structure Export_Theory: EXPORT_THEORY =
    14 struct
    15 
    16 (* approximative syntax *)
    17 
    18 val get_syntax = Syntax.get_approx o Proof_Context.syn_of;
    19 fun get_syntax_type ctxt = get_syntax ctxt o Lexicon.mark_type;
    20 fun get_syntax_const ctxt = get_syntax ctxt o Lexicon.mark_const;
    21 fun get_syntax_fixed ctxt = get_syntax ctxt o Lexicon.mark_fixed;
    22 
    23 fun get_syntax_param ctxt loc x =
    24   let val thy = Proof_Context.theory_of ctxt in
    25     if Class.is_class thy loc then
    26       (case AList.lookup (op =) (Class.these_params thy [loc]) x of
    27         NONE => NONE
    28       | SOME (_, (c, _)) => get_syntax_const ctxt c)
    29     else get_syntax_fixed ctxt x
    30   end;
    31 
    32 val encode_syntax =
    33   XML.Encode.variant
    34    [fn NONE => ([], []),
    35     fn SOME (Syntax.Prefix delim) => ([delim], []),
    36     fn SOME (Syntax.Infix {assoc, delim, pri}) =>
    37       let
    38         val ass =
    39           (case assoc of
    40             Printer.No_Assoc => 0
    41           | Printer.Left_Assoc => 1
    42           | Printer.Right_Assoc => 2);
    43         open XML.Encode Term_XML.Encode;
    44       in ([], triple int string int (ass, delim, pri)) end];
    45 
    46 
    47 (* free variables: not declared in the context *)
    48 
    49 val is_free = not oo Name.is_declared;
    50 
    51 fun add_frees used =
    52   fold_aterms (fn Free (x, T) => is_free used x ? insert (op =) (x, T) | _ => I);
    53 
    54 fun add_tfrees used =
    55   (fold_types o fold_atyps) (fn TFree (a, S) => is_free used a ? insert (op =) (a, S) | _ => I);
    56 
    57 
    58 (* spec rules *)
    59 
    60 fun primrec_types ctxt const =
    61   Spec_Rules.retrieve ctxt (Const const)
    62   |> get_first
    63     (fn (Spec_Rules.Equational (Spec_Rules.Primrec types), _) => SOME (types, false)
    64       | (Spec_Rules.Equational (Spec_Rules.Primcorec types), _) => SOME (types, true)
    65       | _ => NONE)
    66   |> the_default ([], false);
    67 
    68 
    69 (* locales content *)
    70 
    71 fun locale_content thy loc =
    72   let
    73     val ctxt = Locale.init loc thy;
    74     val args =
    75       Locale.params_of thy loc
    76       |> map (fn ((x, T), _) => ((x, T), get_syntax_param ctxt loc x));
    77     val axioms =
    78       let
    79         val (asm, defs) = Locale.specification_of thy loc;
    80         val cprops = map (Thm.cterm_of ctxt) (the_list asm @ defs);
    81         val (intro1, intro2) = Locale.intros_of thy loc;
    82         val intros_tac = Method.try_intros_tac ctxt (the_list intro1 @ the_list intro2) [];
    83         val res =
    84           Goal.init (Conjunction.mk_conjunction_balanced cprops)
    85           |> (ALLGOALS Goal.conjunction_tac THEN intros_tac)
    86           |> try Seq.hd;
    87       in
    88         (case res of
    89           SOME goal => Thm.prems_of goal
    90         | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
    91       end;
    92     val typargs = rev (fold Term.add_tfrees (map (Free o #1) args @ axioms) []);
    93   in {typargs = typargs, args = args, axioms = axioms} end;
    94 
    95 fun get_locales thy =
    96   Locale.get_locales thy |> map_filter (fn loc =>
    97     if Experiment.is_experiment thy loc then NONE else SOME (loc, ()));
    98 
    99 fun get_dependencies prev_thys thy =
   100   Locale.dest_dependencies prev_thys thy |> map_filter (fn dep =>
   101     if Experiment.is_experiment thy (#source dep) orelse
   102       Experiment.is_experiment thy (#target dep) then NONE
   103     else
   104       let
   105         val (type_params, params) = Locale.parameters_of thy (#source dep);
   106         val typargs = fold (Term.add_tfreesT o #2 o #1) params type_params;
   107         val substT =
   108           typargs |> map_filter (fn v =>
   109             let
   110               val T = TFree v;
   111               val T' = Morphism.typ (#morphism dep) T;
   112             in if T = T' then NONE else SOME (v, T') end);
   113         val subst =
   114           params |> map_filter (fn (v, _) =>
   115             let
   116               val t = Free v;
   117               val t' = Morphism.term (#morphism dep) t;
   118             in if t aconv t' then NONE else SOME (v, t') end);
   119       in SOME (dep, (substT, subst)) end);
   120 
   121 
   122 (* general setup *)
   123 
   124 fun setup_presentation f =
   125   Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
   126     if Options.bool (#options context) "export_theory" then f context thy else ()));
   127 
   128 fun export_body thy name body =
   129   Export.export thy (Path.binding0 (Path.make ["theory", name]))
   130     (Buffer.chunks (YXML.buffer_body body Buffer.empty));
   131 
   132 
   133 (* presentation *)
   134 
   135 val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
   136   let
   137     val parents = Theory.parents_of thy;
   138     val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
   139 
   140     val thy_ctxt = Proof_Context.init_global thy;
   141 
   142 
   143     (* entities *)
   144 
   145     fun make_entity_markup name xname pos serial =
   146       let
   147         val props =
   148           Position.offset_properties_of (adjust_pos pos) @
   149           Position.id_properties_of pos @
   150           Markup.serial_properties serial;
   151       in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
   152 
   153     fun entity_markup space name =
   154       let
   155         val xname = Name_Space.extern_shortest thy_ctxt space name;
   156         val {serial, pos, ...} = Name_Space.the_entry space name;
   157       in make_entity_markup name xname pos serial end;
   158 
   159     fun export_entities export_name export get_space decls =
   160       let val elems =
   161         let
   162           val parent_spaces = map get_space parents;
   163           val space = get_space thy;
   164         in
   165           (decls, []) |-> fold (fn (name, decl) =>
   166             if exists (fn space => Name_Space.declared space name) parent_spaces then I
   167             else
   168               (case export name decl of
   169                 NONE => I
   170               | SOME body =>
   171                   cons (#serial (Name_Space.the_entry space name),
   172                     XML.Elem (entity_markup space name, body))))
   173           |> sort (int_ord o apply2 #1) |> map #2
   174         end;
   175       in if null elems then () else export_body thy export_name elems end;
   176 
   177 
   178     (* types *)
   179 
   180     val encode_type =
   181       let open XML.Encode Term_XML.Encode
   182       in triple encode_syntax (list string) (option typ) end;
   183 
   184     fun export_type c (Type.LogicalType n) =
   185           SOME (encode_type (get_syntax_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
   186       | export_type c (Type.Abbreviation (args, U, false)) =
   187           SOME (encode_type (get_syntax_type thy_ctxt c, args, SOME U))
   188       | export_type _ _ = NONE;
   189 
   190     val _ =
   191       export_entities "types" export_type Sign.type_space
   192         (Name_Space.dest_table (#types rep_tsig));
   193 
   194 
   195     (* consts *)
   196 
   197     val encode_const =
   198       let open XML.Encode Term_XML.Encode in
   199         pair encode_syntax
   200           (pair (list string)
   201             (pair typ (pair (option term) (pair bool (pair (list string) bool)))))
   202       end;
   203 
   204     fun export_const c (T, abbrev) =
   205       let
   206         val syntax = get_syntax_const thy_ctxt c;
   207         val U = Logic.unvarifyT_global T;
   208         val U0 = Type.strip_sorts U;
   209         val recursion = primrec_types thy_ctxt (c, U);
   210         val abbrev' = abbrev
   211           |> Option.map (Proofterm.standard_vars_term Name.context #> map_types Type.strip_sorts);
   212         val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, U0));
   213         val propositional = Object_Logic.is_propositional thy_ctxt (Term.body_type U0);
   214       in encode_const (syntax, (args, (U0, (abbrev', (propositional, recursion))))) end;
   215 
   216     val _ =
   217       export_entities "consts" (SOME oo export_const) Sign.const_space
   218         (#constants (Consts.dest (Sign.consts_of thy)));
   219 
   220 
   221     (* axioms and facts *)
   222 
   223     fun prop_of raw_thm =
   224       let
   225         val thm = raw_thm
   226           |> Thm.transfer thy
   227           |> Thm.check_hyps (Context.Theory thy)
   228           |> Thm.strip_shyps;
   229         val prop = thm
   230           |> Thm.full_prop_of;
   231       in (Thm.extra_shyps thm, prop) end;
   232 
   233     fun encode_prop used (Ss, raw_prop) =
   234       let
   235         val prop = Proofterm.standard_vars_term used raw_prop;
   236         val args = rev (add_frees used prop []);
   237         val typargs = rev (add_tfrees used prop []);
   238         val used' = fold (Name.declare o #1) typargs used;
   239         val sorts = Name.invent used' Name.aT (length Ss) ~~ Ss;
   240       in
   241         (sorts @ typargs, args, prop) |>
   242           let open XML.Encode Term_XML.Encode
   243           in triple (list (pair string sort)) (list (pair string typ)) term end
   244       end;
   245 
   246     fun encode_axiom used t = encode_prop used ([], t);
   247 
   248     val encode_fact = encode_prop Name.context;
   249     val encode_fact_single = encode_fact o prop_of;
   250     val encode_fact_multi = XML.Encode.list encode_fact o map prop_of;
   251 
   252     val _ =
   253       export_entities "axioms" (fn _ => fn t => SOME (encode_axiom Name.context t))
   254         Theory.axiom_space (Theory.axioms_of thy);
   255     val _ =
   256       export_entities "facts" (K (SOME o encode_fact_multi))
   257         (Facts.space_of o Global_Theory.facts_of)
   258         (Facts.dest_static true [] (Global_Theory.facts_of thy));
   259 
   260 
   261     (* type classes *)
   262 
   263     val encode_class =
   264       let open XML.Encode Term_XML.Encode
   265       in pair (list (pair string typ)) (list encode_fact_single) end;
   266 
   267     fun export_class name =
   268       (case try (Axclass.get_info thy) name of
   269         NONE => ([], [])
   270       | SOME {params, axioms, ...} => (params, axioms))
   271       |> encode_class |> SOME;
   272 
   273     val _ =
   274       export_entities "classes" (fn name => fn () => export_class name)
   275         Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
   276 
   277 
   278     (* sort algebra *)
   279 
   280     local
   281       val prop = encode_axiom Name.context o Logic.varify_global;
   282 
   283       val encode_classrel =
   284         let open XML.Encode
   285         in list (pair prop (pair string string)) end;
   286 
   287       val encode_arities =
   288         let open XML.Encode Term_XML.Encode
   289         in list (pair prop (triple string (list sort) string)) end;
   290     in
   291       val export_classrel =
   292         maps (fn (c, cs) => map (pair c) cs) #> map (`Logic.mk_classrel) #> encode_classrel;
   293 
   294       val export_arities = map (`Logic.mk_arity) #> encode_arities;
   295 
   296       val {classrel, arities} =
   297         Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
   298           (#2 (#classes rep_tsig));
   299 
   300     end;
   301 
   302     val _ = if null classrel then () else export_body thy "classrel" (export_classrel classrel);
   303     val _ = if null arities then () else export_body thy "arities" (export_arities arities);
   304 
   305 
   306     (* locales *)
   307 
   308     fun encode_locale used =
   309       let open XML.Encode Term_XML.Encode in
   310         triple (list (pair string sort)) (list (pair (pair string typ) encode_syntax))
   311           (list (encode_axiom used))
   312       end;
   313 
   314     fun export_locale loc =
   315       let
   316         val {typargs, args, axioms} = locale_content thy loc;
   317         val used = fold Name.declare (map #1 typargs @ map (#1 o #1) args) Name.context;
   318       in encode_locale used (typargs, args, axioms) end
   319       handle ERROR msg =>
   320         cat_error msg ("The error(s) above occurred in locale " ^
   321           quote (Locale.markup_name thy_ctxt loc));
   322 
   323     val _ =
   324       export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
   325         Locale.locale_space (get_locales thy);
   326 
   327 
   328     (* locale dependencies *)
   329 
   330     fun encode_locale_dependency (dep: Locale.locale_dependency, subst) =
   331       (#source dep, (#target dep, (#prefix dep, subst))) |>
   332         let
   333           open XML.Encode Term_XML.Encode;
   334           val encode_subst =
   335             pair (list (pair (pair string sort) typ)) (list (pair (pair string typ) term));
   336         in pair string (pair string (pair (list (pair string bool)) encode_subst)) end;
   337 
   338     val _ =
   339       (case get_dependencies parents thy of
   340         [] => ()
   341       | deps =>
   342           deps |> map_index (fn (i, dep) =>
   343             let
   344               val xname = string_of_int (i + 1);
   345               val name = Long_Name.implode [Context.theory_name thy, xname];
   346               val markup = make_entity_markup name xname (#pos (#1 dep)) (#serial (#1 dep));
   347               val body = encode_locale_dependency dep;
   348             in XML.Elem (markup, body) end)
   349           |> export_body thy "locale_dependencies");
   350 
   351 
   352     (* parents *)
   353 
   354     val _ =
   355       export_body thy "parents"
   356         (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
   357 
   358   in () end);
   359 
   360 end;