src/Pure/General/path.ML
changeset 5011 37c253fd3dc6
child 6118 caa439435666
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/General/path.ML	Wed Jun 10 11:51:28 1998 +0200
     1.3 @@ -0,0 +1,104 @@
     1.4 +(*  Title:      Pure/Thy/path.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Markus Wenzel, TU Muenchen
     1.7 +
     1.8 +Abstract algebra of file paths.  External representation Unix-style.
     1.9 +*)
    1.10 +
    1.11 +signature PATH =
    1.12 +sig
    1.13 +  type T
    1.14 +  val pack: T -> string
    1.15 +  val unpack: string -> T
    1.16 +  val current: T
    1.17 +  val parent: T
    1.18 +  val root: T
    1.19 +  val variable: string -> T
    1.20 +  val absolute: T -> bool
    1.21 +  val base: T -> string
    1.22 +  val append: T -> T -> T
    1.23 +  val evaluate: (string -> T) -> T -> T
    1.24 +  val expand: (string -> string) -> string -> string
    1.25 +  val base_name: string -> string
    1.26 +  val is_base: string -> bool
    1.27 +end;
    1.28 +
    1.29 +structure Path: PATH =
    1.30 +struct
    1.31 +
    1.32 +(* type path *)
    1.33 +
    1.34 +datatype T = Path of string list;
    1.35 +
    1.36 +val current = Path [];
    1.37 +val parent = Path [".."];
    1.38 +val root = Path ["/"];
    1.39 +
    1.40 +fun absolute (Path ("/" :: _)) = true
    1.41 +  | absolute _ = false;
    1.42 +
    1.43 +fun base (Path []) = ""
    1.44 +  | base (Path ["/"]) = ""
    1.45 +  | base (Path xs) = snd (split_last xs);
    1.46 +
    1.47 +fun variable name = Path ["$" ^ name];
    1.48 +fun is_variable elem = ord elem = ord "$";
    1.49 +
    1.50 +
    1.51 +(* append and norm *)
    1.52 +
    1.53 +(*append non-normal path (2n arg) to reversed normal one, result is normal*)
    1.54 +fun rev_app xs [] = rev xs
    1.55 +  | rev_app _ ("/" :: ys) = rev_app ["/"] ys
    1.56 +  | rev_app xs ("." :: ys) = rev_app xs ys
    1.57 +  | rev_app (x :: xs) (".." :: ys) =
    1.58 +      if x = ".." orelse is_variable x then rev_app (".." :: x :: xs) ys
    1.59 +      else if x = "/" then rev_app (x :: xs) ys
    1.60 +      else rev_app xs ys
    1.61 +  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
    1.62 +
    1.63 +fun norm path = rev_app [] path;
    1.64 +
    1.65 +fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
    1.66 +
    1.67 +
    1.68 +(* pack and unpack *)
    1.69 +
    1.70 +fun pack (Path []) = "."
    1.71 +  | pack (Path ("/" :: xs)) = "/" ^ space_implode "/" xs
    1.72 +  | pack (Path xs) = space_implode "/" xs;
    1.73 +
    1.74 +fun unpack str =
    1.75 +  (case space_explode "/" str of
    1.76 +    [""] => []
    1.77 +  | "" :: xs => "/" :: filter (not_equal "") xs
    1.78 +  | xs => filter (not_equal "") xs)
    1.79 +    |> map (fn "~" => "$HOME" | x => x)
    1.80 +    |> norm
    1.81 +    |> Path;
    1.82 +
    1.83 +
    1.84 +(* eval variables *)
    1.85 +
    1.86 +fun eval env x =
    1.87 +  if is_variable x then
    1.88 +    let val Path ys = env (implode (tl (explode x)))
    1.89 +    in ys end
    1.90 +  else [x];
    1.91 +
    1.92 +fun evaluate env (Path xs) =
    1.93 +  Path (norm (flat (map (eval env) xs)));
    1.94 +
    1.95 +
    1.96 +(* operations on packed paths *)
    1.97 +
    1.98 +fun expand env str =
    1.99 +  pack (evaluate (unpack o env) (unpack str));
   1.100 +
   1.101 +val base_name = base o unpack;
   1.102 +
   1.103 +fun is_base str =
   1.104 +  not (exists (equal "/" orf equal "$") (explode str));
   1.105 +
   1.106 +
   1.107 +end;