| 1 |
\DOC define_word_val |
| 2 |
|
| 3 |
\TYPE {define_word_val : int -> thm list} |
| 4 |
|
| 5 |
\LIBRARY wordn |
| 6 |
|
| 7 |
\SYNOPSIS |
| 8 |
Defines two constants for converting between the type {:wordn} and {:num}. |
| 9 |
|
| 10 |
\DESCRIBE |
| 11 |
{define_word_val} takes a single argument, a positive integer {n}. It |
| 12 |
defines two logical constants for converting between the types |
| 13 |
{:wordn} and {:num}. The constants are named {NVALn} and {NWORDn}, and |
| 14 |
they have the types {:wordn -> num} and {:num -> wordn}, respectively. |
| 15 |
Natural numbers are represented by an n-bit word in a big-endian |
| 16 |
format, for example, {#001} denotes 1 as a 3-bit word, and the |
| 17 |
following expressions are true: |
| 18 |
{ |
| 19 |
NVAL3 #001 = 1 |
| 20 |
NWORD3 1 = #001 |
| 21 |
} |
| 22 |
|
| 23 |
These constants are defined in terms of {VAL} and {WORDN}, which are |
| 24 |
the generic conversion |
| 25 |
functions between the represention type of the n-bit words, namely |
| 26 |
{:(bool)list}, and the type {:num}. The definitions of these generic |
| 27 |
conversion functions are as follows: |
| 28 |
{ |
| 29 |
|- (VAL[] = 0) /\ |
| 30 |
(!b bs. VAL(CONS b bs) = ((BV b) * (2 EXP (LENGTH bs))) + (VAL bs)) |
| 31 |
|- !n m. (LENGTH(WORDN n m) = n) /\ (VAL(WORDN n m) = m MOD (2 EXP n)) |
| 32 |
} |
| 33 |
|
| 34 |
For a particular word length {n}, the conversion from {:wordn} to |
| 35 |
{:num} is defined to be |
| 36 |
{ |
| 37 |
!w. NVALn w = VAL(BITSn w) |
| 38 |
} |
| 39 |
and the conversion from {:num} to {:wordn} is defined to be |
| 40 |
{ |
| 41 |
!m. NWORDn m = WORDn(WORDN n m) |
| 42 |
} |
| 43 |
|
| 44 |
The functions {NVALn} converting words to natural numbers are one-one |
| 45 |
and onto, but the functions {NWORDn} are not one-one due to the finite |
| 46 |
length of a word. From the definition of {WORDN}, the theorem |
| 47 |
{ |
| 48 |
|- !m. NWORDn m = NWORDn (m MOD (2 EXP n)) |
| 49 |
} |
| 50 |
can be deduced. The conversions can be better characterized by the |
| 51 |
theorem |
| 52 |
{ |
| 53 |
|- (!w. NWORDn(NVALn w) = w) /\ |
| 54 |
(!m. m < (2 EXP m) ==> (NVALn(NWORDn m) = m)) |
| 55 |
} |
| 56 |
which can be obtained by calling the function {prove_word_val}. |
| 57 |
|
| 58 |
\FAILURE |
| 59 |
Fails if the type {:wordn} of the specified word length {n} has not |
| 60 |
been defined. |
| 61 |
|
| 62 |
\EXAMPLE |
| 63 |
The following HOL session illustrates the use of this function: |
| 64 |
{ |
| 65 |
# let [NVAL3_DEF; NWORD3_DEF] = define_word_val 3;; |
| 66 |
|
| 67 |
NVAL3_DEF = |- !w. NVAL3 w = VAL(BITS3 w) |
| 68 |
NWORD3_DEF = |- !n. NWORD3 n = WORD3(WORDN 3 n) |
| 69 |
} |
| 70 |
|
| 71 |
\SEEALSO |
| 72 |
{define_wordn_type}, {prove_word_val}. |
| 73 |
|
| 74 |
\ENDDOC |