src/Pure/System/session.ML
author wenzelm
Thu, 26 Jul 2012 16:54:44 +0200
changeset 49533 0c86acc069ad
parent 49531 c5d0f19ef7cb
child 49557 0a5f598cacec
permissions -rw-r--r--
proper arguments for old usedir;
     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 welcome: unit -> string
    12   val finish: unit -> unit
    13   val init: bool -> bool -> bool -> string -> string -> bool -> string list ->
    14     string -> string -> string * string -> string -> bool -> unit
    15   val with_timing: string -> bool -> ('a -> 'b) -> 'a -> 'b
    16   val use_dir: string -> string -> bool -> string list -> bool -> bool -> string ->
    17     string -> bool -> string list -> string -> string -> bool * string ->
    18     string -> int -> bool -> bool -> int -> int -> int -> int -> unit
    19 end;
    20 
    21 structure Session: SESSION =
    22 struct
    23 
    24 
    25 (* session state *)
    26 
    27 val session = Unsynchronized.ref ([Context.PureN]: string list);
    28 val session_path = Unsynchronized.ref ([]: string list);
    29 val session_finished = Unsynchronized.ref false;
    30 val remote_path = Unsynchronized.ref (NONE: Url.T option);
    31 
    32 
    33 (* access path *)
    34 
    35 fun id () = ! session;
    36 fun path () = ! session_path;
    37 
    38 fun str_of [] = Context.PureN
    39   | str_of elems = space_implode "/" elems;
    40 
    41 fun name () = "Isabelle/" ^ str_of (path ());
    42 
    43 
    44 (* welcome *)
    45 
    46 fun welcome () =
    47   if Distribution.is_official then
    48     "Welcome to " ^ name () ^ " (" ^ Distribution.version ^ ")"
    49   else
    50     "Unofficial version of " ^ name () ^ " (" ^ Distribution.version ^ ")" ^
    51     (if Distribution.changelog <> "" then "\nSee also " ^ Distribution.changelog else "");
    52 
    53 val _ =
    54   Outer_Syntax.improper_command ("welcome", Keyword.diag) "print welcome message"
    55     (Scan.succeed (Toplevel.no_timing o Toplevel.imperative (writeln o welcome)));
    56 
    57 
    58 (* add_path *)
    59 
    60 fun add_path reset s =
    61   let val sess = ! session @ [s] in
    62     (case duplicates (op =) sess of
    63       [] => (session := sess; session_path := ((if reset then [] else ! session_path) @ [s]))
    64     | dups => error ("Duplicate session identifiers " ^ commas_quote dups ^ " in " ^ str_of sess))
    65   end;
    66 
    67 
    68 (* init_name *)
    69 
    70 fun init_name reset parent name =
    71   if not (member (op =) (! session) parent) orelse not (! session_finished) then
    72     error ("Unfinished parent session " ^ quote parent ^ " for " ^ quote name)
    73   else (add_path reset name; session_finished := false);
    74 
    75 
    76 (* finish *)
    77 
    78 fun finish () =
    79   (Thy_Info.finish ();
    80     Present.finish ();
    81     Outer_Syntax.check_syntax ();
    82     Future.shutdown ();
    83     session_finished := true);
    84 
    85 
    86 (* use_dir *)
    87 
    88 fun with_timing _ false f x = f x
    89   | with_timing item true f x =
    90       let
    91         val start = Timing.start ();
    92         val y = f x;
    93         val timing = Timing.result start;
    94         val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
    95           |> Real.fmt (StringCvt.FIX (SOME 2));
    96         val _ =
    97           Output.physical_stderr ("Timing " ^ item ^ " (" ^
    98             string_of_int (Multithreading.max_threads_value ()) ^ " threads, " ^
    99             Timing.message timing ^ ", factor " ^ factor ^ ")\n");
   100       in y end;
   101 
   102 fun get_rpath rpath =
   103   (if rpath = "" then () else
   104      if is_some (! remote_path) then
   105        error "Path for remote theory browsing information may only be set once"
   106      else
   107        remote_path := SOME (Url.explode rpath);
   108    (! remote_path, rpath <> ""));
   109 
   110 fun init build reset info info_path doc doc_graph doc_variants parent name doc_dump rpath verbose =
   111  (init_name reset parent name;
   112   Present.init build info info_path (if doc = "false" then "" else doc) doc_graph doc_variants
   113     (path ()) name doc_dump (get_rpath rpath) verbose
   114     (map Thy_Info.get_theory (Thy_Info.get_names ())));
   115 
   116 local
   117 
   118 fun doc_dump (cp, dump) = (dump, if cp then "all" else "tex+sty");
   119 
   120 in
   121 
   122 fun use_dir item root build modes reset info info_path doc doc_graph doc_variants parent
   123     name dump rpath level timing verbose max_threads trace_threads
   124     parallel_proofs parallel_proofs_threshold =
   125   ((fn () =>
   126      (init build reset info info_path doc doc_graph doc_variants parent name
   127         (doc_dump dump) rpath verbose;
   128       with_timing item timing use root;
   129       finish ()))
   130     |> Unsynchronized.setmp Proofterm.proofs level
   131     |> Unsynchronized.setmp print_mode (modes @ print_mode_value ())
   132     |> Unsynchronized.setmp Goal.parallel_proofs parallel_proofs
   133     |> Unsynchronized.setmp Goal.parallel_proofs_threshold parallel_proofs_threshold
   134     |> Unsynchronized.setmp Multithreading.trace trace_threads
   135     |> Unsynchronized.setmp Multithreading.max_threads
   136       (if Multithreading.available then max_threads
   137        else (if max_threads = 1 then () else warning "Multithreading support unavailable"; 1))) ()
   138   handle exn => (Output.error_msg (ML_Compiler.exn_message exn); exit 1);
   139 
   140 end;
   141 
   142 end;