src/Pure/Isar/session.ML
author wenzelm
Thu Aug 30 22:50:01 2001 +0200 (2001-08-30)
changeset 11509 d54301357129
parent 10937 5651e0636e38
child 11527 4db3876f1224
permissions -rw-r--r--
export name;
wenzelm@6346
     1
(*  Title:      Pure/Isar/session.ML
wenzelm@6346
     2
    ID:         $Id$
wenzelm@6346
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8807
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@6346
     5
wenzelm@6346
     6
Session management -- maintain state of logic images.
wenzelm@6346
     7
*)
wenzelm@6346
     8
wenzelm@6346
     9
signature SESSION =
wenzelm@6346
    10
sig
wenzelm@11509
    11
  val name: unit -> string
wenzelm@6346
    12
  val welcome: unit -> string
wenzelm@10571
    13
  val use_dir:
wenzelm@10571
    14
    string list -> bool -> bool -> string -> string -> string -> string -> string -> unit
wenzelm@6346
    15
  val finish: unit -> unit
wenzelm@6346
    16
end;
wenzelm@6346
    17
wenzelm@6346
    18
structure Session: SESSION =
wenzelm@6346
    19
struct
wenzelm@6346
    20
wenzelm@6346
    21
wenzelm@6346
    22
(* session state *)
wenzelm@6346
    23
wenzelm@6346
    24
val pure = "Pure";
wenzelm@6346
    25
wenzelm@6346
    26
val session = ref ([pure]: string list);
wenzelm@6346
    27
val session_path = ref ([]: string list);
wenzelm@6346
    28
val session_finished = ref false;
wenzelm@9414
    29
val rpath = ref (None: Url.T option);
wenzelm@9414
    30
wenzelm@9414
    31
wenzelm@9414
    32
(* access path *)
wenzelm@6346
    33
wenzelm@6346
    34
fun path () = ! session_path;
wenzelm@6346
    35
wenzelm@10937
    36
fun str_of [] = pure
wenzelm@6346
    37
  | str_of elems = space_implode "/" elems;
wenzelm@6346
    38
wenzelm@11509
    39
fun name () = "Isabelle/" ^ str_of (path ());
wenzelm@11509
    40
fun welcome () = "Welcome to " ^ name () ^ " (" ^ version ^ ")";
wenzelm@6346
    41
wenzelm@6346
    42
wenzelm@9414
    43
(* add_path *)
wenzelm@9414
    44
wenzelm@9414
    45
fun add_path reset s =
wenzelm@9414
    46
  let val sess = ! session @ [s] in
wenzelm@9414
    47
    (case Library.duplicates sess of
wenzelm@9414
    48
      [] => (session := sess; session_path := ((if reset then [] else ! session_path) @ [s]))
wenzelm@9414
    49
    | dups => error ("Duplicate session identifiers " ^ commas_quote dups ^ " in " ^ str_of sess))
wenzelm@9414
    50
  end;
wenzelm@9414
    51
wenzelm@9414
    52
wenzelm@6346
    53
(* init *)
wenzelm@6346
    54
wenzelm@6346
    55
fun init reset parent name =
wenzelm@6346
    56
  if not (parent mem_string (! session)) orelse not (! session_finished) then
wenzelm@6346
    57
    error ("Unfinished parent session " ^ quote parent ^ " for " ^ quote name)
wenzelm@6346
    58
  else (add_path reset name; session_finished := false);
wenzelm@6346
    59
wenzelm@6346
    60
wenzelm@6346
    61
(* finish *)
wenzelm@6346
    62
wenzelm@6346
    63
fun finish () =
wenzelm@6663
    64
  (ThyInfo.finish ();
wenzelm@6346
    65
    Present.finish ();
wenzelm@6346
    66
    session_finished := true);
wenzelm@6346
    67
wenzelm@6346
    68
wenzelm@6346
    69
(* use_dir *)
wenzelm@6346
    70
wenzelm@6346
    71
val root_file = ThyLoad.ml_path "ROOT";
wenzelm@6346
    72
berghofe@7236
    73
fun get_rpath rpath_str =
berghofe@7236
    74
  (if rpath_str = "" then () else
berghofe@7227
    75
     if is_some (!rpath) then
berghofe@7227
    76
       error "Path for remote theory browsing information may only be set once"
berghofe@7227
    77
     else
berghofe@7227
    78
       rpath := Some (Url.unpack rpath_str);
berghofe@7236
    79
   (!rpath, rpath_str <> ""));
berghofe@7236
    80
wenzelm@10571
    81
fun use_dir modes reset info doc parent name dump rpath_str =
wenzelm@10571
    82
  Library.setmp print_mode (modes @ ! print_mode) (fn () =>
wenzelm@10571
    83
    (init reset parent name;
wenzelm@10571
    84
     Present.init info doc (path ()) name
wenzelm@10571
    85
       (if dump = "" then None else Some (Path.unpack dump)) (get_rpath rpath_str);
wenzelm@10571
    86
     File.symbol_use root_file;
wenzelm@10571
    87
     finish ())) ()
wenzelm@10571
    88
  handle exn => (writeln (Toplevel.exn_message exn); exit 1);
wenzelm@6346
    89
wenzelm@6346
    90
wenzelm@6346
    91
end;