author | wenzelm |
Tue, 10 Jul 2007 23:29:43 +0200 | |
changeset 23719 | ccd9cb15c062 |
parent 23672 | 3fd7770f6795 |
child 23863 | 8f3099589cfa |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/path.ML |
5011 | 2 |
ID: $Id$ |
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
6183 | 5 |
Abstract algebra of file paths (external encoding Unix-style). |
5011 | 6 |
*) |
7 |
||
8 |
signature PATH = |
|
9 |
sig |
|
6183 | 10 |
datatype elem = Root | Parent | Basic of string | Variable of string |
11 |
eqtype T |
|
12 |
val rep: T -> elem list |
|
6460 | 13 |
val is_current: T -> bool |
6183 | 14 |
val current: T |
15 |
val root: T |
|
16 |
val parent: T |
|
17 |
val basic: string -> T |
|
18 |
val variable: string -> T |
|
19 |
val is_absolute: T -> bool |
|
20 |
val is_basic: T -> bool |
|
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 |
14912 | 26 |
val dir: T -> T |
6183 | 27 |
val base: T -> T |
28 |
val ext: string -> T -> T |
|
14912 | 29 |
val split_ext: T -> T * string |
6183 | 30 |
val expand: T -> T |
5011 | 31 |
end; |
32 |
||
6187 | 33 |
structure Path: PATH = |
5011 | 34 |
struct |
35 |
||
6183 | 36 |
|
37 |
(* path elements *) |
|
38 |
||
39 |
datatype elem = Root | Parent | Basic of string | Variable of string; |
|
40 |
||
6223 | 41 |
fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs)); |
6183 | 42 |
|
6319 | 43 |
fun check_elem (chs as []) = err_elem "Illegal" chs |
44 |
| check_elem (chs as ["~"]) = err_elem "Illegal" chs |
|
6223 | 45 |
| check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs |
46 |
| check_elem chs = |
|
47 |
(case ["/", "\\", "$", ":"] inter_string chs of |
|
48 |
[] => chs |
|
49 |
| bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs); |
|
50 |
||
51 |
val basic_elem = Basic o implode o check_elem; |
|
52 |
val variable_elem = Variable o implode o check_elem; |
|
6183 | 53 |
|
54 |
fun is_var (Variable _) = true |
|
55 |
| is_var _ = false; |
|
56 |
||
57 |
||
5011 | 58 |
(* type path *) |
59 |
||
6183 | 60 |
datatype T = Path of elem list; |
61 |
||
62 |
fun rep (Path xs) = xs; |
|
5011 | 63 |
|
6460 | 64 |
fun is_current (Path []) = true |
65 |
| is_current _ = false; |
|
66 |
||
5011 | 67 |
val current = Path []; |
6183 | 68 |
val root = Path [Root]; |
69 |
val parent = Path [Parent]; |
|
70 |
fun basic s = Path [basic_elem (explode s)]; |
|
71 |
fun variable s = Path [variable_elem (explode s)]; |
|
5011 | 72 |
|
6183 | 73 |
fun is_absolute (Path (Root :: _)) = true |
74 |
| is_absolute _ = false; |
|
5011 | 75 |
|
6183 | 76 |
fun is_basic (Path [Basic _]) = true |
77 |
| is_basic _ = false; |
|
5011 | 78 |
|
79 |
||
80 |
(* append and norm *) |
|
81 |
||
82 |
(*append non-normal path (2n arg) to reversed normal one, result is normal*) |
|
83 |
fun rev_app xs [] = rev xs |
|
6183 | 84 |
| rev_app _ (Root :: ys) = rev_app [Root] ys |
85 |
| rev_app (x :: xs) (Parent :: ys) = |
|
86 |
if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys |
|
87 |
else if x = Root then rev_app (x :: xs) ys |
|
5011 | 88 |
else rev_app xs ys |
89 |
| rev_app xs (y :: ys) = rev_app (y :: xs) ys; |
|
90 |
||
6183 | 91 |
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys); |
15570 | 92 |
fun appends paths = Library.foldl (uncurry append) (current, paths); |
6319 | 93 |
val make = appends o map basic; |
5011 | 94 |
fun norm path = rev_app [] path; |
95 |
||
6183 | 96 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
97 |
(* implode *) |
5011 | 98 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
99 |
fun implode_elem Root = "" |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
100 |
| implode_elem Parent = ".." |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
101 |
| implode_elem (Basic s) = s |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
102 |
| implode_elem (Variable s) = "$" ^ s; |
5011 | 103 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
104 |
fun implode_path (Path []) = "." |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
105 |
| implode_path (Path (Root :: xs)) = "/" ^ space_implode "/" (map implode_elem xs) |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
106 |
| implode_path (Path xs) = space_implode "/" (map implode_elem xs); |
5011 | 107 |
|
108 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
109 |
(* explode *) |
6183 | 110 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
111 |
fun explode_elem "" = Root |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
112 |
| explode_elem ".." = Parent |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
113 |
| explode_elem "~" = Variable "HOME" |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
114 |
| explode_elem "~~" = Variable "ISABELLE_HOME" |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
115 |
| explode_elem s = |
6183 | 116 |
(case explode s of |
117 |
"$" :: cs => variable_elem cs |
|
118 |
| cs => basic_elem cs); |
|
119 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
120 |
val explode_elems = map explode_elem o filter_out (fn c => c = "" orelse c = "."); |
6183 | 121 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
122 |
fun explode_path str = Path (norm |
6183 | 123 |
(case space_explode "/" str of |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
124 |
"" :: ss => Root :: explode_elems ss |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
125 |
| ss => explode_elems ss)); |
6183 | 126 |
|
5011 | 127 |
|
6183 | 128 |
(* base element *) |
129 |
||
7929 | 130 |
fun split_path f (path as Path xs) = |
131 |
(case try split_last xs of |
|
15531 | 132 |
SOME (prfx, Basic s) => f (prfx, s) |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
133 |
| _ => error ("Cannot split path into dir/base: " ^ quote (implode_path path))); |
6183 | 134 |
|
14912 | 135 |
val dir = split_path (fn (prfx, _) => Path prfx); |
7929 | 136 |
val base = split_path (fn (_, s) => Path [Basic s]); |
6183 | 137 |
|
7929 | 138 |
fun ext "" path = path |
139 |
| ext e path = split_path (fn (prfx, s) => append (Path prfx) (basic (s ^ "." ^ e))) path; |
|
6183 | 140 |
|
14912 | 141 |
val split_ext = split_path (fn (prfx, s) => apfst (append (Path prfx)) |
19305 | 142 |
(case take_suffix (fn c => c <> ".") (explode s) of |
14912 | 143 |
([], _) => (Path [Basic s], "") |
15570 | 144 |
| (cs, e) => (Path [Basic (implode (Library.take (length cs - 1, cs)))], implode e))); |
6319 | 145 |
|
6183 | 146 |
|
17827 | 147 |
(* expand variables *) |
5011 | 148 |
|
17827 | 149 |
fun eval (Variable s) = |
150 |
(case getenv s of |
|
151 |
"" => error ("Undefined Isabelle environment variable: " ^ quote s) |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
152 |
| path => rep (explode_path path)) |
17827 | 153 |
| eval x = [x]; |
5011 | 154 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19305
diff
changeset
|
155 |
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
|
156 |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
157 |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
158 |
(*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
|
159 |
val implode = implode_path; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19482
diff
changeset
|
160 |
val explode = explode_path; |
5011 | 161 |
|
162 |
end; |