LMC is a programming language that combines elements of lambda calculus with practical programming features, implemented in OCaml.
- Basic types:
Int,Long(64-bit integers),Float,Bool,String - Function types
- Hindley-Milner type inference
- Numeric literals: integers (
123), longs (123l), floats (3.14,.5,123f) - String literals (in double quotes:
"hello") - Boolean literals (
true,false) - Variables
- Lambda functions
- Function application
- Arithmetic operations (
+,-,*,/) - Equality testing (
eq)
- Variable declarations (with optional type annotations)
- Function definitions
printexpression for output
// Variable declarations
@i_v1 4 // Integer
@l_v3 4l // Long integer
@f_v5 2.4 // Float
@s_v2 "string" // String
// Function definitions
~add x,y x + y
~mul x,y x * y
~div x,y x / y
// Main program block
~(
print (add 2 3)
print (div 9 4) // Returns 2.25 as a float
)
LMC is implemented in OCaml with several key components:
- Lexer/Parser (
parser.ml): Tokenizes and parses the source code into an abstract syntax tree - AST (
ast.ml): Defines the abstract syntax tree for the language - Type System (
types.ml): Defines types and type operations - Type Inference (
infer.ml): Implements Hindley-Milner type inference - Evaluator (
eval.ml): Interprets the AST to execute programs
- Type inference: Types are inferred automatically
- First-class functions: Functions can be passed as arguments and returned as values
- Floating-point division: Division always gives precise floating-point results
- Smart number formatting: Whole number results are displayed without decimal points
# Run a program
./lmc.exe examples/operators.lmc
# Run with type inference output
./lmc.exe --show-types examples/operators.lmcLMC combines functional programming concepts with practical features like arithmetic operations and I/O. It serves as an educational tool for understanding lambda calculus and functional programming concepts while providing practical utility.