Skip to content

tarao/lambda-scala3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type-level lambda calculus in Scala 3

This repository demonstrates compile-time lambda calculus parser, evalator, and type checker in Scala 3. It is heavily depending on match types feature.

Components

Parser

Parse[_] returns a type representing an abstract syntax tree of the parsed term.

import lambda.{Parse, Var, Abs, App}

summon[Parse["λx.x"] =:= Abs["x", Var["x"]]]

summon[Parse["λxy.x"] =:= Abs["x", Abs["y", Var["x"]]]]

summon[Parse["x y"] =:= App[Var["x"], Var["y"]]]

Evaluator

Eval[_] returns beta-normal form of the specified term. The evaluation strategy is leftmost-outermost.

import lambda.{Show, Eval, Parse}

summon[Show[Eval[Parse["(λxy.x) a b"]]] =:= "a"]

You can also use ReadEvalPrint[_] to Parse and Show together at once.

import lambda.ReadEvalPrint

summon[ReadEvalPrint["(λxy.x) a b"] =:= "a"]

Type checker

Type.Infer[_] returns a (Scala) type representing an abstract syntax tree of the type (of simply typed lambda calculus) of the specified term.

import lambda.Parse
import typing.{Show, Type}

summon[Show[Type.Infer[Parse["λx.x"]]] =:= "a -> a"]

You can also use Type.Check[_] (returns a boolean literal type) to determine whether a term is typeable.

Related Work