src/Pure/General/path.ML
author skalberg
Fri, 05 Dec 2003 19:39:39 +0100
changeset 14278 ae499452700a
parent 8806 a202293db3f6
child 14912 88b9d9165452
permissions -rw-r--r--
Added lazy sequences and parser combinators for same.
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
8806
wenzelm
parents: 7929
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     5
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
     6
Abstract algebra of file paths (external encoding Unix-style).
5011
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
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     9
signature PATH =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    10
sig
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    11
  datatype elem = Root | Parent | Basic of string | Variable of string
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    12
  eqtype T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    13
  val rep: T -> elem list
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    14
  val is_current: T -> bool
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    15
  val current: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    16
  val root: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    17
  val parent: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    18
  val basic: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    19
  val variable: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    20
  val is_absolute: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    21
  val is_basic: T -> bool
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    22
  val append: T -> T -> T
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    23
  val appends: T list -> T
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    24
  val make: string list -> T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    25
  val pack: T -> string
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    26
  val unpack: string -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    27
  val base: T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    28
  val ext: string -> T -> T
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
    29
  val drop_ext: T -> T
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    30
  val dir: T -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    31
  val expand: T -> T
7714
e6aa4fca983e added position;
wenzelm
parents: 6460
diff changeset
    32
  val position: T -> Position.T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    33
end;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    34
6187
c6c4626ef693 enabled sig;
wenzelm
parents: 6183
diff changeset
    35
structure Path: PATH =
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    36
struct
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    37
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    38
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    39
(* path elements *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    40
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    41
datatype elem = Root | Parent | Basic of string | Variable of string;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    42
6223
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    43
fun err_elem msg chs = error (msg ^ " path element specification: " ^ quote (implode chs));
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    44
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    45
fun check_elem (chs as []) = err_elem "Illegal" chs
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    46
  | check_elem (chs as ["~"]) = err_elem "Illegal" chs
6223
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    47
  | check_elem (chs as ["~", "~"]) = err_elem "Illegal" chs
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    48
  | check_elem chs =
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    49
      (case ["/", "\\", "$", ":"] inter_string chs of
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    50
        [] => chs
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    51
      | bads => err_elem ("Illegal character(s) " ^ commas_quote bads ^ " in") chs);
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    52
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    53
val basic_elem = Basic o implode o check_elem;
e8807883e3e3 check_elem: allow ~, except for '~' and '~~';
wenzelm
parents: 6187
diff changeset
    54
val variable_elem = Variable o implode o check_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    55
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    56
fun is_var (Variable _) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    57
  | is_var _ = false;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    58
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    59
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    60
(* type path *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    61
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    62
datatype T = Path of elem list;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    63
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    64
fun rep (Path xs) = xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    65
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    66
fun is_current (Path []) = true
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    67
  | is_current _ = false;
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    68
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    69
val current = Path [];
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    70
val root = Path [Root];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    71
val parent = Path [Parent];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    72
fun basic s = Path [basic_elem (explode s)];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    73
fun variable s = Path [variable_elem (explode s)];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    74
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    75
fun is_absolute (Path (Root :: _)) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    76
  | is_absolute _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    77
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    78
fun is_basic (Path [Basic _]) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    79
  | is_basic _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    80
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    81
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    82
(* append and norm *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    83
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    84
(*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
    85
fun rev_app xs [] = rev xs
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    86
  | rev_app _ (Root :: ys) = rev_app [Root] ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    87
  | rev_app (x :: xs) (Parent :: ys) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    88
      if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    89
      else if x = Root then rev_app (x :: xs) ys
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    90
      else rev_app xs ys
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    91
  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    92
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    93
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    94
fun appends paths = foldl (uncurry append) (current, paths);
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
    95
val make = appends o map basic;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    96
fun norm path = rev_app [] path;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    97
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    98
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    99
(* pack *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   100
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   101
fun pack_elem Root = ""
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   102
  | pack_elem Parent = ".."
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   103
  | pack_elem (Basic s) = s
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   104
  | pack_elem (Variable s) = "$" ^ s;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   105
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   106
fun pack (Path []) = "."
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   107
  | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   108
  | pack (Path xs) = space_implode "/" (map pack_elem xs);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   109
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   110
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   111
(* unpack *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   112
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   113
fun unpack_elem "" = Root
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   114
  | unpack_elem ".." = Parent
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   115
  | unpack_elem "~" = Variable "HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   116
  | unpack_elem "~~" = Variable "ISABELLE_HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   117
  | unpack_elem s =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   118
      (case explode s of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   119
        "$" :: cs => variable_elem cs
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   120
      | cs => basic_elem cs);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   121
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   122
val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   123
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   124
fun unpack str = Path (norm
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   125
  (case space_explode "/" str of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   126
    "" :: ss => Root :: unpack_elems ss
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   127
  | ss => unpack_elems ss));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   128
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   129
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   130
(* base element *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   131
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   132
fun split_path f (path as Path xs) =
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   133
  (case try split_last xs of
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   134
    Some (prfx, Basic s) => f (prfx, s)
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   135
  | _ => error ("Cannot split path into dir/base: " ^ quote (pack path)));
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   136
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   137
val base = split_path (fn (_, s) => Path [Basic s]);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   138
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   139
fun ext "" path = path
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   140
  | ext e path = split_path (fn (prfx, s) => append (Path prfx) (basic (s ^ "." ^ e))) path;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   141
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   142
val drop_ext = split_path (fn (prfx, s) => append (Path prfx)
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   143
  (case take_suffix (not_equal ".") (explode s) of
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   144
    ([], _) => Path [Basic s]
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   145
  | (cs, _) => Path [Basic (implode (take (length cs - 1, cs)))]));
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   146
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   147
val dir = split_path (fn (prfx, _) => Path prfx);
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   148
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   149
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   150
(* evaluate variables *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   151
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   152
fun eval env (Variable s) = rep (env s)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   153
  | eval _ x = [x];
5011
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
fun evaluate env (Path xs) =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   156
  Path (norm (flat (map (eval env) xs)));
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   157
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   158
val expand = evaluate (unpack o getenv);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   159
7714
e6aa4fca983e added position;
wenzelm
parents: 6460
diff changeset
   160
val position = Position.line_name 1 o quote o pack o expand;
e6aa4fca983e added position;
wenzelm
parents: 6460
diff changeset
   161
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   162
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   163
end;