2 ms·
Nicely done. It reads like Ikea instructions. I didn't really know how blockchain worked until I read this code. If this was written as a teaching tool or a por
by m-j-fox 9y ago
Nicely done. It reads like Ikea instructions. I didn't really know how blockchain worked until I read this code. If this was written as a teaching tool or a portfolio piece or even an art project, then I say its a big success.
One question:
getBlockChain :: (SpockState m ~ BlockChainState, MonadIO m, HasSpock m) => m [Block]
What does the squiggle mean?
- eutectic 9y agoIt is an equality assertion: https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_guide/equality-constraints.html https://downloads.haskell.org/~ghc/7.6.1/docs/html/users_gui...
- m-j-fox 9y agoThanks, but I still don't get it. So SpockState m equals BlockChainState? So getState's type is m BlockChainState? (BlockChainState chain _ _) <- getState But it has to be anyway so, I guess I'm still confused. Is the squiggle for extra documentation or is it a hint to help the type inference? Why would type inference need help? Does it have to do with some of the extensions that are applied in the source (but appear to be otherwise unused)?
- aviaviavi 9y agoYeah, it's for the compiler, otherwise it can't figure out that a BlockChainState is actually a SpockState m. I'm not really sure how it could otherwise, since the definition is completely decoupled from Spock. It should be noted that it's entirely possible there's a less confusing way to write that type signature. I'm far from an expert in Haskell.
- m-j-fox 9y agoI'm farther from an expert than you. I tried (and failed) to find a less confusing way... One thing I see people doing when they have real gnarly type signature is to concentrate the horror in one place and paint a veneer of simplicity elsewhere: type Get stateType returnType = forall monad. (SpockState monad ~ stateType, MonadIO monad, HasSpock monad) => monad returnType Then elsewhere: getBlockChain :: Get BlockChainState [Block] getLatestBlock :: Get BlockChainState Block This may only help superficially as type errors will still be hard-to-understand.
- aviaviavi 9y agoAh, I think this is a better though! If you got this working, feel free to open up a PR. Or I can look into it when I get time.
- aviaviavi 9y agoAlso, unless I'm mistaken, TypeFamilies are the relevant GHC extension here. I'm not exactly sure how this would be written differently without that
- aviaviavi 9y agoIf you learned a thing about blockchains from this project, I'd call it a big success too! :D So happy to hear it was helpful
- m-j-fox 9y agoThanks for making this. It reminds me of the day I fell in love with Haskell. I wanted to learn how a Verilog simulator works. There's a couple good open-source ones but I found the code impenetrable, so I went looking for something like an introductory course and found this obscure, disused code called Hydra which is hard to find. Let me see ... here it is: https://github.com/garry-sharp/x/tree/master/CA4/HydraLib-0.8.1 https://github.com/garry-sharp/x/tree/master/CA4/HydraLib-0.... After reading for just an hour, the lightbulb came on and I not-only felt I had learned how a digital simulator works but also what a mighty hammer Haskell can be for teaching. Since then I've read (and unfortunately written) a lot of Haskell that is far less elucidating. It's great to come across something that reminds me how I got turned on in the first place and that I should aspire to produce code that not-only barely gets the job done but also explains itself and embodies a modicum of beauty. Thanks again.