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