author | ballarin |
Fri, 28 Nov 2008 17:43:06 +0100 | |
changeset 28903 | b3fc3a62247a |
parent 26881 | bb68f50644a9 |
child 29418 | d584715a3ebb |
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 |
|
17366 | 22 |
val ml_exts: string list |
6205 | 23 |
val ml_path: string -> Path.T |
7901 | 24 |
val thy_path: string -> Path.T |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
25 |
val check_file: Path.T -> Path.T -> (Path.T * File.ident) option |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
26 |
val check_ml: Path.T -> Path.T -> (Path.T * File.ident) option |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
27 |
val check_thy: Path.T -> string -> Path.T * File.ident |
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
28 |
val deps_thy: Path.T -> string -> |
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
29 |
{master: Path.T * File.ident, text: string list, imports: string list, uses: Path.T list} |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
30 |
val load_ml: Path.T -> Path.T -> Path.T * File.ident |
6168 | 31 |
end; |
32 |
||
26616 | 33 |
structure ThyLoad: THY_LOAD = |
6168 | 34 |
struct |
35 |
||
36 |
||
6232 | 37 |
(* maintain load path *) |
6168 | 38 |
|
23944 | 39 |
local val load_path = ref [Path.current] in |
6168 | 40 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
17366
diff
changeset
|
41 |
fun show_path () = map Path.implode (! load_path); |
23944 | 42 |
|
43 |
fun del_path s = CRITICAL (fn () => change load_path (remove (op =) (Path.explode s))); |
|
44 |
fun add_path s = CRITICAL (fn () => (del_path s; change load_path (cons (Path.explode s)))); |
|
45 |
fun path_add s = |
|
46 |
CRITICAL (fn () => (del_path s; change load_path (fn path => path @ [Path.explode s]))); |
|
6205 | 47 |
fun reset_path () = load_path := [Path.current]; |
23944 | 48 |
|
49 |
fun with_paths ss f x = |
|
50 |
CRITICAL (fn () => Library.setmp load_path (! load_path @ map Path.explode ss) f x); |
|
9655
a4d2da014ec3
renamed cond_with_path to cond_add_path (add to front);
wenzelm
parents:
9103
diff
changeset
|
51 |
fun with_path s f x = with_paths [s] f x; |
6168 | 52 |
|
23944 | 53 |
fun search_path dir path = |
54 |
distinct (op =) (dir :: (if Path.is_basic path then (! load_path) else [Path.current])); |
|
55 |
||
56 |
end; |
|
57 |
||
6168 | 58 |
|
6232 | 59 |
(* file formats *) |
6168 | 60 |
|
23872 | 61 |
val ml_exts = ["ML", "sml"]; |
6232 | 62 |
val ml_path = Path.ext "ML" o Path.basic; |
63 |
val thy_path = Path.ext "thy" o Path.basic; |
|
6168 | 64 |
|
65 |
||
23872 | 66 |
(* check files *) |
6232 | 67 |
|
23944 | 68 |
fun check_ext exts paths dir src_path = |
6232 | 69 |
let |
70 |
val path = Path.expand src_path; |
|
23872 | 71 |
val _ = Path.is_current path andalso error "Bad file specification"; |
6168 | 72 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
73 |
fun try_ext rel_path ext = |
8750 | 74 |
let val ext_path = Path.expand (Path.ext ext rel_path) |
23872 | 75 |
in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end; |
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
76 |
fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts); |
23944 | 77 |
in get_first try_prfx paths end; |
6232 | 78 |
|
23944 | 79 |
fun check_file dir path = check_ext [] (search_path dir path) dir path; |
80 |
fun check_ml dir path = check_ext ml_exts (search_path dir path) dir path; |
|
6168 | 81 |
|
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
82 |
fun check_thy dir name = |
23944 | 83 |
let |
84 |
val thy_file = thy_path name; |
|
85 |
val paths = search_path dir thy_file; |
|
86 |
in |
|
87 |
(case check_ext [] paths dir thy_file of |
|
23872 | 88 |
NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
23944 | 89 |
" in " ^ commas_quote (map Path.implode paths)) |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
90 |
| SOME thy_id => thy_id) |
23872 | 91 |
end; |
6232 | 92 |
|
93 |
||
23872 | 94 |
(* theory deps *) |
7940 | 95 |
|
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
96 |
fun deps_thy dir name = |
23872 | 97 |
let |
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
98 |
val master as (path, _) = check_thy dir name; |
24065 | 99 |
val text = explode (File.read path); |
23872 | 100 |
val (name', imports, uses) = |
26881 | 101 |
ThyHeader.read (Path.position path) (Source.of_list_limited 8000 text); |
23872 | 102 |
val _ = name = name' orelse |
103 |
error ("Filename " ^ quote (Path.implode (Path.base path)) ^ |
|
104 |
" does not agree with theory name " ^ quote name'); |
|
24189
1fa9852643a3
simplified ThyLoad.deps_thy etc.: discontinued attached ML files;
wenzelm
parents:
24065
diff
changeset
|
105 |
val uses = map (Path.explode o #1) uses; |
24065 | 106 |
in {master = master, text = text, imports = imports, uses = uses} end; |
6484
3f098b0ec683
use_thy etc.: may specify path prefix, which is temporarily used as load path;
wenzelm
parents:
6363
diff
changeset
|
107 |
|
6168 | 108 |
|
23872 | 109 |
(* load files *) |
6168 | 110 |
|
23885
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
111 |
fun load_ml dir raw_path = |
09254a1622e3
simplified ThyLoad interfaces: only one additional directory;
wenzelm
parents:
23876
diff
changeset
|
112 |
(case check_ml dir raw_path of |
23872 | 113 |
NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
26455 | 114 |
| SOME (path, id) => (ML_Context.eval_file path; (path, id))); |
6168 | 115 |
|
116 |
end; |
|
117 |
||
118 |
structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
|
119 |
open BasicThyLoad; |