How to write PureScript React to replace TypeScript React in 2021

Blog post:


Nice post! There’s just one part I have a minor objection to:

What is PureScript? It is a dialect of Haskell.

There are some slight differences. A lot of people, myself included, think that PureScript records are an improvement over Haskell records. But they are the same language. purs is now the second-most-popular Haskell compiler implementation, after ghc.

I personally would not describe PureScript as a dialect of Haskell, and neither would I describe purs as a Haskell compiler implementation, because (unlike PureScript) Haskell has an actual specification, and it’s quite easy to find examples where purs does not meet that specification. Laziness is probably the most obvious one, but there are also plenty of other differences in syntax, in the libraries which come with the compiler, and of course records, as you note.

It is sometimes possible to write programs which are accepted by both ghc and purs, but it’s rare, and it’s not something that I would recommend, as you’ll probably find that it quickly becomes awkward to express anything more complicated than f x y z = x + y * z in such a way that both compilers accept it; compatibility with Haskell code is a non-goal for us.


Yes everything you said is true. My intended audience for this post is people who’ve never used PureScript (and perhaps neither Haskell), and I think that for that audience this is fastest route to the best approximate understanding of what PureScript is.


If the question is “if I learn PureScript, then am I also learning Haskell, and vice-versa?” then the answer is “yes definitely.”


And thanks for reading, @hdgarrood


Also @hdgarrood you very helpfully explained the Hook monad to me on Slack, but I left you out of the story for narrative economy. Thanks for your explanation.


Agreed, “dialect” and “same” are very specific, in this case enough to be misleading. “inspired by” and “extremely similar” convey the same points without leading someone to think they’re literally the same thing. Especially since GHCJS exists.

Nice tutorial though!


@jamesbrock I skimmed over it and it looks pretty good. Great work!

However, I have one note (other than what @hdgarrood already said): you recommend rollup-pugin-purs in the “Library substitutions” section, but it’s outdated (doesn’t work with latest PureScript versions) and unmaintaned.

1 Like

I found indexed monad still very confusing to me. React.js itself is so dynamic and I can hardly fit the concept of “components with lifecycles” into PureScript type system.

1 Like

They aren’t really lifecycles in the same way class components worked. You don’t usually need to worry about the indexed monad bits if you’re following React’s hook rules. If there are any particular bits you’re unsure about feel free to ask!

1 Like

I feel the need to say that I have never used the phrase “Rules of Hooks”… :sweat_smile:

The original explanation touched on React internals and the requirement for deterministic ordering of hook invocation and how indexed monads ensure this. The meaning is basically the same, but the original version had more detail and motivation, haha

1 Like

@hdgarrood @spicydonuts @mhmdanas I have made changes based on your suggestions, thank you.