<div dir="ltr">Hi All<br><br>I can make a list of unboxed ints like below:<br><br><font face="monospace">{-# LANGUAGE MagicHash #-}<br><br>import GHC.Exts (Int#, Float#)  <br><br>data IntList = IntTail | IntNode Int# IntList<br><br>intListLength :: IntList -> Int<br>intListLength IntTail = 0<br>intListLength (IntNode _ rest) = 1 + intListLength rest </font><br><div><br>I can then make a list of unboxed floats similarly:<br><br><font face="monospace">data FloatList = FloatTail | FloatNode Int# FloatList<br><br>floatListLength :: FloatList -> Int<br>floatListLength FloatTail = 0<br>floatListLength (FloatNode _ rest) = 1 + floatListLength rest </font><br><br>But as you can see, this is getting a bit copy-pasta, which is not good. So instead, lets try this:<br><br><font face="monospace">newtype GeneralList (a :: l) = Tail | Node a (GeneralList a)</font><br><br>This is not allowed here, I believe because `GeneralList` is expected to have one representation for all `a`, instead of a representation which depends on `a`. This is so that if one writes a function:<br><br><font face="monospace">generalListLength :: GeneralList a -> Int<br>generalListLength Tail = 0<br>generalListLength (Node _ rest) = 1 + generalListLength rest</font><br><br>You can't actually compile this into one function, because the relative location of the "next" pointer can change based on the size of `a` (assuming `a` is stored first).<br><br>However, I can achieve what I want with copy pasting or Template Haskell hackery. <br><br>Is there a way to get GHC to do the copy pasting for me? Or do I have to make a choice between extra runtime indirection and avoiding ugly code or having ugly code but avoiding the runtime indirection? A representation polymorphic list here is something that languages like C++, Rust, and even C# will handle happily, so Haskell seems behind here unless I'm missing something, </div></div>