Created
April 19, 2019 08:41
-
-
Save psttf/ceaee3bdf0380eb5003515f95e9230b9 to your computer and use it in GitHub Desktop.
Lambda reduction template in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Term | |
case class Var(name: String) extends Term | |
case class Lambda(param: Var, body: Term) extends Term | |
case class App(term1: Term, term2: Term) extends Term | |
object LambdaReduce { | |
def substitute(term: Term, v: Var, body: Term): Term = body match { | |
case Var(name) => ??? | |
case Lambda(param, body) => ??? | |
case App(term1, term2) => ??? | |
} | |
val beta: Term => Option[Term] = { | |
case App(Lambda(v, body), term) => | |
Some(substitute(term, v, body)) | |
case _ => | |
None | |
} | |
def toNormalForm(step: Term => Option[Term]): Term => Term = | |
(term: Term) => step(term).fold(term)(toNormalForm(step)) | |
def mu(reduction: Term => Option[Term]): Term => Option[Term] = { | |
case App(term, params) => reduction(term).map(App(_, params)) | |
case _ => None | |
} | |
def nu(reduction: Term => Option[Term]): Term => Option[Term] = { | |
case App(term, param) => reduction(param).map(App(term, _)) | |
case _ => None | |
} | |
def deepMu(reduction: Term => Option[Term]): Term => Option[Term] = { | |
case App(term, params) => mu(reduction)(term).map(App(_, params)) | |
case other => reduction(other) | |
} | |
def deepNu(reduction: Term => Option[Term]): Term => Option[Term] = { | |
case App(term, param) => nu(reduction)(param).map(App(term, _)) | |
case other => reduction(other) | |
} | |
} | |
import LambdaReduce._ | |
val b = beta | |
val nf = toNormalForm(beta) | |
val munu = mu(nu(beta)) | |
val mununf = toNormalForm(munu) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment