r/functionalprogramming • u/Fabus1184 • Feb 10 '23
Question Why is there no simple C-like functional programming language?
To be very clear, this means a compiled, statically-typed, non-garbage collected language with direct memory access and a fixed evaluation order that does not require any runtime system. I read multiple posts/comments saying it would be difficult or impractical. Still, I don't see why a declarative language wouldn't be able to solve that in an elegant way.
Think of something like this arbitrary Haskell-C-Mix:
doubleEach :: int32* -> int32 -> IO ()
doubleEach array 0 = pure ()
doubleEach array n = do
x <- readInt32 array
writeInt32 array (x * 2)
doubleEach (array + 1) (n - 1)
main :: IO ()
main = do
array <- malloc (100 * sizeof int32)
when (array == NULL) $ error "malloc failed"
mapM_ (í -> writeInt32 (array + i) i) [0 .. 99]
doubleEach array 100
mapM_ (\i -> readInt32 (array + i) >>= print) [0 .. 99]
free array
Such a language should be able to compile without a RTS or garbage collection and could be used virtually everywhere C is used. You could even make memory-safe programs eg. by using arrays with type-level lengths.
Admittedly there are a few gotchas like closures with references to stack-allocated variables and stuff, but nothing that couldn't be solved.
Why does this not exist, it seems like such a powerful language?
38
u/luhsya Feb 10 '23 edited Feb 10 '23
I only dabbled very very briefly with Rust (so someone correct me if this is wrong), but I believe they solve this using Lifetimes.
Lifetimes is a rabbit hole to study. In my style of programming (with Kotlin, as an Android dev) where I use higher-order functions, with a non-GC language like Rust, they need a mechanism for keeping track of objects outside of their scope of origin, .e.g., when returning a function from another function. This is what actually turned me off from Rust (but don't get me wrong, I still believe it's an awesome language).
It's one of those seemingly innocuous 'gotchas' that warrant a complicated solution, in this case, Rust Lifetimes.
23
u/lngns Feb 10 '23 edited Feb 10 '23
I'm not familiar with them, but also,
EDIT: Also, if you're fine with less safety, there's a near infinite list of Lisps you can use, including,
14
u/peschkaj Feb 10 '23
Habit may fit your needs. It’s a research language, but its syntax is very close to Haskell and it compiles down to machine code via LLVM.
F* is a dependently typed language that can be transpiled to idiomatic C via the KReMLin compiler. It’s very ML-ish to write and you can leave out some proofs. It also has the benefit of being used to write a formally verified TLS implementation that’s in wide use throughout industry.
I think the answer to “why” is that these languages exist (Rust and Koka were already given as two great examples), they’re just not “popular” yet.
I did some research work with Habit and found that thinking functionally about low-level programming can be a bit of a challenge, even when that’s your only intent. So there’s also a learning curve. That said, it’s totally worth experimenting with all the languages that have been mentioned here and reaching out to the researchers working on them to see if you can help out.
3
u/[deleted] Feb 10 '23
r/gleamlang you are welcome :)
6
u/arthurno1 Feb 10 '23
Have you seen Val Programming language? Probably not exact what you ask for, but might have some interesting aspects for you? I just learn about that one yesterday, from an article about value based programming. I haven't had time to download and test it myself yet.
10
u/sharpcells Feb 10 '23
https://koka-lang.github.io/koka/doc/index.html I believe Koka fits what you are looking for. A functional language that compiles to C without additional runtime. It's at a research level at the moment but worth keeping an eye on or contributing if that's what you're interested in.
5
u/fl00pz Feb 10 '23
Other than
simple, Rust is a lot of what you're talking about.4
u/newstorkcity Feb 10 '23
Direct memory access is an inherently stateful operation, everyone can see it at once. You can wrap it in a monad, sure, but you would be working against the language. Also, fp relies on optimizations like tail call optimizations and lazy evaluation. These could work, but go against the C ethos of being a thin layer over assembly. It lacks what is called "machine sympathy" where the language reflects the hardware.
3
u/corpsmoderne Feb 11 '23
I'm 100% sure you don't want that in your language (I really don't).
If you really want to go that way, malloc should be typed something like:
Or:
with Ref being a custom type designed to handle references.
Anyway, to me the closest that currently exists and is (starting) to be used on a large scale is Rust.
5
u/editor_of_the_beast Feb 10 '23
The language that you presented doesn't look very declarative, it just looks like an alternative syntax for C.
As far as why hasn't someone built this language, it's because imperative programs assume an underlying memory, which makes the syntax almost DSL-like and more convenient for manipulating memory. That would all have to be modeled explicitly in a "functional direct memory" language.
That idea is expanded on in more detail here, though this post is moreso focused on verification.
2
u/[deleted] Feb 16 '23 edited Feb 16 '23
I have ambitions of doing something like this, but i'm a long way off: https://github.com/DanteFalzone0/wlang
all it supports right now is basically hello world programs, like
-2
u/[deleted] Feb 10 '23
Matlab
-2
u/DrummerHead Feb 10 '23
Isn't garbage collection a necessity for any FP lang?
-1
u/[deleted] Feb 11 '23
[removed] — view removed comment