Skip to content

Instantly share code, notes, and snippets.

@psttf
Last active May 7, 2020 11:52
Show Gist options
  • Save psttf/96fe27649b684b9140142b7040094cb8 to your computer and use it in GitHub Desktop.
Save psttf/96fe27649b684b9140142b7040094cb8 to your computer and use it in GitHub Desktop.

Анонимные функции в ООП

// Тип функции A -> B в Scala
trait Function[A, B] {
  def apply(p: A): B
}

// Функциональное значение в Scala
// \x.x+1: Int -> Int
x => x + 1
new Function[Int, Int] {
  def apply(x: Int): Int = { return x + 1 }
}

// Применение функции
f(1)
f.apply(1)

Кодировка вложенных абстракций:

(
  (
    [
      arg = @x => x.arg,
      val = @x => 
        [
          arg = @y => y.arg,
          val = @y => (x.arg) + (y.arg)
        ]
    ]
      .arg := 2
  ).val.arg := 3
).val

Кодировка многоместных функций:

(
  [
    x = @this => 4,
    y = @this => 5,
    val = @this => (this.x) + (this.y)
  ]
    .y := 3)
    .val

Логические значения

В лямбда-исчислении:

tru = \then else.then
fls = \then else.else
if = I

В сигма-исчислении:

if tru then 2 else 3 --> ((tru.then := 2).else := 3).val

Кодировка логических значений:

[
  true = @top => [
    then = @this => this.then,
    else = @this => this.else,
    val = @this => this.then
  ],
  false = @top => [
    then = @this => this.then,
    else = @this => this.else,
    val = @this => this.else
  ],
  prog = @top => 
    ((top.true.then := 2).else := 3).val
].prog

см. https://mephi-toop.herokuapp.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment