src/Pure/General/path.ML
author wenzelm
Thu, 15 Oct 2020 13:47:55 +0200
changeset 72479 7d0861af3cb0
parent 72162 5894859c5c84
child 72511 460d743010bc
permissions -rw-r--r--
proper support for Windows exe;
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
    Author:     Markus Wenzel, TU Muenchen
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
     3
43601
fd650d659275 tuned comments;
wenzelm
parents: 43599
diff changeset
     4
Algebra of file-system paths: basic POSIX notation, extended by named
fd650d659275 tuned comments;
wenzelm
parents: 43599
diff changeset
     5
roots (e.g. //foo) and variables (e.g. $BAR).
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
  eqtype T
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    11
  val is_current: T -> bool
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    12
  val current: T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    13
  val root: T
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    14
  val named_root: string -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    15
  val parent: T
69670
114ae60c4be7 clarified signature;
wenzelm
parents: 69627
diff changeset
    16
  val make: string list -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    17
  val basic: string -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    18
  val variable: string -> T
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents: 69366
diff changeset
    19
  val has_parent: T -> bool
6183
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
56533
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
    22
  val starts_basic: T -> bool
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    23
  val append: T -> T -> T
6270
c905fe5994a2 val appends: T list -> T;
wenzelm
parents: 6223
diff changeset
    24
  val appends: T list -> T
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
    25
  val implode: T -> string
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
    26
  val explode: string -> T
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    27
  val explode_pos: string * Position.T -> T * Position.T
59363
4660b0409096 added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents: 56533
diff changeset
    28
  val decode: T XML.Decode.T
53045
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
    29
  val split: string -> T list
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
    30
  val pretty: T -> Pretty.T
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
    31
  val print: T -> string
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
    32
  val dir: T -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    33
  val base: T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    34
  val ext: string -> T -> T
72479
7d0861af3cb0 proper support for Windows exe;
wenzelm
parents: 72162
diff changeset
    35
  val platform_exe: T -> T
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
    36
  val split_ext: T -> T * string
70013
6de8b7a5cd44 tuned signature -- more operations;
wenzelm
parents: 69784
diff changeset
    37
  val exe: T -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    38
  val expand: T -> T
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 68519
diff changeset
    39
  val file_name: T -> string
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
    40
  val implode_symbolic: T -> string
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
    41
  val position: T -> Position.T
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    42
  eqtype binding
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    43
  val binding: T * Position.T -> binding
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    44
  val binding0: T -> binding
70020
0cb334eee651 tuned signature;
wenzelm
parents: 70015
diff changeset
    45
  val map_binding: (T -> T) -> binding -> binding
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    46
  val dest_binding: binding -> T * Position.T
70048
198bbe73b100 tuned signature;
wenzelm
parents: 70020
diff changeset
    47
  val path_of_binding: binding -> T
198bbe73b100 tuned signature;
wenzelm
parents: 70020
diff changeset
    48
  val pos_of_binding: binding -> Position.T
70055
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
    49
  val proper_binding: binding -> unit
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    50
  val implode_binding: binding -> string
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    51
  val explode_binding: string * Position.T -> binding
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
    52
  val explode_binding0: string -> binding
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    53
end;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    54
6187
c6c4626ef693 enabled sig;
wenzelm
parents: 6183
diff changeset
    55
structure Path: PATH =
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    56
struct
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
(* path elements *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    59
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    60
datatype elem =
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    61
  Root of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    62
  Basic of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    63
  Variable of string |
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    64
  Parent;
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    65
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    66
local
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    67
69547
wenzelm
parents: 69507
diff changeset
    68
fun err_elem msg s = error (msg ^ " path element " ^ quote s);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    69
69548
415dc92050a6 more strict check: avoid confusion of Path.basic with Path.current / Path.parent;
wenzelm
parents: 69547
diff changeset
    70
val illegal_elem = ["", "~", "~~", ".", ".."];
69550
57ff523d9008 reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents: 69549
diff changeset
    71
val illegal_char = ["/", "\\", "$", ":", "\"", "'", "<", ">", "|", "?", "*"];
69547
wenzelm
parents: 69507
diff changeset
    72
69627
3e26471d6d01 clarified Path.check_elem;
wenzelm
parents: 69550
diff changeset
    73
fun check_elem s =
69547
wenzelm
parents: 69507
diff changeset
    74
  if member (op =) illegal_elem s then err_elem "Illegal" s
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
    75
  else
69549
wenzelm
parents: 69548
diff changeset
    76
    (s, ()) |-> fold_string (fn c => fn () =>
69550
57ff523d9008 reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents: 69549
diff changeset
    77
      if ord c < 32 then
57ff523d9008 reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents: 69549
diff changeset
    78
        err_elem ("Illegal control character " ^ string_of_int (ord c) ^ " in") s
57ff523d9008 reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents: 69549
diff changeset
    79
      else if member (op =) illegal_char c then
69547
wenzelm
parents: 69507
diff changeset
    80
        err_elem ("Illegal character " ^ quote c ^ " in") s
69627
3e26471d6d01 clarified Path.check_elem;
wenzelm
parents: 69550
diff changeset
    81
      else ());
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    82
69784
24bbc4e30e5b clarified signature: Path.T as in Generated_Files;
wenzelm
parents: 69670
diff changeset
    83
in
24bbc4e30e5b clarified signature: Path.T as in Generated_Files;
wenzelm
parents: 69670
diff changeset
    84
69627
3e26471d6d01 clarified Path.check_elem;
wenzelm
parents: 69550
diff changeset
    85
val root_elem = Root o tap check_elem;
3e26471d6d01 clarified Path.check_elem;
wenzelm
parents: 69550
diff changeset
    86
val basic_elem = Basic o tap check_elem;
3e26471d6d01 clarified Path.check_elem;
wenzelm
parents: 69550
diff changeset
    87
val variable_elem = Variable o tap check_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    88
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    89
end;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    90
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    91
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    92
(* type path *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    93
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
    94
datatype T = Path of elem list;    (*reversed elements*)
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    95
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    96
fun rep (Path xs) = xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    97
6460
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    98
fun is_current (Path []) = true
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
    99
  | is_current _ = false;
cb8c85435228 added is_current;
wenzelm
parents: 6319
diff changeset
   100
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   101
val current = Path [];
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   102
val root = Path [Root ""];
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   103
fun named_root s = Path [root_elem s];
69670
114ae60c4be7 clarified signature;
wenzelm
parents: 69627
diff changeset
   104
val make = Path o rev o map basic_elem;
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   105
fun basic s = Path [basic_elem s];
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   106
fun variable s = Path [variable_elem s];
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   107
val parent = Path [Parent];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   108
69381
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents: 69366
diff changeset
   109
fun has_parent (Path xs) = exists (fn Parent => true | _ => false) xs;
4c9b4e2c5460 more general command 'generate_file' for registered file types, notably Haskell;
wenzelm
parents: 69366
diff changeset
   110
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   111
fun is_absolute (Path xs) =
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   112
  (case try List.last xs of
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   113
    SOME (Root _) => true
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   114
  | _ => false);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   115
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   116
fun is_basic (Path [Basic _]) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   117
  | is_basic _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   118
69784
24bbc4e30e5b clarified signature: Path.T as in Generated_Files;
wenzelm
parents: 69670
diff changeset
   119
fun all_basic (Path elems) =
24bbc4e30e5b clarified signature: Path.T as in Generated_Files;
wenzelm
parents: 69670
diff changeset
   120
  forall (fn Basic _ => true | _ => false) elems;
24bbc4e30e5b clarified signature: Path.T as in Generated_Files;
wenzelm
parents: 69670
diff changeset
   121
56533
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
   122
fun starts_basic (Path xs) =
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
   123
  (case try List.last xs of
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
   124
    SOME (Basic _) => true
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
   125
  | _ => false);
cd8b6d849b6a explicit 'document_files' in session ROOT specifications;
wenzelm
parents: 56136
diff changeset
   126
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   127
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   128
(* append and norm *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   129
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   130
fun apply (y as Root _) _ = [y]
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   131
  | apply Parent (xs as (Root _ :: _)) = xs
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   132
  | apply Parent (Basic _ :: rest) = rest
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   133
  | apply y xs = y :: xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   134
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   135
fun append (Path xs) (Path ys) = Path (fold_rev apply ys xs);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   136
fun appends paths = Library.foldl (uncurry append) (current, paths);
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   137
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   138
fun norm elems = fold_rev apply elems [];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   139
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   140
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   141
(* implode *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   142
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   143
local
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   144
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   145
fun implode_elem (Root "") = ""
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   146
  | implode_elem (Root s) = "//" ^ s
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   147
  | implode_elem (Basic s) = s
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   148
  | implode_elem (Variable s) = "$" ^ s
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   149
  | implode_elem Parent = "..";
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   150
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   151
in
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   152
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   153
fun implode_path (Path []) = "."
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   154
  | implode_path (Path [Root ""]) = "/"
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   155
  | implode_path (Path xs) = space_implode "/" (rev (map implode_elem xs));
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   156
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   157
end;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   158
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   159
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   160
(* explode *)
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   161
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   162
fun explode_path str =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   163
  let
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   164
    fun explode_elem s =
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   165
     (if s = ".." then Parent
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   166
      else if s = "~" then Variable "USER_HOME"
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   167
      else if s = "~~" then Variable "ISABELLE_HOME"
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   168
      else
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   169
        (case try (unprefix "$") s of
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   170
          SOME s' => variable_elem s'
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   171
        | NONE => basic_elem s))
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   172
      handle ERROR msg => cat_error msg ("The error(s) above occurred in " ^ quote str);
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   173
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   174
    val (roots, raw_elems) =
67522
9e712280cc37 clarified take/drop/chop prefix/suffix;
wenzelm
parents: 65999
diff changeset
   175
      (case chop_prefix (equal "") (space_explode "/" str) |>> length of
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   176
        (0, es) => ([], es)
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   177
      | (1, es) => ([Root ""], es)
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   178
      | (_, []) => ([Root ""], [])
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   179
      | (_, e :: es) => ([root_elem e], es));
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   180
    val elems = raw_elems |> filter_out (fn c => c = "" orelse c = ".") |> map explode_elem;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   181
52106
090a519982e9 tuned messages;
wenzelm
parents: 50201
diff changeset
   182
  in Path (norm (rev elems @ roots)) end;
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   183
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   184
fun explode_pos (s, pos) =
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   185
  (explode_path s handle ERROR msg => error (msg ^ Position.here pos), pos);
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   186
53045
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   187
fun split str =
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   188
  space_explode ":" str
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   189
  |> map_filter (fn s => if s = "" then NONE else SOME (explode_path s));
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 52106
diff changeset
   190
59363
4660b0409096 added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents: 56533
diff changeset
   191
val decode = XML.Decode.string #> explode_path;
4660b0409096 added Path.decode in ML, in correspondence to Path.encode in Scala;
wenzelm
parents: 56533
diff changeset
   192
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   193
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   194
(* print *)
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   195
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   196
fun pretty path =
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   197
  let val s = implode_path path
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 48866
diff changeset
   198
  in Pretty.mark (Markup.path s) (Pretty.str (quote s)) end;
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   199
61877
276ad4354069 renamed Pretty.str_of to Pretty.unformatted_string_of to emphasize its meaning;
wenzelm
parents: 59363
diff changeset
   200
val print = Pretty.unformatted_string_of o pretty;
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   201
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62663
diff changeset
   202
val _ = ML_system_pp (fn _ => fn _ => Pretty.to_polyml o pretty);
62663
bea354f6ff21 clarified modules;
wenzelm
parents: 61877
diff changeset
   203
43593
11140987d415 print Path.T with some markup;
wenzelm
parents: 41944
diff changeset
   204
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   205
(* base element *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   206
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   207
fun split_path f (Path (Basic s :: xs)) = f (Path xs, s)
43599
e0ee016fc4fd proper Path.print;
wenzelm
parents: 43593
diff changeset
   208
  | split_path _ path = error ("Cannot split path into dir/base: " ^ print path);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   209
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   210
val dir = split_path #1;
7929
2010ae0393ca added drop_ext;
wenzelm
parents: 7714
diff changeset
   211
val base = split_path (fn (_, s) => Path [Basic s]);
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   212
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   213
fun ext "" = I
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   214
  | ext e = split_path (fn (prfx, s) => append prfx (basic (s ^ "." ^ e)));
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   215
72479
7d0861af3cb0 proper support for Windows exe;
wenzelm
parents: 72162
diff changeset
   216
val platform_exe = ML_System.platform_is_windows ? ext "exe";
7d0861af3cb0 proper support for Windows exe;
wenzelm
parents: 72162
diff changeset
   217
36135
89d1903fbd50 support named_root, which approximates UNC server prefix (for Cygwin);
wenzelm
parents: 33957
diff changeset
   218
val split_ext = split_path (fn (prfx, s) => apfst (append prfx)
67522
9e712280cc37 clarified take/drop/chop prefix/suffix;
wenzelm
parents: 65999
diff changeset
   219
  (case chop_suffix (fn c => c <> ".") (raw_explode s) of
14912
88b9d9165452 added split_ext; removed drop_ext;
wenzelm
parents: 8806
diff changeset
   220
    ([], _) => (Path [Basic s], "")
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   221
  | (cs, e) => (Path [Basic (implode (take (length cs - 1) cs))], implode e)));
6319
80173cb8691c added make, dir;
wenzelm
parents: 6270
diff changeset
   222
70013
6de8b7a5cd44 tuned signature -- more operations;
wenzelm
parents: 69784
diff changeset
   223
val exe = ML_System.platform_is_windows ? ext "exe";
6de8b7a5cd44 tuned signature -- more operations;
wenzelm
parents: 69784
diff changeset
   224
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   225
17827
b32fad049413 expand: error on undefined/empty env variable;
wenzelm
parents: 15570
diff changeset
   226
(* expand variables *)
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   227
48658
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   228
fun eval (Variable s) =
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   229
      let val path = explode_path (getenv_strict s) in
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   230
        if exists (fn Variable _ => true | _ => false) (rep path) then
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   231
          error ("Illegal path variable nesting: " ^ s ^ "=" ^ print path)
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   232
        else rep path
4c7932270d6d reject path variable nesting explicitly;
wenzelm
parents: 48420
diff changeset
   233
      end
17827
b32fad049413 expand: error on undefined/empty env variable;
wenzelm
parents: 15570
diff changeset
   234
  | eval x = [x];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   235
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19305
diff changeset
   236
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: 19482
diff changeset
   237
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 68519
diff changeset
   238
val file_name = implode_path o base o expand;
b6dacf6eabe3 clarified signature;
wenzelm
parents: 68519
diff changeset
   239
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   240
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
   241
(* implode wrt. given directories *)
44863
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   242
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
   243
fun implode_symbolic path =
44863
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   244
  let
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
   245
    val directories = rev (space_explode ":" (getenv "ISABELLE_DIRECTORIES"));
56134
4a7a07c01857 clarified Path.smart_implode;
wenzelm
parents: 53045
diff changeset
   246
    val full_name = implode_path (expand path);
56136
81c66d9e274b even smarter Path.smart_implode;
wenzelm
parents: 56134
diff changeset
   247
    fun fold_path a =
68519
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   248
      (case try (implode_path o expand o explode_path) a of
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   249
        SOME b =>
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   250
          if full_name = b then SOME a
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   251
          else
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   252
            (case try (unprefix (b ^ "/")) full_name of
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   253
              SOME name => SOME (a ^ "/" ^ name)
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   254
            | NONE => NONE)
e1c24b628ca5 smart_implode "$AFP" as well;
wenzelm
parents: 67522
diff changeset
   255
      | NONE => NONE);
44863
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   256
  in
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
   257
    (case get_first fold_path directories of
56136
81c66d9e274b even smarter Path.smart_implode;
wenzelm
parents: 56134
diff changeset
   258
      SOME name => name
81c66d9e274b even smarter Path.smart_implode;
wenzelm
parents: 56134
diff changeset
   259
    | NONE => implode_path path)
44863
49ea566cb3b4 more symbolic file positions via smart replacement of ISABELLE_HOME -- allows Isabelle distribution to be moved later on;
wenzelm
parents: 44161
diff changeset
   260
  end;
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
   261
72162
5894859c5c84 more systematic support for special directories;
wenzelm
parents: 70055
diff changeset
   262
val position = Position.file o implode_symbolic;
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 23863
diff changeset
   263
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   264
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   265
(* binding: strictly monotonic path with position *)
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   266
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   267
datatype binding = Binding of T * Position.T;
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   268
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   269
fun binding (path, pos) =
70055
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   270
  if all_basic path then Binding (path, pos)
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   271
  else error ("Bad path binding: " ^ print path ^ Position.here pos);
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   272
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   273
fun binding0 path = binding (path, Position.none);
70020
0cb334eee651 tuned signature;
wenzelm
parents: 70015
diff changeset
   274
0cb334eee651 tuned signature;
wenzelm
parents: 70015
diff changeset
   275
fun map_binding f (Binding (path, pos)) = binding (f path, pos);
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   276
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   277
fun dest_binding (Binding args) = args;
70048
198bbe73b100 tuned signature;
wenzelm
parents: 70020
diff changeset
   278
val path_of_binding = dest_binding #> #1;
198bbe73b100 tuned signature;
wenzelm
parents: 70020
diff changeset
   279
val pos_of_binding = dest_binding #> #2;
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   280
70055
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   281
fun proper_binding binding =
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   282
  if is_current (path_of_binding binding)
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   283
  then error ("Empty path" ^ Position.here (pos_of_binding binding))
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   284
  else ();
36fb663145e5 type Path.binding may be empty: check later via proper_binding;
wenzelm
parents: 70048
diff changeset
   285
70048
198bbe73b100 tuned signature;
wenzelm
parents: 70020
diff changeset
   286
val implode_binding = path_of_binding #> implode_path;
70015
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   287
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   288
val explode_binding = binding o explode_pos;
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   289
fun explode_binding0 s = explode_binding (s, Position.none);
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   290
c8e08d8ffb93 clarified signature: more explicit type Path.binding;
wenzelm
parents: 70013
diff changeset
   291
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   292
(*final declarations of this structure!*)
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   293
val implode = implode_path;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 19482
diff changeset
   294
val explode = explode_path;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   295
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   296
end;