src/Pure/Thy/export_theory.ML
author wenzelm
Thu Aug 15 19:35:17 2019 +0200 (5 weeks ago)
changeset 70540 04ef5ee3dd4d
parent 70534 fb876ebbf5a7
child 70541 f3fbc7f3559d
permissions -rw-r--r--
more careful treatment of standard_vars: rename apart from existing frees and avoid approximative Name.declared, proper application of unvarifyT within terms of proof;
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
(* free variables: not declared in the context *)
wenzelm@69023
    48
wenzelm@69023
    49
val is_free = not oo Name.is_declared;
wenzelm@69023
    50
wenzelm@69023
    51
fun add_frees used =
wenzelm@69023
    52
  fold_aterms (fn Free (x, T) => is_free used x ? insert (op =) (x, T) | _ => I);
wenzelm@69023
    53
wenzelm@69023
    54
fun add_tfrees used =
wenzelm@69023
    55
  (fold_types o fold_atyps) (fn TFree (a, S) => is_free used a ? insert (op =) (a, S) | _ => I);
wenzelm@69023
    56
wenzelm@69023
    57
wenzelm@69992
    58
(* spec rules *)
wenzelm@69992
    59
wenzelm@69992
    60
fun primrec_types ctxt const =
wenzelm@69992
    61
  Spec_Rules.retrieve ctxt (Const const)
wenzelm@69996
    62
  |> get_first
wenzelm@69996
    63
    (fn (Spec_Rules.Equational (Spec_Rules.Primrec types), _) => SOME (types, false)
wenzelm@69996
    64
      | (Spec_Rules.Equational (Spec_Rules.Primcorec types), _) => SOME (types, true)
wenzelm@69996
    65
      | _ => NONE)
wenzelm@69996
    66
  |> the_default ([], false);
wenzelm@69992
    67
wenzelm@69992
    68
wenzelm@69087
    69
(* locales content *)
wenzelm@69019
    70
wenzelm@69034
    71
fun locale_content thy loc =
wenzelm@69019
    72
  let
wenzelm@69083
    73
    val ctxt = Locale.init loc thy;
wenzelm@69083
    74
    val args =
wenzelm@69083
    75
      Locale.params_of thy loc
wenzelm@69083
    76
      |> map (fn ((x, T), _) => ((x, T), get_syntax_param ctxt loc x));
wenzelm@69029
    77
    val axioms =
wenzelm@69029
    78
      let
wenzelm@69083
    79
        val (asm, defs) = Locale.specification_of thy loc;
wenzelm@69083
    80
        val cprops = map (Thm.cterm_of ctxt) (the_list asm @ defs);
wenzelm@69029
    81
        val (intro1, intro2) = Locale.intros_of thy loc;
wenzelm@69083
    82
        val intros_tac = Method.try_intros_tac ctxt (the_list intro1 @ the_list intro2) [];
wenzelm@69029
    83
        val res =
wenzelm@69083
    84
          Goal.init (Conjunction.mk_conjunction_balanced cprops)
wenzelm@69083
    85
          |> (ALLGOALS Goal.conjunction_tac THEN intros_tac)
wenzelm@69029
    86
          |> try Seq.hd;
wenzelm@69029
    87
      in
wenzelm@69029
    88
        (case res of
wenzelm@69083
    89
          SOME goal => Thm.prems_of goal
wenzelm@69029
    90
        | NONE => raise Fail ("Cannot unfold locale " ^ quote loc))
wenzelm@69029
    91
      end;
wenzelm@69076
    92
    val typargs = rev (fold Term.add_tfrees (map (Free o #1) args @ axioms) []);
wenzelm@69029
    93
  in {typargs = typargs, args = args, axioms = axioms} end;
wenzelm@69019
    94
wenzelm@69087
    95
fun get_locales thy =
wenzelm@69087
    96
  Locale.get_locales thy |> map_filter (fn loc =>
wenzelm@69087
    97
    if Experiment.is_experiment thy loc then NONE else SOME (loc, ()));
wenzelm@69087
    98
wenzelm@69087
    99
fun get_dependencies prev_thys thy =
wenzelm@69087
   100
  Locale.dest_dependencies prev_thys thy |> map_filter (fn dep =>
wenzelm@69087
   101
    if Experiment.is_experiment thy (#source dep) orelse
wenzelm@69087
   102
      Experiment.is_experiment thy (#target dep) then NONE
wenzelm@69087
   103
    else
wenzelm@69087
   104
      let
wenzelm@69087
   105
        val (type_params, params) = Locale.parameters_of thy (#source dep);
wenzelm@69087
   106
        val typargs = fold (Term.add_tfreesT o #2 o #1) params type_params;
wenzelm@69087
   107
        val substT =
wenzelm@69087
   108
          typargs |> map_filter (fn v =>
wenzelm@69087
   109
            let
wenzelm@69087
   110
              val T = TFree v;
wenzelm@69087
   111
              val T' = Morphism.typ (#morphism dep) T;
wenzelm@69087
   112
            in if T = T' then NONE else SOME (v, T') end);
wenzelm@69087
   113
        val subst =
wenzelm@69087
   114
          params |> map_filter (fn (v, _) =>
wenzelm@69087
   115
            let
wenzelm@69087
   116
              val t = Free v;
wenzelm@69087
   117
              val t' = Morphism.term (#morphism dep) t;
wenzelm@69087
   118
            in if t aconv t' then NONE else SOME (v, t') end);
wenzelm@69087
   119
      in SOME (dep, (substT, subst)) end);
wenzelm@69069
   120
wenzelm@69019
   121
wenzelm@68201
   122
(* general setup *)
wenzelm@68154
   123
wenzelm@68201
   124
fun setup_presentation f =
wenzelm@68201
   125
  Theory.setup (Thy_Info.add_presentation (fn context => fn thy =>
wenzelm@68201
   126
    if Options.bool (#options context) "export_theory" then f context thy else ()));
wenzelm@68154
   127
wenzelm@68201
   128
fun export_body thy name body =
wenzelm@70015
   129
  Export.export thy (Path.binding0 (Path.make ["theory", name]))
wenzelm@69784
   130
    (Buffer.chunks (YXML.buffer_body body Buffer.empty));
wenzelm@68165
   131
wenzelm@68154
   132
wenzelm@68201
   133
(* presentation *)
wenzelm@68201
   134
wenzelm@68201
   135
val _ = setup_presentation (fn {adjust_pos, ...} => fn thy =>
wenzelm@68201
   136
  let
wenzelm@68900
   137
    val parents = Theory.parents_of thy;
wenzelm@68264
   138
    val rep_tsig = Type.rep_tsig (Sign.tsig_of thy);
wenzelm@68264
   139
wenzelm@69003
   140
    val thy_ctxt = Proof_Context.init_global thy;
wenzelm@68997
   141
wenzelm@68264
   142
wenzelm@68201
   143
    (* entities *)
wenzelm@68154
   144
wenzelm@69069
   145
    fun make_entity_markup name xname pos serial =
wenzelm@68201
   146
      let
wenzelm@68201
   147
        val props =
wenzelm@68830
   148
          Position.offset_properties_of (adjust_pos pos) @
wenzelm@68830
   149
          Position.id_properties_of pos @
wenzelm@68830
   150
          Markup.serial_properties serial;
wenzelm@68997
   151
      in (Markup.entityN, (Markup.nameN, name) :: (Markup.xnameN, xname) :: props) end;
wenzelm@68154
   152
wenzelm@69069
   153
    fun entity_markup space name =
wenzelm@69069
   154
      let
wenzelm@69069
   155
        val xname = Name_Space.extern_shortest thy_ctxt space name;
wenzelm@69069
   156
        val {serial, pos, ...} = Name_Space.the_entry space name;
wenzelm@69069
   157
      in make_entity_markup name xname pos serial end;
wenzelm@69069
   158
wenzelm@68201
   159
    fun export_entities export_name export get_space decls =
wenzelm@68201
   160
      let val elems =
wenzelm@68201
   161
        let
wenzelm@68206
   162
          val parent_spaces = map get_space parents;
wenzelm@68201
   163
          val space = get_space thy;
wenzelm@68201
   164
        in
wenzelm@68201
   165
          (decls, []) |-> fold (fn (name, decl) =>
wenzelm@68201
   166
            if exists (fn space => Name_Space.declared space name) parent_spaces then I
wenzelm@68201
   167
            else
wenzelm@68201
   168
              (case export name decl of
wenzelm@68201
   169
                NONE => I
wenzelm@68724
   170
              | SOME body =>
wenzelm@68724
   171
                  cons (#serial (Name_Space.the_entry space name),
wenzelm@68724
   172
                    XML.Elem (entity_markup space name, body))))
wenzelm@68724
   173
          |> sort (int_ord o apply2 #1) |> map #2
wenzelm@68201
   174
        end;
wenzelm@68201
   175
      in if null elems then () else export_body thy export_name elems end;
wenzelm@68165
   176
wenzelm@68165
   177
wenzelm@68201
   178
    (* types *)
wenzelm@68165
   179
wenzelm@68201
   180
    val encode_type =
wenzelm@68201
   181
      let open XML.Encode Term_XML.Encode
wenzelm@69077
   182
      in triple encode_syntax (list string) (option typ) end;
wenzelm@68165
   183
wenzelm@69003
   184
    fun export_type c (Type.LogicalType n) =
wenzelm@69077
   185
          SOME (encode_type (get_syntax_type thy_ctxt c, Name.invent Name.context Name.aT n, NONE))
wenzelm@69003
   186
      | export_type c (Type.Abbreviation (args, U, false)) =
wenzelm@69077
   187
          SOME (encode_type (get_syntax_type thy_ctxt c, args, SOME U))
wenzelm@69003
   188
      | export_type _ _ = NONE;
wenzelm@68165
   189
wenzelm@68201
   190
    val _ =
wenzelm@69003
   191
      export_entities "types" export_type Sign.type_space
wenzelm@68264
   192
        (Name_Space.dest_table (#types rep_tsig));
wenzelm@68201
   193
wenzelm@68173
   194
wenzelm@68201
   195
    (* consts *)
wenzelm@68201
   196
wenzelm@68201
   197
    val encode_const =
wenzelm@69992
   198
      let open XML.Encode Term_XML.Encode in
wenzelm@69992
   199
        pair encode_syntax
wenzelm@69996
   200
          (pair (list string)
wenzelm@69996
   201
            (pair typ (pair (option term) (pair bool (pair (list string) bool)))))
wenzelm@69992
   202
      end;
wenzelm@68165
   203
wenzelm@68201
   204
    fun export_const c (T, abbrev) =
wenzelm@68201
   205
      let
wenzelm@69077
   206
        val syntax = get_syntax_const thy_ctxt c;
wenzelm@69992
   207
        val U = Logic.unvarifyT_global T;
wenzelm@69992
   208
        val U0 = Type.strip_sorts U;
wenzelm@69996
   209
        val recursion = primrec_types thy_ctxt (c, U);
wenzelm@70529
   210
        val abbrev' = abbrev
wenzelm@70529
   211
          |> Option.map (Proofterm.standard_vars_term Name.context #> map_types Type.strip_sorts);
wenzelm@69992
   212
        val args = map (#1 o dest_TFree) (Consts.typargs (Sign.consts_of thy) (c, U0));
wenzelm@69992
   213
        val propositional = Object_Logic.is_propositional thy_ctxt (Term.body_type U0);
wenzelm@69996
   214
      in encode_const (syntax, (args, (U0, (abbrev', (propositional, recursion))))) end;
wenzelm@68165
   215
wenzelm@68201
   216
    val _ =
wenzelm@69023
   217
      export_entities "consts" (SOME oo export_const) Sign.const_space
wenzelm@68201
   218
        (#constants (Consts.dest (Sign.consts_of thy)));
wenzelm@68201
   219
wenzelm@68208
   220
wenzelm@68232
   221
    (* axioms and facts *)
wenzelm@68208
   222
wenzelm@70534
   223
    fun standard_prop used extra_shyps raw_prop raw_proof =
wenzelm@68727
   224
      let
wenzelm@70534
   225
        val raw_proofs = the_list raw_proof;
wenzelm@70540
   226
        val ([prop], proofs) = Proofterm.standard_vars used ([raw_prop], raw_proofs);
wenzelm@68539
   227
wenzelm@69023
   228
        val args = rev (add_frees used prop []);
wenzelm@69023
   229
        val typargs = rev (add_tfrees used prop []);
wenzelm@70534
   230
        val used_typargs = fold (Name.declare o #1) typargs used;
wenzelm@70534
   231
        val sorts = Name.invent used_typargs Name.aT (length extra_shyps) ~~ extra_shyps;
wenzelm@70534
   232
      in ((sorts @ typargs, args, prop), try hd proofs) end;
wenzelm@70534
   233
wenzelm@70534
   234
    val encode_prop =
wenzelm@70534
   235
      let open XML.Encode Term_XML.Encode
wenzelm@70534
   236
      in triple (list (pair string sort)) (list (pair string typ)) term end;
wenzelm@70534
   237
wenzelm@70534
   238
    fun encode_axiom used prop =
wenzelm@70534
   239
      encode_prop (#1 (standard_prop used [] prop NONE));
wenzelm@68208
   240
wenzelm@70534
   241
    val clean_thm =
wenzelm@70534
   242
      Thm.transfer thy
wenzelm@70534
   243
      #> Thm.check_hyps (Context.Theory thy)
wenzelm@70534
   244
      #> Thm.strip_shyps;
wenzelm@69023
   245
wenzelm@70534
   246
    val encode_fact = clean_thm #> (fn thm =>
wenzelm@70534
   247
      standard_prop Name.context
wenzelm@70534
   248
        (Thm.extra_shyps thm)
wenzelm@70534
   249
        (Thm.full_prop_of thm)
wenzelm@70534
   250
        (try Thm.reconstruct_proof_of thm) |>
wenzelm@70534
   251
      let open XML.Encode Term_XML.Encode
wenzelm@70534
   252
      in pair encode_prop (option Proofterm.encode_full) end);
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@70534
   258
      export_entities "facts" (K (SOME o XML.Encode.list encode_fact))
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@70534
   267
      in pair (list (pair string typ)) (list (encode_axiom Name.context)) 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@70534
   272
      | SOME {params, axioms, ...} => (params, map (Thm.plain_prop_of o clean_thm) 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@70386
   282
    local
wenzelm@70386
   283
      val prop = encode_axiom Name.context o Logic.varify_global;
wenzelm@68295
   284
wenzelm@70386
   285
      val encode_classrel =
wenzelm@70386
   286
        let open XML.Encode
wenzelm@70386
   287
        in list (pair prop (pair string string)) end;
wenzelm@70384
   288
wenzelm@70386
   289
      val encode_arities =
wenzelm@70386
   290
        let open XML.Encode Term_XML.Encode
wenzelm@70386
   291
        in list (pair prop (triple string (list sort) string)) end;
wenzelm@70386
   292
    in
wenzelm@70386
   293
      val export_classrel =
wenzelm@70386
   294
        maps (fn (c, cs) => map (pair c) cs) #> map (`Logic.mk_classrel) #> encode_classrel;
wenzelm@68295
   295
wenzelm@70386
   296
      val export_arities = map (`Logic.mk_arity) #> encode_arities;
wenzelm@70384
   297
wenzelm@70386
   298
      val {classrel, arities} =
wenzelm@70386
   299
        Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@70386
   300
          (#2 (#classes rep_tsig));
wenzelm@68295
   301
wenzelm@70386
   302
    end;
wenzelm@70384
   303
wenzelm@70384
   304
    val _ = if null classrel then () else export_body thy "classrel" (export_classrel classrel);
wenzelm@70384
   305
    val _ = if null arities then () else export_body thy "arities" (export_arities arities);
wenzelm@68295
   306
wenzelm@68862
   307
wenzelm@68862
   308
    (* locales *)
wenzelm@68862
   309
wenzelm@69023
   310
    fun encode_locale used =
wenzelm@69076
   311
      let open XML.Encode Term_XML.Encode in
wenzelm@69077
   312
        triple (list (pair string sort)) (list (pair (pair string typ) encode_syntax))
wenzelm@69076
   313
          (list (encode_axiom used))
wenzelm@69076
   314
      end;
wenzelm@69023
   315
wenzelm@69029
   316
    fun export_locale loc =
wenzelm@68864
   317
      let
wenzelm@69034
   318
        val {typargs, args, axioms} = locale_content thy loc;
wenzelm@69076
   319
        val used = fold Name.declare (map #1 typargs @ map (#1 o #1) args) Name.context;
wenzelm@69027
   320
      in encode_locale used (typargs, args, axioms) end
wenzelm@69027
   321
      handle ERROR msg =>
wenzelm@69027
   322
        cat_error msg ("The error(s) above occurred in locale " ^
wenzelm@69027
   323
          quote (Locale.markup_name thy_ctxt loc));
wenzelm@68862
   324
wenzelm@68862
   325
    val _ =
wenzelm@69029
   326
      export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
wenzelm@69087
   327
        Locale.locale_space (get_locales thy);
wenzelm@68862
   328
wenzelm@68900
   329
wenzelm@69069
   330
    (* locale dependencies *)
wenzelm@69069
   331
wenzelm@69087
   332
    fun encode_locale_dependency (dep: Locale.locale_dependency, subst) =
wenzelm@69087
   333
      (#source dep, (#target dep, (#prefix dep, subst))) |>
wenzelm@69069
   334
        let
wenzelm@69069
   335
          open XML.Encode Term_XML.Encode;
wenzelm@69069
   336
          val encode_subst =
wenzelm@69069
   337
            pair (list (pair (pair string sort) typ)) (list (pair (pair string typ) term));
wenzelm@69069
   338
        in pair string (pair string (pair (list (pair string bool)) encode_subst)) end;
wenzelm@69069
   339
wenzelm@69069
   340
    val _ =
wenzelm@69087
   341
      (case get_dependencies parents thy of
wenzelm@69069
   342
        [] => ()
wenzelm@69069
   343
      | deps =>
wenzelm@69069
   344
          deps |> map_index (fn (i, dep) =>
wenzelm@69069
   345
            let
wenzelm@69069
   346
              val xname = string_of_int (i + 1);
wenzelm@69069
   347
              val name = Long_Name.implode [Context.theory_name thy, xname];
wenzelm@69087
   348
              val markup = make_entity_markup name xname (#pos (#1 dep)) (#serial (#1 dep));
wenzelm@69069
   349
              val body = encode_locale_dependency dep;
wenzelm@69087
   350
            in XML.Elem (markup, body) end)
wenzelm@69069
   351
          |> export_body thy "locale_dependencies");
wenzelm@69069
   352
wenzelm@69069
   353
wenzelm@68900
   354
    (* parents *)
wenzelm@68900
   355
wenzelm@68900
   356
    val _ =
wenzelm@68900
   357
      export_body thy "parents"
wenzelm@68900
   358
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68900
   359
wenzelm@68295
   360
  in () end);
wenzelm@68165
   361
wenzelm@68165
   362
end;