|
| 1 | +Type-level lambda calculus in Scala 3 |
| 2 | +===================================== |
| 3 | + |
| 4 | +This repository demonstrates compile-time lambda calculus parser, evalator, and type |
| 5 | +checker in Scala 3. It is heavily depending on [match types][] feature. |
| 6 | + |
| 7 | +Components |
| 8 | +---------- |
| 9 | + |
| 10 | +### Parser |
| 11 | + |
| 12 | +`Parse[_]` returns a type representing an abstract syntax tree of the parsed term. |
| 13 | + |
| 14 | +```scala |
| 15 | +import lambda.{Parse, Var, Abs, App} |
| 16 | + |
| 17 | +summon[Parse["λx.x"] =:= Abs["x", Var["x"]]] |
| 18 | + |
| 19 | +summon[Parse["λxy.x"] =:= Abs["x", Abs["y", Var["x"]]]] |
| 20 | + |
| 21 | +summon[Parse["x y"] =:= App[Var["x"], Var["y"]]] |
| 22 | +``` |
| 23 | + |
| 24 | +### Evaluator |
| 25 | + |
| 26 | +`Eval[_]` returns beta-normal form of the specified term. The evaluation strategy is |
| 27 | +leftmost-outermost. |
| 28 | + |
| 29 | +```scala |
| 30 | +import lambda.{Show, Eval, Parse} |
| 31 | + |
| 32 | +summon[Show[Eval[Parse["(λxy.x) a b"]]] =:= "a"] |
| 33 | +``` |
| 34 | + |
| 35 | +You can also use `ReadEvalPrint[_]` to `Parse` and `Show` together at once. |
| 36 | + |
| 37 | +```scala |
| 38 | +import lambda.ReadEvalPrint |
| 39 | + |
| 40 | +summon[ReadEvalPrint["(λxy.x) a b"] =:= "a"] |
| 41 | +``` |
| 42 | + |
| 43 | +### Type checker |
| 44 | + |
| 45 | +`Type.Infer[_]` returns a (Scala) type representing an abstract syntax tree of the type |
| 46 | +(of simply typed lambda calculus) of the specified term. |
| 47 | + |
| 48 | +```scala |
| 49 | +import lambda.Parse |
| 50 | +import typing.{Show, Type} |
| 51 | + |
| 52 | +summon[Show[Type.Infer[Parse["λx.x"]]] =:= "a -> a"] |
| 53 | +``` |
| 54 | + |
| 55 | +You can also use `Type.Check[_]` (returns a boolean literal type) to determine whether a |
| 56 | +term is typeable. |
| 57 | + |
| 58 | +Related Work |
| 59 | +------------ |
| 60 | + |
| 61 | +- [tarao/lambda-scala: Type level lambda calculus in Scala](https://github.com/tarao/lambda-scala) |
| 62 | + - Scala 2 implmentation of type-level lambda calculus |
| 63 | + - It has no type checker |
| 64 | +- [About type-level lambda calculus in C++03 templates](https://tarao.hatenablog.com/entry/20111101/1320143278) (in Japanese) |
| 65 | + - [source code](https://gist.github.com/tarao/1330110) |
| 66 | + - lambda-scala3 is a translation from this implementation (except the parser) |
| 67 | +- [About match types and compile-time string literals](https://xuwei-k.hatenablog.com/entry/2022/03/02/081401) by @xuwei-k (in Japanese) |
| 68 | + - [source code 1](https://gist.github.com/xuwei-k/dca46ea655c0a1666891901af80b6790) |
| 69 | + - [source code 2](https://gist.github.com/xuwei-k/521638aa17ebc839c8d8519bcdfdc7ae) |
| 70 | + - This taught me how to write a compile-time parser |
| 71 | + |
| 72 | +[match types]: https://dotty.epfl.ch/docs/reference/new-types/match-types.html |
0 commit comments