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