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