r/Compilers • u/Just_Shine9577 • 21d ago
I built a compiler for a controlled English-like mathematical language in Haskell — looking for compiler design feedback
I've been developing HumMac, a compiler for a controlled, English-like mathematical language, using Haskell. (the final idea is to build towards a human-to-machine language compiler)
The project explores how a human-readable mathematical language can be given precise semantics and translated into a structured intermediate representation.
The compiler is organized around several components:
- A parser that constructs a structured abstract syntax tree
- A symbol table with explicit symbol identifiers
- Conversion from the source representation into Structured Mathematical IR (SMIR)
- Expression and proposition inference
- Numeric type promotion and semantic validation
- Definite-initialization analysis, including conditional branches
- Termination-analysis infrastructure
HumMac currently supports arithmetic expressions, implicit multiplication, bounded repetition, conditionals and six comparison operators. The compiler successfully parses this program, generates SMIR, constructs its symbol table and infers the types of its variables. Since it is currently a compiler rather than an execution environment, variables do not necessarily need to be initialized before they can be introduced or referenced. In particular, the language distinguishes free mathematical symbols, which can remain unassigned, from runtime variables, for which definite-initialization analysis is performed. This distinction allows mathematical expressions to contain symbolic variables without treating them as uninitialized runtime values.
One design distinction was separating free mathematical symbols from runtime variables. An unassigned mathematical symbol can represent a free variable in an expression, whereas a runtime variable may require definite-initialization analysis.
The project is still a prototype. SMIR generation and analysis are implemented, but a complete SMIR execution engine is not yet available.
Implementation: Haskell (GHC 9.10.3, Cabal)
Repository: https://github.com/RupsaMC/HumMac
I'd be glad to hear thoughts from the Haskell community, particularly on:
- The organization of compiler passes and intermediate representations
- Representing mathematical expressions and proposition in Haskell
- Semantic analysis and type inference architecture
- Approaches to making the implementation easier to extend as the language grows
This is my independent project and I'm sharing it in the hope of receiving useful technical feedback.
***Why Haskell?
I chose Haskell partly because of my interest in the relationship between convergence and divergence in lambda calculus, and the way these ideas connect to the behaviour and evaluation of computational expressions. Haskell's functional and strongly typed approach made it an interesting language in which to explore these ideas while building HumMac's parser, intermediate representation and semantic analysis pipeline.
5
u/The2ndSlimShady 21d ago
I'm currently looking through your codebase, however I'm not quite acquianted with function programming and Haskell in general so a more detailed feedback will take a bit of time for me to form. But on the first look, as far as I can understand, your parser is doing two things at once: tokenizing and parsing. This is fine as is, for now, but as the project gets bigger and bigger, if you try to add more functionality to the AST, it will be harder to modify the parser. So I can suggest you to create a separate step for lexing, and then parse the token list later.
But in any case, take my advice with a bit of salt, since I'm a self taught compiler enthusiast meself.
Cheers!
2
u/recursion_is_love 20d ago
COBOL never die, it reincarnated again and again.
3
u/arthurno1 21d ago
https://codewithrockstar.com/