Skip to content

Instantly share code, notes, and snippets.

@bakenezumi
Created September 19, 2019 04:12
Show Gist options
  • Save bakenezumi/050b0785bbd6b18eccffa6edc1d41033 to your computer and use it in GitHub Desktop.
Save bakenezumi/050b0785bbd6b18eccffa6edc1d41033 to your computer and use it in GitHub Desktop.
// λx.t :: (x: Any) => t
// t t :: t(t)
implicit class DirtyAny(self: Any) {
def apply(): Any = self match {
case f: (Unit => Any) => f()
}
def apply(that: Any): Any = self match {
case f: (Any => Any) => f(that)
}
}
val tru = (x: Any) => (_: Any) => x
val fls = (_: Any) => (y: Any) => y
val test = (b: Any) => (m: Unit => Any) => (n: Unit => Any) => b(m)(n)()
val and = (a: Any) => (b: Any) => a(b)(fls)
val or = (a: Any) => (b: Any) => a(tru)(b)
val pair = (f: Any) => (s: Any) => (b: Any) => b(f)(s)
val fst = (p: Any) => p(tru)
val snd = (p: Any) => p(fls)
val c0 = (s: Any => Any) => (z: Any) => z
val c1 = (s: Any => Any) => (z: Any) => s(z)
val c2 = (s: Any => Any) => (z: Any) => s(s(z))
val c3 = (s: Any => Any) => (z: Any) => s(s(s(z)))
def realnat(x: Any) =
x { acc: Int =>
acc + 1
}(0)
val succ = (n: Any) => (s: Any) => (z: Any) => s(n(s)(z))
val plus = (n: Any) => (m: Any) => (s: Any) => (z: Any) => n(s)(m(s)(z))
val times = (n: Any) => (m: Any) => n(plus(m))(c0)
//λn.λs.λz.n (λx.λy. y (x s)) (λx.z) (λx.x)
val pred = (n: Any) =>
(s: Any) =>
(z: Any) =>
n((x: Any) => (y: Any) => y(x(s)))((_: Any) => z)((x: Any) => x)
//(λx.x x) (λx. x x)
//val omega = ((x: Any => Any) => x(x)) { case x: (Any => Any) => x(x) }
// Y = λf.(λx.f (x x)) (λx.f (x x))
val y = (f: Any => Any) => ((x: Any) => x(f)(x(x)))((x: Any) => f(x(x)))
// Z = λf.(λx.f (λy.x x y)) (λx.f (λy.x x y))
val z = (f: Any => Any => Any) =>
((x: Any => Any => Any) => f((y: Any) => x(x)(y)))(
x => f((y: Any) => x(x)(y))
)
val fctWithInt =
z {
case f: (Int => Int) => {
case x: Int =>
if (x - 1 == 0) 1
else x * f(x - 1)
}
}
val iszero = (x: Any) => x((_: Any) => fls)(tru)
val fct = z(f =>
x =>
test(iszero(pred(x)))((_: Unit) => c1)((_: Unit) => times(x)(f(pred(x)))))
val fib = z(f =>
x =>
test(iszero(pred(x)))((_: Unit) => x)((_: Unit) =>
plus(f(pred(x)))(f(pred(pred(x))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment