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