Every functional language that compiles to native code relies on quite big runtime written in C programming language (well at least ones that I know of, for example Haskell, OCaml, Gambit/Chicken Scheme, SBCL, Mercury). Basically C is used to implement all the low level stuff like garbage collection, lazyness, OS abstraction layer and etc.

I'm interested if it is even possible to implement functional programming language all in itself, without doing anything in other programming language like C? If it is possible how for example garbage collection would need to be implemented?

I know that there are some projects that somehow compile OCaml, Haskell and Erlang to run on bare bones Xen hypervisor. Does the Xen provide low level features like garbage collection or there is still some C layer involved in these projects?

Is there still any functional programming language that doesn't rely on C runtime?

closed as too broad by gnat, user40980, durron597, GlenH7, user22815 Jul 4 '15 at 17:40

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It's certainly possible. If nothing else, one can program all those bits in machine code and let the compiler add them to all programs. But I suppose you were looking for something more glamorous? – user7043 Mar 2 '15 at 8:32
  • Some non-functional languages also have a runtime coded in C or C++ (in particular the JVM for Java) – Basile Starynkevitch Mar 2 '15 at 9:11
  • Some JVMs are written in C++. Some are written in Java. There is one written in Ruby, I believe, and one written in ECMAScript. – Jörg W Mittag Mar 2 '15 at 9:22
  • 4
    Lisp was written in 1958 (for high level programming languages, only Fortran is older than it)... C was written in 1972... so just by that it is certainly possible to write a functional language without C. The Lisp machine was even bare metal functional programming. – user40980 Mar 3 '15 at 1:43
  • 3
    @ALXGTV it was one of the first languages that one would put in the functional grouping though in the 50s, they weren't as concerned with the name (which has evolved). The point is that Lisp existed a decade before C and also had a bare metal implementation - neither of which were based on a C runtime. That these existed makes the answer to the "is it possible" a simple "yes". – user40980 Jun 30 '15 at 14:05

Is there still any functional programming language that doesn't rely on C runtime?

Yes, at least if you consider (like I do) that Lisp variants are functional languages (because they have the application and abstraction operators of the lambda-calculus), and that any language with closures and abstraction is functional:

  • Bones is an implementation of Scheme, compiled to x86-64 assembly code in nasm syntax, which can be built without using any C compiler (but uses gcc as a linker only; with enough knowledge, you could replace that with ld).

  • Scheme48 is another implementation of Scheme which is entirely compiled to C; AFAIK its runtime system (including its GC) is coded in a subset of Scheme called PreScheme, and is translated to C (so IIUC no human written C code exist in it)

  • Jacques Pitrat's CAIA system is a declarative language bootstrapped artificial intelligence system (and all of its C code is generated by itself), so contains no human-written C code (but uses C as a target object language, like many other implementations)

  • Lissac is an object oriented language with some functional abilities (AFAIK it has closures so can count as functional programming language) entirely compiled to the x86-64 code.

  • old LISP machines from the 1980s did not rely on C at all.

You are probably asking: how can be a runtime system written in a high-level functional language (sort of chicken and egg problem, called bootstrapping compilers). The answer is "simple" in principle; you need to ensure that the lower part of your runtime does not need any runtime (e.g. any garbage collection) when it runs. This may be tricky to code (if you don't have tools ensuring that property) and generally need some knowledge about implementation details of the compiler, and uses some basic unsafe construct (e.g. the one requesting memory from the OS, e.g. thru POSIX mmap function or the underlying mmap(2) system call)

I know that there are some projects that somehow compile OCaml (...) to run on bare bones Xen hypervisor

You are probably thinking of MirageOS, an unikernel coded mostly in Ocaml. But it uses an adaptation of Ocaml runtime (notably the GC) ported to Xen and hand-coded in C. Xen is an hypervisor emulating (in a VM) some kind of x86-64 hardware system, so don't provide any garbage collector (but abstracts some common hardware devices and perhaps very low level drivers).

Notice that C is widely used as a "portable assembler". You could use LLVM (or C-- by S.Peyton Jones & Norman Ramsey, which unfortunately stalled) for that role (but LLVM is written in C++).


Is there still any functional programming language that doesn't rely on C runtime?

The answer to this exact question is yes: C is not the only low-level language which is suitable as a compilation target (although definitely the most popular one). For example, the Embedded ML described in Functional Programming for Embedded Flight Software Development by A. J. Harris has a Forth runtime (as well as a C one).

The answer to your other question, namely "is it possible to implement a functional language all in itself", i.e. without a huge runtime written in an external language, is also yes as far as I'm concerned. Uniqueness Types, as presented in e.g. the Clean language, enable possibility to model destructive updates while maintaining a referential transparency. The Mercury language, which is mentioned in your question, has a Compile Time Garbage Collection feature, which eliminates the need for a conventional GC. Lazyness, I believe, has to be handled explicitly, i.e. you can't have true call-by-need semantics without the runtime support, but you still can implement lazy data structures which would cover most of the need for lazyness.

I believe the reason we don't see any mainstream functional languages implemented this way is that although possible, it's rather impractical to do so, due to both efficiency and language complexity reasons.

  • In Haskell, laziness is just implemented with thunks - that is, the value becomes a function that calculates the value the first time it is used and then caches (memoizes) it as long as it remains in scope for. – Benjamin Gruenbaum Jun 30 '15 at 8:45

In principle? Yes. C is a programming language like all others. What C can do, other languages can be made to do.

In practice? Rarely. C is near-ubiquitous as a systems programming language (there are exceptions, but they will be provided in the outraged comments, so I don't bother listing them), and it is almost always a better idea to use what is already available than to write your own low-level OS wrapper. (There is also a bit of synergy in that many OS kernels are themselves written in C, so calling conventions etc. are a bit easier to handle when you use C). There is value in having a universal standard, even if it is such an old, warty standard as C is, therefore people rarely bother avoiding it on purpose.

Not the answer you're looking for? Browse other questions tagged or ask your own question.