r/Compilers • • 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.

0 Upvotes

8 comments sorted by

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!

3

u/Just_Shine9577 21d ago

Thanks for looking through the codebase! I can definitely see what you mean about the complexity. I will consider introducing a distinct lexical-analysis stage as the language grows. Really appreciate the feedback!

3

u/The2ndSlimShady 21d ago

Also, when you have a token construct, my advice is to at least store one token per statement, so that you can use them in error reporting. Another advice would be to not store strings in the tokens themselves. Store the sources in a variable, and store the ranges in tokens. Will be much lighter and faster in comparison. My brain thinks imperatively but you can have a compiler context created in the entry of the program and pass it around into the functions to have a global context. Oh, though Haskell would hate global mutable state.

3

u/FransFaase 21d ago

I do not think there is anything wrong with it. Actually having a grammar that steers the tokenizer has some benefits as it allows for different tokenization depending on the grammar. To implement this in a lexer can lead to very tricky code. Only split it when there is a need for it. The split in lexing and parsing is primarily based on historic reasons that had to do with very limited memory availability.

2

u/Just_Shine9577 21d ago

Wow, that's an interesting perspective. I hadn't considered the historical reasons behind the separation or the advantages of grammar-aware tokenization. I will definitely read up on this and keep it in mind as HumMac grows rather than splitting the stages just for the sake of following a conventional architecture. Slim Shady also raised some useful points about source ranges and diagnostics, so I have quite a bit to think about. Thanks for sharing your perspective!

2

u/recursion_is_love 20d ago

COBOL never die, it reincarnated again and again.

1

u/Just_Shine9577 20d ago

I mean, who can resist the appeal XD