| author | bulwahn | 
| Wed, 21 Jul 2010 18:13:15 +0200 | |
| changeset 37921 | 1e846be00ddf | 
| parent 36135 | 89d1903fbd50 | 
| child 40627 | becf5d5187cc | 
| permissions | -rw-r--r-- | 
| 6118 | 1 | (* Title: Pure/General/path.ML | 
| 5011 | 2 | Author: Markus Wenzel, TU Muenchen | 
| 3 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 4 | Abstract algebra of file paths: basic POSIX notation, extended by | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 5 | named 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: 
33957diff
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 | |
| 20 | val append: T -> T -> T | |
| 6270 | 21 | val appends: T list -> T | 
| 6319 | 22 | val make: string list -> T | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 23 | val implode: T -> string | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 24 | val explode: string -> T | 
| 14912 | 25 | val dir: T -> T | 
| 6183 | 26 | val base: T -> T | 
| 27 | val ext: string -> T -> T | |
| 14912 | 28 | val split_ext: T -> T * string | 
| 6183 | 29 | val expand: T -> T | 
| 26881 | 30 | val position: T -> Position.T | 
| 5011 | 31 | end; | 
| 32 | ||
| 6187 | 33 | structure Path: PATH = | 
| 5011 | 34 | struct | 
| 35 | ||
| 6183 | 36 | (* path elements *) | 
| 37 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 38 | datatype elem = | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 39 | Root of string | | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 40 | Basic of string | | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 41 | Variable of string | | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 42 | Parent; | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 43 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 44 | local | 
| 6183 | 45 | |
| 6223 | 46 | fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs)); | 
| 6183 | 47 | |
| 6319 | 48 | fun check_elem (chs as []) = err_elem "Illegal" chs | 
| 49 | | check_elem (chs as ["~"]) = err_elem "Illegal" chs | |
| 6223 | 50 | | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs | 
| 51 | | check_elem chs = | |
| 33049 
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
 haftmann parents: 
33038diff
changeset | 52 | (case inter (op =) ["/", "\\", "$", ":"] chs of | 
| 6223 | 53 | [] => chs | 
| 54 |       | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
 | |
| 55 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 56 | in | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 57 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 58 | val root_elem = Root o implode o check_elem; | 
| 6223 | 59 | val basic_elem = Basic o implode o check_elem; | 
| 60 | val variable_elem = Variable o implode o check_elem; | |
| 6183 | 61 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 62 | end; | 
| 6183 | 63 | |
| 64 | ||
| 5011 | 65 | (* type path *) | 
| 66 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 67 | datatype T = Path of elem list; (*reversed elements*) | 
| 6183 | 68 | |
| 69 | fun rep (Path xs) = xs; | |
| 5011 | 70 | |
| 6460 | 71 | fun is_current (Path []) = true | 
| 72 | | is_current _ = false; | |
| 73 | ||
| 5011 | 74 | val current = Path []; | 
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 75 | val root = Path [Root ""]; | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 76 | fun named_root s = Path [root_elem (explode s)]; | 
| 6183 | 77 | fun basic s = Path [basic_elem (explode s)]; | 
| 78 | fun variable s = Path [variable_elem (explode s)]; | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 79 | val parent = Path [Parent]; | 
| 5011 | 80 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 81 | fun is_absolute (Path xs) = | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 82 | (case try List.last xs of | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 83 | SOME (Root _) => true | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 84 | | _ => false); | 
| 5011 | 85 | |
| 6183 | 86 | fun is_basic (Path [Basic _]) = true | 
| 87 | | is_basic _ = false; | |
| 5011 | 88 | |
| 89 | ||
| 90 | (* append and norm *) | |
| 91 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 92 | fun apply (y as Root _) _ = [y] | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 93 | | apply Parent (xs as (Root _ :: _)) = xs | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 94 | | apply Parent (Basic _ :: rest) = rest | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 95 | | apply y xs = y :: xs; | 
| 5011 | 96 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 97 | fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs); | 
| 15570 | 98 | fun appends paths = Library.foldl (uncurry append) (current, paths); | 
| 6319 | 99 | val make = appends o map basic; | 
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 100 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 101 | fun norm elems = fold_rev apply elems []; | 
| 5011 | 102 | |
| 6183 | 103 | |
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 104 | (* implode *) | 
| 5011 | 105 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 106 | local | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 107 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 108 | fun implode_elem (Root "") = "" | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 109 | | implode_elem (Root s) = "//" ^ s | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 110 | | implode_elem (Basic s) = s | 
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 111 | | implode_elem (Variable s) = "$" ^ s | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 112 | | implode_elem Parent = ".."; | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 113 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 114 | in | 
| 5011 | 115 | |
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 116 | fun implode_path (Path []) = "." | 
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 117 | | implode_path (Path [Root ""]) = "/" | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 118 | | implode_path (Path xs) = space_implode "/" (rev (map implode_elem xs)); | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 119 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 120 | end; | 
| 5011 | 121 | |
| 122 | ||
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 123 | (* explode *) | 
| 6183 | 124 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 125 | local | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 126 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 127 | fun explode_elem ".." = Parent | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 128 | | explode_elem "~" = Variable "HOME" | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 129 | | explode_elem "~~" = Variable "ISABELLE_HOME" | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 130 | | explode_elem s = | 
| 6183 | 131 | (case explode s of | 
| 132 | "$" :: cs => variable_elem cs | |
| 133 | | cs => basic_elem cs); | |
| 134 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 135 | val explode_elems = | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 136 | rev o map explode_elem o filter_out (fn c => c = "" orelse c = "."); | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 137 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 138 | in | 
| 6183 | 139 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 140 | fun explode_path str = | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 141 | let val (roots, raw_elems) = | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 142 | (case take_prefix (equal "") (space_explode "/" str) |>> length of | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 143 | (0, es) => ([], es) | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 144 | | (1, es) => ([Root ""], es) | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 145 | | (_, []) => ([Root ""], []) | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 146 | | (_, e :: es) => ([root_elem (explode e)], es)) | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 147 | in Path (norm (explode_elems raw_elems @ roots)) end; | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 148 | |
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 149 | end; | 
| 6183 | 150 | |
| 5011 | 151 | |
| 6183 | 152 | (* base element *) | 
| 153 | ||
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 154 | fun split_path f (Path (Basic s :: xs)) = f (Path xs, s) | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 155 |   | split_path _ path = error ("Cannot split path into dir/base: " ^ quote (implode_path path));
 | 
| 6183 | 156 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 157 | val dir = split_path #1; | 
| 7929 | 158 | val base = split_path (fn (_, s) => Path [Basic s]); | 
| 6183 | 159 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 160 | fun ext "" = I | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 161 | | ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e))); | 
| 6183 | 162 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 163 | val split_ext = split_path (fn (prfx, s) => apfst (append prfx) | 
| 19305 | 164 | (case take_suffix (fn c => c <> ".") (explode s) of | 
| 14912 | 165 | ([], _) => (Path [Basic s], "") | 
| 33957 | 166 | | (cs, e) => (Path [Basic (implode (take (length cs - 1) cs))], implode e))); | 
| 6319 | 167 | |
| 6183 | 168 | |
| 17827 | 169 | (* expand variables *) | 
| 5011 | 170 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 171 | local | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 172 | |
| 17827 | 173 | fun eval (Variable s) = | 
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 174 | (case getenv s of | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 175 |         "" => error ("Undefined Isabelle environment variable: " ^ quote s)
 | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 176 | | path => rep (explode_path path)) | 
| 17827 | 177 | | eval x = [x]; | 
| 5011 | 178 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 179 | in | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 180 | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19305diff
changeset | 181 | 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: 
19482diff
changeset | 182 | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 183 | end; | 
| 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 184 | |
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 185 | |
| 26881 | 186 | (* source position *) | 
| 187 | ||
| 188 | val position = Position.file o implode_path o expand; | |
| 189 | ||
| 190 | ||
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 191 | (*final declarations of this structure!*) | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 192 | val implode = implode_path; | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19482diff
changeset | 193 | val explode = explode_path; | 
| 5011 | 194 | |
| 195 | end; | |
| 36135 
89d1903fbd50
support named_root, which approximates UNC server prefix (for Cygwin);
 wenzelm parents: 
33957diff
changeset | 196 |