src/Pure/Thy/thy_load.ML
author wenzelm
Mon, 20 Aug 2012 21:52:31 +0200
changeset 48868 aeea516440c8
parent 48867 e9beabf045ab
child 48869 4371a8d029f2
permissions -rw-r--r--
more robust cleaning of "% tag" and "-- cmt";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_load.ML
42002
wenzelm
parents: 41955
diff changeset
     2
    Author:     Makarius
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     3
42002
wenzelm
parents: 41955
diff changeset
     4
Loading files that contribute to a theory.  Global master path.
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     5
*)
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     6
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     7
signature THY_LOAD =
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
     8
sig
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
     9
  val master_directory: theory -> Path.T
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    10
  val imports_of: theory -> string list
41955
703ea96b13c6 files are identified via SHA1 digests -- discontinued ISABELLE_FILE_IDENT;
wenzelm
parents: 41944
diff changeset
    11
  val provide: Path.T * (Path.T * SHA1.digest) -> theory -> theory
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    12
  val check_file: Path.T -> Path.T -> Path.T
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    13
  val thy_path: Path.T -> Path.T
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    14
  val check_thy: Path.T -> string ->
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    15
    {master: Path.T * SHA1.digest, text: string, imports: string list, uses: (Path.T * bool) list}
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
    16
  val load_file: theory -> Path.T -> (Path.T * SHA1.digest) * string
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
    17
  val use_file: Path.T -> theory -> string * theory
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    18
  val loaded_files: theory -> Path.T list
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    19
  val all_current: theory -> bool
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    20
  val use_ml: Path.T -> unit
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    21
  val exec_ml: Path.T -> generic_theory -> generic_theory
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    22
  val begin_theory: Path.T -> Thy_Header.header -> theory list -> theory
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    23
  val load_thy: int -> Path.T -> Thy_Header.header -> Position.T -> string ->
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    24
    theory list -> theory * unit future
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
    25
  val resolve_files: Path.T -> Thy_Syntax.span -> Thy_Syntax.span
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
    26
  val parse_files: string -> (theory -> Token.files) parser
42002
wenzelm
parents: 41955
diff changeset
    27
  val set_master_path: Path.T -> unit
wenzelm
parents: 41955
diff changeset
    28
  val get_master_path: unit -> Path.T
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    29
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    30
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 33221
diff changeset
    31
structure Thy_Load: THY_LOAD =
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    32
struct
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    33
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    34
(* manage source files *)
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    35
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    36
type files =
40741
17d6293a1e26 moved file identification to thy_load.ML (where it is actually used);
wenzelm
parents: 40625
diff changeset
    37
 {master_dir: Path.T,  (*master directory of theory source*)
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    38
  imports: string list,  (*source specification of imports*)
40741
17d6293a1e26 moved file identification to thy_load.ML (where it is actually used);
wenzelm
parents: 40625
diff changeset
    39
  required: Path.T list,  (*source path*)
41955
703ea96b13c6 files are identified via SHA1 digests -- discontinued ISABELLE_FILE_IDENT;
wenzelm
parents: 41944
diff changeset
    40
  provided: (Path.T * (Path.T * SHA1.digest)) list};  (*source path, physical path, digest*)
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    41
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    42
fun make_files (master_dir, imports, required, provided): files =
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    43
 {master_dir = master_dir, imports = imports, required = required, provided = provided};
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    44
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    45
structure Files = Theory_Data
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    46
(
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    47
  type T = files;
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    48
  val empty = make_files (Path.current, [], [], []);
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    49
  fun extend _ = empty;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    50
  fun merge _ = empty;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    51
);
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    52
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    53
fun map_files f =
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    54
  Files.map (fn {master_dir, imports, required, provided} =>
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    55
    make_files (f (master_dir, imports, required, provided)));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    56
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    57
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    58
val master_directory = #master_dir o Files.get;
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    59
val imports_of = #imports o Files.get;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    60
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    61
fun put_deps dir imports = map_files (fn _ => (dir, imports, [], []));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    62
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    63
fun require src_path =
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    64
  map_files (fn (master_dir, imports, required, provided) =>
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    65
    if member (op =) required src_path then
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 41886
diff changeset
    66
      error ("Duplicate source file dependency: " ^ Path.print src_path)
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    67
    else (master_dir, imports, src_path :: required, provided));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    68
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37977
diff changeset
    69
fun provide (src_path, path_id) =
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    70
  map_files (fn (master_dir, imports, required, provided) =>
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    71
    if AList.defined (op =) provided src_path then
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 41886
diff changeset
    72
      error ("Duplicate resolution of source file dependency: " ^ Path.print src_path)
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
    73
    else (master_dir, imports, required, (src_path, path_id) :: provided));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    74
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    75
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    76
(* check files *)
6232
4336add1c251 include full paths in file info;
wenzelm
parents: 6219
diff changeset
    77
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    78
fun check_file dir file = File.check_file (File.full_path dir file);
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    79
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    80
val thy_path = Path.ext "thy";
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    81
41886
aa8dce9ab8a9 discontinued legacy load path;
wenzelm
parents: 41548
diff changeset
    82
fun check_thy dir name =
23872
f449381e2caa tuned signature;
wenzelm
parents: 22145
diff changeset
    83
  let
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    84
    val path = thy_path (Path.basic name);
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    85
    val master_file = check_file dir path;
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    86
    val text = File.read master_file;
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    87
    val (name', imports, uses) =
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    88
      if name = Context.PureN then (Context.PureN, [], [])
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    89
      else
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    90
        let val {name, imports, uses, ...} = Thy_Header.read (Path.position master_file) text
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
    91
        in (name, imports, uses) end;
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    92
    val _ = name <> name' andalso
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44478
diff changeset
    93
      error ("Bad file name " ^ Path.print path ^ " for theory " ^ quote name');
42003
6e45dc518ebb replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents: 42002
diff changeset
    94
  in {master = (master_file, SHA1.digest text), text = text, imports = imports, uses = uses} end;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
    95
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
    96
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
    97
(* load files *)
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
    98
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
    99
fun load_file thy src_path =
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   100
  let
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   101
    val full_path = check_file (master_directory thy) src_path;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   102
    val text = File.read full_path;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   103
    val id = SHA1.digest text;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   104
  in ((full_path, id), text) end;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   105
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   106
fun use_file src_path thy =
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   107
  let
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   108
    val (path_id, text) = load_file thy src_path;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   109
    val thy' = provide (src_path, path_id) thy;
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   110
  in (text, thy') end;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   111
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   112
val loaded_files = map (#1 o #2) o #provided o Files.get;
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   113
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   114
fun check_loaded thy =
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   115
  let
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   116
    val {required, provided, ...} = Files.get thy;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   117
    val provided_paths = map #1 provided;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   118
    val _ =
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   119
      (case subtract (op =) provided_paths required of
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   120
        [] => NONE
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 41886
diff changeset
   121
      | bad => error ("Pending source file dependencies: " ^ commas (map Path.print (rev bad))));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   122
    val _ =
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   123
      (case subtract (op =) required provided_paths of
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   124
        [] => NONE
41944
b97091ae583a Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
wenzelm
parents: 41886
diff changeset
   125
      | bad => error ("Undeclared source file dependencies: " ^ commas (map Path.print (rev bad))));
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   126
  in () end;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   127
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   128
fun all_current thy =
37939
965537d86fcc discontinued special treatment of ML files -- no longer complete extensions on demand;
wenzelm
parents: 37938
diff changeset
   129
  let
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   130
    val {provided, ...} = Files.get thy;
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37977
diff changeset
   131
    fun current (src_path, (_, id)) =
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   132
      (case try (load_file thy) src_path of
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   133
        NONE => false
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   134
      | SOME ((_, id'), _) => id = id');
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   135
  in can check_loaded thy andalso forall current provided end;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   136
37952
f6c40213675b Thy_Load.check_loaded via Theory.at_end;
wenzelm
parents: 37950
diff changeset
   137
val _ = Context.>> (Context.map_theory (Theory.at_end (fn thy => (check_loaded thy; NONE))));
f6c40213675b Thy_Load.check_loaded via Theory.at_end;
wenzelm
parents: 37950
diff changeset
   138
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   139
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   140
(* provide files *)
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   141
43700
e4ece46a9ca7 clarified Thy_Load.digest_file -- read ML files only once;
wenzelm
parents: 42003
diff changeset
   142
fun eval_file path text = ML_Context.eval_text true (Path.position path) text;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   143
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   144
fun use_ml src_path =
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   145
  if is_none (Context.thread_data ()) then
43700
e4ece46a9ca7 clarified Thy_Load.digest_file -- read ML files only once;
wenzelm
parents: 42003
diff changeset
   146
    let val path = check_file Path.current src_path
e4ece46a9ca7 clarified Thy_Load.digest_file -- read ML files only once;
wenzelm
parents: 42003
diff changeset
   147
    in eval_file path (File.read path) end
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   148
  else
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   149
    let
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   150
      val thy = ML_Context.the_global_context ();
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   151
43702
24fb44c1086a more abstract Thy_Load.load_file/use_file for external theory resources;
wenzelm
parents: 43700
diff changeset
   152
      val ((path, id), text) = load_file thy src_path;
43700
e4ece46a9ca7 clarified Thy_Load.digest_file -- read ML files only once;
wenzelm
parents: 42003
diff changeset
   153
      val _ = eval_file path text;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   154
      val _ = Context.>> Local_Theory.propagate_ml_env;
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   155
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37977
diff changeset
   156
      val provide = provide (src_path, (path, id));
38757
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38133
diff changeset
   157
      val _ = Context.>> (Context.mapping provide (Local_Theory.background_theory provide));
37978
548f3f165d05 simplified Thy_Header.read -- include Source.of_string_limited here;
wenzelm
parents: 37977
diff changeset
   158
    in () end;
37949
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   159
48a874444164 moved management of auxiliary theory source files to Thy_Load -- as theory data instead of accidental loader state;
wenzelm
parents: 37943
diff changeset
   160
fun exec_ml src_path = ML_Context.exec (fn () => use_ml src_path);
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   161
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: 37952
diff changeset
   162
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   163
(* inlined files *)
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   164
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   165
fun read_files cmd dir tok =
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   166
  let
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   167
    val path = Path.explode (Token.content_of tok);
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   168
    val exts = Keyword.command_files cmd;
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   169
    val variants =
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   170
      if null exts then [path]
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   171
      else map (fn ext => Path.ext ext path) exts;
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   172
  in (dir, variants |> map (Path.append dir #> (fn p => (File.read p, Path.position p)))) end;
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   173
48868
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   174
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   175
fun clean ((i1, t1) :: (i2, t2) :: toks) =
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   176
      if Token.keyword_with (fn s => s = "%" orelse s = "--") t1 then clean toks
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   177
      else (i1, t1) :: clean ((i2, t2) :: toks)
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   178
  | clean toks = toks;
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   179
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   180
fun clean_tokens toks =
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   181
  ((0 upto length toks - 1) ~~ toks)
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   182
  |> filter (fn (_, tok) => Token.is_proper tok) |> clean;
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   183
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   184
fun resolve_files dir (span as Thy_Syntax.Span (Thy_Syntax.Command cmd, toks)) =
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   185
      if Keyword.is_theory_load cmd then
48868
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   186
        (case find_first (Token.is_name o #2) (rev (clean_tokens toks)) of
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   187
          NONE => span
aeea516440c8 more robust cleaning of "% tag" and "-- cmt";
wenzelm
parents: 48867
diff changeset
   188
        | SOME (i, _) =>
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   189
            let
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   190
              val toks' = toks |> map_index (fn (j, tok) =>
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   191
                if i = j then Token.put_files (read_files cmd dir tok) tok
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   192
                else tok);
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   193
            in Thy_Syntax.Span (Thy_Syntax.Command cmd, toks') end)
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   194
      else span
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   195
  | resolve_files _ span = span;
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   196
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   197
fun parse_files cmd =
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   198
  Scan.ahead Parse.not_eof -- Parse.group (fn () => "file name/path specification") Parse.name
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   199
    >> (fn (tok, name) => fn thy =>
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   200
      (case Token.get_files tok of
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   201
        SOME files => files
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   202
      | NONE =>
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   203
          (warning ("Dynamic loading of files: " ^ quote name ^
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   204
              Position.str_of (Token.position_of tok));
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   205
            read_files cmd (master_directory thy) tok)));
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   206
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   207
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   208
(* load_thy *)
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: 37952
diff changeset
   209
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
   210
fun begin_theory dir {name, imports, keywords, uses} 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: 37952
diff changeset
   211
  Theory.begin_theory name parents
41548
bd0bebf93fa6 Thy_Load.begin_theory: maintain source specification of imports;
wenzelm
parents: 41414
diff changeset
   212
  |> put_deps dir imports
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
   213
  |> fold Thy_Header.declare_keyword keywords
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: 37952
diff changeset
   214
  |> fold (require o fst) uses
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: 37952
diff changeset
   215
  |> fold (fn (path, true) => Context.theory_map (exec_ml path) o Theory.checkpoint | _ => I) uses
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: 37952
diff changeset
   216
  |> Theory.checkpoint;
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: 37952
diff changeset
   217
46959
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   218
fun excursion init elements =
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   219
  let
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   220
    val immediate = not (Goal.future_enabled ());
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   221
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   222
    fun proof_result (tr, trs) (st, _) =
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   223
      let
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   224
        val (result, st') = Toplevel.proof_result immediate (tr, trs) st;
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   225
        val pos' = Toplevel.pos_of (List.last (tr :: trs));
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   226
      in (result, (st', pos')) end;
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   227
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   228
    fun element_result elem x =
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   229
      fold_map proof_result
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   230
        (Outer_Syntax.read_element (#2 (Outer_Syntax.get_syntax ())) init elem) x;
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   231
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   232
    val (results, (end_state, end_pos)) =
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   233
      fold_map element_result elements (Toplevel.toplevel, Position.none);
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   234
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   235
    val thy = Toplevel.end_theory end_pos end_state;
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   236
  in (flat results, thy) end;
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   237
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
   238
fun load_thy update_time dir header pos text parents =
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   239
  let
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   240
    val time = ! Toplevel.timing;
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   241
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   242
    val {name, uses, ...} = header;
46958
0ec8f04e753a define keywords early when processing the theory header, before running the body commands;
wenzelm
parents: 46938
diff changeset
   243
    val _ = Thy_Header.define_keywords header;
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   244
    val _ = Present.init_theory name;
44186
806f0ec1a43d simplified Toplevel.init_theory: discontinued special master argument;
wenzelm
parents: 44113
diff changeset
   245
    fun init () =
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46811
diff changeset
   246
      begin_theory dir header parents
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   247
      |> Present.begin_theory update_time dir uses;
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   248
46959
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   249
    val lexs = Keyword.get_lexicons ();
46958
0ec8f04e753a define keywords early when processing the theory header, before running the body commands;
wenzelm
parents: 46938
diff changeset
   250
46811
03a2dc9e0624 clarified command span: include trailing whitespace/comments and thus reduce number of ignored spans with associated transactions and states (factor 2);
wenzelm
parents: 46737
diff changeset
   251
    val toks = Thy_Syntax.parse_tokens lexs pos text;
48867
e9beabf045ab some support for inlining file content into outer syntax token language;
wenzelm
parents: 46959
diff changeset
   252
    val spans = map (resolve_files dir) (Thy_Syntax.parse_spans toks);
46959
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   253
    val elements = Thy_Syntax.parse_elements spans;
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   254
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   255
    val _ = Present.theory_source name
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   256
      (fn () => HTML.html_mode (implode o map Thy_Syntax.present_span) spans);
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   257
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   258
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
46959
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   259
    val (results, thy) = cond_timeit time "" (fn () => excursion init elements);
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   260
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   261
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   262
    val present =
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   263
      singleton (Future.cond_forks {name = "Outer_Syntax.present:" ^ name, group = NONE,
44113
0baa8bbd355a future_job: explicit indication of interrupts;
wenzelm
parents: 43712
diff changeset
   264
        deps = map Future.task_of results, pri = 0, interrupts = true})
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   265
      (fn () =>
46959
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   266
        let val ((minor, _), outer_syntax) = Outer_Syntax.get_syntax () in
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   267
          Thy_Output.present_thy minor Keyword.command_tags
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   268
            (Outer_Syntax.is_markup outer_syntax)
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   269
            (maps Future.join results) toks
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   270
          |> Buffer.content
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   271
          |> Present.theory_output name
cdc791910460 defer actual parsing of command spans and thus allow new commands to be used in the same theory where defined;
wenzelm
parents: 46958
diff changeset
   272
        end);
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   273
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   274
  in (thy, present) end;
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43702
diff changeset
   275
42002
wenzelm
parents: 41955
diff changeset
   276
wenzelm
parents: 41955
diff changeset
   277
(* global master path *)
wenzelm
parents: 41955
diff changeset
   278
wenzelm
parents: 41955
diff changeset
   279
local
wenzelm
parents: 41955
diff changeset
   280
  val master_path = Unsynchronized.ref Path.current;
wenzelm
parents: 41955
diff changeset
   281
in
wenzelm
parents: 41955
diff changeset
   282
wenzelm
parents: 41955
diff changeset
   283
fun set_master_path path = master_path := path;
wenzelm
parents: 41955
diff changeset
   284
fun get_master_path () = ! master_path;
wenzelm
parents: 41955
diff changeset
   285
6168
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   286
end;
9d5b74068bf9 Theory loader primitives.
wenzelm
parents:
diff changeset
   287
42002
wenzelm
parents: 41955
diff changeset
   288
end;