author | wenzelm |
Fri, 20 Jul 2007 17:54:17 +0200 | |
changeset 23885 | 09254a1622e3 |
parent 23876 | d3b05e7bc5d2 |
child 23890 | 9a75e9772761 |
permissions | -rw-r--r-- |
6168 | 1 |
(* Title: Pure/Thy/thy_load.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
15801 | 5 |
Theory loader primitives, including load path. |
6168 | 6 |
*) |
7 |
||
8 |
signature BASIC_THY_LOAD = |
|
9 |
sig |
|
10 |
val show_path: unit -> string list |
|
11 |
val add_path: string -> unit |
|
10252 | 12 |
val path_add: string -> unit |
6168 | 13 |
val del_path: string -> unit |
7438 | 14 |
val with_path: string -> ('a -> 'b) -> 'a -> 'b |
9103 | 15 |
val with_paths: string list -> ('a -> 'b) -> 'a -> 'b |
6205 | 16 |
val reset_path: unit -> unit |
6168 | 17 |
end; |
18 |
||
19 |
signature THY_LOAD = |
|
20 |
sig |
|
21 |
include BASIC_THY_LOAD |
|
17366 | 22 |
val ml_exts: string list |
6205 | 23 |
val ml_path: string -> Path.T |
7901 | 24 |
val thy_path: string -> Path.T |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
25 |
val check_file: Path.T -> Path.T -> (Path.T * File.ident) option |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
26 |
val check_ml: Path.T -> Path.T -> (Path.T * File.ident) option |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
27 |
val check_thy: Path.T -> string -> bool -> (Path.T * File.ident) * (Path.T * File.ident) option |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
28 |
val deps_thy: Path.T -> string -> bool -> |
23872 | 29 |
((Path.T * File.ident) * (Path.T * File.ident) option) * (string list * Path.T list) |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
30 |
val load_ml: Path.T -> Path.T -> Path.T * File.ident |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
31 |
val load_thy: Path.T -> string -> bool -> bool -> |
23872 | 32 |
(Path.T * File.ident) * (Path.T * File.ident) option |
6168 | 33 |
end; |
34 |
||
7940 | 35 |
(*this backdoor sealed later*) |
6168 | 36 |
signature PRIVATE_THY_LOAD = |
37 |
sig |
|
38 |
include THY_LOAD |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
39 |
val load_thy_fn: (Path.T -> string -> bool -> bool -> |
23872 | 40 |
(Path.T * File.ident) * (Path.T * File.ident) option) ref |
6168 | 41 |
end; |
42 |
||
43 |
structure ThyLoad: PRIVATE_THY_LOAD = |
|
44 |
struct |
|
45 |
||
46 |
||
6232 | 47 |
(* maintain load path *) |
6168 | 48 |
|
49 |
val load_path = ref [Path.current]; |
|
50 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
17366
diff
changeset
|
51 |
fun show_path () = map Path.implode (! load_path); |
23872 | 52 |
fun del_path s = change load_path (remove (op =) (Path.explode s)); |
53 |
fun add_path s = (del_path s; change load_path (cons (Path.explode s))); |
|
54 |
fun path_add s = (del_path s; change load_path (fn path => path @ [Path.explode s])); |
|
6205 | 55 |
fun reset_path () = load_path := [Path.current]; |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
17366
diff
changeset
|
56 |
fun with_paths ss f x = Library.setmp load_path (! load_path @ map Path.explode ss) f x; |
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
57 |
fun with_path s f x = with_paths [s] f x; |
6168 | 58 |
|
59 |
||
6232 | 60 |
(* file formats *) |
6168 | 61 |
|
23872 | 62 |
val ml_exts = ["ML", "sml"]; |
6232 | 63 |
val ml_path = Path.ext "ML" o Path.basic; |
64 |
val thy_path = Path.ext "thy" o Path.basic; |
|
6168 | 65 |
|
66 |
||
23872 | 67 |
(* check files *) |
6232 | 68 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
69 |
fun check_ext exts dir src_path = |
6232 | 70 |
let |
71 |
val path = Path.expand src_path; |
|
23872 | 72 |
val _ = Path.is_current path andalso error "Bad file specification"; |
6168 | 73 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
74 |
fun try_ext rel_path ext = |
8750 | 75 |
let val ext_path = Path.expand (Path.ext ext rel_path) |
23872 | 76 |
in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end; |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
77 |
fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts); |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
78 |
|
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
79 |
val via_load_path = if Path.is_basic path then get_first try_prfx (! load_path) else NONE; |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
80 |
in (case via_load_path of NONE => try_prfx dir | res => res) end; |
6232 | 81 |
|
23872 | 82 |
val check_file = check_ext []; |
83 |
val check_ml = check_ext ml_exts; |
|
6168 | 84 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
85 |
fun check_thy dir name ml = |
23872 | 86 |
let val thy_file = thy_path name in |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
87 |
(case check_file dir thy_file of |
23872 | 88 |
NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
89 |
" in " ^ commas_quote (map Path.implode (dir :: ! load_path))) (* FIXME imprecise *) |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
90 |
| SOME thy_id => (thy_id, if ml then check_file dir (ml_path name) else NONE)) |
23872 | 91 |
end; |
6232 | 92 |
|
93 |
||
23872 | 94 |
(* theory deps *) |
7940 | 95 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
96 |
fun deps_thy dir name ml = |
23872 | 97 |
let |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
98 |
val master as ((path, _), _) = check_thy dir name ml; |
23872 | 99 |
val (name', imports, uses) = |
23876 | 100 |
ThyHeader.read (Position.path path) (Source.of_string_limited (File.read path)); |
23872 | 101 |
val _ = name = name' orelse |
102 |
error ("Filename " ^ quote (Path.implode (Path.base path)) ^ |
|
103 |
" does not agree with theory name " ^ quote name'); |
|
104 |
val ml_file = |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
105 |
if ml andalso is_some (check_file dir (ml_path name)) |
23872 | 106 |
then [ml_path name] else []; |
107 |
in (master, (imports, map (Path.explode o #1) uses @ ml_file)) end; |
|
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
108 |
|
6168 | 109 |
|
23872 | 110 |
(* load files *) |
6168 | 111 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
112 |
fun load_ml dir raw_path = |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
113 |
(case check_ml dir raw_path of |
23872 | 114 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
115 |
| SOME (path, id) => (ML_Context.use path; (path, id))); |
|
6168 | 116 |
|
23872 | 117 |
(*dependent on outer syntax*) |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
118 |
val load_thy_fn = ref (undefined: Path.T -> string -> bool -> bool -> |
23872 | 119 |
(Path.T * File.ident) * (Path.T * File.ident) option); |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
120 |
fun load_thy dir name ml time = ! load_thy_fn dir name ml time; |
6168 | 121 |
|
122 |
end; |
|
123 |
||
124 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
125 |
open BasicThyLoad; |