author | wenzelm |
Thu, 16 Jul 2009 20:32:05 +0200 | |
changeset 32018 | 3370cea95387 |
parent 29515 | 53bda11e0d3b |
child 32466 | a393b7e2a2f8 |
permissions | -rw-r--r-- |
6168 | 1 |
(* Title: Pure/Thy/thy_load.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
15801 | 4 |
Theory loader primitives, including load path. |
6168 | 5 |
*) |
6 |
||
7 |
signature BASIC_THY_LOAD = |
|
8 |
sig |
|
9 |
val show_path: unit -> string list |
|
10 |
val add_path: string -> unit |
|
10252 | 11 |
val path_add: string -> unit |
6168 | 12 |
val del_path: string -> unit |
7438 | 13 |
val with_path: string -> ('a -> 'b) -> 'a -> 'b |
9103 | 14 |
val with_paths: string list -> ('a -> 'b) -> 'a -> 'b |
6205 | 15 |
val reset_path: unit -> unit |
6168 | 16 |
end; |
17 |
||
18 |
signature THY_LOAD = |
|
19 |
sig |
|
20 |
include BASIC_THY_LOAD |
|
17366 | 21 |
val ml_exts: string list |
6205 | 22 |
val ml_path: string -> Path.T |
7901 | 23 |
val thy_path: string -> Path.T |
29418 | 24 |
val split_thy_path: Path.T -> Path.T * string |
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 |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
27 |
val check_thy: Path.T -> string -> Path.T * File.ident |
29515 | 28 |
val check_name: string -> string -> unit |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
29 |
val deps_thy: Path.T -> string -> |
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
30 |
{master: Path.T * File.ident, 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 |
6168 | 32 |
end; |
33 |
||
26616 | 34 |
structure ThyLoad: THY_LOAD = |
6168 | 35 |
struct |
36 |
||
37 |
||
6232 | 38 |
(* maintain load path *) |
6168 | 39 |
|
23944 | 40 |
local val load_path = ref [Path.current] in |
6168 | 41 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
17366
diff
changeset
|
42 |
fun show_path () = map Path.implode (! load_path); |
23944 | 43 |
|
44 |
fun del_path s = CRITICAL (fn () => change load_path (remove (op =) (Path.explode s))); |
|
45 |
fun add_path s = CRITICAL (fn () => (del_path s; change load_path (cons (Path.explode s)))); |
|
46 |
fun path_add s = |
|
47 |
CRITICAL (fn () => (del_path s; change load_path (fn path => path @ [Path.explode s]))); |
|
6205 | 48 |
fun reset_path () = load_path := [Path.current]; |
23944 | 49 |
|
50 |
fun with_paths ss f x = |
|
51 |
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
|
52 |
fun with_path s f x = with_paths [s] f x; |
6168 | 53 |
|
23944 | 54 |
fun search_path dir path = |
55 |
distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current])); |
|
56 |
||
57 |
end; |
|
58 |
||
6168 | 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 |
|
29418 | 66 |
fun split_thy_path path = |
67 |
(case Path.split_ext path of |
|
68 |
(path', "thy") => (Path.dir path', Path.implode (Path.base path')) |
|
69 |
| _ => error ("Bad theory file specification " ^ Path.implode path)); |
|
70 |
||
6168 | 71 |
|
23872 | 72 |
(* check files *) |
6232 | 73 |
|
23944 | 74 |
fun check_ext exts paths dir src_path = |
6232 | 75 |
let |
76 |
val path = Path.expand src_path; |
|
23872 | 77 |
val _ = Path.is_current path andalso error "Bad file specification"; |
6168 | 78 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
79 |
fun try_ext rel_path ext = |
8750 | 80 |
let val ext_path = Path.expand (Path.ext ext rel_path) |
23872 | 81 |
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
|
82 |
fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts); |
23944 | 83 |
in get_first try_prfx paths end; |
6232 | 84 |
|
23944 | 85 |
fun check_file dir path = check_ext [] (search_path dir path) dir path; |
86 |
fun check_ml dir path = check_ext ml_exts (search_path dir path) dir path; |
|
6168 | 87 |
|
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
88 |
fun check_thy dir name = |
23944 | 89 |
let |
90 |
val thy_file = thy_path name; |
|
91 |
val paths = search_path dir thy_file; |
|
92 |
in |
|
93 |
(case check_ext [] paths dir thy_file of |
|
23872 | 94 |
NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
23944 | 95 |
" in " ^ commas_quote (map Path.implode paths)) |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
96 |
| SOME thy_id => thy_id) |
23872 | 97 |
end; |
6232 | 98 |
|
29515 | 99 |
fun check_name name name' = |
100 |
if name = name' then () |
|
101 |
else error ("Filename " ^ quote (Path.implode (thy_path name)) ^ |
|
102 |
" does not agree with theory name " ^ quote name'); |
|
103 |
||
6232 | 104 |
|
23872 | 105 |
(* theory deps *) |
7940 | 106 |
|
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
107 |
fun deps_thy dir name = |
23872 | 108 |
let |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
109 |
val master as (path, _) = check_thy dir name; |
24065 | 110 |
val text = explode (File.read path); |
23872 | 111 |
val (name', imports, uses) = |
26881 | 112 |
ThyHeader.read (Path.position path) (Source.of_list_limited 8000 text); |
29515 | 113 |
val _ = check_name name name'; |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
114 |
val uses = map (Path.explode o #1) uses; |
24065 | 115 |
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
|
116 |
|
6168 | 117 |
|
23872 | 118 |
(* load files *) |
6168 | 119 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
120 |
fun load_ml dir raw_path = |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
121 |
(case check_ml dir raw_path of |
23872 | 122 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
26455 | 123 |
| SOME (path, id) => (ML_Context.eval_file path; (path, id))); |
6168 | 124 |
|
125 |
end; |
|
126 |
||
127 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
128 |
open BasicThyLoad; |