Hacker News new | past | comments | ask | show | jobs | submit login

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 often find them handy for locking down admissible states at compile time. Maybe ~10 years ago in a processor design class, I wrote some CPUs in Haskell/Clash for FPGA usage. A nice thing I could do was write a single top-level instruction set, but then lock down the instructions based on what stages of the processor they could exist at.

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")


Wow, someone else who (used to be) using Clash. I still use it for everything I can in my (hobby) FPGA projects. I'm not sure I've used GADTs, but I've certainly made use of other more "advanced" parts of the type system, like type families.

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.)


Clash is awesome, IMO by far the best extant HDL.

I semi-recently used it for this ADAT fiber optic audio codec https://github.com/YagerICS/adat-codec


Awesome. I see you've made full use of Hedgehog as well.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: