Probably useful to include a &quot;mkFixed&quot; function example as well, to show how a Fixed can be constructed using the &quot;optimal&quot; data representation:<br><br><span style="font-family:times new roman,serif">-- | Make a fixed field.</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">--</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">-- Note that this type constructor function chooses the minimal type</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">-- representation for the fixed value stored. Unsigned representations</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">-- are preferred over signed.</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">mkFixed :: String -&gt; Int -&gt; Integer -&gt; Member</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">mkFixed name len val</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">  | len &lt;= 0  = error $ &quot;mkFixed &quot; ++ name ++ &quot;: length &lt; 0&quot;</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  | len &lt; 8  &amp;&amp; validUnsigned len val = Fixed name len False (fromIntegral val :: Word8)</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">  | len &lt; 8  &amp;&amp; validSigned   len val = Fixed name len True  (fromIntegral val :: Int8)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  | len &lt; 16 &amp;&amp; validUnsigned len val = Fixed name len False (fromIntegral val :: Word16)</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">  | len &lt; 16 &amp;&amp; validSigned   len val = Fixed name len True  (fromIntegral val :: Int16)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  | len &lt; 32 &amp;&amp; validUnsigned len val = Fixed name len False (fromIntegral val :: Word32)</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">  | len &lt; 32 &amp;&amp; validSigned   len val = Fixed name len True  (fromIntegral val :: Int32)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  | len &lt; 64 &amp;&amp; validUnsigned len val = Fixed name len False (fromIntegral val :: Word64)</span><br style="font-family:times new roman,serif">
<span style="font-family:times new roman,serif">  | len &lt; 64 &amp;&amp; validSigned   len val = Fixed name len True  (fromIntegral val :: Int64)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  | otherwise = error $ &quot;mkFixed &quot; ++ name ++ &quot;: cannot represent this bit field&quot;</span><br>
<br><br><div class="gmail_quote">On Thu, Aug 23, 2012 at 11:47 AM, Scott Michel <span dir="ltr">&lt;<a href="mailto:scooter.phd@gmail.com" target="_blank">scooter.phd@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Here&#39;s an example (not a complete module) I was using to represent bit-oriented structures as they occur in certain space applications, notably GPS frames. &quot;Fixed&quot; allows for fixed-sized fields and lets the end user choose the integral type that&#39;s best for the structure.<br>

<br>At least it&#39;s not a parser or language example. :-)<br><br><br>-scooter<br><br><span style="font-family:times new roman,serif">-- | Member fields, etc., that comprise a &#39;BitStruct&#39;</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">data Member where</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  Field    :: String                    -- Field name</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Bool                   -- Signed (True) or unsigned (False)</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  ZeroPad  :: String                    -- Field name</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">  OnesPad  :: String                    -- Field name</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  ArbPad   :: String                    -- Field name</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">  Reserved :: String                    -- Field name</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  Fixed    :: (Integral x, Show x) =&gt;</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              String                    -- Field name</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Int                    -- Field length</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Bool                   -- Signed (True) or unsigned (False)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; x                      -- Type of the fixed field&#39;s value</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  Variant  :: (Integral x, Show x) =&gt;</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              String                    -- Variant prefix name</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Maybe BitStruct        -- Header before the tag</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; TagElement             -- The tag element itself</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Maybe BitStruct        -- Common elements after the tag</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Seq (x, BitStruct)     -- Variant element tuples (value, structure)</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">  -- Mult-value variant: Use this when multiple variant tag values have the</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">  -- same structure:</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">  MultiValueVariant :: (Integral x, Show x) =&gt;</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              String                            -- Variant prefix name</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Maybe BitStruct                -- Header before the tag</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; TagElement                     -- The tag element itself</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Maybe BitStruct                -- Common elements after the tag</span><br style="font-family:times new roman,serif"><span style="font-family:times new roman,serif">              -&gt; Seq ([x], BitStruct)           -- Variant element tuples ([values], structure)</span><br style="font-family:times new roman,serif">

<span style="font-family:times new roman,serif">              -&gt; Member</span><br style="font-family:times new roman,serif"><br>
</blockquote></div><br>