Upgrade to Pro — share decks privately, control downloads, hide ads and more …

自作ラムダ計算インタプリタで不動点演算をする

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 自作ラムダ計算インタプリタで不動点演算をする

Avatar for Arata Sato

Arata Sato

August 30, 2021
Tweet

More Decks by Arata Sato

Other Decks in Programming

Transcript

  1. シンタックス - 関数適用は左結合: s t u = (s t) u

    - 適当にかっこはつけて良い - 変数のスコープはその関数の本体
  2. 目標: nの階乗を求めたい f(0) = 1 f(n) = f(n-1) * n

    必要なもの: - 条件分岐 - 数 - 掛け算 - 再帰
  3. Boolean - プリミティブな定数がない代わりにエンコードで表す - Booleanは true = λt. λf. t

    false = λt. λf. f とエンコードする - 条件分岐は次のように書けたりする λl. λm. λn. l m n
  4. Number c0 = λs. λz. z c1 = λs. λz.

    s z c2 = λs. λz. s (s z) c3 = λs. λz. s (s (s z)) etc. - 例えば加算は λm. λn. λs. λz. m s (n s z) と書けたりする
  5. 再帰演算 不動点コンビネータを使う - 不動点コンビネータ: F g → g (F g)

    となるようなFのこと。 g = λf. <fを含む本体> とすると、 F g x → g (F g) x より、<fを含む本体>でgを複製して繰り返し使える。
  6. 階乗の計算 f(0) = 1 f(n) = f(n-1) * n 必要なもの:

    - 条件分岐: test = λl. λm. λn. l m n - 数: c2 = λs. λz. s (s z) など - 掛け算: times = λm. λn. m (plus n) c0 - 再帰: 不動点コンビネータ
  7. デモ g = λf. λn. test (iszro n) (λx. c1)

    (λx. (times n (f (prd n)))) c0 (fix g) cn → g (fix g) cn = (λf. λn. test (iszro n) (λx. c1) (λx. (times n (f (prd n)))) c0) (fix g) cn → test (iszro cn) (λx. c1) (λx. (times cn ((fix g) (prd cn)))) c0 ...