src/Pure/System/session.ML
author wenzelm
Thu, 24 Jan 2013 17:18:13 +0100
changeset 52013 630c0895d9d1
parent 51796 a0f22c2d60cc
child 52413 05522141d244
permissions -rw-r--r--
more efficient inlined properties, especially relevant for voluminous tasks trace;
     1 (*  Title:      Pure/System/session.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Session management -- maintain state of logic images.
     5 *)
     6 
     7 signature SESSION =
     8 sig
     9   val id: unit -> string list
    10   val name: unit -> string
    11   val path: unit -> string list
    12   val welcome: unit -> string
    13   val finish: unit -> unit
    14   val init: bool -> bool -> bool -> string -> string -> bool -> string -> (string * string) list ->
    15     string -> string -> bool * string -> string -> bool -> unit
    16   val with_timing: string -> bool -> ('a -> 'b) -> 'a -> 'b
    17   val use_dir: string -> string -> bool -> string list -> bool -> bool -> string ->
    18     string -> bool -> string list -> string -> string -> bool * string ->
    19     string -> int -> bool -> bool -> int -> int -> int -> int -> unit
    20 end;
    21 
    22 structure Session: SESSION =
    23 struct
    24 
    25 (* session state *)
    26 
    27 val session = Unsynchronized.ref ([Context.PureN]: string list);
    28 val session_finished = Unsynchronized.ref false;
    29 
    30 fun id () = ! session;
    31 fun name () = "Isabelle/" ^ List.last (! session);
    32 
    33 
    34 (* access path *)
    35 
    36 val session_path = Unsynchronized.ref ([]: string list);
    37 val remote_path = Unsynchronized.ref (NONE: Url.T option);
    38 
    39 fun path () = ! session_path;
    40 
    41 
    42 (* welcome *)
    43 
    44 fun welcome () =
    45   if Distribution.is_official then
    46     "Welcome to " ^ name () ^ " (" ^ Distribution.version ^ ")"
    47   else "Unofficial version of " ^ name () ^ " (" ^ Distribution.version ^ ")";
    48 
    49 
    50 (* add_path *)
    51 
    52 fun add_path reset s =
    53   let val sess = ! session @ [s] in
    54     (case duplicates (op =) sess of
    55       [] => (session := sess; session_path := ((if reset then [] else ! session_path) @ [s]))
    56     | dups => error ("Duplicate session identifiers " ^ commas_quote dups))
    57   end;
    58 
    59 
    60 (* init_name *)
    61 
    62 fun init_name reset parent name =
    63   if not (member (op =) (! session) parent) orelse not (! session_finished) then
    64     error ("Unfinished parent session " ^ quote parent ^ " for " ^ quote name)
    65   else (add_path reset name; session_finished := false);
    66 
    67 
    68 (* finish *)
    69 
    70 fun finish_futures () =
    71   (case map_filter Task_Queue.group_status (Goal.reset_futures ()) of
    72     [] => ()
    73   | exns => raise Par_Exn.make exns);
    74 
    75 fun finish () =
    76  (Future.shutdown ();
    77   finish_futures ();
    78   Thy_Info.finish ();
    79   Present.finish ();
    80   Keyword.status ();
    81   Outer_Syntax.check_syntax ();
    82   Options.reset_default ();
    83   Future.shutdown ();
    84   session_finished := true);
    85 
    86 
    87 (* use_dir *)
    88 
    89 fun with_timing name verbose f x =
    90   let
    91     val start = Timing.start ();
    92     val y = f x;
    93     val timing = Timing.result start;
    94 
    95     val threads = string_of_int (Multithreading.max_threads_value ());
    96     val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
    97       |> Real.fmt (StringCvt.FIX (SOME 2));
    98 
    99     val timing_props =
   100       [("threads", threads)] @ Markup.timing_properties timing @ [("factor", factor)];
   101     val _ = writeln ("\fTiming = " ^ YXML.string_of_body (XML.Encode.properties timing_props));
   102     val _ =
   103       if verbose then
   104         Output.physical_stderr ("Timing " ^ name ^ " (" ^
   105           threads ^ " threads, " ^ Timing.message timing ^ ", factor " ^ factor ^ ")\n")
   106       else ();
   107   in y end;
   108 
   109 fun get_rpath rpath =
   110   (if rpath = "" then () else
   111      if is_some (! remote_path) then
   112        error "Path for remote theory browsing information may only be set once"
   113      else
   114        remote_path := SOME (Url.explode rpath);
   115    (! remote_path, rpath <> ""));
   116 
   117 fun init build reset info info_path doc doc_graph doc_output doc_variants
   118     parent name doc_dump rpath verbose =
   119  (init_name reset parent name;
   120   Present.init build info info_path (if doc = "false" then "" else doc) doc_graph doc_output
   121     doc_variants (path ()) name doc_dump (get_rpath rpath) verbose
   122     (map Thy_Info.get_theory (Thy_Info.get_names ())));
   123 
   124 local
   125 
   126 fun read_variants strs =
   127   rev (distinct (eq_fst (op =)) (rev (("document", "") :: map Present.read_variant strs)))
   128   |> filter_out (fn (_, s) => s = "-");
   129 
   130 in
   131 
   132 fun use_dir item root build modes reset info info_path doc doc_graph doc_variants parent
   133     name doc_dump rpath level timing verbose max_threads trace_threads
   134     parallel_proofs parallel_proofs_threshold =
   135   ((fn () =>
   136     let
   137       val _ =
   138         Output.physical_stderr
   139           "### Legacy feature: old \"isabelle usedir\" -- use \"isabelle build\" instead!\n";
   140       val _ =
   141         init build reset info info_path doc doc_graph "" (read_variants doc_variants) parent name
   142           doc_dump rpath verbose;
   143       val res1 = (use |> with_timing item timing |> Exn.capture) root;
   144       val res2 = Exn.capture finish ();
   145     in ignore (Par_Exn.release_all [res1, res2]) end)
   146     |> Unsynchronized.setmp Proofterm.proofs level
   147     |> Unsynchronized.setmp print_mode (modes @ print_mode_value ())
   148     |> Unsynchronized.setmp Goal.parallel_proofs parallel_proofs
   149     |> Unsynchronized.setmp Goal.parallel_proofs_threshold parallel_proofs_threshold
   150     |> Unsynchronized.setmp Multithreading.trace trace_threads
   151     |> Unsynchronized.setmp Multithreading.max_threads
   152       (if Multithreading.available then max_threads
   153        else (if max_threads = 1 then () else warning "Multithreading support unavailable"; 1))) ()
   154   handle exn => (Output.error_msg (ML_Compiler.exn_message exn); exit 1);
   155 
   156 end;
   157 
   158 end;