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