Interesting, thanks for posting.
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
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.)
I semi-recently used it for this ADAT fiber optic audio codec https://github.com/YagerICS/adat-codec