-------------------------------- MODULE Hex --------------------------------
EXTENDS
Naturals,
Sequences,
TLC
VARIABLES
hexValue
HexDigit == 0 .. 15
Hex == Seq(HexDigit)
HexChar ==
<<"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F">>
RECURSIVE HexToString(_)
HexToString(hex) ==
IF hex = <<>>
THEN ""
ELSE HexChar[Head(hex) + 1] \o HexToString(Tail(hex))
TypeInvariant ==
/\ hexValue \in Hex
Init ==
/\ hexValue = <<0>>
RECURSIVE IncrementedHexValue(_)
IncrementedHexValue(hex) ==
LET
prefix == SubSeq(hex, 1, Len(hex) - 1)
last == hex[Len(hex)]
IN
IF hex = <<>>
THEN <<1>>
ELSE
IF last < 15
THEN Append(prefix, last + 1)
ELSE Append(IncrementedHexValue(prefix), 0)
Increment ==
/\ Print(HexToString(hexValue), TRUE)
/\ hexValue' = IncrementedHexValue(hexValue)
Next ==
\/ Increment
=============================================================================
Another approach is to consider hex values as simple natural numbers, and operate on them as natural numbers. You can then write a function to convert from natural number to hex string representation.
As an aside, this was my first encounter with string manipulation in TLA+ - I wish TLC treated them as sequences!