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?
23
u/lngns Feb 10 '23 edited Feb 10 '23
Ante is what you are looking for. It's an ML descendant with no RTS nor AGC.
ATS is another one, but it starts to derive far from the ML/Haskell family.
Idris2 when restricting yourself to linear typing can be used to achieve what you want.
Aith is another one.
I'm not familiar with them, but also,
Low*
LinearML
Spiral
EDIT: Also, if you're fine with less safety, there's a near infinite list of Lisps you can use, including,
Carp
Dale