author | wenzelm |
Wed, 12 Nov 1997 16:23:11 +0100 | |
changeset 4216 | 419113535e48 |
parent 4138 | af6743b065fb |
child 4217 | 3d5bac2b9fc3 |
permissions | -rw-r--r-- |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Thy/path.ML |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
4 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
5 |
Abstract algebra of file paths. External representation Unix-style. |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
6 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
7 |
TODO: |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
8 |
- support variables and eval:(string->T)->T->T (!?) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
9 |
*) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
10 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
11 |
signature PATH = |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
12 |
sig |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
13 |
type T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
14 |
val pack: T -> string |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
15 |
val unpack: string -> T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
16 |
val current: T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
17 |
val parent: T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
18 |
val root: T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
19 |
val absolute: T -> bool |
4114 | 20 |
val base: T -> string |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
21 |
val append: T -> T -> T |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
22 |
end; |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
23 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
24 |
structure Path: PATH = |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
25 |
struct |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
26 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
27 |
(* type path *) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
28 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
29 |
datatype T = Path of string list; |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
30 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
31 |
val current = Path []; |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
32 |
val parent = Path [".."]; |
4114 | 33 |
val root = Path ["/"]; |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
34 |
|
4114 | 35 |
fun absolute (Path ("/" :: _)) = true |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
36 |
| absolute _ = false; |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
37 |
|
4114 | 38 |
fun base (Path []) = "" |
4138 | 39 |
| base (Path ["/"]) = "" |
4114 | 40 |
| base (Path xs) = snd (split_last xs); |
41 |
||
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
42 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
43 |
(* append and norm *) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
44 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
45 |
(*append non-normal path (2n arg) to reversed normal one, result is normal*) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
46 |
fun rev_app xs [] = rev xs |
4114 | 47 |
| rev_app _ ("/" :: ys) = rev_app ["/"] ys |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
48 |
| rev_app xs ("." :: ys) = rev_app xs ys |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
49 |
| rev_app [] (".." :: ys) = rev_app [".."] ys |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
50 |
| rev_app (xs as ".." :: _) (".." :: ys) = rev_app (".." :: xs) ys |
4114 | 51 |
| rev_app (xs as "/" :: _) (".." :: ys) = rev_app xs ys |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
52 |
| rev_app (_ :: xs) (".." :: ys) = rev_app xs ys |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
53 |
| rev_app xs (y :: ys) = rev_app (y :: xs) ys; |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
54 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
55 |
fun norm path = rev_app [] path; |
4114 | 56 |
|
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
57 |
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys); |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
58 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
59 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
60 |
(* pack and unpack *) |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
61 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
62 |
fun pack (Path []) = "." |
4114 | 63 |
| pack (Path ("/" :: xs)) = "/" ^ space_implode "/" xs |
64 |
| pack (Path xs) = space_implode "/" xs; |
|
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
65 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
66 |
fun unpack str = |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
67 |
Path (norm |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
68 |
(case space_explode "/" str of |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
69 |
[""] => [] |
4114 | 70 |
| "" :: xs => "/" :: filter (not_equal "") xs |
4113
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
71 |
| xs => filter (not_equal "") xs)); |
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
72 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
73 |
|
f7130dcacefa
Abstract algebra of file paths. External representation Unix-style.
wenzelm
parents:
diff
changeset
|
74 |
end; |