Go implemetation of Simple Typed Lambda Calculus (STLC) with base types - with zero external dependencies.
This is a toy interpreter for self learning purposes.
- STLC support: Lambda abstractions, applications, and variables
- Type system: Static type checking with Int and Bool base types, plus function types
- Conditional expressions: if-then-else constructs with type checking
- Literals: Integer and boolean literal support
- Builtin functions: Arithmetic, boolean, comparison operations with currying support
Int- Integer typeBool- Boolean typeT1 -> T2- Function type from T1 to T2
The EBNF grammar for the supported subset of STLC is as follows:
expr ::= var
| "\" var ":" type "." expr (* abstraction *)
| expr expr (* application *)
| "(" expr ")" (* grouping *)
| "true" | "false" (* boolean literals *)
| "if" expr "then" expr "else" expr (* conditional *)
| digit+ (* integer literals *)
type ::= "Bool" (* boolean type *)
| "Int" (* integer type *)
| type "->" type (* function type *)
| "(" type ")" (* grouping *)
var ::= letter (letter | digit)* (* variable names *)
Arithmetic operations:
add : Int -> Int -> Int- Additionsub : Int -> Int -> Int- Subtraction
Comparison operations:
eq : Int -> Int -> Bool- Equalityne : Int -> Int -> Bool- Inequalitylt : Int -> Int -> Bool- Less thanle : Int -> Int -> Bool- Less than or equalgt : Int -> Int -> Bool- Greater thange : Int -> Int -> Bool- Greater than or equal
Boolean operations:
and : Bool -> Bool -> Bool- Logical ANDor : Bool -> Bool -> Bool- Logical ORnot : Bool -> Bool- Logical NOT
go install github.com/shota3506/gostlc/cmd/gostlc@latestStart the REPL by running gostlc without arguments:
$ gostlc
STLC REPL
Type :quit or :q to exit, :help for help
gostlc> (\x:Int. x) 42
=> 42
gostlc> if true then 1 else 0
=> 1gostlc sample.stlcgostlc -c "(\x:Int. x) 42"echo "(\x:Bool. x) true" | gostlc -(\x:Int. x) 42
# Result: 42
(\f:Int->Int. \g:Int->Int. \x:Int. f (g x))
# Church numeral for 2
\f:Int->Int. \x:Int. f (f x)
# Church numeral for 3
\f:Int->Int. \x:Int. f (f (f x))
if (\x:Bool. x) true then 100 else 200
# Result: 100
(\f:Int->Int. f 10) (\x:Int. x)
# Result: 10
# Simple arithmetic
add 2 3
# Result: 5
sub 10 4
# Result: 6
# Partial application
(\inc:Int->Int. inc 10) (add 1)
# Result: 11
# Composition with builtins
add (sub 10 3) 5
# Result: 12
# Logical AND
and true false
# Result: false
# Logical OR
or false true
# Result: true
# Logical NOT
not true
# Result: false
# Complex boolean expressions
and (or false true) (not false)
# Result: true
# Using with conditionals
if (and true true) then 1 else 0
# Result: 1
# Equality
eq 5 5
# Result: true
# Inequality
ne 3 7
# Result: true
# Less than
lt 3 5
# Result: true
# Less than or equal
le 5 5
# Result: true
# Greater than
gt 10 3
# Result: true
# Greater than or equal
ge 7 7
# Result: true
# Using comparisons with conditionals
if (lt 3 5) then 100 else 200
# Result: 100
- Let bindings:
let x = e1 in e2for local variable binding - Recursive functions: Fixed-point operator or recursive let bindings
- Product types: Pairs/tuples with projection operations
- Sum types: Either/variant types with pattern matching
- Unit type:
()for side-effect operations - Type inference: Hindley-Milner style type inference to reduce type annotations
- Arithmetic operators: mul, div, mod (add and sub are already implemented)
- String type and operations: String literals and concatenation
- Debugger: AST inspection and step-by-step evaluation
This project is licensed under the MIT License.