A Nix language compiler into Delta Interaction Nets
Go 64.2%
Yacc 3.6%
Ragel 3.6%
Shell 1.1%
Nix 0.2%
Other 27.4%
2 1 0

Clone this repository

https://tangled.org/oeiuwq.com/godnix
git@tangled.org:oeiuwq.com/godnix

For self-hosted knots, clone URLs may differ based on your setup.

README.md

Go D-Nix -- A Nix compiler into Delta Interaction Nets.

Godnix is a compiler that translates Nix expressions using the GoD-Net interaction net reduction engine.

Features#

This first version supports:

  • Integers: Church numeral encoding
  • Anonymous functions: x: body syntax
  • Function application: Direct application of functions to arguments
  • Let bindings: let x = value; in body expressions
  • Addition operator: Pure Church numeral addition (+)

Building#

go build -o godnix ./cmd/godnix

Testing#

Run the integration tests:

go test ./cmd/godnix/

For verbose output:

go test -v ./cmd/godnix/

The test suite includes:

  • Examples from examples/ directory
  • Inline expression tests
  • Church numeral encoding/decoding
  • Error handling for unsupported features
  • Direct translator unit tests

Usage#

./godnix <file.nix>

Examples#

Simple Integer#

# examples/simple.nix
42

Output:

42

Addition#

# examples/add.nix
2 + 3

Output:

5

Identity Function#

# examples/identity.nix
(x: x) 7

Output:

7

Let Binding#

# examples/let.nix
let
  x = 5;
  y = 3;
in
  x + y

Output:

8

Lambda Functions#

# examples/lambda.nix
let
  add = x: y: x + y;
in
  add 4 6

Output:

10

Architecture#

Components#

  1. Parser (pkg/parser/): Full-featured Nix parser (already present)
  2. Translator (pkg/compiler/translator.go): Converts Nix AST to Lambda calculus terms
  3. Compiler (cmd/godnix/): Main application that coordinates parsing, translation, and evaluation

Translation Strategy#

Godnix translates Nix expressions to pure Lambda calculus:

  • Integers → Church numerals: n = λf. λx. f^n x
  • Addition → Church numeral addition: λm. λn. λf. λx. m f (n f x)
  • Functions → Lambda abstractions: x: bodyλx. body
  • Let bindings → Function application: let x = v in b(λx. b) v
  • Application → Standard application: f x → Lambda application

Evaluation#

The compiled Lambda terms are evaluated using the godnet deltanet reduction engine, which implements optimal reduction through interaction nets. This provides:

  • Parallel reduction opportunities
  • Optimal sharing of computations
  • Efficient memory usage

Performance#

The evaluator provides statistics after each run:

  • Execution time
  • Total reductions performed
  • Reductions per second

Example output:

Stats:
Time: 337.889µs
Total Reductions: 60 (177573.11 ops/sec)

Limitations (v1)#

This first version has intentional limitations:

  • Only integers (no floats, strings, lists, sets)
  • Only + operator (no other arithmetic or comparison)
  • No built-in functions beyond +
  • No attribute sets or recursive bindings
  • No imports or file system operations

These limitations allow focusing on the core compilation pipeline. The full Nix parser is available, so adding support for more features is straightforward.

Future Directions#

Potential additions for future versions:

  • More arithmetic operators (-, *, /)
  • Boolean operations and conditionals
  • Lists and list operations
  • Attribute sets
  • Built-in functions (map, filter, fold)
  • String operations
  • Native functions for performance-critical operations

License#

See LICENSE file.