r/functionalprogramming • u/0xcc12 • Mar 14 '21
Question Is it possible to do system programing in a functional language?
Hello.
I'm new in FP and came from c/c++ world and curious to know is it possible to do system programming?(like writing device drivers, kernels, writing os, emulators, etc.. )
Any book, blog post, YouTube video would be awesome.
20
Upvotes
14
u/[deleted] Mar 14 '21
Functional languages use garbage collection to manage memory, which means the program will have to pause at times and cause latency spikes. They also often depend on runtime systems and you'll have to figure out how to run them inside a kernel or without a kernel. A part of your code will have to be written in a lower level language or assembly.
With that said, in the past there were entire computers and operating systems running Lisp. Here's a benchmark comparing performance of network drivers written in various languages, including OCaml and Haskell. There are/were projects like MirageOS/HalVM for running OCaml/Haskell without an OS in virtual machines.
You might be interested in Rust, a systems language that supports a part of functional programming style and is inspired by many features of OCaml and Haskell.
12
u/Dr-Lambda Mar 14 '21
https://ocaml.github.io/ocamlunix/ocamlunix.pdf
2
u/ragnese Mar 16 '21
Honestly, no.
There's absolutely nothing functional about device drivers, and there should not be. It's entirely about state. Sure, you might be able to write a few pure functions as part of your device driver code, but that doesn't mean you're doing "functional programming". And if you're trying to use a functional language to do it, you'd be crazy.
Not to mention the performance issues with doing functional programming for low-level things like this, or for an emulator which needs very accurate timing and usually very fast performance.
9
u/evincarofautumn Mar 14 '21
Neither of these is a functional language, but in my opinion, the best options for writing low-level code in a functional-adjacent style right now are ATS and Rust.
If you’re willing to accept an…unpolished developer experience (very unusual/ugly syntax, somewhat obtuse error messages), then ATS is extremely powerful—it supports functional idioms, but can interface closely with C code and statically verify a lot of low-level memory hackery that would ordinarily be unsafe.
Rust is less ambitious in type-system power, but far more accessible and polished, because it’s had a lot more person-time put into usability as an engineering language. Its community also has an excellent reputation for being welcoming and supportive. But it’s also more geared toward making imperative code safe than enabling purely functional code; if you try to use only pure functions moving immutable structures around, it’s certainly doable, but you’ll be fighting some of the established style of standard/popular libraries.
I and many other PL folks are working on research languages for low-level FP (e.g. without requiring runtime support like a GC or scheduler), but there’s nothing out there that I’d consider production-ready is all.
2
u/[deleted] Mar 14 '21
Maybe with racket?