I share the author's frustration with the lack of non-compiler-related examples of GADT uses. It seems like such a powerful idea, but I haven't been able to get a feel for when to reach for GADTs in Haskell
For example, something like (not an actual example from my code, just conceptually - may be misremembering details):
data Instruction stages where
MovLit :: Word64 -> Register -> Instruction '[Fetch, Decode, Execute, Writeback]
-- MovReg instruction gets rewritten to MovLit in Execute stage
MovReg :: Register -> Register -> Instruction '[Fetch, Decode, Execute]
...
And then my CPU's writeback handler block could be something like: writeback :: (Writeback `member` stages) => Instruction stages -> WritebackState -> WritebackState
writeback (MovLit v reg) = ...
-- Compiler knows (MovReg _ _) is not required here
So you can use the type parameters to impose constraints on the allowed values, and the compiler is smart enough to use this data during exhaustiveness checks (cf "GADTs Meet Their Match")What you're doing here is pretty cool, I think I will start doing so, too. I have a number of places where I use "undefined" instead. (The "undefined" from the Clash Prelude, which translates into a "don't care" signal state.)
Remember if OCaml wasn't a mature programming language, maybe Rust would not have happened in first place.
But being outside of the mainstream may mean you need to occasionally debug more esoteric stuff: https://gallium.inria.fr/blog/intel-skylake-bug/ I'm sure Jane Street can afford doing that, but I'm not so sure if a small team can.
Because despite them being very open about it, no one else does it, and every distinguished engineer who pushes a weird tech choice will justify and defend it.
rbjorklin•7h ago
ackfoobar•6h ago
Hard to say exactly how much performance one would get, as that depends on access patterns.
misja111•5h ago
Of course it depends on your use case, in some cases a compact byte array performs better anyway, for instance because now you're able to fit it in your CPU cache.
john-h-k•5h ago
ackfoobar•5h ago
Are you saying each byte takes up a word? That is the case in the `char array` in OCaml, but not Java's `byte[]`. AFAIK The size of a byte array is rounded up to words. Byte arrays of length 1-8 all have the same size in a 64-bit machine, then length 7-16 take up one more word.
https://shipilev.net/jvm/objects-inside-out/