Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no completely standard class that does this, but it's pretty easy to make one yourself. I'll sketch one way of doing this:</p> <pre><code>data P = A | B Q deriving Show data Q = C | D R | E deriving Show data R = F | G deriving Show class Finite a where allValues :: [a] instance Finite P where allValues = [A] ++ map B allValues instance Finite Q where allValues = [C] ++ map D allValues ++ [E] instance Finite R where allValues = [F] ++ [G] </code></pre> <p>I've written the instances this way to show that it's very easy and mechanical and could be done by a program (e.g. with generic programming or with Template Haskell). You could also add an instance to do some legwork for you, provided the type is <code>Bounded</code> and <code>Enum</code>erable:</p> <pre><code>instance (Bounded a, Enum a) =&gt; Finite a where allValues = [minBound..maxBound] </code></pre> <p>If you now add <code>deriving (Bounded, Show)</code> to <code>R</code>, that's one less instance to write!</p> <p>Anyway, now we can evaluate <code>allValues :: [P]</code> and get back <code>[A,B C,B (D F),B (D G),B E]</code> - which you can then <code>zip</code> with <code>[0..]</code> to get your encoding and so on.</p> <hr> <p>But surely this has been done before! I'm not using serialization much (if ever), but a quick search shows that <a href="http://hackage.haskell.org/package/binary">the binary package</a> and <a href="http://hackage.haskell.org/package/binary-derive">the binary-derive package</a> can do something similar for you, without having to write the instances yourself. I would see if those do what you want first.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload