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
|
|
21 |
val load_file: Path.T -> (Path.T * File.info)
|
|
22 |
val check_thy: string -> bool -> (Path.T * File.info) list
|
|
23 |
val deps_thy: string -> bool -> (Path.T * File.info) list * (string list * Path.T list)
|
|
24 |
val load_thy: string -> bool -> bool -> (Path.T * File.info) list
|
6168
|
25 |
end;
|
|
26 |
|
|
27 |
signature PRIVATE_THY_LOAD =
|
|
28 |
sig
|
|
29 |
include THY_LOAD
|
6205
|
30 |
val deps_thy_fn: (string -> bool -> Path.T -> string list * Path.T list) ref
|
|
31 |
val load_thy_fn: (string -> bool -> bool -> Path.T -> unit) ref
|
6168
|
32 |
end;
|
|
33 |
|
|
34 |
structure ThyLoad: PRIVATE_THY_LOAD =
|
|
35 |
struct
|
|
36 |
|
|
37 |
|
6232
|
38 |
(* maintain load path *)
|
6168
|
39 |
|
|
40 |
val load_path = ref [Path.current];
|
|
41 |
fun change_path f = load_path := f (! load_path);
|
|
42 |
|
|
43 |
fun show_path () = map Path.pack (! load_path);
|
|
44 |
fun add_path s = change_path (fn path => path @ [Path.unpack s]);
|
|
45 |
fun del_path s = change_path (filter_out (equal (Path.unpack s)));
|
6205
|
46 |
fun reset_path () = load_path := [Path.current];
|
6168
|
47 |
|
|
48 |
|
6232
|
49 |
(* file formats *)
|
6168
|
50 |
|
6232
|
51 |
val ml_exts = ["", "ML", "sml"];
|
|
52 |
val ml_path = Path.ext "ML" o Path.basic;
|
|
53 |
val thy_path = Path.ext "thy" o Path.basic;
|
6168
|
54 |
|
|
55 |
|
6232
|
56 |
(* check_file *)
|
|
57 |
|
|
58 |
fun check_file src_path =
|
|
59 |
let
|
|
60 |
val path = Path.expand src_path;
|
6168
|
61 |
|
6232
|
62 |
fun find_ext rel_path ext =
|
|
63 |
let val ext_path = Path.ext ext rel_path
|
|
64 |
in apsome (fn info => (File.full_path ext_path, info)) (File.info ext_path) end;
|
|
65 |
|
|
66 |
fun find_dir dir =
|
|
67 |
get_first (find_ext (Path.append dir path)) ml_exts;
|
|
68 |
in get_first find_dir (if Path.is_basic path then ! load_path else [Path.current]) end;
|
|
69 |
|
|
70 |
|
|
71 |
(* load_file *)
|
6168
|
72 |
|
|
73 |
fun load_file raw_path =
|
6232
|
74 |
(case check_file raw_path of
|
|
75 |
None => error ("Could not find ML file " ^ quote (Path.pack raw_path))
|
|
76 |
| Some (path, info) => (Symbol.use path; (path, info)));
|
|
77 |
|
|
78 |
|
|
79 |
(* check_thy *)
|
6168
|
80 |
|
6232
|
81 |
fun check_thy name ml =
|
|
82 |
(case check_file (thy_path name) of
|
|
83 |
None => error ("Could not find theory file for " ^ quote name)
|
|
84 |
| Some thy_info => thy_info ::
|
|
85 |
(case if ml then check_file (ml_path name) else None of
|
|
86 |
None => []
|
|
87 |
| Some info => [info]));
|
6205
|
88 |
|
6168
|
89 |
|
|
90 |
(* process theory files *)
|
|
91 |
|
|
92 |
(*hooks for theory syntax dependent operations*)
|
|
93 |
fun undefined _ = raise Match;
|
6205
|
94 |
val deps_thy_fn = ref (undefined: string -> bool -> Path.T -> string list * Path.T list);
|
|
95 |
val load_thy_fn = ref (undefined: string -> bool -> bool -> Path.T -> unit);
|
6168
|
96 |
|
6232
|
97 |
fun process_thy f name ml =
|
|
98 |
let val master = check_thy name ml
|
|
99 |
in (master, f (#1 (hd master))) end;
|
6219
|
100 |
|
6232
|
101 |
fun deps_thy name ml = process_thy (! deps_thy_fn name ml) name ml;
|
|
102 |
fun load_thy name ml time = #1 (process_thy (! load_thy_fn name ml time) name ml);
|
6168
|
103 |
|
|
104 |
|
|
105 |
end;
|
|
106 |
|
|
107 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad;
|
|
108 |
open BasicThyLoad;
|