src/Pure/General/binding.ML
author wenzelm
Tue, 03 Mar 2009 18:31:59 +0100
changeset 30222 4102bbf2af21
parent 30217 894eb2034f02
child 30232 8f551a13ee99
permissions -rw-r--r--
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings; moved separator/is_qualified from binding.ML back to name_space.ML -- only name space introduces an explicit notation for qualified names; type binding: maintain explicit qualifier, indepently of base name; tuned signature of Binding: renamed name_pos to make, renamed base_name to name_of, renamed map_base to map_name, added mandatory flag to qualify, simplified map_prefix (formerly unused); Binding.str_of: include markup with position properties; misc tuning;
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
30214
f84c9f10292a moved name space externalization flags back to name_space.ML;
wenzelm
parents: 29617
diff changeset
     3
    Author:     Makarius
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     4
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     5
Structured name bindings.
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     6
*)
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
     7
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
     8
type bstring = string;    (*primitive names to be bound*)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
     9
30214
f84c9f10292a moved name space externalization flags back to name_space.ML;
wenzelm
parents: 29617
diff changeset
    10
signature BINDING =
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    11
sig
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29338
diff changeset
    12
  type binding
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    13
  val dest: binding -> (string * bool) list * (string * bool) list * bstring
30217
894eb2034f02 renamed Binding.display to Binding.str_of, which is slightly more canonical;
wenzelm
parents: 30214
diff changeset
    14
  val str_of: binding -> string
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    15
  val make: bstring * Position.T -> binding
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    16
  val name: bstring -> binding
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    17
  val pos_of: binding -> Position.T
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    18
  val name_of: binding -> string
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    19
  val map_name: (bstring -> bstring) -> binding -> binding
29617
b36bcbc1be3a tuned signature;
wenzelm
parents: 29581
diff changeset
    20
  val empty: binding
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    21
  val is_empty: binding -> bool
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    22
  val qualify: bool -> string -> binding -> binding
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    23
  val map_prefix: ((string * bool) list -> (string * bool) list) -> binding -> binding
29617
b36bcbc1be3a tuned signature;
wenzelm
parents: 29581
diff changeset
    24
  val add_prefix: bool -> string -> binding -> binding
b36bcbc1be3a tuned signature;
wenzelm
parents: 29581
diff changeset
    25
end;
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    26
30217
894eb2034f02 renamed Binding.display to Binding.str_of, which is slightly more canonical;
wenzelm
parents: 30214
diff changeset
    27
structure Binding: BINDING =
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    28
struct
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    29
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    30
(** representation **)
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    31
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    32
(* datatype *)
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    33
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    34
type component = string * bool;   (*name with mandatory flag*)
29338
52a384648d13 separator, is_qualified
haftmann
parents: 29208
diff changeset
    35
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    36
datatype binding = Binding of
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    37
 {prefix: component list,         (*system prefix*)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    38
  qualifier: component list,      (*user qualifier*)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    39
  name: bstring,                  (*base name*)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    40
  pos: Position.T};               (*source position*)
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    41
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    42
fun make_binding (prefix, qualifier, name, pos) =
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    43
  Binding {prefix = prefix, qualifier = qualifier, name = name, pos = pos};
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    44
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    45
fun map_binding f (Binding {prefix, qualifier, name, pos}) =
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    46
  make_binding (f (prefix, qualifier, name, pos));
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    47
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    48
fun dest (Binding {prefix, qualifier, name, ...}) = (prefix, qualifier, name);
30217
894eb2034f02 renamed Binding.display to Binding.str_of, which is slightly more canonical;
wenzelm
parents: 30214
diff changeset
    49
894eb2034f02 renamed Binding.display to Binding.str_of, which is slightly more canonical;
wenzelm
parents: 30214
diff changeset
    50
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    51
(* diagnostic output *)
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    52
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    53
val str_of_components = implode o map (fn (s, true) => s ^ "!" | (s, false) => s ^ "?");
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    54
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    55
fun str_of (Binding {prefix, qualifier, name, pos}) =
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    56
  let
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    57
    val text =
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    58
      (if null prefix then "" else enclose "(" ")" (str_of_components prefix)) ^
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    59
      str_of_components qualifier ^ ":" ^ name;
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    60
    val props = Position.properties_of pos;
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    61
  in Markup.markup (Markup.properties props (Markup.binding name)) text end;
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    62
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    63
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    64
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    65
(** basic operations **)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    66
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    67
(* name and position *)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    68
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    69
fun make (name, pos) = make_binding ([], [], name, pos);
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    70
fun name name = make (name, Position.none);
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    71
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    72
fun pos_of (Binding {pos, ...}) = pos;
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    73
fun name_of (Binding {name, ...}) = name;
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    74
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    75
fun map_name f = map_binding (fn (prefix, qualifier, name, pos) => (prefix, qualifier, f name, pos));
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    76
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    77
val empty = name "";
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    78
fun is_empty b = name_of b = "";
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    79
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    80
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    81
(* user qualifier *)
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    82
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    83
fun qualify _ "" = I
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    84
  | qualify mandatory qual = map_binding (fn (prefix, qualifier, name, pos) =>
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    85
      (prefix, (qual, mandatory) :: qualifier, name, pos));
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    86
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
    87
30222
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    88
(* system prefix *)
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    89
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    90
fun map_prefix f = map_binding (fn (prefix, qualifier, name, pos) =>
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    91
  (f prefix, qualifier, name, pos));
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    92
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    93
fun add_prefix _ "" = I
4102bbf2af21 moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents: 30217
diff changeset
    94
  | add_prefix mandatory prfx = map_prefix (cons (prfx, mandatory));
28941
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    95
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    96
end;
128459bd72d2 new Binding module
haftmann
parents:
diff changeset
    97
30214
f84c9f10292a moved name space externalization flags back to name_space.ML;
wenzelm
parents: 29617
diff changeset
    98
type binding = Binding.binding;
f84c9f10292a moved name space externalization flags back to name_space.ML;
wenzelm
parents: 29617
diff changeset
    99