src/Pure/Thy/export_theory.ML
author wenzelm
Wed Aug 14 19:21:34 2019 +0200 (2 months ago)
changeset 70529 2ecbbe6b35db
parent 70386 6af87375b95f
child 70534 fb876ebbf5a7
permissions -rw-r--r--
uniform standard_vars for terms and proof terms;
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@69023
   223
    fun prop_of raw_thm =
wenzelm@68727
   224
      let
wenzelm@68727
   225
        val thm = raw_thm
wenzelm@68727
   226
          |> Thm.transfer thy
wenzelm@68727
   227
          |> Thm.check_hyps (Context.Theory thy)
wenzelm@68727
   228
          |> Thm.strip_shyps;
wenzelm@68727
   229
        val prop = thm
wenzelm@69023
   230
          |> Thm.full_prop_of;
wenzelm@68727
   231
      in (Thm.extra_shyps thm, prop) end;
wenzelm@68539
   232
wenzelm@69023
   233
    fun encode_prop used (Ss, raw_prop) =
wenzelm@68208
   234
      let
wenzelm@70529
   235
        val prop = Proofterm.standard_vars_term used raw_prop;
wenzelm@69023
   236
        val args = rev (add_frees used prop []);
wenzelm@69023
   237
        val typargs = rev (add_tfrees used prop []);
wenzelm@69023
   238
        val used' = fold (Name.declare o #1) typargs used;
wenzelm@69023
   239
        val sorts = Name.invent used' Name.aT (length Ss) ~~ Ss;
wenzelm@68726
   240
      in
wenzelm@69023
   241
        (sorts @ typargs, args, prop) |>
wenzelm@68727
   242
          let open XML.Encode Term_XML.Encode
wenzelm@68727
   243
          in triple (list (pair string sort)) (list (pair string typ)) term end
wenzelm@68726
   244
      end;
wenzelm@68208
   245
wenzelm@69023
   246
    fun encode_axiom used t = encode_prop used ([], t);
wenzelm@69023
   247
wenzelm@70386
   248
    val encode_fact = encode_prop Name.context;
wenzelm@70386
   249
    val encode_fact_single = encode_fact o prop_of;
wenzelm@70386
   250
    val encode_fact_multi = XML.Encode.list encode_fact o map prop_of;
wenzelm@68208
   251
wenzelm@68232
   252
    val _ =
wenzelm@69023
   253
      export_entities "axioms" (fn _ => fn t => SOME (encode_axiom Name.context t))
wenzelm@69023
   254
        Theory.axiom_space (Theory.axioms_of thy);
wenzelm@68235
   255
    val _ =
wenzelm@69023
   256
      export_entities "facts" (K (SOME o encode_fact_multi))
wenzelm@69023
   257
        (Facts.space_of o Global_Theory.facts_of)
wenzelm@68232
   258
        (Facts.dest_static true [] (Global_Theory.facts_of thy));
wenzelm@68232
   259
wenzelm@68264
   260
wenzelm@68264
   261
    (* type classes *)
wenzelm@68264
   262
wenzelm@68264
   263
    val encode_class =
wenzelm@68264
   264
      let open XML.Encode Term_XML.Encode
wenzelm@69023
   265
      in pair (list (pair string typ)) (list encode_fact_single) end;
wenzelm@68264
   266
wenzelm@68264
   267
    fun export_class name =
wenzelm@68264
   268
      (case try (Axclass.get_info thy) name of
wenzelm@68264
   269
        NONE => ([], [])
wenzelm@69023
   270
      | SOME {params, axioms, ...} => (params, axioms))
wenzelm@68264
   271
      |> encode_class |> SOME;
wenzelm@68264
   272
wenzelm@68264
   273
    val _ =
wenzelm@68264
   274
      export_entities "classes" (fn name => fn () => export_class name)
wenzelm@68264
   275
        Sign.class_space (map (rpair ()) (Graph.keys (Sorts.classes_of (#2 (#classes rep_tsig)))));
wenzelm@68264
   276
wenzelm@68295
   277
wenzelm@68295
   278
    (* sort algebra *)
wenzelm@68295
   279
wenzelm@70386
   280
    local
wenzelm@70386
   281
      val prop = encode_axiom Name.context o Logic.varify_global;
wenzelm@68295
   282
wenzelm@70386
   283
      val encode_classrel =
wenzelm@70386
   284
        let open XML.Encode
wenzelm@70386
   285
        in list (pair prop (pair string string)) end;
wenzelm@70384
   286
wenzelm@70386
   287
      val encode_arities =
wenzelm@70386
   288
        let open XML.Encode Term_XML.Encode
wenzelm@70386
   289
        in list (pair prop (triple string (list sort) string)) end;
wenzelm@70386
   290
    in
wenzelm@70386
   291
      val export_classrel =
wenzelm@70386
   292
        maps (fn (c, cs) => map (pair c) cs) #> map (`Logic.mk_classrel) #> encode_classrel;
wenzelm@68295
   293
wenzelm@70386
   294
      val export_arities = map (`Logic.mk_arity) #> encode_arities;
wenzelm@70384
   295
wenzelm@70386
   296
      val {classrel, arities} =
wenzelm@70386
   297
        Sorts.dest_algebra (map (#2 o #classes o Type.rep_tsig o Sign.tsig_of) parents)
wenzelm@70386
   298
          (#2 (#classes rep_tsig));
wenzelm@68295
   299
wenzelm@70386
   300
    end;
wenzelm@70384
   301
wenzelm@70384
   302
    val _ = if null classrel then () else export_body thy "classrel" (export_classrel classrel);
wenzelm@70384
   303
    val _ = if null arities then () else export_body thy "arities" (export_arities arities);
wenzelm@68295
   304
wenzelm@68862
   305
wenzelm@68862
   306
    (* locales *)
wenzelm@68862
   307
wenzelm@69023
   308
    fun encode_locale used =
wenzelm@69076
   309
      let open XML.Encode Term_XML.Encode in
wenzelm@69077
   310
        triple (list (pair string sort)) (list (pair (pair string typ) encode_syntax))
wenzelm@69076
   311
          (list (encode_axiom used))
wenzelm@69076
   312
      end;
wenzelm@69023
   313
wenzelm@69029
   314
    fun export_locale loc =
wenzelm@68864
   315
      let
wenzelm@69034
   316
        val {typargs, args, axioms} = locale_content thy loc;
wenzelm@69076
   317
        val used = fold Name.declare (map #1 typargs @ map (#1 o #1) args) Name.context;
wenzelm@69027
   318
      in encode_locale used (typargs, args, axioms) end
wenzelm@69027
   319
      handle ERROR msg =>
wenzelm@69027
   320
        cat_error msg ("The error(s) above occurred in locale " ^
wenzelm@69027
   321
          quote (Locale.markup_name thy_ctxt loc));
wenzelm@68862
   322
wenzelm@68862
   323
    val _ =
wenzelm@69029
   324
      export_entities "locales" (fn loc => fn () => SOME (export_locale loc))
wenzelm@69087
   325
        Locale.locale_space (get_locales thy);
wenzelm@68862
   326
wenzelm@68900
   327
wenzelm@69069
   328
    (* locale dependencies *)
wenzelm@69069
   329
wenzelm@69087
   330
    fun encode_locale_dependency (dep: Locale.locale_dependency, subst) =
wenzelm@69087
   331
      (#source dep, (#target dep, (#prefix dep, subst))) |>
wenzelm@69069
   332
        let
wenzelm@69069
   333
          open XML.Encode Term_XML.Encode;
wenzelm@69069
   334
          val encode_subst =
wenzelm@69069
   335
            pair (list (pair (pair string sort) typ)) (list (pair (pair string typ) term));
wenzelm@69069
   336
        in pair string (pair string (pair (list (pair string bool)) encode_subst)) end;
wenzelm@69069
   337
wenzelm@69069
   338
    val _ =
wenzelm@69087
   339
      (case get_dependencies parents thy of
wenzelm@69069
   340
        [] => ()
wenzelm@69069
   341
      | deps =>
wenzelm@69069
   342
          deps |> map_index (fn (i, dep) =>
wenzelm@69069
   343
            let
wenzelm@69069
   344
              val xname = string_of_int (i + 1);
wenzelm@69069
   345
              val name = Long_Name.implode [Context.theory_name thy, xname];
wenzelm@69087
   346
              val markup = make_entity_markup name xname (#pos (#1 dep)) (#serial (#1 dep));
wenzelm@69069
   347
              val body = encode_locale_dependency dep;
wenzelm@69087
   348
            in XML.Elem (markup, body) end)
wenzelm@69069
   349
          |> export_body thy "locale_dependencies");
wenzelm@69069
   350
wenzelm@69069
   351
wenzelm@68900
   352
    (* parents *)
wenzelm@68900
   353
wenzelm@68900
   354
    val _ =
wenzelm@68900
   355
      export_body thy "parents"
wenzelm@68900
   356
        (XML.Encode.string (cat_lines (map Context.theory_long_name parents)));
wenzelm@68900
   357
wenzelm@68295
   358
  in () end);
wenzelm@68165
   359
wenzelm@68165
   360
end;