Announcing Pure-C: A C backend for PureScript
Pure-C is an alternative backend for PureScript, a strongly typed, purely functional programming language that compiles down to native code via the Clang compiler toolchain.
module Main whereimport Preludeforeign import getLine :: Effect (Maybe String)
foreign import putStrLn :: String -> Effect Unitmain :: Effect Unit
main = echo
where
echo = do
mLine <- getLine
case mLine of
Just line -> do
putStrLn line
echo
Nothing ->
pure unit
Pure-C is — in it’s own right — a tiny, strictly evaluated, purely functional, and garbage collected programming language that compiles down to native code with minimal linkage. This means that useful programs can be produced at just a few hundred kilobytes in size without external dependencies.
The idea is to make use of all the awesome features that PureScript provides — from compiler-guaranteed purity and totality of functions, to higher kinded types, type-classes, functional dependencies, and so on — to quickly build correct, and self-contained programs that are easy to distribute.
Pure-C is a deterministic translation from PureScript code and any associated FFI files to C code that is compatible with the clang toolchain. It does not, for example, dictate a threading model. These decisions are entirely up for library authors to make.
Pure-C also makes FFI dead simple. The examples that ship with the project are illustrate this nicely.
State of the Project
As it stands the core feature set of Pure-C is complete. That is, it translates any valid PureScript module into C. Some effort has also been put into making downstream integration with Pure-C simple: Simply include the target.mk Makefile that ships with Pure-C and derive a target rule. Examples of how to do that can be found in the examples directory that ships with the project.
Integration with pulp is also straight-forward. Given a usual PureScript package structure with ‘src’ and ‘test’ directories, invoking make on every file change is as simple as running:
pulp -w --then make build
In terms of surrounding libraries, only a handful of libraries have been ported, namely: purescript-prelude, purescript-effect, purescript-console, and
purescript-control. Porting over libraries is an ongoing effort that I hope will see community contributions.
A call to action
While the base feature set may have been written already, there’s still tons of
work left to be done. From implementing the C FFI for many popular libraries to implementing optimization passes like TCO, inlining, unboxing, and more.
I’ve put a significant amount of work into making the project friendly for new comers, so come on over and check the issue tracker for some exciting tasks to hack on!
Otherwise, please star the project if you think it’s cool. It helps me and others gauge interest and motivates me to put more hours into it.
- Star the project — https://github.com/pure-c/pure-c
- Follow me on github
- Give feedback, or ask questions in the comment section below or on the project’s issue tracker.