Share
Embed
Transcript
25 slides
-
͋Β͢͡ • 1݄͝Ζ͔Βࣗ࡞ݴޠΛ࡞Γ࢝ΊͨͷͰɺͨ·ʹਐḿΛใ ࠂ͠Α͏ͱࢥ͍·͢…… • ಈػ • Ұ෦ͰCίϯύΠϥࣗ࡞͕ྲྀߦͬͯͨ • ܕγεςϜͷ࣮ݧ͕͍ͨ͠
• JVMͰಈ͘MLܥݴޠΛ࡞͍ͬͯ·͢ • ηϧϑϗεςΟϯάඪͱ͢Δ • https://github.com/todesking/ojaml -
༷ • MLϕʔε • Standard ML, OCamlͳͲ͕༗໊ • ܥͰͳ͍͕ɺHaskellӨڹΛड͚͍ͯΔ •
ϓϩάϥϛϯάݴޠͷݚڀͰΑ͘ΘΕͯΔ • ؔܕݴޠ • call-by-value • ෭࡞༻͋Γ • ܕਪ • λʔήοτJVM • ΫϥεϑΝΠϧΛग़ྗ͢ΔίϯύΠϥ • ܕγεςϜͷ࣮ݧ͚ͩͳΒΠϯλϓϦλͰ͍͍ΜͰ͕͢ɺ কདྷతʹηϧϑϗετ͍ͨ͠ͷͰίϯύΠϥʹ -
ਐḿ Ϟδϡʔϧ Ϟδϡʔϧม ؔ ϩʔΧϧม(let/let rec) جຊతͳԋࢉ ݪ࢝తͳܕਪ Java FFI
ADT ·ͱͳܕਪ(HM) ύϥϝτϦοΫଟ૬ letଟ૬ Refinement types ηϧϑϗεςΟϯά -
͜Μͳͷ 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 *) } -
͜Μͳͷ 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 ೋ߲ԋࢉࢠ ؔݺͼग़͠ ίϝϯτ -
ίϯύΠϥͷߏ • Parser: ιʔείʔυ͔ΒASTͷม • Namer: Ϋϥε໊ม໊ͷղܾ • Typer: ܕ͚
• Assember: όΠτίʔυͷม Source Raw AST Named AST Typed AST Bytecode Parser Namer Typer Assembler -
Parser • ιʔείʔυจࣈྻΛղੳͯ͠ߏ(Raw AST)ΛಘΔ • ߏจΤϥʔ͜ͷஈ֊ͰνΣοΫ struct A { let
x = 1 ;; } Struct(name = "A") Let( name = "x", body = LitInt(1)) -
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) -
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) -
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 -
ίϯύΠϧ: Ϟδϡʔϧ • ϞδϡʔϧΫϥεɺϝϯόstaticϑΟʔϧυʹϚοϐ ϯά͞ΕΔ • ϝϯόͷstaticΠχγϟϥΠβͰॳظԽ͞ΕΔ package a module
X { let x = 10 ;; let y = x + 1 ;; } package a class X { static int x, y; static { x = 10; y = x + 1; } } -
ίϯύΠϧ: ୯७ͳࣜ • JavaͰදݱ͢Δͱɺׂͱͦͷ··(όΠτίʔυϨϕϧͷ Λ͢Δͱ͘ͳΔͷͰΊ·͢) let x = if true
then 1 else 2 if(true) x = 1 else x = 2 -
ίϯύΠϧ: ؔݺͼग़͠ • ؔFunΫϥεͷαϒΫϥεͱ࣮ͯ͠͞ΕΔ • ΧϦʔԽͷͨΊɺؔͯ͢1ҾͱͳΔ • ؔΦϒδΣΫτͷapp(Object): ObjectΛݺͿ͚ͩ •
ҾɾฦΓదٓboxing͢Δ f 1 2 f = ... g = f.app(1) x = ((Fun)g).app(2) result = ((Integer)x).intValue -
ίϯύΠϧ: ԋࢉ • ʹؔ͢Δԋࢉࢠ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; -
ίϯύΠϧ: ؔఆٛ • FunΫϥεͷαϒΫϥεͱ࣮ͯ͠ • ίϯετϥΫλͷҾޙड़ fun x: int =>
x + 1 class Fun$0(parent: Fun, local: Object) extends Fun(parent, local) { Object app(x: Object) = (int)x + 1 } -
ίϯύΠϧ: ؔఆٛ • ͕ؔωετͨ͠߹ • ؔ͝ͱʹΫϥε͕ੜ͞ΕΔ • 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 } } -
ίϯύΠϧ: มࢀর • ਤͰ͢ 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 -
ίϯύΠϧ: letࣜ • ϞδϡʔϧมͰͳ͘ɺࣜͱͯ͠͏΄͏ͷlet • ؔఆٛ+ݺͼग़͠Ͱ࣮Մೳ • ܕਪͷ্ؔɺ෦తʹ۠ผͯ͠ѻ͍ͬͯ·͢ • ௨ৗͷؔఆٛԾҾͷܕΛ໌ࣔ͢Δඞཁ͕͋Δ
• letͷ߹࣮Ҿͷܕ͔ΒਪՄೳ let tmp = 1 in tmp * 10 ;; // ࠨͷίʔυͱՁ (fun tmp: int => tmp * 10) 1 ;; -
ίϯύΠϧ: 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 -
ίϯύΠϧ: 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]) -
ίϯύΠϧ: 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