r/haskell • u/Worldly_Dish_48 • May 15 '24
question What are your thoughts on PureScript?
Can anyone give me some good reasons why a haskeller should learn purescript?
54
Upvotes
r/haskell • u/Worldly_Dish_48 • May 15 '24
Can anyone give me some good reasons why a haskeller should learn purescript?
3
u/mightybyte May 16 '24
Historically, I've always been working on projects with a Haskell backend. This being the given, I've always felt that using a Haskell frontend with GHCJS gives you far more value due to being able to share code between the frontend and backend than PureScript would give you. This is basically the same reason NodeJS became popular...the value of having the frontend and backend in the same language is high and people took JS on the frontend as the given and came up with a way to also use it on the backend.
However, in recent years the situation has been shifting. GHCJS seems to be basically unmaintained and if you want to use it, you're stuck on very old GHC versions. Work on compiling Haskell to WASM wasn't really ready for production use last I checked, so Haskell frontends are a lot less viable today than they used to be. If I was starting a new frontend project this might tip the scales in favor of PureScript.
41
u/qqwy May 15 '24
The main reason is that PureScript's primary compilation target is JavaScript and there are some battle-tested production-ready browser frontend libraries/frameworks written in PureScript. Using these if you already know Haskell is a breeze because the languages are very similar.
There also exist some Haskell libraries that compile to JS through GHC-JS but YMMV with that build flow. To my knowledge (do be aware, it's been a few years since I last looked into it) this is less battle-tested.
26
u/z3ndo May 15 '24
Ditto what u/qqwy said.
We have a largish (160k LOC) front-end in Purescript that talks to our Haskell back-end. Because of their similarities we can have very similar coding styles between the two code bases, which is nice. There's also no huge awful context switch when going from our back-end to front-end.
Purescript "fixes" some of Haskells warts like Record accessors being first class citizens and comes with most of the good parts of Haskell (i.e. without dropping useful things like higher kinded types ala Elm).
Purescript is certainly not without its warts, though. Many of the otherwise best libraries lack documentation, there's no equivalent of `weeder`, there's an annoying outstanding issue with the treating warnings as errors, etc etc.
We have our own front-end Elm-like framework so I can't speak to the quality of Halogen or others, so there's an open question in my mind if you're going to use Purescript - is there a suitable front-end library for you out there or are you willing to maintain your own?
Of course, you asked about _learning_ it not _using_ it in a team context...my answer really applies to the latter. As for the former...that's a pretty personal question. I don't think Purescript will _teach_ you all that much on top of Haskell aside from front-end specific concepts that you can likely learn in a variety of others contexts without many of the downsides of Purescript.
12
u/ephrion May 15 '24
I’ve used PureScript in production and it is a delightful time. The most productive experience I’ve ever had on the front end. I only really missed laziness from Haskell, and there’s a lot I miss in Haskell from Purescript.
10
u/pr06lefs May 15 '24
I did a little in purescript back in the day, but switched to elm. Bigger community, simpler code. Now elm's popularity has shrunk considerably, but I'd guess its still more active than purescript.
10
u/science-i May 16 '24
You'd learn purescript to do it on the frontend while writing code that's more or less haskell code, although personally it's probably not the language I'd use. Otherwise, I don't think there's a strong motivation to learn it as a haskeller. I used it alongside Haskell in a full-stack position for a bit over a year and here was my experience.
The ecosystem/community is small, and this was the root of all my real troubles with the language.
Disclaimers: I'm much more of a backend dev than a frontend/full-stack dev in general, and I don't have extensive experience with any other js-ecosystem language.
But personally if I were picking a language for frontend, I'd probably just use typescript, or maybe mess with the actual-haskell-on-the-frontend stuff out there, which seems to be getting better year over year. And if I'm not doing frontend/js-ecosystem-stuff, I wouldn't look to purescript.
8
u/omega1612 May 15 '24
They have their differences, but are very subtle.
Is like having some Haskell extensions already enabled, with J's integration.
I miss the fact that you can access a record field behind a newtype without unwrapping it, but I get why it works like that.
The fact that you can put custom warnings in functions is pretty nice.
I don't know if Haskell has a "Partial" typeclass, but it is interesting in Purescript
Using arrays instead of lists at first sounds horrible, but at the end is quite easy.
The biggest difference is that Purescript is strict, recursing should be done with caution compared with Haskell. Also, you may need to reorder you pipeline of transformations.
Sometimes in Haskell I don't care on transforming a
To
The lazy Haskell may made this a single pass at run time (unless compiler transform the former to the the later) but a naive Purescript would traverse w twice.
8
u/ysangkok May 16 '24 edited May 17 '24
If you have any issue at all with GHC, a language like PureScript seems like the most popular option. PureScript doesn't have its own runtime system. GHC's RTS is nice, but it's not standard Haskell and sometimes I'd like to see how Haskell would be, if there were other actual options than GHC. Which is why I am excited about MicroHs.
It's amazing how ergonomic the Aff monad is. When I first learnt JavaScript, I wouldn't have thought that JavaScript's callback hell could be solved with another language on top. It's amazing that PureScript avoids await/async just like GHC before it! But without actually writing a runtime! Best of all worlds.
8
u/Tysonzero May 17 '24
If non-strictness has a million fans, then I am one of them. If non-strictness has ten fans, then I am one of them. If non-strictness has only one fan then that is me. If non-strictness has no fans, then that means I am no longer on earth. If the world is against non-strictness, then I am against the world.
2
u/AresAndy May 16 '24
A band-aid over the worst scar you could imagine
5
u/ysangkok May 16 '24 edited May 16 '24
Not sure what to think about the typeclass granularity, it's interesting that Idris "undid" this change. (I mean, it's newer than PureScript, so they had the advantage of looking at both, and Edwin chose Haskell's monolithic classes)
3
u/Anarchymatt May 16 '24 edited May 16 '24
I'm learning FP in general and Haskell in particular.
I know I want to use Haskell on the backend, and I wasn't sure what to do for the frontend.
I think the Haskell to Javascript compiler results in large amounts of code that need shipped to the browser (I think, I've only heard this), so I thought I'd try Purescript. The language itself is nice, but I had some issues with the tooling (which is to be expected for FP it seems). Maybe it's smooth sailing in VsCode for all I know, but I had difficulty with the Language Server and Neovim.
I liked the language (Purescript) but it is about the same level of difficulty as Haskell, so my chances of
ever using it at work and convincing coworkers to try it is exactly 0.
I got excited about Rescript, so I'm going to try that.
ELM looks nice of course too, but there are rumors of stagnation.
If I was an expert Haskeller, Purescript might be the best because it's so similar to Haskell (feel, difficulty, type guarantees).
3
u/ablygo May 17 '24 edited May 17 '24
So I've tried purescript a little bit (using halogen), and it was interesting, though I eventually went full-stack with reflex-dom instead. Row types are super cool, and halogen uses them everywhere to provide an impressive level of type safety in terms of which HTML elements can have which attributes, but the more I relied on anonymous records outside of that the less usable I found them. They lead to extremely unwieldy type errors, as your records start including other records, and become increasingly large.
So in practice I needed to start giving names to my records using type synonyms, but that wouldn't help much for type errors, so I'd end up needing newtypes, and kind of end of with the exact same situation as just using named records. I really like the idea of anonymous records, and hope Haskell will have them some day, but they didn't turn out to be as easy to use as I'd liked, and extensive use of them kind of exposes ergonomic issues with error messages that you manage to avoid with nominal types.
I also found the developer ecosystem to be a bit more confusing, though part of that would simply be me having more familiarity with Haskell's way of doing things than Purescripts. But I recall having issues with
packages.dhall,and getting weird errors because I hadn't listed the dependencies of my dependencies (isn't finding those the package managers job)?Overall the packaging model was confusing, and at one point I did the most inoccuous file system interaction (I renamed one of my own source files), and suddenly
spagocouldn't find some other source file which apparently came from some transitive dependency of my code (it didn't give a very useful error message, just telling me that this random file didn't exist). This completely stopped any of my code from compiling, despite the fact that it had no issues right up to that point. It felt like the dependency resolution algorithm had somehow locked in some versions of libraries that weren't compatible, and I couldn't figure out how me renaming a file had triggered that, or any easy way to fix it, as pretty much all my attempts to fix the issue still resulted in it, even deleting compiler artifacts, or rolling back to a previous working commit. It was simply bizarre, and basically seemed to have bricked my project.I was kind of thinking of switching to reflex-dom already for some other reasons, and so that problem lead to me to pull the plug and commit to that, which I definitely don't regret. Probably the thing I miss the most about purescript is that it just seemed a lot nicer when having to actually fall-back on raw javascript, or interact with basic javascript APIs. Purescript seems to have reimplemented a huge portion of javascript in its type system, whereas for Haskell it seems like I keep finding the functionality I want is missing from reflex-dom, or jsaddle, and I'll have other weird ergonomic issues like links in haddocks going to pages that don't exist when I'm trying to fix these things, like JSDOM.Generated.FileReader linking to JSDOM.FileReader. For some reason hackage just doesn't bother to generate documentation for some modules, and I've never really gotten why, but it seems to have become a bigger issue in practice when doing projects that use ghcjs.
And one instance I got a compiler error saying I was importing a module from a package I didn't have included in my cabal file, only to still get the exact same error message after I included it. I think it may have been jsaddle-dom, where I needed to instead include some other package that also provided the same modules, though I'm struggling to remember if that was it, so I'm not sure if the fix I ended up using was actually the thing that fixed it in the end. I've found obelisk sometimes can't find packages you've included when using
ob runif you change the cabal file without restarting it afterwards, so I'm wondering if that was the actual issue, and I simply didn't understand the fix at the time.But I still find the overall experience I've gotten with reflex to be superior, especially after the bizarre missing dependency issue that I had with purescript, which really soured me on it. While tedious to fix, none of the missing functionality has been a show stopper, and FRP just provides some flexibility after the initial learning period that I'd really miss having.
2
u/gergoerdi May 16 '24
At least as of 2022, I found PureScript's JavaScript backend severely lacking in performance: https://unsafeperform.io/blog/2022-07-02-a_small_benchmark_for_functional_languages_targeting_web_browsers/
2
u/ruby_object Aug 04 '25
A mix of amazement and disappointment.