src/Pure/Thy/thy_info.ML
author wenzelm
Wed, 14 Jan 2015 14:28:52 +0100
changeset 59364 3b5da177ae6b
parent 59362 41f1645a4f63
child 59366 e94df7f6b608
permissions -rw-r--r--
clarified build_theories;
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
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
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
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15633
diff changeset
     5
file dependencies.
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
26614
1f09a22a1027 export get_names (formerly names);
wenzelm
parents: 26494
diff changeset
    10
  val get_names: unit -> string list
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
    11
  val lookup_theory: string -> theory option
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    12
  val get_theory: string -> theory
26983
e40f28cdd19b exported master_directory;
wenzelm
parents: 26881
diff changeset
    13
  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
    14
  val remove_thy: string -> unit
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
    15
  val use_thys: (string * Position.T) list -> unit
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
    16
  val use_thy: string * Position.T -> unit
59364
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
    17
  val build_theories: (Toplevel.transition -> Time.time option) -> Path.T ->
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
    18
    Options.T * (string * Position.T) list -> unit
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
    19
  val script_thy: Position.T -> string -> theory -> theory
37954
a2e73df0b1e0 simplified/clarified register_thy: more precise treatment of new dependencies, remove descendants;
wenzelm
parents: 37953
diff changeset
    20
  val register_thy: theory -> unit
24080
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
    21
  val finish: unit -> unit
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    22
end;
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    23
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36953
diff changeset
    24
structure Thy_Info: THY_INFO =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    25
struct
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    26
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    27
(** thy database **)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    28
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    29
(* messages *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    30
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
    31
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
    32
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
    33
fun cycle_msg names = "Cyclic dependency of " ^ show_path names;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    34
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    35
6666
be06ed5d0b4c cleaned comments;
wenzelm
parents: 6654
diff changeset
    36
(* derived graph operations *)
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    37
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
    38
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
    39
  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
    40
37984
f26352bd82cf clarified register_thy: clean slate via kill_thy, more precise CRITICAL section;
wenzelm
parents: 37981
diff changeset
    41
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
    42
  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
    43
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    44
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    45
(* global thys *)
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    46
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    47
type deps =
41955
703ea96b13c6 files are identified via SHA1 digests -- discontinued ISABELLE_FILE_IDENT;
wenzelm
parents: 41678
diff changeset
    48
 {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
    49
  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
    50
38148
d2f3c8d4a89f uniform naming of imports (source specification) vs. parents (thy node names) vs. parent_thys (theory values);
wenzelm
parents: 38147
diff changeset
    51
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
    52
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    53
fun master_dir (d: deps option) =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    54
  the_default Path.current (Option.map (Path.dir o #1 o #master) d);
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    55
44225
a8f921e6484f more robust Thy_Header.base_name, with minimal assumptions about path syntax;
wenzelm
parents: 44222
diff changeset
    56
fun base_name s = Path.implode (Path.base (Path.explode s));
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
    57
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    58
local
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    59
  val global_thys =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    60
    Synchronized.var "Thy_Info.thys"
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    61
      (String_Graph.empty: (deps option * theory option) String_Graph.T);
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    62
in
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    63
  fun get_thys () = Synchronized.value global_thys;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    64
  fun change_thys f = Synchronized.change global_thys f;
3604
6bf9f09f3d61 Moved functions for theory information storage / retrieval
berghofe
parents:
diff changeset
    65
end;
5209
a69fe5a61b6c theory_of renamed to theory (and made public);
wenzelm
parents: 4975
diff changeset
    66
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
    67
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
    68
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    69
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    70
(* access thy *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    71
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    72
fun lookup thys name = try (String_Graph.get_node thys) name;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    73
fun lookup_thy name = lookup (get_thys ()) name;
7935
ac62465ed06c added check_known_thy, if_known_thy;
wenzelm
parents: 7910
diff changeset
    74
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    75
fun get thys name =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    76
  (case lookup thys name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15158
diff changeset
    77
    SOME thy => thy
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
    78
  | 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
    79
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    80
fun get_thy name = get (get_thys ()) name;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    81
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    82
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    83
(* access deps *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    84
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    85
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
    86
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
    87
val master_directory = master_dir o #1 o get_thy;
7191
fcce2387ad6d added pretend_use;
wenzelm
parents: 7099
diff changeset
    88
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    89
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    90
(* access theory *)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    91
7687
3383b8223e46 fixed lookup_theory;
wenzelm
parents: 7589
diff changeset
    92
fun lookup_theory name =
3383b8223e46 fixed lookup_theory;
wenzelm
parents: 7589
diff changeset
    93
  (case lookup_thy name of
38142
c202426474c3 more precise CRITICAL sections;
wenzelm
parents: 38141
diff changeset
    94
    SOME (_, SOME theory) => SOME theory
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15158
diff changeset
    95
  | _ => NONE);
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
    96
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
    97
fun get_theory name =
7288
21ff5bb68a5c lookup_theory;
wenzelm
parents: 7267
diff changeset
    98
  (case lookup_theory name of
23871
0dd5696f58b6 removed obsolete use/update_thy_only;
wenzelm
parents: 22215
diff changeset
    99
    SOME theory => theory
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   100
  | _ => 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
   101
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   102
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
   103
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   104
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   105
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   106
(** thy operations **)
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   107
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   108
(* remove *)
29421
db532e37cda2 use_thys: perform consolidate_thy on loaded theories, which removes failed nodes in post-hoc fashion;
wenzelm
parents: 29118
diff changeset
   109
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   110
fun remove name thys =
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   111
  (case lookup thys name of
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   112
    NONE => thys
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   113
  | SOME (NONE, _) => error ("Cannot update finished theory " ^ quote name)
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   114
  | SOME _ =>
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   115
      let
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   116
        val succs = String_Graph.all_succs thys [name];
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   117
        val _ = writeln ("Theory loader: removing " ^ commas_quote succs);
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   118
      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
   119
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   120
val remove_thy = change_thys o remove;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   121
7099
8142ccd13963 added update_thy_only;
wenzelm
parents: 7066
diff changeset
   122
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   123
(* update *)
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   124
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   125
fun update deps theory thys =
43640
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   126
  let
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   127
    val name = Context.theory_name theory;
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   128
    val parents = map Context.theory_name (Theory.parents_of theory);
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   129
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   130
    val thys' = remove name thys;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   131
    val _ = map (get thys') parents;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   132
  in new_entry name parents (SOME deps, SOME theory) thys' end;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   133
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   134
fun update_thy deps theory = change_thys (update deps theory);
43640
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   135
7099
8142ccd13963 added update_thy_only;
wenzelm
parents: 7066
diff changeset
   136
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   137
(* scheduling loader tasks *)
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   138
51330
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   139
datatype result =
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   140
  Result of {theory: theory, exec_id: Document_ID.exec,
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   141
    present: unit -> unit, commit: unit -> unit, weight: int};
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   142
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   143
fun theory_result theory =
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   144
  Result {theory = theory, exec_id = Document_ID.none, present = I, commit = I, weight = 0};
51330
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   145
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   146
fun result_theory (Result {theory, ...}) = theory;
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   147
fun result_present (Result {present, ...}) = present;
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   148
fun result_commit (Result {commit, ...}) = commit;
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   149
fun result_ord (Result {weight = i, ...}, Result {weight = j, ...}) = int_ord (j, i);
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   150
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   151
fun join_theory (Result {theory, exec_id, ...}) =
54370
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   152
  let
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   153
    (*toplevel proofs and diags*)
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   154
    val _ = Future.join_tasks (maps Future.group_snapshot (Execution.peek exec_id));
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   155
    (*fully nested proofs*)
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   156
    val res = Exn.capture Thm.join_theory_proofs theory;
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   157
  in res :: map Exn.Exn (maps Task_Queue.group_status (Execution.peek exec_id)) end;
43641
081e009549dc uniform finish_thy -- always Global_Theory.join_proofs, even with sequential scheduling;
wenzelm
parents: 43640
diff changeset
   158
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
   159
datatype task =
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   160
  Task of Path.T * string list * (theory list -> result) |
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
   161
  Finished of theory;
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
   162
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
   163
fun task_finished (Task _) = false
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
   164
  | task_finished (Finished _) = true;
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   165
44162
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   166
fun task_parents deps (parents: string list) = map (the o AList.lookup (op =) deps) parents;
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   167
29118
8f2481aa363d removed old scheduler;
wenzelm
parents: 29088
diff changeset
   168
local
8f2481aa363d removed old scheduler;
wenzelm
parents: 29088
diff changeset
   169
44162
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   170
val schedule_seq =
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
   171
  String_Graph.schedule (fn deps => fn (_, task) =>
44162
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   172
    (case task of
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   173
      Task (_, parents, body) =>
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   174
        let
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   175
          val result = body (task_parents deps parents);
53190
5d92649a310e simplified Goal.forked_proofs: status is determined via group instead of dummy future (see also Pure/PIDE/execution.ML);
wenzelm
parents: 51661
diff changeset
   176
          val _ = Par_Exn.release_all (join_theory result);
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   177
          val _ = result_present result ();
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   178
          val _ = result_commit result ();
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   179
        in result_theory result end
44162
5434899d955c general Graph.schedule;
wenzelm
parents: 44113
diff changeset
   180
    | Finished thy => thy)) #> ignore;
29000
5e03bc760355 improved future_schedule: more robust handling of failed parents (explicit join), final join of all futures, including Exn.release_all;
wenzelm
parents: 28980
diff changeset
   181
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   182
val schedule_futures = uninterruptible (fn _ => fn tasks =>
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   183
  let
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   184
    val futures = tasks
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   185
      |> String_Graph.schedule (fn deps => fn (name, task) =>
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   186
        (case task of
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   187
          Task (_, parents, body) =>
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   188
            (singleton o Future.forks)
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   189
              {name = "theory:" ^ name, group = NONE,
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   190
                deps = map (Future.task_of o #2) deps, pri = 0, interrupts = true}
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   191
              (fn () =>
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   192
                (case filter (not o can Future.join o #2) deps of
51330
f249bd08d851 more explicit datatype result;
wenzelm
parents: 51294
diff changeset
   193
                  [] => body (map (result_theory o Future.join) (task_parents deps parents))
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   194
                | bad =>
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   195
                    error
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   196
                      ("Failed to load theory " ^ quote name ^
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   197
                        " (unresolved " ^ commas_quote (map #1 bad) ^ ")")))
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   198
        | Finished theory => Future.value (theory_result theory)));
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   199
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   200
    val results1 = futures
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   201
      |> maps (fn future =>
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   202
          (case Future.join_result future of
53190
5d92649a310e simplified Goal.forked_proofs: status is determined via group instead of dummy future (see also Pure/PIDE/execution.ML);
wenzelm
parents: 51661
diff changeset
   203
            Exn.Res result => join_theory result
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   204
          | Exn.Exn exn => [Exn.Exn exn]));
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   205
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   206
    val results2 = futures
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   207
      |> map_filter (Exn.get_res o Future.join_result)
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   208
      |> sort result_ord
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   209
      |> Par_List.map (fn result => Exn.capture (result_present result) ());
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   210
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   211
    (* FIXME more precise commit order (!?) *)
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   212
    val results3 = futures
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   213
      |> map (fn future => Exn.capture (fn () => result_commit (Future.join future) ()) ());
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   214
54370
39ac1a02c60c join all theory body forks, notably Toplevel.atom_result (diagnostic commands), before peeking at full status;
wenzelm
parents: 53375
diff changeset
   215
    (* FIXME avoid global Execution.reset (!??) *)
53192
04df1d236e1c maintain goal forks as part of global execution;
wenzelm
parents: 53190
diff changeset
   216
    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
   217
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   218
    val _ = Par_Exn.release_all (results1 @ results2 @ results3 @ results4);
51284
59a03019f3bf fork diagnostic commands (theory loader and PIDE interaction);
wenzelm
parents: 51228
diff changeset
   219
  in () end);
29429
a6c641f08af7 schedule_futures: tuned final consolidation, explicit after_load phase;
wenzelm
parents: 29421
diff changeset
   220
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   221
in
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   222
32794
7b100d30eb32 eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   223
fun schedule_tasks tasks =
59180
c0fa3b3bdabd discontinued central critical sections: NAMED_CRITICAL / CRITICAL;
wenzelm
parents: 59178
diff changeset
   224
  if Multithreading.enabled () then schedule_futures tasks else schedule_seq tasks;
24209
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   225
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   226
end;
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   227
8a2c8d623e43 schedule: misc cleanup, more precise task model;
wenzelm
parents: 24190
diff changeset
   228
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   229
(* require_thy -- checking database entries wrt. the file-system *)
15065
9feac0b7f935 Some changes to allow qualified theory import.
berghofe
parents: 14981
diff changeset
   230
7211
8e4aa9044599 fixed thy_only;
wenzelm
parents: 7191
diff changeset
   231
local
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   232
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
   233
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
   234
  | 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
   235
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   236
fun load_thy document last_timing initiators update_time deps text (name, 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
   237
  let
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   238
    val _ = remove_thy name;
58843
521cea5fa777 discontinued obsolete Output.urgent_message;
wenzelm
parents: 57918
diff changeset
   239
    val _ = writeln ("Loading theory " ^ quote name ^ required_by " " initiators);
56333
38f1422ef473 support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
wenzelm
parents: 56208
diff changeset
   240
    val _ = Output.try_protocol_message (Markup.loading_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
   241
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41536
diff changeset
   242
    val {master = (thy_path, _), imports} = deps;
38134
3de75ca6f166 load_thy: refer to physical master directory (not accumulated source import directory) and enable loading files relatively to that;
wenzelm
parents: 37985
diff changeset
   243
    val dir = Path.dir thy_path;
51294
0850d43cb355 discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents: 51293
diff changeset
   244
    val header = Thy_Header.make (name, pos) imports keywords;
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
   245
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
   246
    val _ = Position.reports (map #2 imports ~~ map Theory.get_markup 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
   247
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   248
    val exec_id = Document_ID.make ();
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   249
    val _ =
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   250
      Execution.running Document_ID.none exec_id [] orelse
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   251
        raise Fail ("Failed to register execution: " ^ Document_ID.print exec_id);
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   252
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   253
    val text_pos = Position.put_id (Document_ID.print exec_id) (Path.position thy_path);
51331
e7fab0b5dbe7 join all proofs before scheduling present phase (ordered according to weight);
wenzelm
parents: 51330
diff changeset
   254
    val (theory, present, weight) =
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   255
      Resources.load_thy document last_timing update_time dir header text_pos text
48638
22d65e375c01 more standard bootstrapping of Pure.thy;
wenzelm
parents: 46958
diff changeset
   256
        (if name = Context.PureN then [ML_Context.the_global_context ()] else parents);
43640
f57bcfb54808 misc tuning;
wenzelm
parents: 42129
diff changeset
   257
    fun commit () = update_thy deps theory;
53375
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   258
  in
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   259
    Result {theory = theory, exec_id = exec_id, present = present, commit = commit, weight = weight}
78693e46a237 Execution.fork formally requires registered Execution.running;
wenzelm
parents: 53192
diff changeset
   260
  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
   261
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48888
diff changeset
   262
fun check_deps dir name =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   263
  (case lookup_deps name of
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   264
    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
   265
  | NONE =>
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   266
      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
   267
      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
   268
  | SOME (SOME {master, ...}) =>
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   269
      let
48928
698fb0e27b11 more accurate defining position of theory;
wenzelm
parents: 48927
diff changeset
   270
        val {master = master', text = text', theory_pos = theory_pos', imports = imports',
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   271
          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
   272
        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
   273
        val current =
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   274
          #2 master = #2 master' andalso
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   275
            (case lookup_theory name of
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   276
              NONE => false
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   277
            | SOME theory => Resources.loaded_files_current theory);
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   278
      in (current, deps', theory_pos', imports', keywords') end);
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   279
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   280
in
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   281
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   282
fun require_thys document last_timing initiators dir strs tasks =
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   283
      fold_map (require_thy document last_timing initiators dir) strs tasks |>> forall I
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   284
and require_thy document last_timing initiators dir (str, require_pos) tasks =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   285
  let
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20664
diff changeset
   286
    val path = Path.expand (Path.explode str);
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20664
diff changeset
   287
    val name = Path.implode (Path.base path);
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   288
    val node_name = File.full_path dir (Resources.thy_path path);
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   289
    fun check_entry (Task (node_name', _, _)) =
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   290
          if node_name = node_name' then ()
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   291
          else
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   292
            error ("Incoherent imports for theory " ^ quote name ^
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   293
              Position.here require_pos ^ ":\n" ^
55488
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 55461
diff changeset
   294
              "  " ^ Path.print node_name ^ "\n" ^
60c159d490a2 more integrity checks of theory names vs. full node names;
wenzelm
parents: 55461
diff changeset
   295
              "  " ^ Path.print node_name')
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   296
      | check_entry _ = ();
7066
febce8eee487 require_thy: fixed performance leak;
wenzelm
parents: 6900
diff changeset
   297
  in
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
   298
    (case try (String_Graph.get_node tasks) name of
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   299
      SOME task => (check_entry task; (task_finished task, tasks))
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   300
    | NONE =>
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   301
        let
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents: 43641
diff changeset
   302
          val dir' = Path.append dir (Path.dir path);
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents: 43641
diff changeset
   303
          val _ = member (op =) initiators name andalso error (cycle_msg initiators);
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents: 43641
diff changeset
   304
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   305
          val (current, deps, theory_pos, imports, keywords) = check_deps dir' name
55797
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   306
            handle ERROR msg =>
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   307
              cat_error msg
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   308
                ("The error(s) above occurred for theory " ^ quote name ^
6a59b4bb7506 tuned errors -- in accordance to Scala version;
wenzelm
parents: 55488
diff changeset
   309
                  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
   310
48927
ef462b5558eb theory def/ref position reports, which enable hyperlinks etc.;
wenzelm
parents: 48898
diff changeset
   311
          val parents = map (base_name 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
   312
          val (parents_current, tasks') =
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   313
            require_thys document last_timing (name :: initiators)
48898
9fc880720663 simplified Thy_Load.check_thy (again) -- no need to pass keywords nor find files in body text;
wenzelm
parents: 48888
diff changeset
   314
              (Path.append dir (master_dir (Option.map #1 deps))) imports tasks;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   315
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   316
          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
   317
          val task =
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
   318
            if all_current then Finished (get_theory name)
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
   319
            else
37980
b8c3d4dc1e3e avoid repeated File.read for theory text (as before);
wenzelm
parents: 37979
diff changeset
   320
              (case deps of
b8c3d4dc1e3e avoid repeated File.read for theory text (as before);
wenzelm
parents: 37979
diff changeset
   321
                NONE => raise Fail "Malformed deps"
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 41955
diff changeset
   322
              | SOME (dep, text) =>
48876
157dd47032e0 more standard Thy_Load.check_thy for Pure.thy, relying on its header;
wenzelm
parents: 48874
diff changeset
   323
                  let
157dd47032e0 more standard Thy_Load.check_thy for Pure.thy, relying on its header;
wenzelm
parents: 48874
diff changeset
   324
                    val update_time = serial ();
48928
698fb0e27b11 more accurate defining position of theory;
wenzelm
parents: 48927
diff changeset
   325
                    val load =
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   326
                      load_thy document last_timing initiators update_time dep
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 51284
diff changeset
   327
                        text (name, theory_pos) keywords;
55461
ce676a750575 more integrity checks of theory names vs. full node names -- at least for the scope of a single use_thys (or "theories" section in ROOT);
wenzelm
parents: 54722
diff changeset
   328
                  in Task (node_name, parents, load) end);
48874
ff9cd47be39b refined Thy_Load.check_thy: find more uses in body text, based on keywords;
wenzelm
parents: 48710
diff changeset
   329
ff9cd47be39b refined Thy_Load.check_thy: find more uses in body text, based on keywords;
wenzelm
parents: 48710
diff changeset
   330
          val tasks'' = new_entry 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
   331
        in (all_current, tasks'') end)
7066
febce8eee487 require_thy: fixed performance leak;
wenzelm
parents: 6900
diff changeset
   332
  end;
6484
3f098b0ec683 use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents: 6389
diff changeset
   333
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   334
end;
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   335
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   336
37979
0f21ebea4a73 tuned messages and comments;
wenzelm
parents: 37978
diff changeset
   337
(* use_thy *)
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   338
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   339
fun use_theories {document, last_timing, master_dir} imports =
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   340
  schedule_tasks (snd (require_thys document last_timing [] master_dir imports String_Graph.empty));
23967
92130b24e87f require_thy: explicit tasks graph;
wenzelm
parents: 23939
diff changeset
   341
54458
96ccc8972fc7 prefer explicit "document" flag -- eliminated stateful Present.no_document;
wenzelm
parents: 54446
diff changeset
   342
val use_thys = use_theories {document = false, last_timing = K NONE, master_dir = Path.current};
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37942
diff changeset
   343
val use_thy = use_thys o single;
7211
8e4aa9044599 fixed thy_only;
wenzelm
parents: 7191
diff changeset
   344
59364
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   345
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   346
(* theories in batch build *)
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   347
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   348
fun build_theories last_timing master_dir (options, thys) =
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   349
  let
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   350
    val condition = space_explode "," (Options.string options "condition");
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   351
    val conds = filter_out (can getenv_strict) condition;
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   352
  in
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   353
    if null conds then
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   354
      (Options.set_default options;
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   355
        (use_theories {
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   356
          document = Present.document_enabled (Options.string options "document"),
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   357
          last_timing = last_timing,
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   358
          master_dir = master_dir}
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   359
        |> Unsynchronized.setmp print_mode
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   360
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   361
        |> Unsynchronized.setmp Goal.parallel_proofs (Options.int options "parallel_proofs")
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   362
        |> Unsynchronized.setmp Multithreading.trace (Options.int options "threads_trace")
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   363
        |> Multithreading.max_threads_setmp (Options.int options "threads")
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   364
        |> Unsynchronized.setmp Future.ML_statistics true) thys)
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   365
    else
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   366
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   367
        " (undefined " ^ commas conds ^ ")\n")
3b5da177ae6b clarified build_theories;
wenzelm
parents: 59362
diff changeset
   368
  end;
59362
41f1645a4f63 some support for PIDE batch session;
wenzelm
parents: 59180
diff changeset
   369
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   370
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   371
(* toplevel scripting -- without maintaining database *)
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   372
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
   373
fun script_thy pos txt thy =
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   374
  let
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
   375
    val trs =
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58856
diff changeset
   376
      Outer_Syntax.parse thy pos txt
58856
4fced55fc5b7 provide explicit theory (amending 621c052789b4);
wenzelm
parents: 58852
diff changeset
   377
      |> map (Toplevel.modify_init (K thy));
57626
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   378
    val end_pos = if null trs then pos else Toplevel.pos_of (List.last trs);
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   379
    val end_state = fold (Toplevel.command_exception true) trs Toplevel.toplevel;
2288a6f17930 more official Thy_Info.script_thy;
wenzelm
parents: 56333
diff changeset
   380
  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
   381
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   382
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
   383
(* register theory *)
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   384
37954
a2e73df0b1e0 simplified/clarified register_thy: more precise treatment of new dependencies, remove descendants;
wenzelm
parents: 37953
diff changeset
   385
fun register_thy theory =
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   386
  let
16454
af39c6510b86 removed obsolete theory_of_sign, theory_of_thm;
wenzelm
parents: 16047
diff changeset
   387
    val name = Context.theory_name theory;
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   388
    val {master, ...} = Resources.check_thy (Resources.master_directory theory) name;
06cc31dff138 clarifed module name;
wenzelm
parents: 55797
diff changeset
   389
    val imports = Resources.imports_of theory;
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   390
  in
59178
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   391
    change_thys (fn thys =>
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   392
      let
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   393
        val thys' = remove name thys;
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   394
        val _ = writeln ("Registering theory " ^ quote name);
e819a6683f87 proper Synchronized.var;
wenzelm
parents: 59177
diff changeset
   395
      in update (make_deps master imports) theory thys' end)
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   396
  end;
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   397
24080
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   398
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   399
(* finish all theories *)
8c67d869531b added register_thy (replaces pretend_use_thy_only and really flag);
wenzelm
parents: 24071
diff changeset
   400
50862
5fc8b83322f5 more sensible order of theory nodes (correspondance to Scala version), e.g. relevant to theory progress;
wenzelm
parents: 50845
diff changeset
   401
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
   402
6211
43d0efafa025 Theory loader database: theory and file dependencies, theory values
wenzelm
parents: 5211
diff changeset
   403
end;