src/Pure/General/binding.ML
changeset 59886 e0dc738eb08c
parent 59885 3470a265d404
child 59939 7d46aa03696e
equal deleted inserted replaced
59885:3470a265d404 59886:e0dc738eb08c
     7 
     7 
     8 type bstring = string;    (*primitive names to be bound*)
     8 type bstring = string;    (*primitive names to be bound*)
     9 
     9 
    10 signature BINDING =
    10 signature BINDING =
    11 sig
    11 sig
       
    12   eqtype scope
       
    13   val new_scope: unit -> scope
    12   eqtype binding
    14   eqtype binding
    13   val path_of: binding -> (string * bool) list
    15   val path_of: binding -> (string * bool) list
    14   val make: bstring * Position.T -> binding
    16   val make: bstring * Position.T -> binding
    15   val pos_of: binding -> Position.T
    17   val pos_of: binding -> Position.T
    16   val set_pos: Position.T -> binding -> binding
    18   val set_pos: Position.T -> binding -> binding
    26   val qualified: bool -> string -> binding -> binding
    28   val qualified: bool -> string -> binding -> binding
    27   val qualified_name: string -> binding
    29   val qualified_name: string -> binding
    28   val prefix_of: binding -> (string * bool) list
    30   val prefix_of: binding -> (string * bool) list
    29   val map_prefix: ((string * bool) list -> (string * bool) list) -> binding -> binding
    31   val map_prefix: ((string * bool) list -> (string * bool) list) -> binding -> binding
    30   val prefix: bool -> string -> binding -> binding
    32   val prefix: bool -> string -> binding -> binding
    31   val private: binding -> binding
    33   val private: scope -> binding -> binding
       
    34   val private_default: scope option -> binding -> binding
    32   val concealed: binding -> binding
    35   val concealed: binding -> binding
    33   val pretty: binding -> Pretty.T
    36   val pretty: binding -> Pretty.T
    34   val print: binding -> string
    37   val print: binding -> string
    35   val pp: binding -> Pretty.T
    38   val pp: binding -> Pretty.T
    36   val bad: binding -> string
    39   val bad: binding -> string
    37   val check: binding -> unit
    40   val check: binding -> unit
    38   val name_spec: (string * bool) list -> binding ->
    41   val name_spec: scope list -> (string * bool) list -> binding ->
    39     {private: bool, concealed: bool, spec: (string * bool) list}
    42     {accessible: bool, concealed: bool, spec: (string * bool) list}
    40 end;
    43 end;
    41 
    44 
    42 structure Binding: BINDING =
    45 structure Binding: BINDING =
    43 struct
    46 struct
    44 
    47 
    45 (** representation **)
    48 (** representation **)
    46 
    49 
    47 (* datatype *)
    50 (* scope of private entries *)
       
    51 
       
    52 datatype scope = Scope of serial;
       
    53 
       
    54 fun new_scope () = Scope (serial ());
       
    55 
       
    56 
       
    57 (* binding *)
    48 
    58 
    49 datatype binding = Binding of
    59 datatype binding = Binding of
    50  {private: bool,                    (*entry is strictly private -- no name space accesses*)
    60  {private: scope option,            (*entry is strictly private within its scope*)
    51   concealed: bool,                  (*entry is for foundational purposes -- please ignore*)
    61   concealed: bool,                  (*entry is for foundational purposes -- please ignore*)
    52   prefix: (string * bool) list,     (*system prefix*)
    62   prefix: (string * bool) list,     (*system prefix*)
    53   qualifier: (string * bool) list,  (*user qualifier*)
    63   qualifier: (string * bool) list,  (*user qualifier*)
    54   name: bstring,                    (*base name*)
    64   name: bstring,                    (*base name*)
    55   pos: Position.T};                 (*source position*)
    65   pos: Position.T};                 (*source position*)
    67 
    77 
    68 (** basic operations **)
    78 (** basic operations **)
    69 
    79 
    70 (* name and position *)
    80 (* name and position *)
    71 
    81 
    72 fun make (name, pos) = make_binding (false, false, [], [], name, pos);
    82 fun make (name, pos) = make_binding (NONE, false, [], [], name, pos);
    73 
    83 
    74 fun pos_of (Binding {pos, ...}) = pos;
    84 fun pos_of (Binding {pos, ...}) = pos;
    75 fun set_pos pos =
    85 fun set_pos pos =
    76   map_binding (fn (private, concealed, prefix, qualifier, name, _) =>
    86   map_binding (fn (private, concealed, prefix, qualifier, name, _) =>
    77     (private, concealed, prefix, qualifier, name, pos));
    87     (private, concealed, prefix, qualifier, name, pos));
   105     in (private, concealed, prefix, qualifier', name', pos) end);
   115     in (private, concealed, prefix, qualifier', name', pos) end);
   106 
   116 
   107 fun qualified_name "" = empty
   117 fun qualified_name "" = empty
   108   | qualified_name s =
   118   | qualified_name s =
   109       let val (qualifier, name) = split_last (Long_Name.explode s)
   119       let val (qualifier, name) = split_last (Long_Name.explode s)
   110       in make_binding (false, false, [], map (rpair false) qualifier, name, Position.none) end;
   120       in make_binding (NONE, false, [], map (rpair false) qualifier, name, Position.none) end;
   111 
   121 
   112 
   122 
   113 (* system prefix *)
   123 (* system prefix *)
   114 
   124 
   115 fun prefix_of (Binding {prefix, ...}) = prefix;
   125 fun prefix_of (Binding {prefix, ...}) = prefix;
   122   | prefix mandatory prfx = map_prefix (cons (prfx, mandatory));
   132   | prefix mandatory prfx = map_prefix (cons (prfx, mandatory));
   123 
   133 
   124 
   134 
   125 (* visibility flags *)
   135 (* visibility flags *)
   126 
   136 
   127 val private =
   137 fun private scope =
   128   map_binding (fn (_, concealed, prefix, qualifier, name, pos) =>
   138   map_binding (fn (_, concealed, prefix, qualifier, name, pos) =>
   129     (true, concealed, prefix, qualifier, name, pos));
   139     (SOME scope, concealed, prefix, qualifier, name, pos));
       
   140 
       
   141 fun private_default private' =
       
   142   map_binding (fn (private, concealed, prefix, qualifier, name, pos) =>
       
   143     (if is_some private then private else private', concealed, prefix, qualifier, name, pos));
   130 
   144 
   131 val concealed =
   145 val concealed =
   132   map_binding (fn (private, _, prefix, qualifier, name, pos) =>
   146   map_binding (fn (private, _, prefix, qualifier, name, pos) =>
   133     (private, true, prefix, qualifier, name, pos));
   147     (private, true, prefix, qualifier, name, pos));
   134 
   148 
   159 
   173 
   160 (** resulting name_spec **)
   174 (** resulting name_spec **)
   161 
   175 
   162 val bad_specs = ["", "??", "__"];
   176 val bad_specs = ["", "??", "__"];
   163 
   177 
   164 fun name_spec path binding =
   178 fun name_spec scopes path binding =
   165   let
   179   let
   166     val Binding {private, concealed, prefix, qualifier, name, ...} = binding;
   180     val Binding {private, concealed, prefix, qualifier, name, ...} = binding;
   167     val _ = Long_Name.is_qualified name andalso error (bad binding);
   181     val _ = Long_Name.is_qualified name andalso error (bad binding);
       
   182 
       
   183     val accessible =
       
   184       (case private of
       
   185         NONE => true
       
   186       | SOME scope => member (op =) scopes scope);
   168 
   187 
   169     val spec1 =
   188     val spec1 =
   170       maps (fn (a, b) => map (rpair b) (Long_Name.explode a)) (path @ prefix @ qualifier);
   189       maps (fn (a, b) => map (rpair b) (Long_Name.explode a)) (path @ prefix @ qualifier);
   171     val spec2 = if name = "" then [] else [(name, true)];
   190     val spec2 = if name = "" then [] else [(name, true)];
   172     val spec = spec1 @ spec2;
   191     val spec = spec1 @ spec2;
   173     val _ =
   192     val _ =
   174       exists (fn (a, _) => member (op =) bad_specs a orelse exists_string (fn s => s = "\"") a) spec
   193       exists (fn (a, _) => member (op =) bad_specs a orelse exists_string (fn s => s = "\"") a) spec
   175       andalso error (bad binding);
   194       andalso error (bad binding);
   176   in {private = private, concealed = concealed, spec = if null spec2 then [] else spec} end;
   195   in {accessible = accessible, concealed = concealed, spec = if null spec2 then [] else spec} end;
   177 
   196 
   178 end;
   197 end;
   179 
   198 
   180 type binding = Binding.binding;
   199 type binding = Binding.binding;