src/Pure/General/path.ML
author wenzelm
Tue, 09 Mar 1999 12:09:51 +0100
changeset 6319 80173cb8691c
parent 6270 c905fe5994a2
child 6460 cb8c85435228
permissions -rw-r--r--
added make, dir;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5011
diff changeset
     1
(*  Title:      Pure/General/path.ML
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     4
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
     5
Abstract algebra of file paths (external encoding Unix-style).
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     6
*)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     7
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     8
signature PATH =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     9
sig
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    10
  datatype elem = Root | Parent | Basic of string | Variable of string
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    11
  eqtype T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    12
  val rep: T -> elem list
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    13
  val current: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    14
  val root: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    15
  val parent: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    16
  val basic: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    17
  val variable: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    18
  val is_absolute: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    19
  val is_basic: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    20
  val append: T -> T -> T
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    21
  val appends: T list -> T
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    22
  val make: string list -> T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    23
  val pack: T -> string
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    24
  val unpack: string -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    25
  val base: T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    26
  val ext: string -> T -> T
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    27
  val dir: T -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    28
  val expand: T -> T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    29
end;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    30
6187
c6c4626ef693 enabled sig;
wenzelm
parents: 6183
diff changeset
    31
structure Path: PATH =
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    32
struct
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    33
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    34
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    35
(* path elements *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    36
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    37
datatype elem = Root | Parent | Basic of string | Variable of string;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    38
6223
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    39
fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs));
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    40
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    41
fun check_elem (chs as []) = err_elem "Illegal" chs
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    42
  | check_elem (chs as ["~"]) = err_elem "Illegal" chs
6223
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    43
  | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    44
  | check_elem chs =
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    45
      (case ["/", "\\", "$", ":"] inter_string chs of
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    46
        [] => chs
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    47
      | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    48
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    49
val basic_elem = Basic o implode o check_elem;
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    50
val variable_elem = Variable o implode o check_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    51
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    52
fun is_var (Variable _) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    53
  | is_var _ = false;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    54
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    55
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    56
(* type path *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    57
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    58
datatype T = Path of elem list;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    59
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    60
fun rep (Path xs) = xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    61
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    62
val current = Path [];
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    63
val root = Path [Root];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    64
val parent = Path [Parent];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    65
fun basic s = Path [basic_elem (explode s)];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    66
fun variable s = Path [variable_elem (explode s)];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    67
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    68
fun is_absolute (Path (Root :: _)) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    69
  | is_absolute _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    70
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    71
fun is_basic (Path [Basic _]) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    72
  | is_basic _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    73
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    74
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    75
(* append and norm *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    76
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    77
(*append non-normal path (2n arg) to reversed normal one, result is normal*)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    78
fun rev_app xs [] = rev xs
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    79
  | rev_app _ (Root :: ys) = rev_app [Root] ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    80
  | rev_app (x :: xs) (Parent :: ys) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    81
      if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    82
      else if x = Root then rev_app (x :: xs) ys
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    83
      else rev_app xs ys
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    84
  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    85
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    86
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    87
fun appends paths = foldl (uncurry append) (current, paths);
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    88
val make = appends o map basic;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    89
fun norm path = rev_app [] path;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    90
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    91
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    92
(* pack *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    93
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    94
fun pack_elem Root = ""
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    95
  | pack_elem Parent = ".."
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    96
  | pack_elem (Basic s) = s
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    97
  | pack_elem (Variable s) = "$" ^ s;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    98
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    99
fun pack (Path []) = "."
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   100
  | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   101
  | pack (Path xs) = space_implode "/" (map pack_elem xs);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   102
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   103
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   104
(* unpack *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   105
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   106
fun unpack_elem "" = Root
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   107
  | unpack_elem ".." = Parent
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   108
  | unpack_elem "~" = Variable "HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   109
  | unpack_elem "~~" = Variable "ISABELLE_HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   110
  | unpack_elem s =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   111
      (case explode s of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   112
        "$" :: cs => variable_elem cs
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   113
      | cs => basic_elem cs);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   114
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   115
val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   116
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   117
fun unpack str = Path (norm
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   118
  (case space_explode "/" str of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   119
    "" :: ss => Root :: unpack_elems ss
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   120
  | ss => unpack_elems ss));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   121
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   122
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   123
(* base element *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   124
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   125
fun err_no_base path =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   126
  error ("No base path element in " ^ quote (pack path));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   127
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   128
fun base (path as Path xs) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   129
  (case try split_last xs of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   130
    Some (_, x as Basic _) => Path [x]
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   131
  | _ => err_no_base path);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   132
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   133
fun ext e (path as Path xs) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   134
  (case try split_last xs of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   135
    Some (prfx, Basic s) => if e = "" then path else Path (prfx @ [Basic (s ^ "." ^ e)])
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   136
  | _ => err_no_base path);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   137
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   138
fun dir (path as Path xs) =
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   139
  (case try split_last xs of
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   140
    Some (prfx, _) => Path prfx
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   141
  | _ => error ("Cannot split path " ^ quote (pack path)));
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   142
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   143
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   144
(* evaluate variables *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   145
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   146
fun eval env (Variable s) = rep (env s)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   147
  | eval _ x = [x];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   148
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   149
fun evaluate env (Path xs) =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   150
  Path (norm (flat (map (eval env) xs)));
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   151
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   152
val expand = evaluate (unpack o getenv);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   153
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   154
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   155
end;