简体   繁体   中英

Function programming language and bare metal code?

I find modern functional programming languages and the paradigm in general to be very interesting. A lot of functional programming languages are able to generate efficient native code either by using C as an intermediate language or with their own code generators. I'm talking about languages like Haskell, OCaml, LISP or Scheme (SBCL, Chiken, Gambit and etc). But knowing that functional programming languages requires a big runtime library, that is usually implemented in C (for garbage collection for example), I'd like to ask is it really possible to create bare metal code in such language and only using it without the need to go back to C (for example for OS development, or for running native code on embedded device without operating system)? Is there any functional programming language that doesn't depend on runtime (even if it would be a language subset)? Is there any functional language in which I can reimplement the runtime that it needs (for example languages like Ada, D, Nimrod, Pascal has runtimes that are written in the language itself)? What alternative functional languages do I have for bare metal development these days?

Why such hate for C? Functional languages have to use a lower-level language for producing machine-level code.

And you can also write C in a very pure functional way. C offers to the developer the freedom to follow any programming style she wishes.

You can easily follow the functional paradigm in C by applying principles that you have learned from other functional languages, ie:

  • No mutation.
  • No malloc.
  • Avoid state.
  • Model in terms of verbs rather than nouns.
  • Use recursion instead of iteration.
  • et.c.

    There is a book with an intro to Functional C.

I'd like to ask is it really possible to create bare metal code in such language and only using it without the need to go back to C

Bare metal/machine-level/object code are 1GL (1st generation programming languages) made of binary numbers, represented by 1s and 0s.

Most 3GL or 4GL that use a compiler can generate object code.

Is there any functional programming language that doesn't depend on runtime (even if it would be a language subset)

Well, every application that runs on top of an OS has to use the OS's API. Nearly all popular OSes are written in C/C++. That includes Windows, Linux, MacOS, Android et.c.

So, when creating an app for an OS, you may have to depend on the OS's runtime environment (that is usually written in C/C++).

Is there any functional language in which I can reimplement the runtime that it needs (for example languages like Ada, D, Nimrod, Pascal has runtimes that are written in the language itself)

Yeap, you can write your own runtime library for any language you wish, but that's not an easy task to do.

What alternative functional languages do I have for bare metal development these days?

I suggest that you choose your favorite functional language (Haskell, F#, Scala or whatever) and just forget about the runtime. There are many and good reason that it is written in C. And AFAIK that isn't going to change anytime soon.

By programming in Scala you use the JVM and with F# the CLR or the WinRT. Maybe these options could help you.

EDIT: If what you really want to ask is if it is possible to write an OS in a functional language, then the answer is yes.

House is an OS written in Haskell. The FFI provides the necessary functionality for (a) calling low-level routines for accessing hardware, and (b) managing memory explicitly.

MirageOS is in OCaml, and there have been OSs implemented in ML.

Generally, any "Turing Complete" programming language can be used to create an operating system, and most functional languages like LISP, Haskell, OCaml, and so on are Turing Complete.

I'm thinking there will be very few (if any) alternatives since the primitives will then be system dependent and your object file needs to have it's own GC. (functional languages need a runtime GC)

You can make your own domain specific language in your favorite functional language, perhaps Scheme, that utilize some sort of assembler, like Sassy , to eventually output raw machine code. But really this is just making your own compiler.

A much simpler option to use C as an intermediate. Many does this since most systems have a C compiler. Devices comes with reference implementations in C.

A different alternative is to make a system like SBCL cross compile to your architecture and use it as the runtime OS of your device.

BTW: SBCL has very little implemented in other languages than Common Lisp. The main part is probably the GC. The GC code could have been implemented in CL, but that would have made debugging far more complex and with a buggy GC you get all sorts of strange behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM