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

Scalalr

 Scalalr

Scalaの型システムを悪用して好きな文法のDSLをScala上で実現するためのライブラリ

Kazuhiro Ichikawa

March 17, 2018
Tweet

More Decks by Kazuhiro Ichikawa

Other Decks in Programming

Transcript

  1. ٕज़తৄࡉ w !ETM͸ΞϊςʔγϣϯϚΫϩ w JNQMJDJUQBSBNFUFSΛѱ༻ͯ͠ίϯύΠϧ࣌-"-3ߏจղੳ w ܕˠඇऴ୺ه߸ จ๏Λදݱ͢Δ΍ͭ  w

    ೚ҙͷ෦෼ࣜΛׅހͰׅͬͨ4DBMBࣜͰஔ͖׵͑Մೳʹ͢ΔͨΊʹ
 ͪΐͬͱෳࡶͳ͜ͱΛ͍ͯ͠Δ w ͜ͷख๏ΛԠ༻ͯ͠ࣈ۟ղੳΛมߋՄೳʹ͢Δ͜ͱ΋Ͱ͖Δ ͸ͣ  w ΞϯΧϯϑΝϨϯεͰ࿩ͯ͠Έ͍ͨ ͚ͲεϥΠυ͸༻ҙͯ͠ͳ͍
  2. ࣮૷Πϝʔδ implicit class PlusTransition[Sn] (sn: Sn) { def plus (implicit

    reduce: SnからS2にreduce可能): S3 = { reduce(sn).plus() } } ೚ҙͷঢ়ଶ4O͸
 4ʹSFEVDFՄೳͰ͋Ε͹
 QMVTΛݺͿ͜ͱ͕Ͱ͖Δ
  3. 3FEVDFͷఆٛ trait Reduce[Sx, Sy] { def apply (sx: Sx): Sy

    } implicit def doubleReduce [Sx, Sy, Sz] (implicit reduce1: Reduce[Sx, Sy], reduce2: Reduce[Sy, Sz]) : Reduce[Sx, Sz] = { new Reduce[Sx, Sz] { def apply (sx: Sx): Sz = reduce2(reduce1(sx)) } } SFEVDFՄೳ͔Ͳ͏͔Λදݱ͢Δ
 NVMUJQBSBN ܕΫϥε SFEVDF͸ਪҠత 4Y4Z͔ͭ4Z4[ͳΒ4Y4[
  4. 3FEVDFͷఆٛ implicit val reduceFromS5ToS6: Reduce[S5, S6] = { new Reduce[S5,

    S6] { def apply (s5: S5): S6 = S6(Term(s5.value)) } } 44ͷSFEVDFఆٛ /VNͷ୅ΘΓʹ5FSNΛಡΜͩ͜ͱʹ͢Δ SFEVDF5FSN/VN SFEVDF&YQS&YQS 5FSN S1 S2 S3 S4 &YQS 5FSN S5 /VN /VN S6 5FSN
  5. ຊ౰ͷ࣮૷͸΋͏গ͠ෳࡶ w -"-3ߏจղੳʹ͢ΔͨΊʹɺ
 3FEVDFՄೳ͔Ͳ͏͔ͷ൑அʹ΋࣍ͷτʔΫϯΛ࢖͏ w BCDEFʜͰ͸͕ඞཁͳͷͰɺ
 ຊ౰͸BC D E F

    ʜͷܗʹ͍ͨ͠ w ͜͏͢Ε͹BCDEFʜͷΑ͏ʹॻ͚Δʂ w CϝιουΛDΛҾ਺ʹͱΔϝιουʹͯ͠ɺ
 ͭͿΜͷঢ়ଶભҠΛҰؾʹ΍Ε͹ྑ͍
  6. ຊ౰ͷ࣮૷͸ߋʹෳࡶ w ೚ҙͷ෦෼ࣜΛ 4DBMBͷࣜ Ͱஔ͖׵͑ΒΕΔΑ͏ʹ͍ͨ͠ w BC D E F

    ʜͷͲͷ෦෼Λஔ͖׵͑Δ͔ʹΑͬͯ࿩͕ҧ͏ w Bͷ෦෼JNQMJDJUDMBTTΛఆٛͯ͠CϝιουΛݺ΂ΔΑ͏ʹ w Dͷ෦෼Cϝιου͕4DBMBͷࣜΛҾ਺ʹऔΕΔΑ͏ʹ͢Δ w Cͷ෦෼ʹ4DBMB͕ࣜೖΔͱB C D E F ͱͳΓ݁߹͕มΘΔʂ w B C D Ͱͭ෼ͷঢ়ଶભҠΛҰ౓ʹ΍Ε͹ྑ͍ʂ w ಉ༷ʹOݸ෼ͷঢ়ଶભҠΛҰ౓ʹ΍Δ࢓૊Έ͕ඞཁ
  7. 4DBMBMS<IUUQTHJUIVCDPNQIFOBOTDBMBMS> w ࣮ଶ͸-"-3  ύʔβδΣωϨʔλ w ී௨ͷύʔβͰ͸ͳ͘JNQMJDJUWBMVF౳ͷఆٛΛੜ੒͢Δ w !ETMΞϊςʔγϣϯϚΫϩʹΑΓίϯύΠϧ࣌ʹίʔυੜ੒ w

    ϚΫϩύϥμΠε͸ϚΫϩ஍ࠈͩͥ w %4-Ϣʔβ͸JNQPSU͢Δ͚ͩͰ%4-Λ࢖͑Δʂ w ίϯύΠϧʹ͕͔͔࣌ؒΔ w -"-3͸ઢܗͷΞϧΰϦζϜ͕ͩɺ࣮ଌͩͱ0 O ͘Β͍ʹݟ͑Δ