src/Pure/Thy/thy_info.ML
author haftmann
Sat, 19 Jul 2025 18:41:55 +0200
changeset 82886 8d1e295aab70
parent 82681 b77163d7e847
permissions -rw-r--r--
clarified name and status of auxiliary operation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_info.ML
72651
52cb065aa916 clarified modules;
wenzelm
parents: 72638
diff changeset
     2
    Author:     Makarius
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     3
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37977
diff changeset
     4
Global theory info database, with auto-loading according to theory and
72651
52cb065aa916 clarified modules;
wenzelm
parents: 72638
diff changeset
     5
file dependencies, and presentation of theory documents.
3976
wenzelm
parents: 3604
diff changeset
     6
*)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     7
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     8
signature THY_INFO =
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
     9
sig
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    10
  type presentation_context =
68184
6c693b2700b3 support for dynamic document output while editing;
wenzelm
parents: 68182
diff changeset
    11
    {options: Options.T, file_pos: Position.T, adjust_pos: Position.T -> Position.T,
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 73691
diff changeset
    12
      segments: Document_Output.segment list}
71249
877316c54ed3 clarified signature;
wenzelm
parents: 70991
diff changeset
    13
  val adjust_pos_properties: presentation_context -> Position.T -> Properties.T
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    14
  val apply_presentation: presentation_context -> theory -> unit
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    15
  val add_presentation: (presentation_context -> theory -> unit) -> theory -> theory
26614
1f09a22a1027 export get_names (formerly names);
wenzelm
parents: 26494
diff changeset
    16
  val get_names: unit -> string list
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
    17
  val lookup_theory: string -> theory option
76405
aaf307f865c9 tuned signature;
wenzelm
parents: 76404
diff changeset
    18
  val defined_theory: string -> bool
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    19
  val get_theory: string -> theory
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
    20
  val get_theory_segments: string -> Document_Output.segment list
82679
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
    21
  val get_theory_elements: string -> Document_Output.segment Thy_Element.element list
78527
374611eb3055 tuned signature: more operations for formal theory context vs. theory loader;
wenzelm
parents: 78021
diff changeset
    22
  val check_theory: Proof.context -> string * Position.T -> theory
26983
e40f28cdd19b exported master_directory;
wenzelm
parents: 26881
diff changeset
    23
  val master_directory: string -> Path.T
29421
db532e37cda2 use_thys: perform consolidate_thy on loaded theories, which removes failed nodes in post-hoc fashion;
wenzelm
parents: 29118
diff changeset
    24
  val remove_thy: string -> unit
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
    25
  val use_theories: Options.T -> string -> (string * Position.T) list ->
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
    26
    (theory * Document_Output.segment list) list
65444
1f5821b19741 tuned signature;
wenzelm
parents: 65443
diff changeset
    27
  val use_thy: string -> unit
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
    28
  val script_thy: Position.T -> string -> theory -> theory
24080
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
    29
  val finish: unit -> unit
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    30
end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    31
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36953
diff changeset
    32
structure Thy_Info: THY_INFO =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    33
struct
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    34
72651
52cb065aa916 clarified modules;
wenzelm
parents: 72638
diff changeset
    35
(** theory presentation **)
52cb065aa916 clarified modules;
wenzelm
parents: 72638
diff changeset
    36
52cb065aa916 clarified modules;
wenzelm
parents: 72638
diff changeset
    37
(* hook for consolidated theory *)
68153
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    38
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    39
type presentation_context =
68184
6c693b2700b3 support for dynamic document output while editing;
wenzelm
parents: 68182
diff changeset
    40
  {options: Options.T, file_pos: Position.T, adjust_pos: Position.T -> Position.T,
73761
ef1a18e20ace clarified modules;
wenzelm
parents: 73691
diff changeset
    41
    segments: Document_Output.segment list};
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    42
71249
877316c54ed3 clarified signature;
wenzelm
parents: 70991
diff changeset
    43
fun adjust_pos_properties (context: presentation_context) pos =
77770
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    44
  let
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    45
    val props = Position.properties_of pos;
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    46
    val props' = Position.properties_of (#adjust_pos context pos);
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    47
  in
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    48
    filter (fn (a, _) => a = Markup.offsetN orelse a = Markup.end_offsetN) props' @
78021
ce6e3bc34343 more informative position information;
wenzelm
parents: 77889
diff changeset
    49
    filter (fn (a, _) => a = Markup.labelN orelse a = Markup.fileN orelse a = Markup.idN) props
77770
472e48ed9c79 clarified signature;
wenzelm
parents: 76884
diff changeset
    50
  end;
71249
877316c54ed3 clarified signature;
wenzelm
parents: 70991
diff changeset
    51
68153
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    52
structure Presentation = Theory_Data
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    53
(
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    54
  type T = ((presentation_context -> theory -> unit) * stamp) list;
68153
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    55
  val empty = [];
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    56
  fun merge data : T = Library.merge (eq_snd op =) data;
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    57
);
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    58
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    59
fun apply_presentation (context: presentation_context) thy =
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
    60
  ignore (Par_List.map (fn (f, _) => f context thy) (Presentation.get thy));
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    61
68153
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    62
fun add_presentation f = Presentation.map (cons (f, stamp ()));
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    63
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    64
val _ =
73819
wenzelm
parents: 73818
diff changeset
    65
  Theory.setup (add_presentation (fn {options, segments, ...} => fn thy =>
68182
fa3cf61121ee tuned signature (see Command.eval_state);
wenzelm
parents: 68181
diff changeset
    66
    if exists (Toplevel.is_skipped_proof o #state) segments then ()
68180
112d9624c020 more general presentation hook, with document preparation as application;
wenzelm
parents: 68179
diff changeset
    67
    else
68181
34592bf86424 export generated document.tex, unless explicit document=false;
wenzelm
parents: 68180
diff changeset
    68
      let
76432
d0079b509d99 clarified signature;
wenzelm
parents: 76415
diff changeset
    69
        val keywords = Thy_Header.get_keywords thy;
77889
5db014c36f42 clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents: 77770
diff changeset
    70
        val thy_name = Context.theory_base_name thy;
76432
d0079b509d99 clarified signature;
wenzelm
parents: 76415
diff changeset
    71
        val latex = Document_Output.present_thy options keywords thy_name segments;
68181
34592bf86424 export generated document.tex, unless explicit document=false;
wenzelm
parents: 68180
diff changeset
    72
      in
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72511
diff changeset
    73
        if Options.string options "document" = "false" then ()
76432
d0079b509d99 clarified signature;
wenzelm
parents: 76415
diff changeset
    74
        else Export.export thy \<^path_binding>\<open>document/latex\<close> latex
72710
6bc199a70bf9 more robust: include reports from Thy_Output.present_thy/output_document;
wenzelm
parents: 72705
diff changeset
    75
      end));
68153
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    76
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    77
e469d529e6da support for general theory presentation;
wenzelm
parents: 68130
diff changeset
    78
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    79
(** thy database **)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    80
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    81
(* messages *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    82
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
    83
val show_path = space_implode " via " o map quote;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    84
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
    85
fun cycle_msg names = "Cyclic dependency of " ^ show_path names;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    86
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    87
6666
be06ed5d0b4c cleaned comments;
wenzelm
parents: 6654
diff changeset
    88
(* derived graph operations *)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    89
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
    90
fun add_deps name parents G = String_Graph.add_deps_acyclic (name, parents) G
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
    91
  handle String_Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    92
37984
f26352bd82cf clarified register_thy: clean slate via kill_thy, more precise CRITICAL section;
wenzelm
parents: 37981
diff changeset
    93
fun new_entry name parents entry =
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
    94
  String_Graph.new_node (name, entry) #> add_deps name parents;
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
    95
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    96
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    97
(* global thys *)
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    98
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    99
type deps =
41955
703ea96b13c6 files are identified via SHA1 digests -- discontinued ISABELLE_FILE_IDENT;
wenzelm
parents: 41678
diff changeset
   100
 {master: (Path.T * SHA1.digest),  (*master dependencies for thy file*)
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
   101
  imports: (string * Position.T) list};  (*source specification of imports (partially qualified)*)
23886
f40fba467384 simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents: 23871
diff changeset
   102
38148
d2f3c8d4a89f uniform naming of imports (source specification) vs. parents (thy node names) vs. parent_thys (theory values);
wenzelm
parents: 38147
diff changeset
   103
fun make_deps master imports : deps = {master = master, imports = imports};
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   104
65454
2b22b7d8649f clarified, according to Scala version;
wenzelm
parents: 65452
diff changeset
   105
fun master_dir_deps (d: deps option) =
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   106
  the_default Path.current (Option.map (Path.dir o #1 o #master) d);
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   107
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   108
local
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   109
  type thy = deps option * (theory * Document_Output.segment list) option;
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   110
  val global_thys = Synchronized.var "Thy_Info.thys" (String_Graph.empty: thy String_Graph.T);
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   111
in
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   112
  fun get_thys () = Synchronized.value global_thys;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   113
  fun change_thys f = Synchronized.change global_thys f;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
   114
end;
5209
a69fe5a61b6c theory_of renamed to theory (and made public);
wenzelm
parents: 4975
diff changeset
   115
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
   116
fun get_names () = String_Graph.topological_order (get_thys ());
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   117
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   118
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   119
(* access thy *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   120
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   121
fun lookup thys name = try (String_Graph.get_node thys) name;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   122
fun lookup_thy name = lookup (get_thys ()) name;
7935
ac62465ed06c added check_known_thy, if_known_thy;
wenzelm
parents: 7910
diff changeset
   123
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   124
fun get thys name =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   125
  (case lookup thys name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15158
diff changeset
   126
    SOME thy => thy
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   127
  | NONE => error ("Theory loader: nothing known about theory " ^ quote name));
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   128
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   129
fun get_thy name = get (get_thys ()) name;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   130
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   131
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   132
(* access deps *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   133
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   134
val lookup_deps = Option.map #1 o lookup_thy;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   135
65454
2b22b7d8649f clarified, according to Scala version;
wenzelm
parents: 65452
diff changeset
   136
val master_directory = master_dir_deps o #1 o get_thy;
7191
fcce2387ad6d added pretend_use;
wenzelm
parents: 7099
diff changeset
   137
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   138
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   139
(* access theory *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   140
7687
3383b8223e46 fixed lookup_theory;
wenzelm
parents: 7589
diff changeset
   141
fun lookup_theory name =
3383b8223e46 fixed lookup_theory;
wenzelm
parents: 7589
diff changeset
   142
  (case lookup_thy name of
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   143
    SOME (_, SOME (theory, _)) => SOME theory
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15158
diff changeset
   144
  | _ => NONE);
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
   145
76405
aaf307f865c9 tuned signature;
wenzelm
parents: 76404
diff changeset
   146
val defined_theory = is_some o lookup_theory;
aaf307f865c9 tuned signature;
wenzelm
parents: 76404
diff changeset
   147
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   148
fun get_theory name =
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
   149
  (case lookup_theory name of
23871
0dd5696f58b6 removed obsolete use/update_thy_only;
wenzelm
parents: 22215
diff changeset
   150
    SOME theory => theory
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   151
  | _ => error ("Theory loader: undefined entry for theory " ^ quote name));
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   152
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   153
fun get_theory_segments name =
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   154
  let
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   155
    val _ = get_theory name;
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   156
    val segments =
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   157
      (case lookup_thy name of
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   158
        SOME (_, SOME (_, segments)) => segments
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   159
      | _ => []);
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   160
  in
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   161
    if null segments then
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   162
      error ("Theory loader: no theory segments for theory " ^ quote name ^
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   163
        "\n Need to build with option " ^ quote \<^system_option>\<open>record_theories\<close>)
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   164
    else segments
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   165
  end;
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   166
82679
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   167
fun get_theory_elements name =
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   168
  let
82681
wenzelm
parents: 82680
diff changeset
   169
    val keywords = Thy_Header.get_keywords (get_theory name);
82679
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   170
    val stopper = Document_Output.segment_stopper;
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   171
    val segments = get_theory_segments name;
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   172
  in Thy_Element.parse_elements_generic keywords #span stopper segments end;
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 82678
diff changeset
   173
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   174
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   175
val get_imports = Resources.imports_of o get_theory;
44337
d453faed4815 clarified get_imports -- should not rely on accidental order within graph;
wenzelm
parents: 44302
diff changeset
   176
78527
374611eb3055 tuned signature: more operations for formal theory context vs. theory loader;
wenzelm
parents: 78021
diff changeset
   177
val check_theory = Theory.check_theory {get = get_theory, all = get_names};
374611eb3055 tuned signature: more operations for formal theory context vs. theory loader;
wenzelm
parents: 78021
diff changeset
   178
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   179
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   180
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   181
(** thy operations **)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   182
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   183
(* remove *)
29421
db532e37cda2 use_thys: perform consolidate_thy on loaded theories, which removes failed nodes in post-hoc fashion;
wenzelm
parents: 29118
diff changeset
   184
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   185
fun remove name thys =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   186
  (case lookup thys name of
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   187
    NONE => thys
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   188
  | SOME (NONE, _) => error ("Cannot update finished theory " ^ quote name)
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   189
  | SOME _ =>
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   190
      let
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   191
        val succs = String_Graph.all_succs thys [name];
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   192
        val _ = writeln ("Theory loader: removing " ^ commas_quote succs);
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   193
      in fold String_Graph.del_node succs thys end);
29421
db532e37cda2 use_thys: perform consolidate_thy on loaded theories, which removes failed nodes in post-hoc fashion;
wenzelm
parents: 29118
diff changeset
   194
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   195
val remove_thy = change_thys o remove;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   196
7099
8142ccd13963 added update_thy_only;
wenzelm
parents: 7066
diff changeset
   197
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   198
(* update *)
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   199
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   200
fun update deps (res as (theory, _)) thys =
43640
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   201
  let
65459
da59e8e0a663 clarified theory_long_name (for qualified access to Thy_Info) vs. short theory_name (which is unique within any given theory context);
wenzelm
parents: 65455
diff changeset
   202
    val name = Context.theory_long_name theory;
da59e8e0a663 clarified theory_long_name (for qualified access to Thy_Info) vs. short theory_name (which is unique within any given theory context);
wenzelm
parents: 65455
diff changeset
   203
    val parents = map Context.theory_long_name (Theory.parents_of theory);
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   204
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   205
    val thys' = remove name thys;
82678
wenzelm
parents: 82677
diff changeset
   206
    val _ = List.app (ignore o get thys') parents;
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   207
  in new_entry name parents (SOME deps, SOME res) thys' end;
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   208
82681
wenzelm
parents: 82680
diff changeset
   209
val update_thy = change_thys oo update;
43640
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   210
7099
8142ccd13963 added update_thy_only;
wenzelm
parents: 7066
diff changeset
   211
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   212
(* scheduling loader tasks *)
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   213
51330
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   214
datatype result =
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   215
  Result of
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   216
   {theory: theory,
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   217
    exec_id: Document_ID.exec,
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   218
    present: unit -> presentation_context option,
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   219
    commit: Document_Output.segment list -> unit};
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   220
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   221
fun theory_result theory =
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   222
  Result
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   223
   {theory = theory,
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   224
    exec_id = Document_ID.none,
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   225
    present = K NONE,
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   226
    commit = K ()};
51330
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   227
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   228
fun result_theory (Result {theory, ...}) = theory;
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   229
fun result_commit (Result {commit, ...}) = commit;
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   230
73825
wenzelm
parents: 73821
diff changeset
   231
datatype task =
wenzelm
parents: 73821
diff changeset
   232
  Task of string list * (theory list -> result) |
wenzelm
parents: 73821
diff changeset
   233
  Finished of theory;
wenzelm
parents: 73821
diff changeset
   234
wenzelm
parents: 73821
diff changeset
   235
local
wenzelm
parents: 73821
diff changeset
   236
73817
wenzelm
parents: 73800
diff changeset
   237
fun consolidate_theory (Exn.Exn exn) = [Exn.Exn exn]
wenzelm
parents: 73800
diff changeset
   238
  | consolidate_theory (Exn.Res (Result {theory, exec_id, ...})) =
wenzelm
parents: 73800
diff changeset
   239
      let
wenzelm
parents: 73800
diff changeset
   240
        val _ = Execution.join [exec_id];
78717
wenzelm
parents: 78527
diff changeset
   241
        val result = Exn.capture Thm.consolidate_theory theory;
73817
wenzelm
parents: 73800
diff changeset
   242
        val exns = maps Task_Queue.group_status (Execution.peek exec_id);
78717
wenzelm
parents: 78527
diff changeset
   243
      in result :: map Exn.Exn exns end;
43641
081e009549dc uniform finish_thy -- always Global_Theory.join_proofs, even with sequential scheduling;
wenzelm
parents: 43640
diff changeset
   244
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   245
fun present_theory (Exn.Exn exn) = SOME (Exn.Exn exn)
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   246
  | present_theory (Exn.Res (Result {theory, present, ...})) =
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   247
      present () |> Option.map (fn context as {segments, ...} =>
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   248
        Exn.capture_body (fn () => (apply_presentation context theory; (theory, segments))));
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   249
73825
wenzelm
parents: 73821
diff changeset
   250
in
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   251
73818
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   252
val schedule_theories = Thread_Attributes.uninterruptible (fn _ => fn tasks =>
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   253
  let
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   254
    fun join_parents deps name parents =
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   255
      (case map #1 (filter (not o can Future.join o #2) deps) of
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   256
        [] => map (result_theory o Future.join o the o AList.lookup (op =) deps) parents
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   257
      | bad =>
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   258
          error ("Failed to load theory " ^ quote name ^ " (unresolved " ^ commas_quote bad ^ ")"));
44162
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   259
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   260
    val futures = tasks
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   261
      |> String_Graph.schedule (fn deps => fn (name, task) =>
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   262
        (case task of
66711
80fa1401cf76 discontinued extra checks (see ce676a750575 and 60c159d490a2) -- qualified theory names are meant to cover this;
wenzelm
parents: 66377
diff changeset
   263
          Task (parents, body) =>
73818
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   264
            if Multithreading.max_threads () > 1 then
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   265
              (singleton o Future.forks)
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   266
                {name = "theory:" ^ name, group = NONE,
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   267
                  deps = map (Future.task_of o #2) deps, pri = 0, interrupts = true}
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   268
                (fn () => body (join_parents deps name parents))
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   269
            else
78757
a094bf81a496 clarified signature;
wenzelm
parents: 78717
diff changeset
   270
              Future.value_result (Exn.capture_body (fn () => body (join_parents deps name parents)))
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   271
        | Finished theory => Future.value (theory_result theory)));
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   272
73817
wenzelm
parents: 73800
diff changeset
   273
    val results1 = futures |> maps (consolidate_theory o Future.join_result);
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   274
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   275
    val presented_results = futures |> map (present_theory o Future.join_result);
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   276
    val present_results = map_filter I presented_results;
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   277
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   278
    val results2 = (map o Exn.map_res) (K ()) present_results;
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   279
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   280
    val results3 = (futures, presented_results) |> ListPair.map (fn (future, presented) =>
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   281
      let val segments = (case presented of SOME (Exn.Res (_, segments)) => segments | _ => []);
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   282
      in Exn.capture_body (fn () => result_commit (Future.join future) segments) end);
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   283
53192
04df1d236e1c maintain goal forks as part of global execution;
wenzelm
parents: 53190
diff changeset
   284
    val results4 = map Exn.Exn (maps Task_Queue.group_status (Execution.reset ()));
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   285
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   286
    val _ = Par_Exn.release_all (results1 @ results2 @ results3 @ results4);
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   287
  in Par_Exn.release_all present_results end);
29429
a6c641f08af7 schedule_futures: tuned final consolidation, explicit after_load phase;
wenzelm
parents: 29421
diff changeset
   288
73825
wenzelm
parents: 73821
diff changeset
   289
end;
wenzelm
parents: 73821
diff changeset
   290
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   291
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   292
(* eval theory *)
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   293
72705
0471eb6a4b99 unused;
wenzelm
parents: 72692
diff changeset
   294
fun eval_thy options master_dir header text_pos text parents =
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   295
  let
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   296
    val (name, _) = #name header;
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   297
    val keywords =
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   298
      fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   299
        (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   300
68178
e3dd94d04eee more explicit type Thy_Output.segment;
wenzelm
parents: 68153
diff changeset
   301
    val spans = Outer_Syntax.parse_spans (Token.explode keywords text_pos text);
68839
d8251a61cce8 clarified modules;
wenzelm
parents: 68506
diff changeset
   302
    val elements = Thy_Element.parse_elements keywords spans;
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   303
73046
wenzelm
parents: 73045
diff changeset
   304
    val text_id = Position.copy_id text_pos Position.none;
wenzelm
parents: 73045
diff changeset
   305
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72620
diff changeset
   306
    fun init () = Resources.begin_theory master_dir header parents;
73046
wenzelm
parents: 73045
diff changeset
   307
wenzelm
parents: 73045
diff changeset
   308
    fun excursion () =
wenzelm
parents: 73045
diff changeset
   309
      let
wenzelm
parents: 73045
diff changeset
   310
        fun element_result span_elem (st, _) =
wenzelm
parents: 73045
diff changeset
   311
          let
wenzelm
parents: 73045
diff changeset
   312
            fun prepare span =
wenzelm
parents: 73045
diff changeset
   313
              let
wenzelm
parents: 73045
diff changeset
   314
                val tr =
wenzelm
parents: 73045
diff changeset
   315
                  Position.setmp_thread_data text_id
wenzelm
parents: 73045
diff changeset
   316
                    (fn () => Command.read_span keywords st master_dir init span) ();
wenzelm
parents: 73045
diff changeset
   317
              in Toplevel.timing (Resources.last_timing tr) tr end;
wenzelm
parents: 73045
diff changeset
   318
            val elem = Thy_Element.map_element prepare span_elem;
wenzelm
parents: 73045
diff changeset
   319
            val (results, st') = Toplevel.element_result keywords elem st;
wenzelm
parents: 73045
diff changeset
   320
            val pos' = Toplevel.pos_of (Thy_Element.last_element elem);
wenzelm
parents: 73045
diff changeset
   321
          in (results, (st', pos')) end;
wenzelm
parents: 73045
diff changeset
   322
wenzelm
parents: 73045
diff changeset
   323
        val (results, (end_state, end_pos)) =
76415
f362975e8ba1 clarified signature;
wenzelm
parents: 76405
diff changeset
   324
          fold_map element_result elements (Toplevel.make_state NONE, Position.none);
73046
wenzelm
parents: 73045
diff changeset
   325
wenzelm
parents: 73045
diff changeset
   326
        val thy = Toplevel.end_theory end_pos end_state;
wenzelm
parents: 73045
diff changeset
   327
      in (results, thy) end;
wenzelm
parents: 73045
diff changeset
   328
wenzelm
parents: 73045
diff changeset
   329
    val (results, thy) = cond_timeit true ("theory " ^ quote name) excursion;
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   330
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   331
    fun present () : presentation_context =
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   332
      let
73687
54fe8cc0e1c6 clarified signature: provide access to previous state;
wenzelm
parents: 73559
diff changeset
   333
        val segments =
54fe8cc0e1c6 clarified signature: provide access to previous state;
wenzelm
parents: 73559
diff changeset
   334
          (spans ~~ maps Toplevel.join_results results) |> map (fn (span, (st, tr, st')) =>
54fe8cc0e1c6 clarified signature: provide access to previous state;
wenzelm
parents: 73559
diff changeset
   335
            {span = span, prev_state = st, command = tr, state = st'});
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   336
      in {options = options, file_pos = text_pos, adjust_pos = I, segments = segments} end;
73046
wenzelm
parents: 73045
diff changeset
   337
73798
1ca35197108f more predictable sequential presentation (2f9877db82a1), without somewhat pointless result_ord (e7fab0b5dbe7);
wenzelm
parents: 73785
diff changeset
   338
  in (thy, present) end;
65505
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   339
741fad555d82 exclude theories from other sessions;
wenzelm
parents: 65459
diff changeset
   340
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   341
(* require_thy -- checking database entries wrt. the file-system *)
15065
9feac0b7f935 Some changes to allow qualified theory import.
berghofe
parents: 14981
diff changeset
   342
7211
8e4aa9044599 fixed thy_only;
wenzelm
parents: 7191
diff changeset
   343
local
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   344
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   345
fun required_by _ [] = ""
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   346
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   347
73045
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   348
fun load_thy options initiators deps text (name, header_pos) keywords parents =
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   349
  let
73045
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   350
    val {master = (thy_path, _), imports} = deps;
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   351
    val dir = Path.dir thy_path;
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   352
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   353
    val exec_id = Document_ID.make ();
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   354
    val id = Document_ID.print exec_id;
73045
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   355
    val put_id = Position.put_id id;
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   356
    val _ =
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   357
      Execution.running Document_ID.none exec_id [] orelse
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   358
        raise Fail ("Failed to register execution: " ^ id);
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   359
76884
a004c5322ea4 clarified modules;
wenzelm
parents: 76880
diff changeset
   360
    val text_pos = put_id (Position.file (File.symbolic_path thy_path));
72692
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   361
    val text_props = Position.properties_of text_pos;
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   362
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   363
    val _ = remove_thy name;
22aeec526ffd support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents: 72669
diff changeset
   364
    val _ = writeln ("Loading theory " ^ quote name ^ required_by " " initiators);
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73046
diff changeset
   365
    val _ = Output.try_protocol_message (Markup.loading_theory name @ text_props) [XML.blob [text]];
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   366
73045
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   367
    val _ =
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   368
      Position.setmp_thread_data (Position.id_only id) (fn () =>
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   369
        (parents, map #2 imports) |> ListPair.app (fn (thy, pos) =>
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   370
          Context_Position.reports_global thy [(put_id pos, Theory.get_markup thy)])) ();
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   371
66873
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   372
    val timing_start = Timing.start ();
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   373
73045
1edf30bc1008 proper thread position for reports;
wenzelm
parents: 73044
diff changeset
   374
    val header = Thy_Header.make (name, put_id header_pos) imports keywords;
73798
1ca35197108f more predictable sequential presentation (2f9877db82a1), without somewhat pointless result_ord (e7fab0b5dbe7);
wenzelm
parents: 73785
diff changeset
   375
    val (theory, present) =
72705
0471eb6a4b99 unused;
wenzelm
parents: 72692
diff changeset
   376
      eval_thy options dir header text_pos text
62876
507c90523113 clarified modules -- simplified bootstrap;
wenzelm
parents: 62847
diff changeset
   377
        (if name = Context.PureN then [Context.the_global_context ()] else parents);
66873
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   378
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   379
    val timing_result = Timing.result timing_start;
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   380
    val timing_props = [Markup.theory_timing, (Markup.nameN, name)];
73559
22b5ecb53dd9 more uniform use of Byte_Message;
wenzelm
parents: 73046
diff changeset
   381
    val _ = Output.try_protocol_message (timing_props @ Markup.timing_properties timing_result) []
66873
9953ae603a23 provide theory timing information, similar to command timing but always considered relevant;
wenzelm
parents: 66711
diff changeset
   382
82677
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   383
    fun commit segments =
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   384
      update_thy deps (theory,
c603d41dc183 support for Thy_Info.get_theories_segments, depending on system option "record_theories";
wenzelm
parents: 78757
diff changeset
   385
        if Options.bool options \<^system_option>\<open>record_theories\<close> then segments else []);
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   386
  in Result {theory = theory, exec_id = exec_id, present = SOME o present, commit = commit} end;
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   387
70711
wenzelm
parents: 70015
diff changeset
   388
fun check_thy_deps dir name =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   389
  (case lookup_deps name of
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   390
    SOME NONE => (true, NONE, Position.none, get_imports name, [])
23893
8babfcaaf129 deps: maintain source specification of parents (prevents repeated ThyLoad.deps_thy);
wenzelm
parents: 23886
diff changeset
   391
  | NONE =>
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   392
      let val {master, text, theory_pos, imports, keywords} = Resources.check_thy dir name
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   393
      in (false, SOME (make_deps master imports, text), theory_pos, imports, keywords) end
42129
c17508a61f49 present theory content as future, depending on intermediate proof state futures -- potential to reduce memory requirements and improve parallelization;
wenzelm
parents: 42003
diff changeset
   394
  | SOME (SOME {master, ...}) =>
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   395
      let
48928
698fb0e27b11 more accurate defining position of theory;
wenzelm
parents: 48927
diff changeset
   396
        val {master = master', text = text', theory_pos = theory_pos', imports = imports',
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   397
          keywords = keywords'} = Resources.check_thy dir name;
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   398
        val deps' = SOME (make_deps master' imports', text');
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   399
        val current =
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   400
          #2 master = #2 master' andalso
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   401
            (case lookup_theory name of
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   402
              NONE => false
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   403
            | SOME theory => Resources.loaded_files_current theory);
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   404
      in (current, deps', theory_pos', imports', keywords') end);
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   405
73817
wenzelm
parents: 73800
diff changeset
   406
fun task_finished (Task _) = false
wenzelm
parents: 73800
diff changeset
   407
  | task_finished (Finished _) = true;
wenzelm
parents: 73800
diff changeset
   408
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   409
in
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   410
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72624
diff changeset
   411
fun require_thys options initiators qualifier dir strs tasks =
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72624
diff changeset
   412
      fold_map (require_thy options initiators qualifier dir) strs tasks |>> forall I
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72624
diff changeset
   413
and require_thy options initiators qualifier dir (s, require_pos) tasks =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   414
  let
66711
80fa1401cf76 discontinued extra checks (see ce676a750575 and 60c159d490a2) -- qualified theory names are meant to cover this;
wenzelm
parents: 66377
diff changeset
   415
    val {master_dir, theory_name, ...} = Resources.import_name qualifier dir s;
7066
febce8eee487 require_thy: fixed performance leak;
wenzelm
parents: 6900
diff changeset
   416
  in
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65058
diff changeset
   417
    (case try (String_Graph.get_node tasks) theory_name of
66711
80fa1401cf76 discontinued extra checks (see ce676a750575 and 60c159d490a2) -- qualified theory names are meant to cover this;
wenzelm
parents: 66377
diff changeset
   418
      SOME task => (task_finished task, tasks)
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   419
    | NONE =>
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   420
        let
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65058
diff changeset
   421
          val _ = member (op =) initiators theory_name andalso error (cycle_msg initiators);
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents: 43641
diff changeset
   422
70711
wenzelm
parents: 70015
diff changeset
   423
          val (current, deps, theory_pos, imports, keywords) = check_thy_deps master_dir theory_name
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   424
            handle ERROR msg =>
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   425
              cat_error msg
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65058
diff changeset
   426
                ("The error(s) above occurred for theory " ^ quote theory_name ^
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   427
                  Position.here require_pos ^ required_by "\n" initiators);
37981
9a15982f41fe theory loader: removed obsolete touch/outdate operations (require_thy no longer changes the database implicitly);
wenzelm
parents: 37980
diff changeset
   428
65455
ff09d29498b0 proper qualifier for imports;
wenzelm
parents: 65454
diff changeset
   429
          val qualifier' = Resources.theory_qualifier theory_name;
72511
460d743010bc clarified signature: overloaded "+" for Path.append;
wenzelm
parents: 72311
diff changeset
   430
          val dir' = dir + master_dir_deps (Option.map #1 deps);
65455
ff09d29498b0 proper qualifier for imports;
wenzelm
parents: 65454
diff changeset
   431
ff09d29498b0 proper qualifier for imports;
wenzelm
parents: 65454
diff changeset
   432
          val parents = map (#theory_name o Resources.import_name qualifier' dir' o #1) imports;
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48888
diff changeset
   433
          val (parents_current, tasks') =
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72624
diff changeset
   434
            require_thys options (theory_name :: initiators) qualifier' dir' imports tasks;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   435
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   436
          val all_current = current andalso parents_current;
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   437
          val task =
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65058
diff changeset
   438
            if all_current then Finished (get_theory theory_name)
37977
3ceccd415145 simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents: 37955
diff changeset
   439
            else
37980
b8c3d4dc1e3e avoid repeated File.read for theory text (as before);
wenzelm
parents: 37979
diff changeset
   440
              (case deps of
b8c3d4dc1e3e avoid repeated File.read for theory text (as before);
wenzelm
parents: 37979
diff changeset
   441
                NONE => raise Fail "Malformed deps"
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   442
              | SOME (dep, text) =>
72705
0471eb6a4b99 unused;
wenzelm
parents: 72692
diff changeset
   443
                  Task (parents,
0471eb6a4b99 unused;
wenzelm
parents: 72692
diff changeset
   444
                    load_thy options initiators dep text (theory_name, theory_pos) keywords));
48874
ff9cd47be39b refined Thy_Load.check_thy: find more uses in body text, based on keywords;
wenzelm
parents: 48710
diff changeset
   445
65443
dccbfc715904 provide Resources.import_name in ML, similar to Scala version;
wenzelm
parents: 65058
diff changeset
   446
          val tasks'' = new_entry theory_name parents task tasks';
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48888
diff changeset
   447
        in (all_current, tasks'') end)
7066
febce8eee487 require_thy: fixed performance leak;
wenzelm
parents: 6900
diff changeset
   448
  end;
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6389
diff changeset
   449
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   450
end;
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   451
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   452
65444
1f5821b19741 tuned signature;
wenzelm
parents: 65443
diff changeset
   453
(* use theories *)
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   454
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72624
diff changeset
   455
fun use_theories options qualifier imports =
73818
d67688992bde more uniform schedule_theories, notably for "present" and "commit" phase after loading;
wenzelm
parents: 73817
diff changeset
   456
  schedule_theories (#2 (require_thys options [] qualifier Path.current imports String_Graph.empty));
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   457
65444
1f5821b19741 tuned signature;
wenzelm
parents: 65443
diff changeset
   458
fun use_thy name =
73821
9ead8d9be3ab clarified hook for Mirabelle: provide all loaded theories at once (for each 'theories' section within the session ROOT);
wenzelm
parents: 73819
diff changeset
   459
  ignore (use_theories (Options.default ()) Resources.default_qualifier [(name, Position.none)]);
7211
8e4aa9044599 fixed thy_only;
wenzelm
parents: 7191
diff changeset
   460
59364
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   461
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   462
(* toplevel scripting -- without maintaining database *)
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   463
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
   464
fun script_thy pos txt thy =
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   465
  let
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69886
diff changeset
   466
    val trs = Outer_Syntax.parse_text thy (K thy) pos txt;
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   467
    val end_pos = if null trs then pos else Toplevel.pos_of (List.last trs);
76415
f362975e8ba1 clarified signature;
wenzelm
parents: 76405
diff changeset
   468
    val end_state = fold (Toplevel.command_exception true) trs (Toplevel.make_state NONE);
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   469
  in Toplevel.end_theory end_pos end_state end;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   470
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   471
24080
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   472
(* finish all theories *)
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   473
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
   474
fun finish () = change_thys (String_Graph.map (fn _ => fn (_, entry) => (NONE, entry)));
24080
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   475
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   476
end;
62847
1bd1d8492931 clarified bootstrap;
wenzelm
parents: 61381
diff changeset
   477
65444
1f5821b19741 tuned signature;
wenzelm
parents: 65443
diff changeset
   478
fun use_thy name = Runtime.toplevel_program (fn () => Thy_Info.use_thy name);