author | wenzelm |
Sun, 29 Jul 2007 22:41:58 +0200 | |
changeset 24065 | 21483400c2ca |
parent 23944 | 2ea068548a83 |
child 24189 | 1fa9852643a3 |
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 -> |
24065 | 29 |
{master: (Path.T * File.ident) * (Path.T * File.ident) option, |
30 |
text: string list, imports: string list, uses: Path.T list} |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
31 |
val load_ml: Path.T -> Path.T -> Path.T * File.ident |
24065 | 32 |
val load_thy: Path.T -> string -> Position.T -> string list -> bool -> bool -> unit |
6168 | 33 |
end; |
34 |
||
35 |
signature PRIVATE_THY_LOAD = |
|
36 |
sig |
|
37 |
include THY_LOAD |
|
24065 | 38 |
(*this backdoor is sealed later*) |
39 |
val load_thy_fn: (Path.T -> string -> Position.T -> string list -> bool -> bool -> unit) ref |
|
6168 | 40 |
end; |
41 |
||
42 |
structure ThyLoad: PRIVATE_THY_LOAD = |
|
43 |
struct |
|
44 |
||
45 |
||
6232 | 46 |
(* maintain load path *) |
6168 | 47 |
|
23944 | 48 |
local val load_path = ref [Path.current] in |
6168 | 49 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
17366
diff
changeset
|
50 |
fun show_path () = map Path.implode (! load_path); |
23944 | 51 |
|
52 |
fun del_path s = CRITICAL (fn () => change load_path (remove (op =) (Path.explode s))); |
|
53 |
fun add_path s = CRITICAL (fn () => (del_path s; change load_path (cons (Path.explode s)))); |
|
54 |
fun path_add s = |
|
55 |
CRITICAL (fn () => (del_path s; change load_path (fn path => path @ [Path.explode s]))); |
|
6205 | 56 |
fun reset_path () = load_path := [Path.current]; |
23944 | 57 |
|
58 |
fun with_paths ss f x = |
|
59 |
CRITICAL (fn () => 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
|
60 |
fun with_path s f x = with_paths [s] f x; |
6168 | 61 |
|
23944 | 62 |
fun search_path dir path = |
63 |
distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current])); |
|
64 |
||
65 |
end; |
|
66 |
||
6168 | 67 |
|
6232 | 68 |
(* file formats *) |
6168 | 69 |
|
23872 | 70 |
val ml_exts = ["ML", "sml"]; |
6232 | 71 |
val ml_path = Path.ext "ML" o Path.basic; |
72 |
val thy_path = Path.ext "thy" o Path.basic; |
|
6168 | 73 |
|
74 |
||
23872 | 75 |
(* check files *) |
6232 | 76 |
|
23944 | 77 |
fun check_ext exts paths dir src_path = |
6232 | 78 |
let |
79 |
val path = Path.expand src_path; |
|
23872 | 80 |
val _ = Path.is_current path andalso error "Bad file specification"; |
6168 | 81 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
82 |
fun try_ext rel_path ext = |
8750 | 83 |
let val ext_path = Path.expand (Path.ext ext rel_path) |
23872 | 84 |
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
|
85 |
fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts); |
23944 | 86 |
in get_first try_prfx paths end; |
6232 | 87 |
|
23944 | 88 |
fun check_file dir path = check_ext [] (search_path dir path) dir path; |
89 |
fun check_ml dir path = check_ext ml_exts (search_path dir path) dir path; |
|
6168 | 90 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
91 |
fun check_thy dir name ml = |
23944 | 92 |
let |
93 |
val thy_file = thy_path name; |
|
94 |
val paths = search_path dir thy_file; |
|
95 |
in |
|
96 |
(case check_ext [] paths dir thy_file of |
|
23872 | 97 |
NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
23944 | 98 |
" in " ^ commas_quote (map Path.implode paths)) |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
99 |
| SOME thy_id => (thy_id, if ml then check_file dir (ml_path name) else NONE)) |
23872 | 100 |
end; |
6232 | 101 |
|
102 |
||
23872 | 103 |
(* theory deps *) |
7940 | 104 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
105 |
fun deps_thy dir name ml = |
23872 | 106 |
let |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
107 |
val master as ((path, _), _) = check_thy dir name ml; |
24065 | 108 |
val text = explode (File.read path); |
23872 | 109 |
val (name', imports, uses) = |
24065 | 110 |
ThyHeader.read (Position.path path) (Source.of_list_limited 8000 text); |
23872 | 111 |
val _ = name = name' orelse |
112 |
error ("Filename " ^ quote (Path.implode (Path.base path)) ^ |
|
113 |
" does not agree with theory name " ^ quote name'); |
|
114 |
val ml_file = |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
115 |
if ml andalso is_some (check_file dir (ml_path name)) |
23872 | 116 |
then [ml_path name] else []; |
24065 | 117 |
val uses = map (Path.explode o #1) uses @ ml_file; |
118 |
in {master = master, text = text, imports = imports, uses = uses} end; |
|
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
119 |
|
6168 | 120 |
|
23872 | 121 |
(* load files *) |
6168 | 122 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
123 |
fun load_ml dir raw_path = |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
124 |
(case check_ml dir raw_path of |
23872 | 125 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
126 |
| SOME (path, id) => (ML_Context.use path; (path, id))); |
|
6168 | 127 |
|
23872 | 128 |
(*dependent on outer syntax*) |
24065 | 129 |
val load_thy_fn = |
130 |
ref (undefined: Path.T -> string -> Position.T -> string list -> bool -> bool -> unit); |
|
131 |
fun load_thy dir name pos text ml time = ! load_thy_fn dir name pos text ml time; |
|
6168 | 132 |
|
133 |
end; |
|
134 |
||
135 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
136 |
open BasicThyLoad; |