src/Pure/General/binding.ML
author haftmann
Wed, 21 Jan 2009 23:40:23 +0100
changeset 29609 a010aab5bed0
parent 29581 b3b33e0298eb
child 29617 b36bcbc1be3a
permissions -rw-r--r--
changed import hierarchy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/General/binding.ML
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     3
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     4
Structured name bindings.
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     5
*)
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     6
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     7
signature BASIC_BINDING =
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     8
sig
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29338
diff changeset
     9
  type binding
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    10
  val long_names: bool ref
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    11
  val short_names: bool ref
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    12
  val unique_names: bool ref
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    13
end;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    14
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    15
signature BINDING =
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    16
sig
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    17
  include BASIC_BINDING
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    18
  type T
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    19
  val name_pos: string * Position.T -> T
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    20
  val name: string -> T
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    21
  val empty: T
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    22
  val map_base: (string -> string) -> T -> T
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    23
  val qualify: string -> T -> T
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    24
  val add_prefix: bool -> string -> T -> T
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    25
  val map_prefix: ((string * bool) list -> T -> T) -> T -> T
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    26
  val is_empty: T -> bool
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
    27
  val base_name: T -> string
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    28
  val pos_of: T -> Position.T
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    29
  val dest: T -> (string * bool) list * string
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    30
  val separator: string
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    31
  val is_qualified: string -> bool
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    32
  val display: T -> string
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    33
end
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    34
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    35
structure Binding : BINDING =
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    36
struct
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    37
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    38
(** global flags **)
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    39
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    40
val long_names = ref false;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    41
val short_names = ref false;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    42
val unique_names = ref true;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    43
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    44
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    45
(** qualification **)
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    46
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    47
val separator = ".";
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    48
val is_qualified = exists_string (fn s => s = separator);
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    49
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    50
fun reject_qualified kind s =
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    51
  if is_qualified s then
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    52
    error ("Attempt to declare qualified " ^ kind ^ " " ^ quote s)
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    53
  else s;
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    54
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    55
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    56
(** binding representation **)
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    57
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    58
datatype T = Binding of ((string * bool) list * string) * Position.T;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    59
  (* (prefix components (with mandatory flag), base name, position) *)
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    60
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    61
fun name_pos (name, pos) = Binding (([], name), pos);
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    62
fun name name = name_pos (name, Position.none);
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    63
val empty = name "";
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    64
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    65
fun map_binding f (Binding (prefix_name, pos)) = Binding (f prefix_name, pos);
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    66
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    67
val map_base = map_binding o apsnd;
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    68
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    69
fun qualify_base path name =
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    70
  if path = "" orelse name = "" then name
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    71
  else path ^ separator ^ name;
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    72
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    73
val qualify = map_base o qualify_base;
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
    74
  (*FIXME should all operations on bare names move here from name_space.ML ?*)
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    75
29208
b0c81b9a0133 Use prefix component of bindings for locale prefixes.
ballarin
parents: 29006
diff changeset
    76
fun add_prefix sticky "" b = b
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    77
  | add_prefix sticky prfx b = (map_binding o apfst)
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    78
      (cons ((*reject_qualified "prefix"*) prfx, sticky)) b;
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    79
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    80
fun map_prefix f (Binding ((prefix, name), pos)) =
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    81
  f prefix (name_pos (name, pos));
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    82
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    83
fun is_empty (Binding ((_, name), _)) = name = "";
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
    84
fun base_name (Binding ((_, name), _)) = name;
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    85
fun pos_of (Binding (_, pos)) = pos;
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    86
fun dest (Binding (prefix_name, _)) = prefix_name;
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    87
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    88
fun display (Binding ((prefix, name), _)) =
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    89
  let
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    90
    fun mk_prefix (prfx, true) = prfx
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    91
      | mk_prefix (prfx, false) = enclose "(" ")" prfx
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    92
  in if not (! long_names) orelse null prefix orelse name = "" then name
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    93
    else space_implode "." (map mk_prefix prefix) ^ ":" ^ name
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    94
  end;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    95
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29338
diff changeset
    96
type binding = T;
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29338
diff changeset
    97
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    98
end;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    99
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
   100
structure Basic_Binding : BASIC_BINDING = Binding;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
   101
open Basic_Binding;