src/Pure/Thy/export_theory.ML
changeset 69087 06017b2c4552
parent 69086 2859dcdbc849
child 69784 24bbc4e30e5b
equal deleted inserted replaced
69086:2859dcdbc849 69087:06017b2c4552
    99 
    99 
   100 fun add_tfrees used =
   100 fun add_tfrees used =
   101   (fold_types o fold_atyps) (fn TFree (a, S) => is_free used a ? insert (op =) (a, S) | _ => I);
   101   (fold_types o fold_atyps) (fn TFree (a, S) => is_free used a ? insert (op =) (a, S) | _ => I);
   102 
   102 
   103 
   103 
   104 (* locale content *)
   104 (* locales content *)
   105 
   105 
   106 fun locale_content thy loc =
   106 fun locale_content thy loc =
   107   let
   107   let
   108     val ctxt = Locale.init loc thy;
   108     val ctxt = Locale.init loc thy;
   109     val args =
   109     val args =
   125         | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
   125         | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
   126       end;
   126       end;
   127     val typargs = rev (fold Term.add_tfrees (map (Free o #1) args @ axioms) []);
   127     val typargs = rev (fold Term.add_tfrees (map (Free o #1) args @ axioms) []);
   128   in {typargs = typargs, args = args, axioms = axioms} end;
   128   in {typargs = typargs, args = args, axioms = axioms} end;
   129 
   129 
   130 fun locale_dependency_subst thy (dep: Locale.locale_dependency) =
   130 fun get_locales thy =
   131   let
   131   Locale.get_locales thy |> map_filter (fn loc =>
   132     val (type_params, params) = Locale.parameters_of thy (#source dep);
   132     if Experiment.is_experiment thy loc then NONE else SOME (loc, ()));
   133     val typargs = fold (Term.add_tfreesT o #2 o #1) params type_params;
   133 
   134     val substT =
   134 fun get_dependencies prev_thys thy =
   135       typargs |> map_filter (fn v =>
   135   Locale.dest_dependencies prev_thys thy |> map_filter (fn dep =>
   136         let
   136     if Experiment.is_experiment thy (#source dep) orelse
   137           val T = TFree v;
   137       Experiment.is_experiment thy (#target dep) then NONE
   138           val T' = Morphism.typ (#morphism dep) T;
   138     else
   139         in if T = T' then NONE else SOME (v, T') end);
   139       let
   140     val subst =
   140         val (type_params, params) = Locale.parameters_of thy (#source dep);
   141       params |> map_filter (fn (v, _) =>
   141         val typargs = fold (Term.add_tfreesT o #2 o #1) params type_params;
   142         let
   142         val substT =
   143           val t = Free v;
   143           typargs |> map_filter (fn v =>
   144           val t' = Morphism.term (#morphism dep) t;
   144             let
   145         in if t aconv t' then NONE else SOME (v, t') end);
   145               val T = TFree v;
   146   in (substT, subst) end;
   146               val T' = Morphism.typ (#morphism dep) T;
       
   147             in if T = T' then NONE else SOME (v, T') end);
       
   148         val subst =
       
   149           params |> map_filter (fn (v, _) =>
       
   150             let
       
   151               val t = Free v;
       
   152               val t' = Morphism.term (#morphism dep) t;
       
   153             in if t aconv t' then NONE else SOME (v, t') end);
       
   154       in SOME (dep, (substT, subst)) end);
   147 
   155 
   148 
   156 
   149 (* general setup *)
   157 (* general setup *)
   150 
   158 
   151 fun setup_presentation f =
   159 fun setup_presentation f =
   328         cat_error msg ("The error(s) above occurred in locale " ^
   336         cat_error msg ("The error(s) above occurred in locale " ^
   329           quote (Locale.markup_name thy_ctxt loc));
   337           quote (Locale.markup_name thy_ctxt loc));
   330 
   338 
   331     val _ =
   339     val _ =
   332       export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
   340       export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
   333         Locale.locale_space
   341         Locale.locale_space (get_locales thy);
   334         (map (rpair ()) (Locale.get_locales thy));
       
   335 
   342 
   336 
   343 
   337     (* locale dependencies *)
   344     (* locale dependencies *)
   338 
   345 
   339     fun encode_locale_dependency (dep: Locale.locale_dependency) =
   346     fun encode_locale_dependency (dep: Locale.locale_dependency, subst) =
   340       (#source dep, (#target dep, (#prefix dep, locale_dependency_subst thy dep))) |>
   347       (#source dep, (#target dep, (#prefix dep, subst))) |>
   341         let
   348         let
   342           open XML.Encode Term_XML.Encode;
   349           open XML.Encode Term_XML.Encode;
   343           val encode_subst =
   350           val encode_subst =
   344             pair (list (pair (pair string sort) typ)) (list (pair (pair string typ) term));
   351             pair (list (pair (pair string sort) typ)) (list (pair (pair string typ) term));
   345         in pair string (pair string (pair (list (pair string bool)) encode_subst)) end;
   352         in pair string (pair string (pair (list (pair string bool)) encode_subst)) end;
   346 
   353 
   347     val _ =
   354     val _ =
   348       (case Locale.dest_dependencies parents thy of
   355       (case get_dependencies parents thy of
   349         [] => ()
   356         [] => ()
   350       | deps =>
   357       | deps =>
   351           deps |> map_index (fn (i, dep) =>
   358           deps |> map_index (fn (i, dep) =>
   352             let
   359             let
   353               val xname = string_of_int (i + 1);
   360               val xname = string_of_int (i + 1);
   354               val name = Long_Name.implode [Context.theory_name thy, xname];
   361               val name = Long_Name.implode [Context.theory_name thy, xname];
       
   362               val markup = make_entity_markup name xname (#pos (#1 dep)) (#serial (#1 dep));
   355               val body = encode_locale_dependency dep;
   363               val body = encode_locale_dependency dep;
   356             in XML.Elem (make_entity_markup name xname (#pos dep) (#serial dep), body) end)
   364             in XML.Elem (markup, body) end)
   357           |> export_body thy "locale_dependencies");
   365           |> export_body thy "locale_dependencies");
   358 
   366 
   359 
   367 
   360     (* parents *)
   368     (* parents *)
   361 
   369