src/Pure/General/name_space.ML
author paulson
Fri, 05 Oct 2007 09:59:03 +0200
changeset 24854 0ebcd575d3c6
parent 24361 52a14669f9e9
child 25072 03f57b516e12
permissions -rw-r--r--
filtering out some package theorems
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5682
diff changeset
     1
(*  Title:      Pure/General/name_space.ML
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
     4
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
     5
Generic name spaces with declared and hidden entries.  Unknown names
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
     6
are considered global; no support for absolute addressing.
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
     7
*)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
     8
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
     9
type bstring = string;    (*names to be bound / internalized*)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    10
type xstring = string;    (*externalized names / to be printed*)
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    11
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    12
signature BASIC_NAME_SPACE =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    13
sig
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    14
  val long_names: bool ref
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    15
  val short_names: bool ref
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    16
  val unique_names: bool ref
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    17
end;
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    18
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    19
signature NAME_SPACE =
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    20
sig
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    21
  include BASIC_NAME_SPACE
9120
04a46ceace35 export hidden: string -> string;
wenzelm
parents: 8806
diff changeset
    22
  val hidden: string -> string
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    23
  val separator: string                 (*single char*)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    24
  val is_qualified: string -> bool
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    25
  val implode: string list -> string
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    26
  val explode: string -> string list
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    27
  val append: string -> string -> string
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    28
  val qualified: string -> string -> string
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    29
  val base: string -> string
20530
448594cbd82b renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents: 19367
diff changeset
    30
  val qualifier: string -> string
11829
f252646080fc added map_base;
wenzelm
parents: 9120
diff changeset
    31
  val map_base: (string -> string) -> string -> string
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    32
  val suffixes_prefixes: 'a list -> 'a list list
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    33
  val suffixes_prefixes_split: int -> int -> 'a list -> 'a list list
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    34
  val accesses: string -> string list
13332
f130bcf29620 added accesses';
wenzelm
parents: 12588
diff changeset
    35
  val accesses': string -> string list
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    36
  type T
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    37
  val empty: T
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    38
  val valid_accesses: T -> string -> xstring list
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    39
  val intern: T -> xstring -> string
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    40
  val extern: T -> string -> xstring
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    41
  val hide: bool -> string -> T -> T
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    42
  val merge: T * T -> T
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    43
  type naming
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    44
  val path_of: naming -> string
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    45
  val full: naming -> bstring -> string
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    46
  val declare: naming -> string -> T -> T
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    47
  val default_naming: naming
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    48
  val add_path: string -> naming -> naming
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    49
  val no_base_names: naming -> naming
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    50
  val qualified_names: naming -> naming
19055
4f408867f9d4 removed qualified_force_prefix;
wenzelm
parents: 19030
diff changeset
    51
  val sticky_prefix: string -> naming -> naming
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    52
  val set_policy: (string -> bstring -> string) * (string list -> string list list) ->
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    53
    naming -> naming
24361
52a14669f9e9 tuned signature;
wenzelm
parents: 23668
diff changeset
    54
  type 'a table = T * 'a Symtab.table
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
    55
  val empty_table: 'a table
23086
12320f6e2523 tuned Pure/General/name_space.ML
haftmann
parents: 22057
diff changeset
    56
  val extend_table: naming -> (bstring * 'a) list -> 'a table -> 'a table
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
    57
  val merge_tables: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table
16848
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
    58
  val dest_table: 'a table -> (string * 'a) list
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
    59
  val extern_table: 'a table -> (xstring * 'a) list
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    60
end;
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    61
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    62
structure NameSpace: NAME_SPACE =
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    63
struct
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    64
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    65
(** long identifiers **)
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    66
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    67
fun hidden name = "??." ^ name;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    68
val is_hidden = String.isPrefix "??."
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    69
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    70
val separator = ".";
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 19055
diff changeset
    71
val is_qualified = exists_string (fn s => s = separator);
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    72
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    73
val implode_name = space_implode separator;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    74
val explode_name = space_explode separator;
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    75
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    76
fun append name1 "" = name1
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    77
  | append "" name2 = name2
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    78
  | append name1 name2 = name1 ^ separator ^ name2;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    79
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    80
fun qualified path name =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    81
  if path = "" orelse name = "" then name
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    82
  else path ^ separator ^ name;
11829
f252646080fc added map_base;
wenzelm
parents: 9120
diff changeset
    83
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    84
fun base "" = ""
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    85
  | base name = List.last (explode_name name);
11829
f252646080fc added map_base;
wenzelm
parents: 9120
diff changeset
    86
20530
448594cbd82b renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents: 19367
diff changeset
    87
fun qualifier "" = ""
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    88
  | qualifier name = implode_name (#1 (split_last (explode_name name)));
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
    89
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    90
fun map_base _ "" = ""
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    91
  | map_base f name =
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    92
      let val names = explode_name name
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
    93
      in implode_name (nth_map (length names - 1) f names) end;
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
    94
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    95
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    96
(* accesses *)
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    97
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    98
infixr 6 @@;
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
    99
fun ([] @@ yss) = []
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   100
  | ((xs :: xss) @@ yss) = map (fn ys => xs @ ys) yss @ (xss @@ yss);
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   101
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   102
fun suffixes_prefixes list =
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   103
  let
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   104
    val (xs, ws) = chop (length list - 1) list;
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   105
    val sfxs = suffixes xs @@ [ws];
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   106
    val pfxs = prefixes1 xs @@ [ws];
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   107
  in sfxs @ pfxs end;
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   108
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   109
fun suffixes_prefixes_split i k list =
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   110
  let
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   111
    val (((xs, ys), zs), ws) = list |> chop i ||>> chop k ||>> chop (length list - (i + k + 1));
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   112
    val sfxs =
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   113
      [ys] @@ suffixes zs @@ [ws] @
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   114
      suffixes1 xs @@ [ys @ zs @ ws];
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   115
    val pfxs =
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   116
      prefixes1 xs @@ [ys @ ws] @
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   117
      [xs @ ys] @@ prefixes1 zs @@ [ws];
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   118
  in sfxs @ pfxs end;
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   119
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   120
val accesses = map implode_name o suffixes_prefixes o explode_name;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   121
val accesses' = map implode_name o Library.suffixes1 o explode_name;
13332
f130bcf29620 added accesses';
wenzelm
parents: 12588
diff changeset
   122
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   123
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   124
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   125
(** name spaces **)
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   126
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   127
(* datatype T *)
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   128
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   129
datatype T =
19030
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   130
  NameSpace of ((string list * string list) * stamp) Symtab.table;
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   131
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   132
val empty = NameSpace Symtab.empty;
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   133
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   134
fun lookup (NameSpace tab) xname =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   135
  (case Symtab.lookup tab xname of
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   136
    NONE => (xname, true)
19030
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   137
  | SOME (([], []), _) => (xname, true)
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   138
  | SOME (([name], _), _) => (name, true)
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   139
  | SOME ((name :: _, _), _) => (name, false)
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   140
  | SOME (([], name' :: _), _) => (hidden name', true));
8728
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   141
19030
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   142
fun valid_accesses (NameSpace tab) name = Symtab.fold (fn (xname, ((names, _), _)) =>
16444
80c8f742c6fc Symtab.fold;
wenzelm
parents: 16341
diff changeset
   143
  if not (null names) andalso hd names = name then cons xname else I) tab [];
8728
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   144
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   145
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   146
(* intern and extern *)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   147
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   148
fun intern space xname = #1 (lookup space xname);
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   149
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   150
val long_names = ref false;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   151
val short_names = ref false;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   152
val unique_names = ref true;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   153
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   154
fun extern space name =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   155
  let
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   156
    fun valid unique xname =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   157
      let val (name', uniq) = lookup space xname
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   158
      in name = name' andalso (uniq orelse (not unique)) end;
8728
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   159
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   160
    fun ex [] = if valid false name then name else hidden name
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   161
      | ex (nm :: nms) = if valid (! unique_names) nm then nm else ex nms;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   162
  in
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   163
    if ! long_names then name
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   164
    else if ! short_names then base name
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   165
    else ex (accesses' name)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   166
  end;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   167
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   168
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   169
(* basic operations *)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   170
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   171
fun map_space f xname (NameSpace tab) =
23086
12320f6e2523 tuned Pure/General/name_space.ML
haftmann
parents: 22057
diff changeset
   172
  NameSpace (Symtab.map_default (xname, (([], []), stamp ()))
12320f6e2523 tuned Pure/General/name_space.ML
haftmann
parents: 22057
diff changeset
   173
    (fn (entry, _) => (f entry, stamp ())) tab);
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   174
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   175
fun del (name: string) = remove (op =) name;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   176
fun add name names = name :: del name names;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   177
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   178
val del_name = map_space o apfst o del;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   179
val add_name = map_space o apfst o add;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   180
val add_name' = map_space o apsnd o add;
8728
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   181
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   182
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   183
(* hide *)
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   184
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   185
fun hide fully name space =
8728
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   186
  if not (is_qualified name) then
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   187
    error ("Attempt to hide global name " ^ quote name)
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   188
  else if is_hidden name then
33a9643ba626 improved output of ambiguous entries;
wenzelm
parents: 6845
diff changeset
   189
    error ("Attempt to hide hidden name " ^ quote name)
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   190
  else
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   191
    let val names = valid_accesses space name in
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   192
      space
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   193
      |> add_name' name name
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   194
      |> fold (del_name name) (if fully then names else names inter_string [base name])
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   195
    end;
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   196
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   197
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   198
(* merge *)
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   199
19030
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   200
fun merge (NameSpace tab1, NameSpace tab2) =
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   201
  NameSpace ((tab1, tab2) |> Symtab.join
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   202
    (K (fn (((names1, names1'), stamp1), ((names2, names2'), stamp2)) =>
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   203
      if stamp1 = stamp2 then raise Symtab.SAME
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   204
      else
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   205
        ((Library.merge (op =) (names2, names1),
78d43fe9ac65 low-level tuning of merge: maintain identity of accesses;
wenzelm
parents: 19015
diff changeset
   206
          Library.merge (op =) (names2', names1')), stamp ()))));
6845
598d2f32d452 added cond_extern_table;
wenzelm
parents: 6118
diff changeset
   207
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   208
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   209
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   210
(** naming contexts **)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   211
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   212
(* datatype naming *)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   213
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   214
datatype naming = Naming of
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   215
  string * ((string -> string -> string) * (string list -> string list list));
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   216
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   217
fun path_of (Naming (path, _)) = path;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   218
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   219
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   220
(* declarations *)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   221
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   222
fun full (Naming (path, (qualify, _))) = qualify path;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   223
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   224
fun declare (Naming (_, (_, accs))) name space =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   225
  if is_hidden name then
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   226
    error ("Attempt to declare hidden name " ^ quote name)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   227
  else
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 21914
diff changeset
   228
    let
279b129498b6 removed conditional combinator;
wenzelm
parents: 21914
diff changeset
   229
      val names = explode_name name;
23668
8b5a2a79a6e3 declare: disallow quote (") in names;
wenzelm
parents: 23086
diff changeset
   230
      val _ = (null names orelse exists (fn s => s = "") names
8b5a2a79a6e3 declare: disallow quote (") in names;
wenzelm
parents: 23086
diff changeset
   231
          orelse exists_string (fn s => s = "\"") name) andalso
21962
279b129498b6 removed conditional combinator;
wenzelm
parents: 21914
diff changeset
   232
        error ("Bad name declaration " ^ quote name);
279b129498b6 removed conditional combinator;
wenzelm
parents: 21914
diff changeset
   233
    in fold (add_name name) (map implode_name (accs names)) space end;
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   234
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   235
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   236
(* manipulate namings *)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   237
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   238
fun reject_qualified name =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   239
  if is_qualified name then
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   240
    error ("Attempt to declare qualified name " ^ quote name)
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   241
  else name;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   242
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   243
val default_naming =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   244
  Naming ("", (fn path => qualified path o reject_qualified, suffixes_prefixes));
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   245
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   246
fun add_path elems (Naming (path, policy)) =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   247
  if elems = "//" then Naming ("", (qualified, #2 policy))
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   248
  else if elems = "/" then Naming ("", policy)
20530
448594cbd82b renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents: 19367
diff changeset
   249
  else if elems = ".." then Naming (qualifier path, policy)
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   250
  else Naming (append path elems, policy);
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   251
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   252
fun no_base_names (Naming (path, (qualify, accs))) =
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   253
  Naming (path, (qualify, filter_out (fn [_] => true | _ => false) o accs));
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   254
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   255
fun qualified_names (Naming (path, (_, accs))) = Naming (path, (qualified, accs));
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   256
19055
4f408867f9d4 removed qualified_force_prefix;
wenzelm
parents: 19030
diff changeset
   257
fun sticky_prefix prfx (Naming (path, (qualify, _))) =
4f408867f9d4 removed qualified_force_prefix;
wenzelm
parents: 19030
diff changeset
   258
  Naming (append path prfx,
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   259
    (qualify, suffixes_prefixes_split (length (explode_name path)) (length (explode_name prfx))));
19015
1075db658d91 removed custom_accesses;
wenzelm
parents: 18939
diff changeset
   260
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   261
fun set_policy policy (Naming (path, _)) = Naming (path, policy);
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   262
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   263
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   264
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   265
(** name spaces coupled with symbol tables **)
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   266
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   267
type 'a table = T * 'a Symtab.table;
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   268
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   269
val empty_table = (empty, Symtab.empty);
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   270
23086
12320f6e2523 tuned Pure/General/name_space.ML
haftmann
parents: 22057
diff changeset
   271
fun extend_table naming bentries (space, tab) =
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   272
  let val entries = map (apfst (full naming)) bentries
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   273
  in (fold (declare naming o #1) entries space, Symtab.extend (tab, entries)) end;
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   274
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   275
fun merge_tables eq ((space1, tab1), (space2, tab2)) =
19367
wenzelm
parents: 19305
diff changeset
   276
  (merge (space1, space2), Symtab.merge eq (tab1, tab2));
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   277
16848
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   278
fun ext_table (space, tab) =
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   279
  Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab []
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   280
  |> Library.sort_wrt (#2 o #1);
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   281
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   282
fun dest_table tab = map (apfst #1) (ext_table tab);
1c8a5bb7c688 added dest_table;
wenzelm
parents: 16444
diff changeset
   283
fun extern_table tab = map (apfst #2) (ext_table tab);
16341
e573e5167eda added type NameSpace.table with basic operations;
wenzelm
parents: 16262
diff changeset
   284
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   285
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   286
(*final declarations of this structure!*)
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   287
val implode = implode_name;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   288
val explode = explode_name;
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 20530
diff changeset
   289
5012
086b055c4d73 moved name_space.ML to General/name_space.ML;
wenzelm
parents:
diff changeset
   290
end;
5175
2dbef0104bcf moved long_names / cond_extern to name_space.ML;
wenzelm
parents: 5012
diff changeset
   291
16137
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   292
structure BasicNameSpace: BASIC_NAME_SPACE = NameSpace;
00e9ca1e7261 renamed cond_extern to extern;
wenzelm
parents: 16002
diff changeset
   293
open BasicNameSpace;