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