1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
<HTML>
<HEAD>
<TITLE>The Hugs-GHC Extension Libraries: Word </TITLE>
</HEAD>
<BODY>
<A HREF="libs-4.html">Previous</A>
<A HREF="libs-6.html">Next</A>
<A HREF="libs.html#toc5">Table of Contents</A>
<HR>
<H2><A NAME="s5">5. Word </A></H2>
<P>This library provides unsigned integers of various sizes.
The types supported are as follows:</P>
<P>
<BR>
type number of bits <BR>
<HR>
Word8 8 <BR>
Word16 16 <BR>
Word32 32 <BR>
Word64 64 <BR>
<HR>
</P>
<P>For each type <I>W</I> above, we provide the following functions and
instances. The type <I>I</I> refers to the signed integer type of the
same size.</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
data W -- Unsigned Ints
instance Eq W
instance Ord W
instance Show W
instance Read W
instance Bounded W
instance Num W
instance Real W
instance Integral W
instance Enum W
instance Ix W
instance Bits W
</PRE>
</CODE></BLOCKQUOTE>
Plus
<BLOCKQUOTE><CODE>
<PRE>
word8ToWord32 :: Word8 -> Word32
word32ToWord8 :: Word32 -> Word8
word16ToWord32 :: Word16 -> Word32
word32ToWord16 :: Word32 -> Word16
word8ToInt :: Word8 -> Int
intToWord8 :: Int -> Word8
word16ToInt :: Word16 -> Int
intToWord16 :: Int -> Word16
word32ToInt :: Word32 -> Int
intToWord32 :: Int -> Word32
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Notes:
<UL>
<LI>All arithmetic is performed modulo 2ˆn
One non-obvious consequequence of this is that <CODE>negate</CODE>
should <EM>not</EM> raise an error on negative arguments.
</LI>
<LI>The coercion <CODE>wToI</CODE> converts an unsigned n-bit value to the
signed n-bit value with the same representation. For example,
<CODE>word8ToInt8 0xff = -1</CODE>.
Likewise, <CODE>iToW</CODE> converts signed n-bit values to the
corresponding unsigned n-bit value.
</LI>
<LI>ToDo: complete the set of coercion functions.
</LI>
<LI>Use <CODE>Prelude.fromIntegral :: (Integral a, Num b) => a -> b</CODE> to
coerce between different sizes or to preserve sign when converting
between values of the same size.
</LI>
<LI>It would be very natural to add a type a type <CODE>Natural</CODE> providing
an unbounded size unsigned integer --- just as <CODE>Integer</CODE> provides
unbounded size signed integers. We do not do that yet since there is
no demand for it. Doing so would require <CODE>Bits.bitSize</CODE> to return
<CODE>Maybe Int</CODE>.
</LI>
<LI>The <CODE>Enum</CODE> instances stop when they reach their upper or lower
bound --- they don't overflow the way the <CODE>Int</CODE> and <CODE>Float</CODE>
instances do.
</LI>
<LI>It would be useful to provide a function (or a family of functions?)
which coerced between any two Word types (without going through
Integer).
</LI>
</UL>
</P>
<P>Hugs only provides <CODE>Eq</CODE>, <CODE>Ord</CODE>, <CODE>Read</CODE> and <CODE>Show</CODE>
instances for <CODE>Word64</CODE> at the moment.</P>
<HR>
<A HREF="libs-4.html">Previous</A>
<A HREF="libs-6.html">Next</A>
<A HREF="libs.html#toc5">Table of Contents</A>
</BODY>
</HTML>
|