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

つくってあそぼ ラムダ計算インタプリタ / Implement an Interpreter ...

KMC_JP
October 18, 2018

つくってあそぼ ラムダ計算インタプリタ / Implement an Interpreter of Lambda Calculus

KMC例会講座 / 型無しラムダ計算のインタプリタを実装してみよう

KMC_JP

October 18, 2018
Tweet

More Decks by KMC_JP

Other Decks in Technology

Transcript

  1. Е߲ w .YcЕY.c.. w Y͸ม਺ w ؔ਺ந৅͸ӈ݁߹ЕYЕZ.ЕY ЕZ.  w

    ؔ਺ద༻͸ࠨ݁߹123 12 3 w ద౰ʹׅހΛՃ͑ͯΑ͍
  2. ม਺ͷஔ׵ͷྫ w Y<YЕZZ>ЕZZ w Z<YЕ[[>Z Y㱠Z  w Y[ ZY

    <YЕXX> ЕXX [ Z ЕXX  w ЕYY<YЕZZ>ЕYY w ЕZY<YЕ[[>ЕZЕ[[ Y㱠Z
  3. Ќ؆໿Λମײ͢Δ w EFGG Y SFUVSOY Y w G  

       w ͜Ε͸͋͘·ͰମײͰ͢
  4. Ќ؆໿ ЕYЕZЕ[Y[ Z[  ЕYЕZY  ЕYЕZY   ЕZЕ[

    ЕYЕZY [ Z[  ЕYЕZY  Е[ ЕYЕZY [ ЕYЕZY [  Е[ ЕZ[ ЕYЕZY [  Е[ ЕZ[ ЕZ[  Е[[
  5. ؆໿·ͰͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿
  6. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  7. DGΠϯλϓϦλ τʔΫϯྻ *% Y &2/6.   ߏจ໦ 45.5 <"44*(/

    Y  > Ծ૝ػցίʔυ -0"% 4503&  YY   ࣈ۟ղੳ ߏจղੳ Ծ૝ػցίʔυ΁ͷม׵ ධՁ ˞ͲΕ͘Β͍σʔλߏ଄Λม׵͢Δ͔͸ઃܭʹΑΔ
  8. Е߲ͷσʔλܕΛఆٛ͠Α͏ type id = string
 
 type term =
 |

    Var of id
 | Fun of id * term
 | App of term * term
  9. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  10. จ๏Λఆٛ͢Δ %start main
 %type <Syntax.term> main
 %% main:
 Expr EOL

    { $1 }
 
 Expr:
 | LAMBDA i=ID DOT e=Expr { Fun (i, e) }
 | AppExpr { $1 }
 
 AppExpr:
 | e1=AppExpr e2=AExpr { App (e1, e2) }
 | AExpr { $1 }
 
 AExpr:
 | i=ID { Var i }
 | LPAREN e=Expr RPAREN { e }
  11. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  12. ࣈ۟ղੳϧʔϧΛఆٛ͢Δ {
 open Parser
 }
 
 rule main = parse


    [' ' '\t']+ { main lexbuf }
 | ['\n'] { EOL }
 | ['a'-'z'] { ID (Lexing.lexeme lexbuf) }
 | '\\' { LAMBDA }
 | '.' { DOT }
 | '(' { LPAREN }
 | ')' { RPAREN }
 | eof { exit 0 }
  13. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  14. Ќ؆໿ ·͕͍ͪ ЕYЕZЕ[Y[ Z[  ЕYЕ[Y  ЕYЕZY  

    ЕZЕ[ ЕYЕ[Y [ Z[  ЕYЕZY  Е[ ЕYЕ[Y [ ЕYЕZY [  Е[ Е[[ ЕYЕZY [  Е[ Е[[ ЕZ[  Е[ЕZ[
  15. %F#SVJKOJOEFY΁ͷม׵ w % ϵ / Y /ϵ Y  ϵ

    Y ͕ఆٛ͞Ε͍ͯΔ  w % ϵ / Y Y ϵ Y ͕ఆٛ͞Εͳ͍ɼࣗ༝ม਺Ͱ͋Δ  w % ϵ / ЕY. Е % ϵ\Y/ ^ /  .  w % ϵ / 12  % ϵ / 1 % ϵ / 2  w %NBJO . % \^  .
  16. %F#SVJKOJOEFY΁ͷม׵ w ྫ w %NBJO ЕYЕZY  w % \^

     ЕYЕZY  w Е % \Y^  ЕZY  w ЕЕ % \Y Z^  Y  w ЕЕ  ЕЕ
  17. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  18. ࣮૷ͷྲྀΕ τʔΫϯྻ -".#%"*%%05 ߏจ໦ 'VO Y  %F#SVJKOJOEFY 'VO ЕYЕZЕ[Y[

    Z[ ЕYЕZY ЕYЕZY ЕYY ࣈ۟ղੳ ߏจղੳ %F#SVJKOJOEFY΁ม׵ Ќ؆໿    
  19. σϞ w aYaZa[Y[ Z[ aYaZY aYaZY  w aYaZa[Y[ Z[

    aYa[Y aYaZY  ม਺໊Λม͚͑ͨͩ  w aG aYG YY aYG YY  ఀࢭ͠ͳ͍