src/Pure/General/path.ML
author wenzelm
Wed, 03 Feb 1999 16:36:38 +0100
changeset 6183 ca3ff2fee318
parent 6118 caa439435666
child 6187 c6c4626ef693
permissions -rw-r--r--
more abstract implementation;
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
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    21
  val pack: T -> string
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    22
  val unpack: string -> T
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    23
  val base: T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    24
  val ext: string -> T -> T
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    25
(* FIXME  val evaluate: (string -> T) -> T -> T	*)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    26
  val expand: T -> T
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    27
end;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    28
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    29
structure Path(* FIXME : PATH *) =
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    30
struct
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    31
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    32
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    33
(* path elements *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    34
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    35
datatype elem = Root | Parent | Basic of string | Variable of string;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    36
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    37
fun no_meta_chars chs =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    38
  (case ["/", "\\", "$", "~"] inter_string chs of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    39
    [] => chs
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    40
  | bads => error ("Illegal character(s) " ^ commas_quote bads ^
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    41
      " in path element specification: " ^ quote (implode chs)));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    42
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    43
val basic_elem = Basic o implode o no_meta_chars;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    44
val variable_elem = Variable o implode o no_meta_chars;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    45
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    46
fun is_var (Variable _) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    47
  | is_var _ = false;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    48
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    49
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    50
(* type path *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    51
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    52
datatype T = Path of elem list;
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    53
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    54
fun rep (Path xs) = xs;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    55
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    56
val current = Path [];
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    57
val root = Path [Root];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    58
val parent = Path [Parent];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    59
fun basic s = Path [basic_elem (explode s)];
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    60
fun variable s = Path [variable_elem (explode s)];
5011
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
fun is_absolute (Path (Root :: _)) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    63
  | is_absolute _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    64
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    65
fun is_basic (Path [Basic _]) = true
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    66
  | is_basic _ = false;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    67
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    68
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    69
(* append and norm *)
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    70
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    71
(*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
    72
fun rev_app xs [] = rev xs
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    73
  | rev_app _ (Root :: ys) = rev_app [Root] ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    74
  | rev_app (x :: xs) (Parent :: ys) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    75
      if x = Parent orelse is_var x then rev_app (Parent :: x :: xs) ys
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    76
      else if x = Root then rev_app (x :: xs) ys
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    77
      else rev_app xs ys
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    78
  | rev_app xs (y :: ys) = rev_app (y :: xs) ys;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    79
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    80
fun append (Path xs) (Path ys) = Path (rev_app (rev xs) ys);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    81
fun norm path = rev_app [] path;
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    82
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    83
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    84
(* pack *)
5011
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 pack_elem Root = ""
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    87
  | pack_elem Parent = ".."
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    88
  | pack_elem (Basic s) = s
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    89
  | pack_elem (Variable s) = "$" ^ s;
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    90
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    91
fun pack (Path []) = "."
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    92
  | pack (Path (Root :: xs)) = "/" ^ space_implode "/" (map pack_elem xs)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    93
  | pack (Path xs) = space_implode "/" (map pack_elem xs);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    94
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
    95
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    96
(* unpack *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    97
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    98
fun unpack_elem "" = Root
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
    99
  | unpack_elem ".." = Parent
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   100
  | unpack_elem "~" = Variable "HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   101
  | unpack_elem "~~" = Variable "ISABELLE_HOME"
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   102
  | unpack_elem s =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   103
      (case explode s of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   104
        "$" :: cs => variable_elem cs
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   105
      | cs => basic_elem cs);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   106
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   107
val unpack_elems = map unpack_elem o filter_out (equal "" orf equal ".");
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   108
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   109
fun unpack str = Path (norm
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   110
  (case space_explode "/" str of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   111
    "" :: ss => Root :: unpack_elems ss
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   112
  | ss => unpack_elems ss));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   113
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   114
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   115
(* base element *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   116
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   117
fun err_no_base path =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   118
  error ("No base path element in " ^ quote (pack path));
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   119
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   120
fun base (path as Path xs) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   121
  (case try split_last xs of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   122
    Some (_, x as Basic _) => Path [x]
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   123
  | _ => err_no_base path);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   124
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   125
fun ext e (path as Path xs) =
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   126
  (case try split_last xs of
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   127
    Some (prfx, Basic s) => if e = "" then path else Path (prfx @ [Basic (s ^ "." ^ e)])
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   128
  | _ => err_no_base path);
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   129
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   130
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   131
(* evaluate variables *)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   132
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   133
fun eval env (Variable s) = rep (env s)
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   134
  | eval _ x = [x];
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   135
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   136
fun evaluate env (Path xs) =
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   137
  Path (norm (flat (map (eval env) xs)));
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   138
6183
ca3ff2fee318 more abstract implementation;
wenzelm
parents: 6118
diff changeset
   139
val expand = evaluate (unpack o getenv);
5011
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   140
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   141
37c253fd3dc6 moved Thy/path.ML to General/path.ML;
wenzelm
parents:
diff changeset
   142
end;