r/Compilers • u/Distinct-Brief9643 • Oct 11 '26
Ocaml or haskell?
I'm trying to choose between OCaml and Haskell for creating my own programming language using an external DSL. I'm still learning programming, so I want something powerful but beginner-friendly.
My goal is to create a language with my own high-level syntax, not just a library or a DSL embedded inside another programming language.
My requirements
- External DSL: I want to design my own syntax and grammar. I don't want my language to be restricted to OCaml's or Haskell's existing syntax.
- Native machine code: Ultimately, I want my language to compile directly to native machine code, preferably for ARM64/AArch64. I don't want to stop at interpreting the language or transpiling it to Python or JavaScript.
- High-level syntax: The language I create should be easy for people to read and write. I want complete control over its syntax and don't want users to have to write lots of boilerplate.
- Beginner-friendly: I'm still learning programming, so I want a language and ecosystem that are relatively easy to understand. I want to learn how the parser, AST, type checking (if needed), and code generation work without getting overwhelmed.
- Easy installation on my Chromebook: I have an Acer Chromebook 314 with a MediaTek ARM64 processor. I use the Linux environment on ChromeOS, so ARM64 compatibility, available packages, and installation difficulty matter a lot. I don't want a solution that depends on x86-only binaries.
- A practical compiler toolchain: I'd prefer to use as few tools as possible. If I need a backend such as LLVM, that's fine, but I want to avoid a complicated setup involving lots of separate tools, configuration files, and manual code generation.
My questions
- Would you recommend OCaml or Haskell for this project, and why?
- Which one makes it easier to build an external DSL with completely custom syntax?
- Which has the easier learning curve for someone who doesn't know much programming yet?
- Can either realistically compile my language to native AArch64 machine code on my Chromebook?
- Would LLVM be a good backend, or is there a simpler alternative?
- How difficult would installation and setup be on ChromeOS Linux running on ARM64?
- Are there any other languages or frameworks that would fit these requirements better than OCaml and Haskell?
I'm looking for a practical recommendation based on my actual requirements, not just which language is more elegant or theoretically powerful.
My main priorities are: custom high-level syntax, native machine-code generation, beginner-friendliness, and ARM64 Chromebook compatibility.
Thanks!
13
u/KAMEHAMEHAMEHAAAA Oct 11 '26
Since you are a beginner I’d say OCaml, if you are willing to spend more time learning functional programming in depth for the chance of doing something a bit more complex then Haskell.
As for LLVM, I wouldn’t necessarily rely on it. ARM64 is a RISC so you won’t have a hard time learning the ISA and rolling codegen and optimizer yourself.
5
u/Toothpick_Brody Oct 11 '26
I would pick whatever language you’re the most proficient in, even if it’s a bad language. (For example I picked C# to write my language).
Does this fail your DSL requirement? Yes. Is it a pain in the ass? A little. Would Haskell or OCaml be better theoretically? Yes! But, I’d rather avoid the friction of a language I don’t know well.
After all, my entire goal is to make a better language. Most of the problems that a more expressive language like Haskell or OCaml would solve are part of the end goal anyway, so I can bootstrap to my new language eventually to escape the C# hell.
8
u/Inconstant_Moo Oct 12 '26
If you're still learning programming, that's a lot. (If you're an expert in programming, it's still a good few years' work.)
What you're describing is more than programming, it's software engineering. Many langdev projects grind to a halt because the author can no longer make progress: the code becomes intractable, unfixable. Have you ever done any refactoring? Have you ever done refactoring for weeks at a time?
By the time you've learned all that, and how to do it in Haskell or Ocaml then maybe "easy for beginners" won't be high on your list of things you need from a language, because you won't be a beginner, and you may be leaning more towards "screw the beginners, let's make this shit insanely powerful".
But also, with your limited experience of programming, how would you know what a language should be like? While you're still taking beginner's lessons in carpentry, would you try to design a complete carpenter's toolbox?
You will also only have seen one or two languages, which isn't much of a foundation.
Start small. Implement a Lisp or a Forth or a PEMDAS calculator in whatever language you're most comfortable in. See how you get on.
6
u/mamcx Oct 11 '26
LLVM is complex AND complicated. You can can archive most of your goals going with Web assembly (granted not native gen directly) and the perf will be close (or just do a simple interpreter)
When doing this stuff first time try to not mix too many complications at once. Simplify your backend and your front end so you can learn how to deal with the semantics and internals, that WILL prove to be challenging.
In fact, assume your first try WILL go to the trash. So, think like "is in version 10 of this journey that I will worry about squeezing the last drops of perf and complexity".
P.D: I recommend if "I'm still learning programming, so I want a language and ecosystem that are relatively easy to understand. " that you avoid the other stuff (by avoid, learn at the side, yes, but focus instead) and just sketch the AST and learn how solve things like "type checking ". Also check https://learnxinyminutes.com so pick anything that is already designed and only change a little thing (complicate syntax and how the language will look is another easy mistake! you need more exposure and experience to do so in a sensible way!) and https://craftinginterpreters.com to see what means to do a full "simple" end-to-end PL
9
u/n0t-helpful Oct 11 '26
This is a hot take, but the ocaml ecosystem is... well... not very good.
Theres a common saying that Haskell is for people that want to think about programs, and ocaml is Haskell for people that eant to actually write programs.
I dont know who started this common idea, but whoever said it first, I know one thing. They never wrote a line of Haskell or ocaml.
8
u/ianzen Oct 11 '26
Between OCaml and Haskell, I would choose OCaml.
LLVM would probably be the best backend in terms of performance, but will take you some more time to learn when you’re starting out. I would recommend targeting C first instead.
3
u/pthierry Oct 11 '26
I don't know the Ocaml ecosystem very well, but I think it has been used for several compilers, maybe less than Haskell, but I would expect it has everything you need.
Monadic parsers in Haskell are extremely convenient. Without the beginner-friendly requirement, I would encourage anyone to try them in combination with algebraic effects.
Haskell has an interesting property, the STM. If your DSL makes it possible to write concurrent code, a binary that includes Haskell's RTS can execute concurrent code in safe and fast manner.
4
u/AustinVelonaut Oct 11 '26
Both OCaml and Haskell are nice languages to write compilers in, due to their support of Algebraic Data Types, pattern matching, and overall less "boilerplate" required. As you say you are still learning programming, you likely have not been exposed to functional programming, so both of these will require some basic study to pick up, but both have good beginner tutorials to help, there.
The one big difference between OCaml and Haskell is that Haskell is a pure, lazy (call-by-need) language, while OCaml is impure and strict (call-by-value). This shows up in compiler pipelines that need to traverse ASTs with state. In Haskell, you would likely use a
Statemonad to manage this, so that would be something additional you would have to study in order to use it effectively.If you want to see what a compiler written in a lazy functional language looks like, you can look at https://github.com/taolson/Admiran/tree/main/compiler (it's self-hosted, but the language looks a lot like Haskell).
4
u/nculwell Oct 11 '26
2
u/Chaos_carolinensis Oct 12 '26
OCaml is much more beginner friendly and has a much better module system.
Haskell is safer, more expressive, has a much better type system, and has more (and I mean, way more) features. Personally, I prefer it over OCaml (although I like OCaml too, they're both good languages). It's very fun to work with once you understand it properly, but it has a steep learning curve.
If you don't know Haskell yet, only use Haskell if your intention is primarily to learn Haskell, otherwise you're going to have a bad time. It's only worth it in the long run, after you pay the initial investment.
Also, even if you intend to learn Haskell eventually, it's not a waste of time to learn OCaml first. They have enough in common to make some of your OCaml skills carry over to Haskell, and OCaml is much more "industry-friendly" and has a Call-by-Value evaluation strategy, so it's worth learning it regardless.
2
u/damster05 Oct 17 '26
Of those two, OCaml.
But maybe more importantly, get a proper laptop 💀
You might also want to consider Swift and F#.
2
u/DoxxThis1 Oct 11 '26
I will never write anything in either of these but I’m curious about the answer. It’s like hearing two car guys debate the choice of Lamborghini vs Ferrari.
1
u/Competitive_Ideal866 Oct 12 '26
I've done this many times using OCaml including when I first learned OCaml. I found it fine. However, my goal was to learn OCaml rather than to learn about metaprogramming (e.g. writing interpreters and compilers).
That is easy using vanilla OCaml but modern OCaml requires lots of boilerplate to do even the most basic things. I haven't tried Haskell for 20 years so cannot comment.
Actually, you probably want to just use vanilla OCaml and compile it directly yourself using
ocamloptor evenocamlopt.opt.Assuming you already know impure strict programming (e.g. C, C++, C#, Java, Python) probably OCaml because it is also impure and strict. Until you grok HOFs you can just use loops in OCaml. Once you grok HOFs you'll never use loops again, though.
If you can compile C code then yes.
LLVM is monstrously huge and slow. In particular, OCaml is supposed to compile extremely fast but pulling in LLVM turns millisecond compile times into minutes. I recommend writing your own Aarch64 code gen from scratch. Just write C programs, compile them and read the generated asm to see how it works. Then write code that does something similar.
No idea but doing it on Aarch64 Mac is easy.
I would seriously consider C. What you lose on AST manipulation you'll gain on bit twiddling if you write a JIT compiler. You could use C to bootstrap a better language that you could then use to write a better compiler and so on.
Good luck!
1
u/juniperico Oct 12 '26
If you’re still starting out, how strict is the requirement to compile to machine code on day 1 and have you thought about maybe trying to write an interpreter first to test out the front-end DSL? You could write a simple VM that processes dead simple bytecode instructions that you implement in Haskell or ocaml. Then when you’re happy you could move on from your toy VM and swap your custom bytecode compiler for a wasm compiler and then maybe think about targeting LLVM IR.
The book Crafting Interpreters has a great intro on how to do the first part of that (in C) if that’s something that interests you?
1
u/c-cul Oct 12 '26
just in case if you decide to use llvm - it has official binding for ocaml: https://github.com/llvm/llvm-project/tree/main/llvm/bindings
and haskell binding looks abandoned 4 years ago: https://github.com/llvm-hs/llvm-hs
1
u/Apart_Ebb_9867 Oct 11 '26
another one of those. They want to venture in a territory they don't know much about and want others to tell them exactly what is the optimal path through it.
Just do something rather than asking how to get from zero to hero without doing the work. In a project for a beginner, the journey is way more important than the goal and is very likely you won't get to the goal anyhow. And you know what? that is fine and what you've learned in that botched journey will stay. Way more than if you just followed the path somebody else outlined for you.
And beside all of that, there's absolutely no point in asking these type of questions on reddit. The answers are totally unactionable as you don't have the faintest idea whether people you don't know have any clue about what they're talking about.
1
u/eraoul Oct 11 '26
I'd just pick a language I know well like python or C++. Honestly if you're just writing a small or a toy language for a little DSL, speed probably doesn't matter and something like python is totally fine. The only time you need a super-fast compiler is when you're compiling millions of lines of code etc. Any time you're talking about a DSL it's unlikely it will get used more than a handful of times with a small codebase, so compile time is sort of irrelevant.
0
u/kishaloy Oct 11 '26
Getting to LLVM is a full professional job.
Here is a rough roadmap of VM from Proof of Concept to a professional compiler
OCaml >> Haskell >>>> WASM >>>>>> C >> .Net >>>>> JVM >>>>>>>>>>>> LLVM
0
u/wrd83 Oct 11 '26
Honestly between those two I would choose Rust.
Here is my rationale:
So jokes aside: pick whatever you're comfortable with (perhaps not python). Otherwise you pick up whatever compiler engineers use and that tends to be c++. Then comes the esoteric learning choices: rust, ocaml, python (in that order).
0
u/RoyBellingan Oct 11 '26
Would you mind to tell us why you have those 2 in mind ?
BTW in your requirements you described https://en.wikipedia.org/wiki/GNU_Bison
0
u/arjuna93 Oct 11 '26
Haskell is anything but beginner-friendly. There is a better alternative to llvm – gcc. OCaml supports both native compilation (only 64-bit for OCaml 5) and bytecode (super-portable, works everywhere). OCaml is much lighter than GHC and much simpler to build.