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