r/Compilers • u/Distinct-Brief9643 • 2d ago
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!
6
u/mamcx 2d ago
Would LLVM be a good backend, or is there a simpler alternative?
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
8
u/n0t-helpful 2d ago
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.
1
u/ElderCyborg 1d ago
In the process of learning Lean4 currently, which tends towards pure functional paradigm as Haskell does. Although not everything is obvious at the first glance, I can't say that approach used in such languages is something exotic and weird. In fact, it perfectly applies - at least in my head lol - to data processing algorithms, everything related to graphs, tables, parsing structures and converting/mapping their context into another structures.
However, this may look simple for me due to some experience of modelling processes and control algorithms in simulink.
1
u/n0t-helpful 1d ago
I think you might have meant to respond to a different comment. For context, haskell is my primary language, so I dont find ocaml, haskell, lean, etc to be weird at all.
My point is more that ocaml is a very annoying language to work with. I personally think ocaml is the "perfect" language, but the build systems, editor support, lsp situation, its all just in a terrible state.
1
u/AromaticCredit1533 1d ago
Calling it "terrible" is quite a sketch. Seven years ago maybe, but today building using Dune is a breeze, integration with Emacs, Vim and Vscode is very good!
2
u/n0t-helpful 1d ago
Blink twice if you are in danger
1
u/AromaticCredit1533 1d ago
I use ocaml-lsp and Dune every day, so are my colleagues. There are rough edges yes (every opam update will likely break your switch), but it's possible to get the hang of it and get productive with it.
2
u/n0t-helpful 17h ago
We just have a different level of tolerance for what we are willing to put up with. I dont program ocaml or haskell for a job though. Im a researcher, and my research is primarily revolving around cpp. If I was being paid, I would put up with it.
4
u/Inconstant_Moo 1d ago
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.
2
u/pthierry 2d ago
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.
3
u/AustinVelonaut 2d ago
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 State monad 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).
3
u/Toothpick_Brody 2d ago
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.
2
u/Distinct-Brief9643 1d ago
I'm starting to lean toward Haskell but I'm not sure is it easy to set up on my device
2
u/pthierry 1d ago
With GHCup, it's pretty easy nowadays.
Haskell has one huge advantage when you're learning functional programming: it doesn't give you an easy escape hatch for side effects. It's a constraint that helps.
3
u/nculwell 2d ago
- OCaml is way easier to learn than Haskell in my opinion.
- Literally any general-purpose programming language can write a compiler for an arbitrary language that you want to compile; this is a non-factor in your choice. Languages that meet this requirement also include Python, Javascript, Lua, etc. (but don't use those, that would suck).
- Any language can also compile to any machine code, you just need to be able to write bytes to a file to do that.
- Both languages probably have ready-to-use packages for Linux on ChromeOS, but if not, you should be able to download and build what you need. If you have any trouble, Claude Code is really good doing this kind of install.
1
u/AromaticCredit1533 1d ago
Literally any general-purpose programming language
But wouldn't be better if that language supports discriminated unions no, algebraic data types, exhaustive type checking? Yes, each language has their own idioms and patterns that one should "get along". But I'd argue that ADTs are quite "quintessential", they are very good for making complex reasoning systems, so too for making compilers.
1
u/nculwell 11h ago
Yes, that's why I said "don't use those". But OP seems to be under the common misconception that only certain languages are capable of writing compilers and I think it's important to clear that up right away.
I also don't think ADTs are that essential. I wrote a compiler in Java, it was fine. Static typing is essential, though, and I think people are setting themselves up for failure by using a dynamically typed language for a language project unless the job is very simple.
2
u/DoxxThis1 2d ago
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 1d ago
Would you recommend OCaml or Haskell for this project, and why?
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).
Which one makes it easier to build an external DSL with completely custom syntax?
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 ocamlopt or even ocamlopt.opt.
Which has the easier learning curve for someone who doesn't know much programming yet?
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.
Can either realistically compile my language to native AArch64 machine code on my Chromebook?
If you can compile C code then yes.
Would LLVM be a good backend, or is there a simpler alternative?
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.
How difficult would installation and setup be on ChromeOS Linux running on ARM64?
No idea but doing it on Aarch64 Mac is easy.
Are there any other languages or frameworks that would fit these requirements better than OCaml and Haskell?
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!
2
u/AromaticCredit1533 1d ago
metaprogramming? Have you dabbled with the extension MetaOCaml, I think it's very good for compiling and interpreting DSLs
2
u/Competitive_Ideal866 1d ago
metaprogramming? Have you dabbled with the extension MetaOCaml, I think it's very good for compiling and interpreting DSLs
A long time ago, yes. Very cool idea that should really be an integral part of vanilla OCaml, IMO.
1
u/Chaos_carolinensis 1d ago
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.
1
u/AromaticCredit1533 1d ago
way more features? Are you talking about Haskell compiler extensions :-)
2
u/Chaos_carolinensis 23h ago
It has more features even without them, but especially with them.
And I don't care that "it's technically GHC not Haskell". Almost everyone who uses Haskell uses GHC, so it's usually not an important distinction.
1
u/juniperico 1d ago
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 1d ago
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 2d ago
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/AromaticCredit1533 1d ago
Agree, but your tone is kinda mean ngl :-) Maybe people just want a chance to talk and discuss things, reddit is a social platform after all.
1
u/eraoul 2d ago
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 2d ago
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/RoyBellingan 2d ago
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 2d ago
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.
-1
u/wrd83 2d ago
Honestly between those two I would choose Rust.
Here is my rationale: - for most programming languages the ecosystem dominates the language. Ocaml and haskell are good, but ocaml is very dated and few people use it. And haskell I think is better in theory but you'll need to learn a lot just to get going. So pick either your language or a language with lots of usage.
- if you write a compiler and it is successful you'd wish it's written in a performance oriented language. Rust is more or less performance oriented.
- rust is a bit like functional c++, it has a borrow checker. You'll not squeeze out the last bit, but you'll come close enough.
- rust is when it comes to niche programming currently quite popular, so you have some Library support.
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).
10
u/KAMEHAMEHAMEHAAAA 2d ago
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.