src/Pure/Thy/thy_info.ML
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (19 months ago)
changeset 66987 352b23c97ac8
parent 66873 9953ae603a23
child 67163 257bcd20eeec
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
berghofe@3604
     1
(*  Title:      Pure/Thy/thy_info.ML
wenzelm@6211
     2
    Author:     Markus Wenzel, TU Muenchen
berghofe@3604
     3
wenzelm@37978
     4
Global theory info database, with auto-loading according to theory and
wenzelm@15801
     5
file dependencies.
wenzelm@3976
     6
*)
berghofe@3604
     7
berghofe@3604
     8
signature THY_INFO =
berghofe@3604
     9
sig
wenzelm@26614
    10
  val get_names: unit -> string list
wenzelm@7288
    11
  val lookup_theory: string -> theory option
wenzelm@6211
    12
  val get_theory: string -> theory
wenzelm@60937
    13
  val pure_theory: unit -> theory
wenzelm@26983
    14
  val master_directory: string -> Path.T
wenzelm@29421
    15
  val remove_thy: string -> unit
wenzelm@59366
    16
  val use_theories:
wenzelm@61381
    17
    {document: bool,
wenzelm@61381
    18
     symbols: HTML.symbols,
wenzelm@65058
    19
     last_timing: Toplevel.transition -> Time.time,
wenzelm@65445
    20
     qualifier: string,
wenzelm@61381
    21
     master_dir: Path.T} -> (string * Position.T) list -> unit
wenzelm@65444
    22
  val use_thy: string -> unit
wenzelm@58856
    23
  val script_thy: Position.T -> string -> theory -> theory
wenzelm@37954
    24
  val register_thy: theory -> unit
wenzelm@24080
    25
  val finish: unit -> unit
berghofe@3604
    26
end;
berghofe@3604
    27
wenzelm@37216
    28
structure Thy_Info: THY_INFO =
wenzelm@6211
    29
struct
berghofe@3604
    30
wenzelm@6211
    31
(** thy database **)
wenzelm@6211
    32
wenzelm@6211
    33
(* messages *)
wenzelm@6211
    34
wenzelm@55797
    35
val show_path = space_implode " via " o map quote;
wenzelm@6211
    36
wenzelm@55797
    37
fun cycle_msg names = "Cyclic dependency of " ^ show_path names;
berghofe@3604
    38
berghofe@3604
    39
wenzelm@6666
    40
(* derived graph operations *)
berghofe@3604
    41
wenzelm@50862
    42
fun add_deps name parents G = String_Graph.add_deps_acyclic (name, parents) G
wenzelm@50862
    43
  handle String_Graph.CYCLES namess => error (cat_lines (map cycle_msg namess));
berghofe@3604
    44
wenzelm@37984
    45
fun new_entry name parents entry =
wenzelm@50862
    46
  String_Graph.new_node (name, entry) #> add_deps name parents;
wenzelm@37977
    47
berghofe@3604
    48
wenzelm@59178
    49
(* global thys *)
wenzelm@6211
    50
wenzelm@6211
    51
type deps =
wenzelm@41955
    52
 {master: (Path.T * SHA1.digest),  (*master dependencies for thy file*)
wenzelm@48927
    53
  imports: (string * Position.T) list};  (*source specification of imports (partially qualified)*)
wenzelm@23886
    54
wenzelm@38148
    55
fun make_deps master imports : deps = {master = master, imports = imports};
wenzelm@6211
    56
wenzelm@65454
    57
fun master_dir_deps (d: deps option) =
wenzelm@59178
    58
  the_default Path.current (Option.map (Path.dir o #1 o #master) d);
wenzelm@59178
    59
wenzelm@6211
    60
local
wenzelm@59178
    61
  val global_thys =
wenzelm@59178
    62
    Synchronized.var "Thy_Info.thys"
wenzelm@59178
    63
      (String_Graph.empty: (deps option * theory option) String_Graph.T);
wenzelm@6211
    64
in
wenzelm@59178
    65
  fun get_thys () = Synchronized.value global_thys;
wenzelm@59178
    66
  fun change_thys f = Synchronized.change global_thys f;
berghofe@3604
    67
end;
wenzelm@5209
    68
wenzelm@50862
    69
fun get_names () = String_Graph.topological_order (get_thys ());
wenzelm@6211
    70
wenzelm@6211
    71
wenzelm@6211
    72
(* access thy *)
wenzelm@6211
    73
wenzelm@59178
    74
fun lookup thys name = try (String_Graph.get_node thys) name;
wenzelm@59178
    75
fun lookup_thy name = lookup (get_thys ()) name;
wenzelm@7935
    76
wenzelm@59178
    77
fun get thys name =
wenzelm@59178
    78
  (case lookup thys name of
skalberg@15531
    79
    SOME thy => thy
wenzelm@55797
    80
  | NONE => error ("Theory loader: nothing known about theory " ^ quote name));
wenzelm@6211
    81
wenzelm@59178
    82
fun get_thy name = get (get_thys ()) name;
wenzelm@59178
    83
wenzelm@6211
    84
wenzelm@6211
    85
(* access deps *)
wenzelm@6211
    86
skalberg@15570
    87
val lookup_deps = Option.map #1 o lookup_thy;
wenzelm@6211
    88
wenzelm@65454
    89
val master_directory = master_dir_deps o #1 o get_thy;
wenzelm@7191
    90
wenzelm@6211
    91
wenzelm@6211
    92
(* access theory *)
wenzelm@6211
    93
wenzelm@7687
    94
fun lookup_theory name =
wenzelm@7687
    95
  (case lookup_thy name of
wenzelm@38142
    96
    SOME (_, SOME theory) => SOME theory
skalberg@15531
    97
  | _ => NONE);
wenzelm@7288
    98
wenzelm@6211
    99
fun get_theory name =
wenzelm@7288
   100
  (case lookup_theory name of
wenzelm@23871
   101
    SOME theory => theory
wenzelm@55797
   102
  | _ => error ("Theory loader: undefined entry for theory " ^ quote name));
wenzelm@6211
   103
wenzelm@62942
   104
fun pure_theory () = get_theory Context.PureN;
wenzelm@60937
   105
wenzelm@56208
   106
val get_imports = Resources.imports_of o get_theory;
wenzelm@44337
   107
wenzelm@6211
   108
wenzelm@6211
   109
wenzelm@6211
   110
(** thy operations **)
wenzelm@6211
   111
wenzelm@59178
   112
(* remove *)
wenzelm@29421
   113
wenzelm@59178
   114
fun remove name thys =
wenzelm@59178
   115
  (case lookup thys name of
wenzelm@59178
   116
    NONE => thys
wenzelm@59178
   117
  | SOME (NONE, _) => error ("Cannot update finished theory " ^ quote name)
wenzelm@59178
   118
  | SOME _ =>
wenzelm@59178
   119
      let
wenzelm@59178
   120
        val succs = String_Graph.all_succs thys [name];
wenzelm@59178
   121
        val _ = writeln ("Theory loader: removing " ^ commas_quote succs);
wenzelm@59178
   122
      in fold String_Graph.del_node succs thys end);
wenzelm@29421
   123
wenzelm@59178
   124
val remove_thy = change_thys o remove;
wenzelm@59178
   125
wenzelm@7099
   126
wenzelm@59178
   127
(* update *)
wenzelm@59178
   128
wenzelm@59178
   129
fun update deps theory thys =
wenzelm@43640
   130
  let
wenzelm@65459
   131
    val name = Context.theory_long_name theory;
wenzelm@65459
   132
    val parents = map Context.theory_long_name (Theory.parents_of theory);
wenzelm@59178
   133
wenzelm@59178
   134
    val thys' = remove name thys;
wenzelm@59178
   135
    val _ = map (get thys') parents;
wenzelm@59178
   136
  in new_entry name parents (SOME deps, SOME theory) thys' end;
wenzelm@59178
   137
wenzelm@59178
   138
fun update_thy deps theory = change_thys (update deps theory);
wenzelm@43640
   139
wenzelm@7099
   140
wenzelm@24209
   141
(* scheduling loader tasks *)
wenzelm@24209
   142
wenzelm@51330
   143
datatype result =
wenzelm@53375
   144
  Result of {theory: theory, exec_id: Document_ID.exec,
wenzelm@53375
   145
    present: unit -> unit, commit: unit -> unit, weight: int};
wenzelm@51331
   146
wenzelm@53375
   147
fun theory_result theory =
wenzelm@53375
   148
  Result {theory = theory, exec_id = Document_ID.none, present = I, commit = I, weight = 0};
wenzelm@51330
   149
wenzelm@51330
   150
fun result_theory (Result {theory, ...}) = theory;
wenzelm@51331
   151
fun result_present (Result {present, ...}) = present;
wenzelm@51331
   152
fun result_commit (Result {commit, ...}) = commit;
wenzelm@51331
   153
fun result_ord (Result {weight = i, ...}, Result {weight = j, ...}) = int_ord (j, i);
wenzelm@51331
   154
wenzelm@53375
   155
fun join_theory (Result {theory, exec_id, ...}) =
wenzelm@54370
   156
  let
wenzelm@66371
   157
    val _ = Execution.join [exec_id];
wenzelm@64574
   158
    val res = Exn.capture Thm.consolidate_theory theory;
wenzelm@66377
   159
    val exns = maps Task_Queue.group_status (Execution.peek exec_id);
wenzelm@66377
   160
  in res :: map Exn.Exn exns end;
wenzelm@43641
   161
wenzelm@37977
   162
datatype task =
wenzelm@66711
   163
  Task of string list * (theory list -> result) |
wenzelm@37977
   164
  Finished of theory;
wenzelm@37977
   165
wenzelm@37977
   166
fun task_finished (Task _) = false
wenzelm@37977
   167
  | task_finished (Finished _) = true;
wenzelm@24209
   168
wenzelm@44162
   169
fun task_parents deps (parents: string list) = map (the o AList.lookup (op =) deps) parents;
wenzelm@44162
   170
wenzelm@44162
   171
val schedule_seq =
wenzelm@50862
   172
  String_Graph.schedule (fn deps => fn (_, task) =>
wenzelm@44162
   173
    (case task of
wenzelm@66711
   174
      Task (parents, body) =>
wenzelm@51331
   175
        let
wenzelm@51331
   176
          val result = body (task_parents deps parents);
wenzelm@53190
   177
          val _ = Par_Exn.release_all (join_theory result);
wenzelm@51331
   178
          val _ = result_present result ();
wenzelm@51331
   179
          val _ = result_commit result ();
wenzelm@51331
   180
        in result_theory result end
wenzelm@44162
   181
    | Finished thy => thy)) #> ignore;
wenzelm@29000
   182
wenzelm@62923
   183
val schedule_futures = Thread_Attributes.uninterruptible (fn _ => fn tasks =>
wenzelm@51284
   184
  let
wenzelm@51331
   185
    val futures = tasks
wenzelm@51284
   186
      |> String_Graph.schedule (fn deps => fn (name, task) =>
wenzelm@51284
   187
        (case task of
wenzelm@66711
   188
          Task (parents, body) =>
wenzelm@51284
   189
            (singleton o Future.forks)
wenzelm@51284
   190
              {name = "theory:" ^ name, group = NONE,
wenzelm@51284
   191
                deps = map (Future.task_of o #2) deps, pri = 0, interrupts = true}
wenzelm@51284
   192
              (fn () =>
wenzelm@51284
   193
                (case filter (not o can Future.join o #2) deps of
wenzelm@51330
   194
                  [] => body (map (result_theory o Future.join) (task_parents deps parents))
wenzelm@51284
   195
                | bad =>
wenzelm@55797
   196
                    error
wenzelm@55797
   197
                      ("Failed to load theory " ^ quote name ^
wenzelm@55797
   198
                        " (unresolved " ^ commas_quote (map #1 bad) ^ ")")))
wenzelm@51331
   199
        | Finished theory => Future.value (theory_result theory)));
wenzelm@51331
   200
wenzelm@51331
   201
    val results1 = futures
wenzelm@51331
   202
      |> maps (fn future =>
wenzelm@51331
   203
          (case Future.join_result future of
wenzelm@53190
   204
            Exn.Res result => join_theory result
wenzelm@51331
   205
          | Exn.Exn exn => [Exn.Exn exn]));
wenzelm@51331
   206
wenzelm@51331
   207
    val results2 = futures
wenzelm@51331
   208
      |> map_filter (Exn.get_res o Future.join_result)
wenzelm@51331
   209
      |> sort result_ord
wenzelm@51331
   210
      |> Par_List.map (fn result => Exn.capture (result_present result) ());
wenzelm@51331
   211
wenzelm@51331
   212
    (* FIXME more precise commit order (!?) *)
wenzelm@51331
   213
    val results3 = futures
wenzelm@51331
   214
      |> map (fn future => Exn.capture (fn () => result_commit (Future.join future) ()) ());
wenzelm@51284
   215
wenzelm@54370
   216
    (* FIXME avoid global Execution.reset (!??) *)
wenzelm@53192
   217
    val results4 = map Exn.Exn (maps Task_Queue.group_status (Execution.reset ()));
wenzelm@51284
   218
wenzelm@51331
   219
    val _ = Par_Exn.release_all (results1 @ results2 @ results3 @ results4);
wenzelm@51284
   220
  in () end);
wenzelm@29429
   221
wenzelm@24209
   222
wenzelm@65505
   223
(* eval theory *)
wenzelm@65505
   224
wenzelm@65505
   225
fun excursion keywords master_dir last_timing init elements =
wenzelm@65505
   226
  let
wenzelm@65505
   227
    fun prepare_span st span =
wenzelm@65505
   228
      Command_Span.content span
wenzelm@65505
   229
      |> Command.read keywords (Command.read_thy st) master_dir init ([], ~1)
wenzelm@65505
   230
      |> (fn tr => Toplevel.put_timing (last_timing tr) tr);
wenzelm@65505
   231
wenzelm@65505
   232
    fun element_result span_elem (st, _) =
wenzelm@65505
   233
      let
wenzelm@65505
   234
        val elem = Thy_Syntax.map_element (prepare_span st) span_elem;
wenzelm@65505
   235
        val (results, st') = Toplevel.element_result keywords elem st;
wenzelm@65505
   236
        val pos' = Toplevel.pos_of (Thy_Syntax.last_element elem);
wenzelm@65505
   237
      in (results, (st', pos')) end;
wenzelm@65505
   238
wenzelm@65505
   239
    val (results, (end_state, end_pos)) =
wenzelm@65505
   240
      fold_map element_result elements (Toplevel.toplevel, Position.none);
wenzelm@65505
   241
wenzelm@65505
   242
    val thy = Toplevel.end_theory end_pos end_state;
wenzelm@65505
   243
  in (results, thy) end;
wenzelm@65505
   244
wenzelm@65505
   245
fun eval_thy document symbols last_timing update_time master_dir header text_pos text parents =
wenzelm@65505
   246
  let
wenzelm@65505
   247
    val (name, _) = #name header;
wenzelm@65505
   248
    val keywords =
wenzelm@65505
   249
      fold (curry Keyword.merge_keywords o Thy_Header.get_keywords) parents
wenzelm@65505
   250
        (Keyword.add_keywords (#keywords header) Keyword.empty_keywords);
wenzelm@65505
   251
wenzelm@65505
   252
    val toks = Token.explode keywords text_pos text;
wenzelm@65505
   253
    val spans = Outer_Syntax.parse_spans toks;
wenzelm@65505
   254
    val elements = Thy_Syntax.parse_elements keywords spans;
wenzelm@65505
   255
wenzelm@65505
   256
    fun init () =
wenzelm@65505
   257
      Resources.begin_theory master_dir header parents
wenzelm@65505
   258
      |> Present.begin_theory update_time
wenzelm@65505
   259
        (fn () => implode (map (HTML.present_span symbols keywords) spans));
wenzelm@65505
   260
wenzelm@65505
   261
    val (results, thy) =
wenzelm@65505
   262
      cond_timeit true ("theory " ^ quote name)
wenzelm@65505
   263
        (fn () => excursion keywords master_dir last_timing init elements);
wenzelm@65505
   264
wenzelm@65505
   265
    fun present () =
wenzelm@65505
   266
      let
wenzelm@65505
   267
        val res = filter_out (Toplevel.is_ignored o #1) (maps Toplevel.join_results results);
wenzelm@65505
   268
      in
wenzelm@65505
   269
        if exists (Toplevel.is_skipped_proof o #2) res then
wenzelm@65505
   270
          warning ("Cannot present theory with skipped proofs: " ^ quote name)
wenzelm@65505
   271
        else
wenzelm@65505
   272
          let val tex_source = Thy_Output.present_thy thy res toks |> Buffer.content;
wenzelm@65505
   273
          in if document then Present.theory_output thy tex_source else () end
wenzelm@65505
   274
      end;
wenzelm@65505
   275
wenzelm@65505
   276
  in (thy, present, size text) end;
wenzelm@65505
   277
wenzelm@65505
   278
wenzelm@23967
   279
(* require_thy -- checking database entries wrt. the file-system *)
berghofe@15065
   280
wenzelm@7211
   281
local
wenzelm@6211
   282
wenzelm@37977
   283
fun required_by _ [] = ""
wenzelm@37977
   284
  | required_by s initiators = s ^ "(required by " ^ show_path (rev initiators) ^ ")";
wenzelm@37977
   285
wenzelm@61381
   286
fun load_thy document symbols last_timing
wenzelm@61381
   287
    initiators update_time deps text (name, pos) keywords parents =
wenzelm@37977
   288
  let
wenzelm@59178
   289
    val _ = remove_thy name;
wenzelm@58843
   290
    val _ = writeln ("Loading theory " ^ quote name ^ required_by " " initiators);
wenzelm@56333
   291
    val _ = Output.try_protocol_message (Markup.loading_theory name) [];
wenzelm@37977
   292
wenzelm@41548
   293
    val {master = (thy_path, _), imports} = deps;
wenzelm@38134
   294
    val dir = Path.dir thy_path;
wenzelm@51294
   295
    val header = Thy_Header.make (name, pos) imports keywords;
wenzelm@48927
   296
wenzelm@48927
   297
    val _ = Position.reports (map #2 imports ~~ map Theory.get_markup parents);
wenzelm@37977
   298
wenzelm@53375
   299
    val exec_id = Document_ID.make ();
wenzelm@53375
   300
    val _ =
wenzelm@53375
   301
      Execution.running Document_ID.none exec_id [] orelse
wenzelm@53375
   302
        raise Fail ("Failed to register execution: " ^ Document_ID.print exec_id);
wenzelm@53375
   303
wenzelm@66873
   304
    val timing_start = Timing.start ();
wenzelm@66873
   305
wenzelm@53375
   306
    val text_pos = Position.put_id (Document_ID.print exec_id) (Path.position thy_path);
wenzelm@51331
   307
    val (theory, present, weight) =
wenzelm@65505
   308
      eval_thy document symbols last_timing update_time dir header text_pos text
wenzelm@62876
   309
        (if name = Context.PureN then [Context.the_global_context ()] else parents);
wenzelm@66873
   310
wenzelm@66873
   311
    val timing_result = Timing.result timing_start;
wenzelm@66873
   312
    val timing_props = [Markup.theory_timing, (Markup.nameN, name)];
wenzelm@66873
   313
    val _  = Output.try_protocol_message (timing_props @ Markup.timing_properties timing_result) []
wenzelm@66873
   314
wenzelm@43640
   315
    fun commit () = update_thy deps theory;
wenzelm@53375
   316
  in
wenzelm@53375
   317
    Result {theory = theory, exec_id = exec_id, present = present, commit = commit, weight = weight}
wenzelm@53375
   318
  end;
wenzelm@37977
   319
wenzelm@48898
   320
fun check_deps dir name =
wenzelm@6211
   321
  (case lookup_deps name of
wenzelm@51293
   322
    SOME NONE => (true, NONE, Position.none, get_imports name, [])
wenzelm@23893
   323
  | NONE =>
wenzelm@56208
   324
      let val {master, text, theory_pos, imports, keywords} = Resources.check_thy dir name
wenzelm@51293
   325
      in (false, SOME (make_deps master imports, text), theory_pos, imports, keywords) end
wenzelm@42129
   326
  | SOME (SOME {master, ...}) =>
wenzelm@42003
   327
      let
wenzelm@48928
   328
        val {master = master', text = text', theory_pos = theory_pos', imports = imports',
wenzelm@56208
   329
          keywords = keywords'} = Resources.check_thy dir name;
wenzelm@42003
   330
        val deps' = SOME (make_deps master' imports', text');
wenzelm@42003
   331
        val current =
wenzelm@42003
   332
          #2 master = #2 master' andalso
wenzelm@42003
   333
            (case lookup_theory name of
wenzelm@42003
   334
              NONE => false
wenzelm@56208
   335
            | SOME theory => Resources.loaded_files_current theory);
wenzelm@51293
   336
      in (current, deps', theory_pos', imports', keywords') end);
wenzelm@6211
   337
wenzelm@23967
   338
in
wenzelm@23967
   339
wenzelm@65445
   340
fun require_thys document symbols last_timing initiators qualifier dir strs tasks =
wenzelm@65445
   341
      fold_map (require_thy document symbols last_timing initiators qualifier dir) strs tasks
wenzelm@65445
   342
      |>> forall I
wenzelm@65454
   343
and require_thy document symbols last_timing initiators qualifier dir (s, require_pos) tasks =
wenzelm@6211
   344
  let
wenzelm@66711
   345
    val {master_dir, theory_name, ...} = Resources.import_name qualifier dir s;
wenzelm@7066
   346
  in
wenzelm@65443
   347
    (case try (String_Graph.get_node tasks) theory_name of
wenzelm@66711
   348
      SOME task => (task_finished task, tasks)
wenzelm@23967
   349
    | NONE =>
wenzelm@23967
   350
        let
wenzelm@65443
   351
          val _ = member (op =) initiators theory_name andalso error (cycle_msg initiators);
wenzelm@43651
   352
wenzelm@65454
   353
          val (current, deps, theory_pos, imports, keywords) = check_deps master_dir theory_name
wenzelm@55797
   354
            handle ERROR msg =>
wenzelm@55797
   355
              cat_error msg
wenzelm@65443
   356
                ("The error(s) above occurred for theory " ^ quote theory_name ^
wenzelm@55797
   357
                  Position.here require_pos ^ required_by "\n" initiators);
wenzelm@37981
   358
wenzelm@65455
   359
          val qualifier' = Resources.theory_qualifier theory_name;
wenzelm@65455
   360
          val dir' = Path.append dir (master_dir_deps (Option.map #1 deps));
wenzelm@65455
   361
wenzelm@65455
   362
          val parents = map (#theory_name o Resources.import_name qualifier' dir' o #1) imports;
wenzelm@48898
   363
          val (parents_current, tasks') =
wenzelm@65443
   364
            require_thys document symbols last_timing (theory_name :: initiators)
wenzelm@65455
   365
              qualifier' dir' imports tasks;
wenzelm@6211
   366
wenzelm@23967
   367
          val all_current = current andalso parents_current;
wenzelm@37977
   368
          val task =
wenzelm@65443
   369
            if all_current then Finished (get_theory theory_name)
wenzelm@37977
   370
            else
wenzelm@37980
   371
              (case deps of
wenzelm@37980
   372
                NONE => raise Fail "Malformed deps"
wenzelm@42003
   373
              | SOME (dep, text) =>
wenzelm@48876
   374
                  let
wenzelm@48876
   375
                    val update_time = serial ();
wenzelm@48928
   376
                    val load =
wenzelm@61381
   377
                      load_thy document symbols last_timing initiators update_time dep
wenzelm@65443
   378
                        text (theory_name, theory_pos) keywords;
wenzelm@66711
   379
                  in Task (parents, load) end);
wenzelm@48874
   380
wenzelm@65443
   381
          val tasks'' = new_entry theory_name parents task tasks';
wenzelm@48898
   382
        in (all_current, tasks'') end)
wenzelm@7066
   383
  end;
wenzelm@6484
   384
wenzelm@23967
   385
end;
wenzelm@23967
   386
wenzelm@23967
   387
wenzelm@65444
   388
(* use theories *)
wenzelm@23967
   389
wenzelm@65445
   390
fun use_theories {document, symbols, last_timing, qualifier, master_dir} imports =
wenzelm@65445
   391
  let
wenzelm@65445
   392
    val (_, tasks) =
wenzelm@65445
   393
      require_thys document symbols last_timing [] qualifier master_dir imports String_Graph.empty;
wenzelm@66368
   394
  in if Multithreading.enabled () then schedule_futures tasks else schedule_seq tasks end;
wenzelm@23967
   395
wenzelm@65444
   396
fun use_thy name =
wenzelm@61381
   397
  use_theories
wenzelm@65058
   398
    {document = false, symbols = HTML.no_symbols, last_timing = K Time.zeroTime,
wenzelm@65532
   399
     qualifier = Resources.default_qualifier, master_dir = Path.current}
wenzelm@65444
   400
    [(name, Position.none)];
wenzelm@7211
   401
wenzelm@59364
   402
wenzelm@57626
   403
(* toplevel scripting -- without maintaining database *)
wenzelm@57626
   404
wenzelm@58856
   405
fun script_thy pos txt thy =
wenzelm@57626
   406
  let
wenzelm@58856
   407
    val trs =
wenzelm@58928
   408
      Outer_Syntax.parse thy pos txt
wenzelm@58856
   409
      |> map (Toplevel.modify_init (K thy));
wenzelm@57626
   410
    val end_pos = if null trs then pos else Toplevel.pos_of (List.last trs);
wenzelm@57626
   411
    val end_state = fold (Toplevel.command_exception true) trs Toplevel.toplevel;
wenzelm@57626
   412
  in Toplevel.end_theory end_pos end_state end;
wenzelm@6211
   413
wenzelm@6211
   414
wenzelm@37977
   415
(* register theory *)
wenzelm@6211
   416
wenzelm@37954
   417
fun register_thy theory =
wenzelm@6211
   418
  let
wenzelm@65459
   419
    val name = Context.theory_long_name theory;
wenzelm@56208
   420
    val {master, ...} = Resources.check_thy (Resources.master_directory theory) name;
wenzelm@56208
   421
    val imports = Resources.imports_of theory;
wenzelm@6211
   422
  in
wenzelm@59178
   423
    change_thys (fn thys =>
wenzelm@59178
   424
      let
wenzelm@59178
   425
        val thys' = remove name thys;
wenzelm@59178
   426
        val _ = writeln ("Registering theory " ^ quote name);
wenzelm@59178
   427
      in update (make_deps master imports) theory thys' end)
wenzelm@6211
   428
  end;
wenzelm@6211
   429
wenzelm@24080
   430
wenzelm@24080
   431
(* finish all theories *)
wenzelm@24080
   432
wenzelm@50862
   433
fun finish () = change_thys (String_Graph.map (fn _ => fn (_, entry) => (NONE, entry)));
wenzelm@24080
   434
wenzelm@6211
   435
end;
wenzelm@62847
   436
wenzelm@65444
   437
fun use_thy name = Runtime.toplevel_program (fn () => Thy_Info.use_thy name);