author | wenzelm |
Fri, 06 Aug 1999 22:30:42 +0200 | |
changeset 7190 | ba6f09cd7769 |
parent 6978 | a5b67157b94d |
child 7438 | 2e0e4253b6c3 |
permissions | -rw-r--r-- |
6168 | 1 |
(* Title: Pure/Thy/thy_load.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Theory loader primitives. |
|
6 |
*) |
|
7 |
||
8 |
signature BASIC_THY_LOAD = |
|
9 |
sig |
|
10 |
val show_path: unit -> string list |
|
11 |
val add_path: string -> unit |
|
12 |
val del_path: string -> unit |
|
6205 | 13 |
val reset_path: unit -> unit |
6168 | 14 |
end; |
15 |
||
16 |
signature THY_LOAD = |
|
17 |
sig |
|
18 |
include BASIC_THY_LOAD |
|
6205 | 19 |
val ml_path: string -> Path.T |
6232 | 20 |
val check_file: Path.T -> (Path.T * File.info) option |
7190 | 21 |
val may_load_file: bool -> Path.T -> (Path.T * File.info) |
22 |
eqtype master |
|
23 |
val get_thy: master -> Path.T * File.info |
|
24 |
val check_thy: Path.T -> string -> master |
|
25 |
val deps_thy: Path.T -> string -> master * (string list * Path.T list) |
|
26 |
val load_thy: Path.T -> string -> bool -> bool -> master |
|
6168 | 27 |
end; |
28 |
||
6363 | 29 |
(*backdoor sealed later*) |
6168 | 30 |
signature PRIVATE_THY_LOAD = |
31 |
sig |
|
32 |
include THY_LOAD |
|
7190 | 33 |
val deps_thy_fn: (string -> Path.T -> string list * Path.T list) ref |
6205 | 34 |
val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref |
6168 | 35 |
end; |
36 |
||
37 |
structure ThyLoad: PRIVATE_THY_LOAD = |
|
38 |
struct |
|
39 |
||
40 |
||
6232 | 41 |
(* maintain load path *) |
6168 | 42 |
|
43 |
val load_path = ref [Path.current]; |
|
44 |
fun change_path f = load_path := f (! load_path); |
|
45 |
||
46 |
fun show_path () = map Path.pack (! load_path); |
|
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
47 |
fun add_path s = change_path (cons (Path.unpack s)); |
6168 | 48 |
fun del_path s = change_path (filter_out (equal (Path.unpack s))); |
6205 | 49 |
fun reset_path () = load_path := [Path.current]; |
6168 | 50 |
|
7190 | 51 |
fun tmp_add_path path f x = |
52 |
if Path.is_current path then f x else Library.setmp load_path (path :: ! load_path) f x; |
|
53 |
||
6168 | 54 |
|
6232 | 55 |
(* file formats *) |
6168 | 56 |
|
6232 | 57 |
val ml_exts = ["", "ML", "sml"]; |
58 |
val ml_path = Path.ext "ML" o Path.basic; |
|
59 |
val thy_path = Path.ext "thy" o Path.basic; |
|
6168 | 60 |
|
61 |
||
6232 | 62 |
(* check_file *) |
63 |
||
64 |
fun check_file src_path = |
|
65 |
let |
|
66 |
val path = Path.expand src_path; |
|
6168 | 67 |
|
6232 | 68 |
fun find_ext rel_path ext = |
69 |
let val ext_path = Path.ext ext rel_path |
|
70 |
in apsome (fn info => (File.full_path ext_path, info)) (File.info ext_path) end; |
|
71 |
||
72 |
fun find_dir dir = |
|
73 |
get_first (find_ext (Path.append dir path)) ml_exts; |
|
74 |
in get_first find_dir (if Path.is_basic path then ! load_path else [Path.current]) end; |
|
75 |
||
76 |
||
7190 | 77 |
(* may_load_file *) |
6168 | 78 |
|
7190 | 79 |
fun may_load_file really raw_path = |
6232 | 80 |
(case check_file raw_path of |
81 |
None => error ("Could not find ML file " ^ quote (Path.pack raw_path)) |
|
7190 | 82 |
| Some (path, info) => (if really then File.symbol_use path else (); (path, info))); |
83 |
||
84 |
||
85 |
(* datatype master *) |
|
86 |
||
87 |
datatype master = Master of (Path.T * File.info) * (Path.T * File.info) option; |
|
88 |
||
89 |
fun get_thy (Master (thy, _)) = thy; |
|
6232 | 90 |
|
91 |
||
92 |
(* check_thy *) |
|
6168 | 93 |
|
7190 | 94 |
fun check_thy_aux name = |
6232 | 95 |
(case check_file (thy_path name) of |
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
96 |
None => error ("Could not find theory file for " ^ quote name ^ " in dir(s) " ^ |
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
97 |
commas_quote (show_path ())) |
7190 | 98 |
| Some thy_info => (thy_info, check_file (ml_path name))); |
6205 | 99 |
|
7190 | 100 |
fun check_thy dir name = Master (tmp_add_path dir check_thy_aux name); |
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
101 |
|
6168 | 102 |
|
103 |
(* process theory files *) |
|
104 |
||
105 |
(*hooks for theory syntax dependent operations*) |
|
106 |
fun undefined _ = raise Match; |
|
7190 | 107 |
val deps_thy_fn = ref (undefined: string -> Path.T -> string list * Path.T list); |
6205 | 108 |
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit); |
6168 | 109 |
|
7190 | 110 |
fun process_thy dir f name = |
111 |
let val master as Master ((path, _), _) = check_thy dir name |
|
112 |
in (master, tmp_add_path dir f path) end; |
|
6219 | 113 |
|
7190 | 114 |
fun deps_thy dir name = process_thy dir (! deps_thy_fn name) name; |
115 |
fun load_thy dir name ml time = #1 (process_thy dir (! load_thy_fn name ml time) name); |
|
6168 | 116 |
|
117 |
||
118 |
end; |
|
119 |
||
120 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
121 |
open BasicThyLoad; |