src/Pure/Thy/export_theory.ML
author wenzelm
Sat Sep 22 15:22:29 2018 +0200 (12 months ago)
changeset 69034 855c3c501b09
parent 69029 aec64b88e708
child 69069 b9aca3b9619f
permissions -rw-r--r--
obsolete (see aec64b88e708);
     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 
    98 (* general setup *)
    99 
   100 fun setup_presentation f =
   101   Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
   102     if Options.bool (#options context) "export_theory" then f context thy else ()));
   103 
   104 fun export_body thy name body =
   105   Export.export thy ("theory/" ^ name) (Buffer.chunks (YXML.buffer_body body Buffer.empty));
   106 
   107 
   108 (* presentation *)
   109 
   110 val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
   111   let
   112     val parents = Theory.parents_of thy;
   113     val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
   114 
   115     val thy_ctxt = Proof_Context.init_global thy;
   116 
   117 
   118     (* entities *)
   119 
   120     fun entity_markup space name =
   121       let
   122         val xname = Name_Space.extern_shortest thy_ctxt space name;
   123         val {serial, pos, ...} = Name_Space.the_entry space name;
   124         val props =
   125           Position.offset_properties_of (adjust_pos pos) @
   126           Position.id_properties_of pos @
   127           Markup.serial_properties serial;
   128       in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
   129 
   130     fun export_entities export_name export get_space decls =
   131       let val elems =
   132         let
   133           val parent_spaces = map get_space parents;
   134           val space = get_space thy;
   135         in
   136           (decls, []) |-> fold (fn (name, decl) =>
   137             if exists (fn space => Name_Space.declared space name) parent_spaces then I
   138             else
   139               (case export name decl of
   140                 NONE => I
   141               | SOME body =>
   142                   cons (#serial (Name_Space.the_entry space name),
   143                     XML.Elem (entity_markup space name, body))))
   144           |> sort (int_ord o apply2 #1) |> map #2
   145         end;
   146       in if null elems then () else export_body thy export_name elems end;
   147 
   148 
   149     (* infix syntax *)
   150 
   151     fun get_infix_const ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_const;
   152     fun get_infix_type ctxt = Syntax.get_infix (Proof_Context.syn_of ctxt) o Lexicon.mark_type;
   153 
   154     fun encode_infix {assoc, delim, pri} =
   155       let
   156         val ass =
   157           (case assoc of
   158             Syntax_Ext.No_Assoc => 0
   159           | Syntax_Ext.Left_Assoc => 1
   160           | Syntax_Ext.Right_Assoc => 2);
   161         open XML.Encode Term_XML.Encode;
   162       in triple int string int (ass, delim, pri) end;
   163 
   164 
   165     (* types *)
   166 
   167     val encode_type =
   168       let open XML.Encode Term_XML.Encode
   169       in triple (option encode_infix) (list string) (option typ) end;
   170 
   171     fun export_type c (Type.LogicalType n) =
   172           SOME (encode_type (get_infix_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
   173       | export_type c (Type.Abbreviation (args, U, false)) =
   174           SOME (encode_type (get_infix_type thy_ctxt c, args, SOME U))
   175       | export_type _ _ = NONE;
   176 
   177     val _ =
   178       export_entities "types" export_type Sign.type_space
   179         (Name_Space.dest_table (#types rep_tsig));
   180 
   181 
   182     (* consts *)
   183 
   184     val encode_const =
   185       let open XML.Encode Term_XML.Encode
   186       in pair (option encode_infix) (pair (list string) (pair typ (option term))) end;
   187 
   188     fun export_const c (T, abbrev) =
   189       let
   190         val syntax = get_infix_const thy_ctxt c;
   191         val T' = T |> Logic.unvarifyT_global |> Type.strip_sorts;
   192         val abbrev' = abbrev |> Option.map (standard_vars_global #> map_types Type.strip_sorts);
   193         val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, T'));
   194       in encode_const (syntax, (args, (T', abbrev'))) end;
   195 
   196     val _ =
   197       export_entities "consts" (SOME oo export_const) Sign.const_space
   198         (#constants (Consts.dest (Sign.consts_of thy)));
   199 
   200 
   201     (* axioms and facts *)
   202 
   203     fun prop_of raw_thm =
   204       let
   205         val thm = raw_thm
   206           |> Thm.transfer thy
   207           |> Thm.check_hyps (Context.Theory thy)
   208           |> Thm.strip_shyps;
   209         val prop = thm
   210           |> Thm.full_prop_of;
   211       in (Thm.extra_shyps thm, prop) end;
   212 
   213     fun encode_prop used (Ss, raw_prop) =
   214       let
   215         val prop = standard_vars used raw_prop;
   216         val args = rev (add_frees used prop []);
   217         val typargs = rev (add_tfrees used prop []);
   218         val used' = fold (Name.declare o #1) typargs used;
   219         val sorts = Name.invent used' Name.aT (length Ss) ~~ Ss;
   220       in
   221         (sorts @ typargs, args, prop) |>
   222           let open XML.Encode Term_XML.Encode
   223           in triple (list (pair string sort)) (list (pair string typ)) term end
   224       end;
   225 
   226     fun encode_axiom used t = encode_prop used ([], t);
   227 
   228     val encode_fact_single = encode_prop Name.context o prop_of;
   229     val encode_fact_multi = XML.Encode.list (encode_prop Name.context) o map prop_of;
   230 
   231     val _ =
   232       export_entities "axioms" (fn _ => fn t => SOME (encode_axiom Name.context t))
   233         Theory.axiom_space (Theory.axioms_of thy);
   234     val _ =
   235       export_entities "facts" (K (SOME o encode_fact_multi))
   236         (Facts.space_of o Global_Theory.facts_of)
   237         (Facts.dest_static true [] (Global_Theory.facts_of thy));
   238 
   239 
   240     (* type classes *)
   241 
   242     val encode_class =
   243       let open XML.Encode Term_XML.Encode
   244       in pair (list (pair string typ)) (list encode_fact_single) end;
   245 
   246     fun export_class name =
   247       (case try (Axclass.get_info thy) name of
   248         NONE => ([], [])
   249       | SOME {params, axioms, ...} => (params, axioms))
   250       |> encode_class |> SOME;
   251 
   252     val _ =
   253       export_entities "classes" (fn name => fn () => export_class name)
   254         Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
   255 
   256 
   257     (* sort algebra *)
   258 
   259     val {classrel, arities} =
   260       Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
   261         (#2 (#classes rep_tsig));
   262 
   263     val encode_classrel =
   264       let open XML.Encode
   265       in list (pair string (list string)) end;
   266 
   267     val encode_arities =
   268       let open XML.Encode Term_XML.Encode
   269       in list (triple string (list sort) string) end;
   270 
   271     val _ = if null classrel then () else export_body thy "classrel" (encode_classrel classrel);
   272     val _ = if null arities then () else export_body thy "arities" (encode_arities arities);
   273 
   274 
   275     (* locales *)
   276 
   277     fun encode_locale used =
   278       let open XML.Encode Term_XML.Encode
   279       in triple (list (pair string sort)) (list (pair string typ)) (list (encode_axiom used)) end;
   280 
   281     fun export_locale loc =
   282       let
   283         val {typargs, args, axioms} = locale_content thy loc;
   284         val used = fold Name.declare (map #1 typargs @ map #1 args) Name.context;
   285       in encode_locale used (typargs, args, axioms) end
   286       handle ERROR msg =>
   287         cat_error msg ("The error(s) above occurred in locale " ^
   288           quote (Locale.markup_name thy_ctxt loc));
   289 
   290     val _ =
   291       export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
   292         Locale.locale_space
   293         (map (rpair ()) (Locale.get_locales thy));
   294 
   295 
   296     (* parents *)
   297 
   298     val _ =
   299       export_body thy "parents"
   300         (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
   301 
   302   in () end);
   303 
   304 end;