Example of compiling PureScript using PureScript's Haskell API

Hi all,

Does anyone have handy an example file that:

  1. Uses the Haskell API from purescript-compiler
  2. Initializes the compiler context that is basically empty apart from prims
  3. Is able to compile an expression to corefn
  4. Possibly also returning the type of the expression

I’m interested in dabbling with using PureScript in this way:

  1. In Haskell, make a QuasiQuote for PS: [ps|$x * y|] where x may be from Haskell scope, but y will be a scope agreed to be setup in the JS runtime with the right type.
  2. Types that are supposed to be made available are copied from Haskell into the PureScript context, e.g. in Haskell:
data Status = LoggedIn | Trial
[ps| 
\state ->
  case state.status of 
      LoggedIn -> ...
      Trial -> ...
|]

As I’m interested in seeing how far I can go with doing as much as possible server-side and then only when client-side logic is really needed, then run a snippet of PureScript. I noticed that I was always able to get quite far in such a design (where most things on the screen would be server-side generated, and only occasionally where you want to avoid server roundtrip you would use “client-side expression”), but found that when you need a client-side “if” or “case” or that a simple EDSL in Haskell isn’t good enough; you want a proper language with real syntax, its own Haskell-type system, maintained.

While PureScript with all its packages and whatnot is great, I’m interested in exploring it as a judiciously used “shader” language for Haskell web dev. Inverting control so that Haskell is compiling small snippets of it and defining what types are available, rather than there being two separate toolchains I have to run separately (ghc/cabal and purs/psc).

IOW, I want a “pure” scripting language and PS could be that. :slight_smile:

I can spend some hours browsing through the purescript codebase, but someone might already have a starting point for me. Thanks!

3 Likes

This isn’t quite what you’re hoping for, but I think the REPL or Try PureScript might be a good starting point. Try PureScript master is broken right now, though (https://github.com/purescript/trypurescript/issues/222).

3 Likes

@chrisdone AFAIK the quickstrom repo uses Purescript as a scripting language in a manner similar to what you want (though not as a quasiquotation) - https://github.com/quickstrom/quickstrom/tree/main/runner