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