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

自作言語進捗 2019 Mar

自作言語進捗 2019 Mar

todesking

March 19, 2019
Tweet

More Decks by todesking

Other Decks in Technology

Transcript

  1. ࢓༷ • MLϕʔε • Standard ML, OCamlͳͲ͕༗໊ • ௚ܥͰ͸ͳ͍͕ɺHaskell΋ӨڹΛड͚͍ͯΔ •

    ϓϩάϥϛϯάݴޠͷݚڀͰΑ͘࢖ΘΕͯΔ • ؔ਺ܕݴޠ • call-by-value • ෭࡞༻͋Γ • ܕਪ࿦ • λʔήοτ͸JVM • ΫϥεϑΝΠϧΛग़ྗ͢ΔίϯύΠϥ • ܕγεςϜͷ࣮ݧ͚ͩͳΒΠϯλϓϦλͰ͍͍ΜͰ͕͢ɺ কདྷతʹηϧϑϗετ͍ͨ͠ͷͰίϯύΠϥʹ
  2. ਐḿ Ϟδϡʔϧ Ϟδϡʔϧม਺ ؔ਺ ϩʔΧϧม਺(let/let rec) جຊతͳ੔਺ԋࢉ ݪ࢝తͳܕਪ࿦ Java FFI

    ADT ·ͱ΋ͳܕਪ࿦(HM) ύϥϝτϦοΫଟ૬ letଟ૬ Refinement types ηϧϑϗεςΟϯά
  3. ͜Μͳͷ package test.ml0 import com.todesking.ojaml.ml0.lib.Predef.- import com.todesking.ojaml.ml0.lib.Predef.== module Example {

    let x = let rec is_even: int => bool = fun n: int => if n == 0 then true else is_odd (n - 1) ; is_odd: int => bool = fun n: int => if n == 0 then false else is_even (n - 1) in is_even 99 ;; (* x == false *) }
  4. ͜Μͳͷ package test.ml0 import com.todesking.ojaml.ml0.lib.Predef.- import com.todesking.ojaml.ml0.lib.Predef.== module Example {

    let x = let rec is_even: int => bool = fun n: int => if n == 0 then true else is_odd (n - 1) ; is_odd: int => bool = fun n: int => if n == 0 then false else is_even (n - 1) in is_even 99 ;; (* x == false *) } Ϟδϡʔϧ Ϟδϡʔϧม਺ JNQPSU ύοέʔδએݴ ϩʔΧϧม਺ ؔ਺ఆٛ JG ೋ߲ԋࢉࢠ ؔ਺ݺͼग़͠ ίϝϯτ
  5. ίϯύΠϥͷߏ੒ • Parser: ιʔείʔυ͔ΒAST΁ͷม׵ • Namer: Ϋϥε໊΍ม਺໊ͷղܾ • Typer: ܕ෇͚

    • Assember: όΠτίʔυ΁ͷม׵ Source Raw AST Named AST Typed AST Bytecode Parser Namer Typer Assembler
  6. Namer • ϓϩάϥϜ಺ʹग़ݱ͢Δ໊લ͕ԿΛࢦ͢ͷ͔ղܾ͢Δ • "Integer": java.lang.IntegerΫϥε • fun಺ͷ"x": 1൪໨ͷϩʔΧϧม਺ •

    ະ஌ͷ໊લ͕ग़ݱͨ͠ΒΤϥʔ • Named ASTΛग़ྗ // ຊ౰͸Raw AST import java.lang.Integer module A { let x = Integer##valueOf(1);; let f = fun x: int => x ;; } Struct(name="A") Let("x") App Class(java.lang.Integer) "valueOf" LitInt(1) Let("f") Fun(int) Local(1, 0)
  7. Typer • ܕΛਪ࿦͠ɺ੔߹ੑΛνΣοΫ͢Δ • Typed ASTΛग़ྗ Struct(name="A") Let("x"): Integer JCallStatic:

    Integer Method(Integer.valueOf) LitInt(1): int Let("f"): int -> int Fun(int): int -> int Local(1, 0): int Struct(name="A") Let("x") App Class(java.lang.Integer) "valueOf" LitInt(1) Let("f") Fun(int) Local(1, 0)
  8. Assembler • Typed AST͔ΒΫϥεϑΝΠϧΛੜ੒͢Δ • ͜ͷม׵ͰΤϥʔ͸ൃੜ͠ͳ͍ class A { public

    static Integer x; public static Integer f; static { x = Integer.valueOf(1); f = new A$1(); } } class A$1 { ... } Struct(name="A") Let("x"): Integer JCallStatic: Integer Method(Integer.valueOf) LitInt(1): int Let("f"): int -> int Fun(int): int -> int Local(1, 0): int
  9. ίϯύΠϧ: ੔਺ԋࢉ • ੔਺ʹؔ͢Δԋࢉࢠ͸PredefͷϝιουΛݺΜͰ͍Δ • ࣮ࡍͷॲཧ͸JavaͰॻ͔ΕͨϥϯλΠϜ಺ʹهड़ x + 1 =>

    Predef.+ x 1 (* Predef *) let + = fun x => fun y => runtime.Int##add(x, y) // runtime.Int int add(int x, int y) = x + y;
  10. ίϯύΠϧ: ؔ਺ఆٛ • FunΫϥεͷαϒΫϥεͱ࣮ͯ͠૷ • ίϯετϥΫλͷҾ਺͸ޙड़ fun x: int =>

    x + 1 class Fun$0(parent: Fun, local: Object) extends Fun(parent, local) { Object app(x: Object) = (int)x + 1 }
  11. ίϯύΠϧ: ؔ਺ఆٛ • ؔ਺͕ωετͨ͠৔߹ • ؔ਺͝ͱʹΫϥε͕ੜ੒͞ΕΔ • Fun$1ͷੜ੒࣌ɺFun$0ͷҾ਺ͱͯ͠౉͞ΕͨxΛίϯετϥΫλʹ౉͍ͯ͠Δ • getLocal(1,

    0) Ͱ depth = 1, index = 0 ͷ஋(͜ͷ৔߹Fun$1.local)Λऔಘ • indexʹ͍ͭͯ͸ޙड़(let recͰ࢖༻) fun x: int => fun y: int => x + y class Fun$0(parent: Fun, local: Object) extends Fun(parent, local) { int depth = 0 Object app(x: Object) = new Fun$1(this, x) } clas Fun$1(...) extends Fun(...) { int depth = 1 Object app(y: Object) = { val x = getLocal(1, 0) (int)x + (int)y } }
  12. ίϯύΠϧ: ม਺ࢀর • ਤͰ͢ class Fun$0 depth = 0 local

    = null let f = fun x: int => // F0 fun y: int => // F1 x + y let g = f 10 class Fun$1 depth = 1 local = 10 g f def app(x: Any) = new Fun$0(this, x) def app(y: Any) = getLocal(1,0) + y
  13. ίϯύΠϧ: let rec • จ๏ͷ֬ೝ • let rec x1 =

    e1; x2 = e2; ... in body • એݴ͞Εͨม਺x1, x2, ... ͸ɺ ࣜe1, e2, ..., body͔ΒࢀরՄ ೳ let rec is_even: int => bool = fun x: int => if x == 0 then true else is_odd (n - 1) ; is_odd: int => bool = fun x: int => if x == 0 then false else is_even (n - 1) in is_even 99
  14. ίϯύΠϧ: let rec • Մม഑ྻΛڞ༗͓͖ͯ͠ɺޙ͔Β஋Ληοτ͢Δ • ม਺΁ͷࢀর͸഑ྻཁૉ΁ͷࢀরʹஔ͖׵͑Δ let rec is_even:

    int => bool = fun x: int => if x == 0 then true else is_odd (n - 1) ; is_odd: int => bool = fun x: int => if x == 0 then false else is_even (n - 1) in is_even 99 // ٖࣅίʔυ let body env = env[0] := fun x => ... else env[1] (n-1) env[1] := fun x => ... else env[0] (n-1) env[0] 99 in body (new Array[2])
  15. ίϯύΠϧ: let rec • ਤʹ͢Δͱ͜͏ͳΔ class Fun$0 depth = 0

    local = null let rec f = fun ... g = fun ... in f 1 def app(env: Array) = { env(0) = new Fun$1(this, env) env(1) = new Fun$2(this, env) env(0).app(1) } class Fun$1 depth = 1 local = env class Fun$2 depth = 1 local = env f g