author | wenzelm |
Wed, 18 Oct 2000 23:27:16 +0200 | |
changeset 10247 | 33e542b4934c |
parent 9696 | 5c8acc0282c8 |
child 10252 | dd46544e259d |
permissions | -rw-r--r-- |
6168 | 1 |
(* Title: Pure/Thy/thy_load.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
8808 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
6168 | 5 |
|
6 |
Theory loader primitives. |
|
7 |
*) |
|
8 |
||
9 |
signature BASIC_THY_LOAD = |
|
10 |
sig |
|
11 |
val show_path: unit -> string list |
|
12 |
val add_path: string -> unit |
|
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 |
|
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
22 |
val cond_add_path: Path.T -> ('a -> 'b) -> 'a -> 'b |
6205 | 23 |
val ml_path: string -> Path.T |
7901 | 24 |
val thy_path: string -> Path.T |
6232 | 25 |
val check_file: Path.T -> (Path.T * File.info) option |
7940 | 26 |
val load_file: Path.T -> (Path.T * File.info) |
7190 | 27 |
eqtype master |
28 |
val get_thy: master -> Path.T * File.info |
|
7940 | 29 |
val check_thy: Path.T -> string -> bool -> master |
30 |
val deps_thy: Path.T -> string -> bool -> master * (string list * Path.T list) |
|
7190 | 31 |
val load_thy: Path.T -> string -> bool -> bool -> master |
6168 | 32 |
end; |
33 |
||
7940 | 34 |
(*this backdoor sealed later*) |
6168 | 35 |
signature PRIVATE_THY_LOAD = |
36 |
sig |
|
37 |
include THY_LOAD |
|
7940 | 38 |
val deps_thy_fn: (string -> bool -> Path.T -> string list * Path.T list) ref |
6205 | 39 |
val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref |
6168 | 40 |
end; |
41 |
||
42 |
structure ThyLoad: PRIVATE_THY_LOAD = |
|
43 |
struct |
|
44 |
||
45 |
||
6232 | 46 |
(* maintain load path *) |
6168 | 47 |
|
48 |
val load_path = ref [Path.current]; |
|
49 |
fun change_path f = load_path := f (! load_path); |
|
50 |
||
51 |
fun show_path () = map Path.pack (! load_path); |
|
52 |
fun del_path s = change_path (filter_out (equal (Path.unpack s))); |
|
9696 | 53 |
fun add_path s = (del_path s; change_path (cons (Path.unpack s))); |
6205 | 54 |
fun reset_path () = load_path := [Path.current]; |
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
55 |
fun with_paths ss f x = Library.setmp load_path (! load_path @ map Path.unpack ss) f x; |
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
56 |
fun with_path s f x = with_paths [s] f x; |
6168 | 57 |
|
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
58 |
fun cond_add_path path f x = |
7190 | 59 |
if Path.is_current path then f x else Library.setmp load_path (path :: ! load_path) f x; |
60 |
||
6168 | 61 |
|
6232 | 62 |
(* file formats *) |
6168 | 63 |
|
6232 | 64 |
val ml_exts = ["", "ML", "sml"]; |
65 |
val ml_path = Path.ext "ML" o Path.basic; |
|
66 |
val thy_path = Path.ext "thy" o Path.basic; |
|
6168 | 67 |
|
68 |
||
6232 | 69 |
(* check_file *) |
70 |
||
71 |
fun check_file src_path = |
|
72 |
let |
|
73 |
val path = Path.expand src_path; |
|
6168 | 74 |
|
6232 | 75 |
fun find_ext rel_path ext = |
8750 | 76 |
let val ext_path = Path.expand (Path.ext ext rel_path) |
6232 | 77 |
in apsome (fn info => (File.full_path ext_path, info)) (File.info ext_path) end; |
78 |
||
79 |
fun find_dir dir = |
|
80 |
get_first (find_ext (Path.append dir path)) ml_exts; |
|
81 |
in get_first find_dir (if Path.is_basic path then ! load_path else [Path.current]) end; |
|
82 |
||
83 |
||
7940 | 84 |
(* load_file *) |
6168 | 85 |
|
7940 | 86 |
fun load_file raw_path = |
6232 | 87 |
(case check_file raw_path of |
88 |
None => error ("Could not find ML file " ^ quote (Path.pack raw_path)) |
|
7940 | 89 |
| Some (path, info) => (File.symbol_use path; (path, info))); |
7190 | 90 |
|
91 |
||
92 |
(* datatype master *) |
|
93 |
||
94 |
datatype master = Master of (Path.T * File.info) * (Path.T * File.info) option; |
|
95 |
||
96 |
fun get_thy (Master (thy, _)) = thy; |
|
6232 | 97 |
|
98 |
||
7940 | 99 |
(* check / process theory files *) |
6168 | 100 |
|
7940 | 101 |
local |
102 |
||
103 |
fun check_thy_aux (name, ml) = |
|
6232 | 104 |
(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
|
105 |
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
|
106 |
commas_quote (show_path ())) |
7940 | 107 |
| Some thy_info => (thy_info, if ml then check_file (ml_path name) else None)); |
108 |
||
109 |
in |
|
110 |
||
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
111 |
fun check_thy dir name ml = Master (cond_add_path dir check_thy_aux (name, ml)); |
6205 | 112 |
|
7940 | 113 |
fun process_thy dir f name ml = |
114 |
let val master as Master ((path, _), _) = check_thy dir name ml |
|
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
115 |
in (master, cond_add_path dir f path) end; |
7940 | 116 |
|
117 |
end; |
|
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
118 |
|
6168 | 119 |
|
7940 | 120 |
(* deps_thy and load_thy *) |
6168 | 121 |
|
122 |
(*hooks for theory syntax dependent operations*) |
|
123 |
fun undefined _ = raise Match; |
|
7940 | 124 |
val deps_thy_fn = ref (undefined: string -> bool -> Path.T -> string list * Path.T list); |
6205 | 125 |
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit); |
6168 | 126 |
|
7940 | 127 |
fun deps_thy dir name ml = process_thy dir (! deps_thy_fn name ml) name ml; |
128 |
fun load_thy dir name ml time = #1 (process_thy dir (! load_thy_fn name ml time) name ml); |
|
6168 | 129 |
|
130 |
||
131 |
end; |
|
132 |
||
133 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
134 |
open BasicThyLoad; |