| As one who wrote a couple hardware simulators in Haskell and otherwise, I have to disagree with you. The very immutability of Haskell values means one has to use transactional logic - given current situation, compute situation at next step. C++ and many other languages tend to update values in-place and that brought me a lot of bugs to debug and, subsequently, made me use Haskell. Laziness plays a critical role in free composition of parts of the system. Let me present you some examples. Single clock domain computing hardware can be thought as computations that compute values for every raising edge of a clock. It can be described as a function from infinite list of inputs to infinite list of outputs. The adder, for example, is just like this: adder :: Num a => [a] -> [a] -> [a]
adder = zipWith (+)
The register is a thing that produces values remembered from the previous clock cycle. The very first value comes from reset: register :: a -> [a] -> [a]
register = (:)
Mealy machine allows you to apply a function that transforms input and internal state into output and next internal state. Mealy machine can be used for description of all kinds of things, from register files upwards. mealy :: ((input, state) -> (output, state)) -> state -> [input] -> [output]
mealy f resetState inputs = outputs
where
-- here comes shortcircuiting that relies on laziness:
outputsAndStates = map f (zip inputs (register resetState states))
outputs = map fst outputsAndStates
states = map snd outputsAndStates
That's it!Using regular map and other list functions and these two additions, one can simulate single clock domain hardware which amounts to almost anything that computes on silicon - within bounds of approximation; basically, one need to add delays for slower hardware somehow. The trick with shortcircuiting above allows one to freely compose hardware simulation from different parts. You just put blocks there and they start to work. The function in Mealy machine is pure and total and can be tested (or verified) thoroughly in standard simple way. |