Code Monkey home page Code Monkey logo

lambda-scala3's Introduction

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

lambda-scala3's People

Contributors

tarao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lambda-scala3's Issues

Not working on Scala >= 3.1.3

Critical

[error] -- Error: src/main/scala/typing/Typing.scala:30:27
[error] 30 |          Typed[subst, Fun[Subst.Ty[subst, Var[Fresh]], t], fresh]
[error]    |                           ^
[error]    |Match type reduction failed since selector  subst
[error]    |matches none of the cases
[error]    |
[error]    |    case util.HNil => typing.Var[Fresh]
[error]    |    case typing.Subst[typing.Var[j], t] :+: rest => typing.Var[Fresh] match {
[error]    |  case typing.Var[i] =>
[error]    |    i == j match {
[error]    |      case (true : Boolean) => typing.Subst.Ty[rest, t]
[error]    |      case Any => typing.Subst.Ty[rest, typing.Var[Fresh]]
[error]    |    } <: typing.Type
[error]    |  case typing.Fun[s, t] =>
[error]    |    typing.Fun[typing.Subst.Ty[subst, s], typing.Subst.Ty[subst, t]]
[error]    |} <: typing.Type

Trivial

[error] -- Error: src/main/scala/lambda/DeBruijn.scala:20:28
[error] 20 |          HList.Index[v, L] match {
[error]    |                            ^
[error]    |                      Match type reduction failed since selector  (v, L)
[error]    |                      matches none of the cases
[error]    |
[error]    |                          case (i, _) => lambda.DeBruijn.Var[i, v]
--- a/src/main/scala/lambda/DeBruijn.scala
+++ b/src/main/scala/lambda/DeBruijn.scala
@@ -18,7 +18,7 @@ object DeBruijn {
       T match {
         case lambda.Var[v]      =>
           HList.Index[v, L] match {
-            case (i, _) => Var[i, v]
+            case (i, Any) => Var[i, v]
           }
         case lambda.Abs[v, t]   => Abs[v, Transform[t, v :+: L]]
         case lambda.App[t1, t2] => App[Transform[t1, L], Transform[t2, L]]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.