20 sig |
20 sig |
21 include BASIC_THY_LOAD |
21 include BASIC_THY_LOAD |
22 val ml_exts: string list |
22 val ml_exts: string list |
23 val ml_path: string -> Path.T |
23 val ml_path: string -> Path.T |
24 val thy_path: string -> Path.T |
24 val thy_path: string -> Path.T |
25 val check_file: Path.T list -> Path.T -> (Path.T * File.ident) option |
25 val check_file: Path.T -> Path.T -> (Path.T * File.ident) option |
26 val check_ml: Path.T list -> Path.T -> (Path.T * File.ident) option |
26 val check_ml: Path.T -> Path.T -> (Path.T * File.ident) option |
27 val check_thy: Path.T list -> string -> bool -> |
27 val check_thy: Path.T -> string -> bool -> (Path.T * File.ident) * (Path.T * File.ident) option |
28 (Path.T * File.ident) * (Path.T * File.ident) option |
28 val deps_thy: Path.T -> string -> bool -> |
29 val deps_thy: Path.T list -> string -> bool -> |
|
30 ((Path.T * File.ident) * (Path.T * File.ident) option) * (string list * Path.T list) |
29 ((Path.T * File.ident) * (Path.T * File.ident) option) * (string list * Path.T list) |
31 val load_ml: Path.T list -> Path.T -> Path.T * File.ident |
30 val load_ml: Path.T -> Path.T -> Path.T * File.ident |
32 val load_thy: Path.T list -> string -> bool -> bool -> |
31 val load_thy: Path.T -> string -> bool -> bool -> |
33 (Path.T * File.ident) * (Path.T * File.ident) option |
32 (Path.T * File.ident) * (Path.T * File.ident) option |
34 end; |
33 end; |
35 |
34 |
36 (*this backdoor sealed later*) |
35 (*this backdoor sealed later*) |
37 signature PRIVATE_THY_LOAD = |
36 signature PRIVATE_THY_LOAD = |
38 sig |
37 sig |
39 include THY_LOAD |
38 include THY_LOAD |
40 val load_thy_fn: (Path.T list -> string -> bool -> bool -> |
39 val load_thy_fn: (Path.T -> string -> bool -> bool -> |
41 (Path.T * File.ident) * (Path.T * File.ident) option) ref |
40 (Path.T * File.ident) * (Path.T * File.ident) option) ref |
42 end; |
41 end; |
43 |
42 |
44 structure ThyLoad: PRIVATE_THY_LOAD = |
43 structure ThyLoad: PRIVATE_THY_LOAD = |
45 struct |
44 struct |
65 val thy_path = Path.ext "thy" o Path.basic; |
64 val thy_path = Path.ext "thy" o Path.basic; |
66 |
65 |
67 |
66 |
68 (* check files *) |
67 (* check files *) |
69 |
68 |
70 fun augment_path (ps: Path.T list) qs = distinct (op =) (ps @ qs); |
69 fun check_ext exts dir src_path = |
71 |
|
72 fun check_ext exts dirs src_path = |
|
73 let |
70 let |
74 val path = Path.expand src_path; |
71 val path = Path.expand src_path; |
75 val _ = Path.is_current path andalso error "Bad file specification"; |
72 val _ = Path.is_current path andalso error "Bad file specification"; |
76 val paths = augment_path dirs (! load_path); |
|
77 |
73 |
78 fun find_ext rel_path ext = |
74 fun try_ext rel_path ext = |
79 let val ext_path = Path.expand (Path.ext ext rel_path) |
75 let val ext_path = Path.expand (Path.ext ext rel_path) |
80 in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end; |
76 in Option.map (fn id => (File.full_path ext_path, id)) (File.ident ext_path) end; |
81 fun find_dir dir = get_first (find_ext (Path.append dir path)) ("" :: exts); |
77 fun try_prfx prfx = get_first (try_ext (Path.append prfx path)) ("" :: exts); |
82 in get_first find_dir paths end; |
78 |
|
79 val via_load_path = if Path.is_basic path then get_first try_prfx (! load_path) else NONE; |
|
80 in (case via_load_path of NONE => try_prfx dir | res => res) end; |
83 |
81 |
84 val check_file = check_ext []; |
82 val check_file = check_ext []; |
85 val check_ml = check_ext ml_exts; |
83 val check_ml = check_ext ml_exts; |
86 |
84 |
87 fun check_thy dirs name ml = |
85 fun check_thy dir name ml = |
88 let val thy_file = thy_path name in |
86 let val thy_file = thy_path name in |
89 (case check_file dirs thy_file of |
87 (case check_file dir thy_file of |
90 NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
88 NONE => error ("Could not find theory file " ^ quote (Path.implode thy_file) ^ |
91 " in dir(s) " ^ commas_quote (map Path.implode (augment_path dirs (! load_path)))) |
89 " in " ^ commas_quote (map Path.implode (dir :: ! load_path))) (* FIXME imprecise *) |
92 | SOME thy_id => (thy_id, if ml then check_file dirs (ml_path name) else NONE)) |
90 | SOME thy_id => (thy_id, if ml then check_file dir (ml_path name) else NONE)) |
93 end; |
91 end; |
94 |
92 |
95 |
93 |
96 (* theory deps *) |
94 (* theory deps *) |
97 |
95 |
98 fun deps_thy dirs name ml = |
96 fun deps_thy dir name ml = |
99 let |
97 let |
100 val master as ((path, _), _) = check_thy dirs name ml; |
98 val master as ((path, _), _) = check_thy dir name ml; |
101 val (name', imports, uses) = |
99 val (name', imports, uses) = |
102 ThyHeader.read (Position.path path) (Source.of_string_limited (File.read path)); |
100 ThyHeader.read (Position.path path) (Source.of_string_limited (File.read path)); |
103 val _ = name = name' orelse |
101 val _ = name = name' orelse |
104 error ("Filename " ^ quote (Path.implode (Path.base path)) ^ |
102 error ("Filename " ^ quote (Path.implode (Path.base path)) ^ |
105 " does not agree with theory name " ^ quote name'); |
103 " does not agree with theory name " ^ quote name'); |
106 val ml_file = |
104 val ml_file = |
107 if ml andalso is_some (check_file [] (ml_path name)) |
105 if ml andalso is_some (check_file dir (ml_path name)) |
108 then [ml_path name] else []; |
106 then [ml_path name] else []; |
109 in (master, (imports, map (Path.explode o #1) uses @ ml_file)) end; |
107 in (master, (imports, map (Path.explode o #1) uses @ ml_file)) end; |
110 |
108 |
111 |
109 |
112 (* load files *) |
110 (* load files *) |
113 |
111 |
114 fun load_ml dirs raw_path = |
112 fun load_ml dir raw_path = |
115 (case check_ml dirs raw_path of |
113 (case check_ml dir raw_path of |
116 NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
114 NONE => error ("Could not find ML file " ^ quote (Path.implode raw_path)) |
117 | SOME (path, id) => (ML_Context.use path; (path, id))); |
115 | SOME (path, id) => (ML_Context.use path; (path, id))); |
118 |
116 |
119 (*dependent on outer syntax*) |
117 (*dependent on outer syntax*) |
120 val load_thy_fn = ref (undefined: Path.T list -> string -> bool -> bool -> |
118 val load_thy_fn = ref (undefined: Path.T -> string -> bool -> bool -> |
121 (Path.T * File.ident) * (Path.T * File.ident) option); |
119 (Path.T * File.ident) * (Path.T * File.ident) option); |
122 fun load_thy dirs name ml time = ! load_thy_fn dirs name ml time; |
120 fun load_thy dir name ml time = ! load_thy_fn dir name ml time; |
123 |
121 |
124 end; |
122 end; |
125 |
123 |
126 structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
124 structure BasicThyLoad: BASIC_THY_LOAD = ThyLoad; |
127 open BasicThyLoad; |
125 open BasicThyLoad; |