author  wenzelm 
Fri, 11 Apr 2014 11:52:28 +0200  
changeset 56533  cd8b6d849b6a 
parent 56136  81c66d9e274b 
child 59363  4660b0409096 
permissions  rwrr 
6118  1 
(* Title: Pure/General/path.ML 
5011  2 
Author: Markus Wenzel, TU Muenchen 
3 

43601  4 
Algebra of filesystem paths: basic POSIX notation, extended by named 
5 
roots (e.g. //foo) and variables (e.g. $BAR). 

5011  6 
*) 
7 

8 
signature PATH = 

9 
sig 

6183  10 
eqtype T 
6460  11 
val is_current: T > bool 
6183  12 
val current: T 
13 
val root: T 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

14 
val named_root: string > T 
6183  15 
val parent: T 
16 
val basic: string > T 

17 
val variable: string > T 

18 
val is_absolute: T > bool 

19 
val is_basic: T > bool 

56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

20 
val starts_basic: T > bool 
6183  21 
val append: T > T > T 
6270  22 
val appends: T list > T 
6319  23 
val make: string list > T 
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

24 
val implode: T > string 
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

25 
val explode: string > T 
53045  26 
val split: string > T list 
43593  27 
val pretty: T > Pretty.T 
28 
val print: T > string 

14912  29 
val dir: T > T 
6183  30 
val base: T > T 
31 
val ext: string > T > T 

14912  32 
val split_ext: T > T * string 
6183  33 
val expand: T > T 
56134  34 
val smart_implode: T > string 
26881  35 
val position: T > Position.T 
5011  36 
end; 
37 

6187  38 
structure Path: PATH = 
5011  39 
struct 
40 

6183  41 
(* path elements *) 
42 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

43 
datatype elem = 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

44 
Root of string  
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

45 
Basic of string  
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

46 
Variable of string  
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

47 
Parent; 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

48 

89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

49 
local 
6183  50 

52106  51 
fun err_elem msg s = error (msg ^ " path element specification " ^ quote s); 
6183  52 

52106  53 
fun check_elem s = 
54 
if s = "" orelse s = "~" orelse s = "~~" then err_elem "Illegal" s 

55 
else 

56 
let 

57 
fun check c = 

58 
if exists_string (fn c' => c = c') s then 

59 
err_elem ("Illegal character " ^ quote c ^ " in") s 

60 
else (); 

61 
val _ = List.app check ["/", "\\", "$", ":", "\"", "'"]; 

62 
in s end; 

6223  63 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

64 
in 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

65 

52106  66 
val root_elem = Root o check_elem; 
67 
val basic_elem = Basic o check_elem; 

68 
val variable_elem = Variable o check_elem; 

6183  69 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

70 
end; 
6183  71 

72 

5011  73 
(* type path *) 
74 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

75 
datatype T = Path of elem list; (*reversed elements*) 
6183  76 

77 
fun rep (Path xs) = xs; 

5011  78 

6460  79 
fun is_current (Path []) = true 
80 
 is_current _ = false; 

81 

5011  82 
val current = Path []; 
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

83 
val root = Path [Root ""]; 
52106  84 
fun named_root s = Path [root_elem s]; 
85 
fun basic s = Path [basic_elem s]; 

86 
fun variable s = Path [variable_elem s]; 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

87 
val parent = Path [Parent]; 
5011  88 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

89 
fun is_absolute (Path xs) = 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

90 
(case try List.last xs of 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

91 
SOME (Root _) => true 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

92 
 _ => false); 
5011  93 

6183  94 
fun is_basic (Path [Basic _]) = true 
95 
 is_basic _ = false; 

5011  96 

56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

97 
fun starts_basic (Path xs) = 
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

98 
(case try List.last xs of 
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

99 
SOME (Basic _) => true 
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

100 
 _ => false); 
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
56136
diff
changeset

101 

5011  102 

103 
(* append and norm *) 

104 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

105 
fun apply (y as Root _) _ = [y] 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

106 
 apply Parent (xs as (Root _ :: _)) = xs 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

107 
 apply Parent (Basic _ :: rest) = rest 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

108 
 apply y xs = y :: xs; 
5011  109 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

110 
fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs); 
15570  111 
fun appends paths = Library.foldl (uncurry append) (current, paths); 
6319  112 
val make = appends o map basic; 
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

113 

89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

114 
fun norm elems = fold_rev apply elems []; 
5011  115 

6183  116 

21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

117 
(* implode *) 
5011  118 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

119 
local 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

120 

89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

121 
fun implode_elem (Root "") = "" 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

122 
 implode_elem (Root s) = "//" ^ s 
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

123 
 implode_elem (Basic s) = s 
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

124 
 implode_elem (Variable s) = "$" ^ s 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

125 
 implode_elem Parent = ".."; 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

126 

89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

127 
in 
5011  128 

21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

129 
fun implode_path (Path []) = "." 
36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

130 
 implode_path (Path [Root ""]) = "/" 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

131 
 implode_path (Path xs) = space_implode "/" (rev (map implode_elem xs)); 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

132 

89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

133 
end; 
5011  134 

135 

21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

136 
(* explode *) 
6183  137 

52106  138 
fun explode_path str = 
139 
let 

140 
fun explode_elem s = 

141 
(if s = ".." then Parent 

142 
else if s = "~" then Variable "USER_HOME" 

143 
else if s = "~~" then Variable "ISABELLE_HOME" 

144 
else 

145 
(case try (unprefix "$") s of 

146 
SOME s' => variable_elem s' 

147 
 NONE => basic_elem s)) 

148 
handle ERROR msg => cat_error msg ("The error(s) above occurred in " ^ quote str); 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

149 

52106  150 
val (roots, raw_elems) = 
151 
(case take_prefix (equal "") (space_explode "/" str) >> length of 

152 
(0, es) => ([], es) 

153 
 (1, es) => ([Root ""], es) 

154 
 (_, []) => ([Root ""], []) 

155 
 (_, e :: es) => ([root_elem e], es)); 

156 
val elems = raw_elems > filter_out (fn c => c = "" orelse c = ".") > map explode_elem; 

6183  157 

52106  158 
in Path (norm (rev elems @ roots)) end; 
6183  159 

53045  160 
fun split str = 
161 
space_explode ":" str 

162 
> map_filter (fn s => if s = "" then NONE else SOME (explode_path s)); 

163 

5011  164 

43593  165 
(* print *) 
166 

167 
fun pretty path = 

168 
let val s = implode_path path 

50201
c26369c9eda6
Isabellespecific implementation of quasiabstract markup elements  back to module arrangement before d83797ef0d2d;
wenzelm
parents:
48866
diff
changeset

169 
in Pretty.mark (Markup.path s) (Pretty.str (quote s)) end; 
43593  170 

171 
val print = Pretty.str_of o pretty; 

172 

173 

6183  174 
(* base element *) 
175 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

176 
fun split_path f (Path (Basic s :: xs)) = f (Path xs, s) 
43599  177 
 split_path _ path = error ("Cannot split path into dir/base: " ^ print path); 
6183  178 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

179 
val dir = split_path #1; 
7929  180 
val base = split_path (fn (_, s) => Path [Basic s]); 
6183  181 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

182 
fun ext "" = I 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

183 
 ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e))); 
6183  184 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

185 
val split_ext = split_path (fn (prfx, s) => apfst (append prfx) 
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
36135
diff
changeset

186 
(case take_suffix (fn c => c <> ".") (raw_explode s) of 
14912  187 
([], _) => (Path [Basic s], "") 
33957  188 
 (cs, e) => (Path [Basic (implode (take (length cs  1) cs))], implode e))); 
6319  189 

6183  190 

17827  191 
(* expand variables *) 
5011  192 

48658  193 
fun eval (Variable s) = 
194 
let val path = explode_path (getenv_strict s) in 

195 
if exists (fn Variable _ => true  _ => false) (rep path) then 

196 
error ("Illegal path variable nesting: " ^ s ^ "=" ^ print path) 

197 
else rep path 

198 
end 

17827  199 
 eval x = [x]; 
5011  200 

19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset

201 
val expand = rep #> maps eval #> norm #> Path; 
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

202 

05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

203 

56136  204 
(* smart implode *) 
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME  allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset

205 

56134  206 
fun smart_implode path = 
44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME  allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset

207 
let 
56134  208 
val full_name = implode_path (expand path); 
56136  209 
fun fold_path a = 
210 
let val b = implode_path (expand (explode_path a)) in 

211 
if full_name = b then SOME a 

212 
else 

213 
(case try (unprefix (b ^ "/")) full_name of 

214 
SOME name => SOME (a ^ "/" ^ name) 

215 
 NONE => NONE) 

216 
end; 

44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME  allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset

217 
in 
56136  218 
(case get_first fold_path ["~~", "$ISABELLE_HOME_USER", "~"] of 
219 
SOME name => name 

220 
 NONE => implode_path path) 

44863
49ea566cb3b4
more symbolic file positions via smart replacement of ISABELLE_HOME  allows Isabelle distribution to be moved later on;
wenzelm
parents:
44161
diff
changeset

221 
end; 
26881  222 

56134  223 
val position = Position.file o smart_implode; 
26881  224 

21858
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

225 
(*final declarations of this structure!*) 
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

226 
val implode = implode_path; 
05f57309170c
avoid conflict with Alice keywords: renamed pack > implode, unpack > explode, any > many, avoided assert;
wenzelm
parents:
19482
diff
changeset

227 
val explode = explode_path; 
5011  228 

229 
end; 

36135
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents:
33957
diff
changeset

230 