troth

Crates.iotroth
lib.rstroth
version0.1.0
created_at2025-05-31 19:11:31.416787+00
updated_at2025-05-31 19:11:31.416787+00
descriptionA Simple λ-Calculus Interpreter
homepage
repositoryhttps://github.com/azaleacolburn/troth
max_upload_size
id1696559
size35,132
Azalea Colburn (azaleacolburn)

documentation

README

troth

A simple λ-Calculus interpreter

Features

  • λ Abstractions
  • Expression Aliasing
  • Expression Reduction

Syntax

Tokens

ALIAS: [A-Z0-9_$&+,:=?@#|'<>.-^\*%!]+
ID: [a-km-z_]+
OPAREN: \(
CPAREN: \)
DOT: \.
LAMBDA: l
DEFINITION: fn
USE: use "[a-km-z_\\]+"
SEMI: ;

[!NOTE] Comments are denoted by // and run until the end of the line Use statements are one token, just like strings are in other languages

Grammar Rules

definiton: DEFINITION ALIAS expression SEMI
use: USE SEMI
expression: OPAREN (ALIAS | ID | call) CPAREN
call: expression expression
lambda: LAMBDA ID DOT expression

Examples

λ-Calculus Definition Rust Usage
fn T lx.ly.x true
fn F lx.ly.y false
fn ! lx.x F T !x
fn & lx.ly.x y x x & y
fn | lx.ly.((x x) y) x | y
fn SUCC lx.ln.lm.(n ((x n) m)) x + 1
fn 0 ls.lz.z 0
fn 1 SUCC 0 1
fn + lx.ly.x (SUCC y) x + y
fn * lx.ly.lz.(x (y z)) x * y
fn <=0 lx.(x F) N F x <= 0
fn >= lx.ly.(Z (x P) y) x >= y
fn >=1 lx.x F N T x >= 1

[!NOTE] This grammar does support numerical and 'special' characters in (and even as the first character of) ALIASes, meaning full high-level languages could be defined with normal arithmatic. However, IDs must be lowercase alphabetic characters and the '_' character.

TODO

  • Lexer
  • Parser
  • Reducer
  • Automatic Alpha Converter
  • CLI Functionality
  • Cross-File Linking (not object files)
    • Basic Static Linking
    • Standard Library for Basic Functions (auto-included)
  • Multiple Backend Options
    • Beta Reduction
    • Naive transpilation to λ-calculus with JS syntax

Notes

Transpilation of λ-calculus to higher-level languages is a uniquely difficult task. Nearly all constructs that are built into other languages, such as number literals, are simply encoded in the λ-calculus. This means that while sometimes certain patterns simply act like their analog in a higher-level language, sometimes they serve completely unrelated purposes. Additionally, there are certain quirks of the λ-calculus, which allow these analogs to have a dual form, such as numerical lliterals, which in their Church-encoded form can be used as definite for-loops as well as number literals. Decyphering this seems impossible algorithmically, thus until future revelations, I will not attempt it.

Quirks

  • When Troth encounters an ALIAS being invoked, it first runs the alias through a an alpha conversion process, where each id within the aliased expression is appended with an alias-specific postfix to avoid conflation
  • Troth performs rudementry static linking by parsing the included file, then copying all entries in the resultant definitions map into the original parser's map
  • Currently, Troth immediantly evaluates the expression without displaying intermediate steps
Commit count: 59

cargo fmt