10

A fresh Mathematica session with

$Version
"11.0.1 for Microsoft Windows (64-bit) (September 20, 2016)"

produces the following results.

Names["x*"]
(* {} *)
Table[Unique["x"], 4]
(* {x3, x4, x5, x6} *)
Names["x*"]
(* {"x3", "x4", "x5", "x6"} *)

The first thing one notices is that, according to the Mathematica documentation, the results of Unique should be

(* {x1, x2, x3, x4} *)

In fact, even executing (in a fresh session) the first basic example in the Unique documentation changes Out[1] from x1 to x3. To continue,

Remove["x*"]
Names["x*"]
(* {} *)

apparently removes all traces of the x symbols, but it does not reset the Unique counter

Table[Unique["x"], 4]
(* {x7, x8, x9, x10} *)

The counter even persists across different families of symbols.

Table[Unique["z"], 4]
(* {z11, z12, z13, z14} *)

It also persists across contexts, but not across kernels.

How can this Unique counter (which is not $ModuleNumber) be reset? And, is this a bug?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
  • According to the docs, Unique is using the same $ModuleNumber counter as Module. – Leonid Shifrin Oct 10 '16 at 16:04
  • 3
    @LeonidShifrin not for strings. – Feyre Oct 10 '16 at 16:05
  • 2
    I get the same behavior going back to version 9 – Jason B. Oct 10 '16 at 16:15
  • @Feyre Indeed, my bad. – Leonid Shifrin Oct 10 '16 at 16:31
  • Documentation doesn't promise that you will have full control over this counter or that internal procedures won't use Unique["string"]. If you want something you can rely on, try this: http://mathematica.stackexchange.com/a/103751/5478 – Kuba Oct 10 '16 at 17:55
  • 1
    @Kuba Thanks for the custom function to generate unique sequential symbols. Nonetheless, I believe that either Unique or its documentation has a problem, which should be fixed. – bbgodfrey Oct 10 '16 at 18:00
  • @Kuba nonetheless it claims that "Unique["x"] uses the first unused symbol of the form xnnn". One would assume the "first" one in a new kernel is the one where nnn is the first positive integer. – Feyre Oct 10 '16 at 18:23
  • @Feyre I don't know, then you could agrue that $ModuleNumber should be 1 for a fresh kernel too. Both cases probably start with 1 but are used in autoloaded packages or something, why not really. Maybe built in packages should not interfere but there are more problems with documentation that are higher on my concerns' list :) – Kuba Oct 10 '16 at 19:56
  • in a related manner is evaluating the symbol $ModuleNumber supposed to change its value? It does in strange ways and I see nothing in the docs to say it should. – george2079 Oct 11 '16 at 20:49
  • 1
    Same behavior on 8.0.4 Mac OS X 10.11 – QuantumDot Oct 13 '16 at 02:39

0 Answers0