24
Javascript as an Embedded DSL」を 読んで - ScalaにおけるExpression Problemの解法 - Scala基礎勉強会 2012/10/21 前 康 (@maeda)

Javascript as an Embedded DSL - Expression Problemの解法例

Embed Size (px)

DESCRIPTION

LMS(Lightweght Modular Staging)を応用したScala→Javascriptの変換の論文を題材に、Traitを駆使したモジュール分割と合成の例を説明した資料です。

Citation preview

  • 1. Javascript as an Embedded DSL - ScalaExpression Problem - Scala 2012/10/21 (@maeda_)

2. (@maeda_ ) http://www.illi-ichi.com Scala Smalltalk DyNagoya (Dynamic language + Nagoya) http://dynagoya.info @dynagoya_info 3. (2014/6/27 ) Scala.js js-scala 4. JavaScript as an Embedded DSL authors Grzegorz Kossakowski, Nada Amin, Tiark Rompf, and Martin Odersky published ECOOP 2012 Object-Oriented Programming, vol. 7313, p. 409-434 abstract JavascriptScalaDSL Lightweight Modular Staging 5. Multi-Staging Programming(MSP) Staging, Staged MSP O. Kiselyov, K. N. Swadi, and W. Taha. A methodology for generating veried combinatorial circuits. In G. C. Buttazzo, editor, EMSOFT, pages 249258. ACM, 2004. MetaOcamlFFT() 6. Lightweight Modular Staging(LMS) ScalaMSP Lightweight n (MetaOcamlTemplate Haskell) Modular n Staging LMS Delite() http://stanford-ppl.github.com/Delite/ JS-Scala (Scala Javascript) https://github.com/js-scala/virtualization-lms-core 7. Scala Javascript call back ScalaObject Dynamic TraitJS DSL Expression Problem 8. Expression Problem Extensibility in both dimensions () Strong static type safety No modication or duplication Separate compilation Independent extensibility(Scala) 9. Independent extensibility Matthias Zenger, Martin Odersky, Independently Extensible Solutions to the Expression Problem, I&C Technical Report No. 200433, March 2004. 10. Trait Trait JS traitDSL trait trait JS extends LiftNumeric with NumericOps with OrderingOps with Equal with IfThenElse with While with LiftBoolean with BooleanOps with LiftString with StringOps with DynamicBase with Arrays with Variables with JSFunctions with JSLiteral 11. JS JS Generator Scala JS DSL DSL () DSL Javascript Generator mix in implement mix in mix in DSL () DSL DSL DSL () JS mix in Trait Instance correspond 12. JS-Scala jQuery Trait DSLjQueryAPI DSLjQuery JS 13. JS JS Generator Scala JS DSL DSL () DSL Javascript Generator mix in implement mix in mix in DSL () DSL DSL DSL () JS mix in Trait Instance correspond 14. DSL Rep[T]JST // DSL trait SomeDSL { this: JQuery with JS => def code = $(#message).html(Hello World) } // DSL trait JQuery extends JSProxyBase { def $(selector: Rep[String]): Rep[JQElement] trait JQElement{ def html(html: Rep[String]): Rep[JQElement] } implicit def repToJQuery(x: Rep[JQElement]): JQElement = repProxy[JQElement](x) } 15. JS JS Generator Scala JS DSL DSL () DSL Javascript Generator mix in implement mix in mix in DSL () DSL DSL DSL () JS mix in Trait Instance correspond 16. DSL(1) // DSL trait JQueryExp extends JQuery with JSProxyExp { case class JQ(selector: Exp[String]) extends Def[JQElement] def $(selector: Exp[String]): Exp[JQElement] = reectEect(JQ(selector)) } DSLRep[T]Exp[T] StagingreectEect JQElementJSProxyExp 17. DSL(2) // trait SleepExp extends JSLibExp with JSFunctionsExp { def sleep(delay: Exp[Int]) = shift { k: (Exp[Unit] => Exp[Unit]) => window.setTimeout(fun(k), delay) } } setTimeoutCallback (fun : Exp[A] => Exp[B] => Exp[A => B]) 18. JS JS Generator Scala JS DSL DSL () DSL Javascript Generator mix in implement mix in mix in DSL () DSL DSL DSL () JS mix in Trait Instance correspond 19. JS abstract valueIRTrait (IR = Intermediate Representation) Trait // JQueryJS trait GenJQuery extends JSGenProxy with JSGenLib{ val IR: JQueryExp // abstract value import IR._ override def emitNode(sym: Sym[Any], rhs: Def[Any])( implicit stream: PrintWriter) = rhs match { case JQ(s) => emitValDef(sym, "$(" + quote(s) + ")") case _ => super.emitNode(sym, rhs) } } 20. JS JS Generator Scala JS DSL DSL () DSL Javascript Generator mix in implement mix in mix in DSL () DSL DSL DSL () JS mix in Trait Instance correspond 21. DSLJS TraitGenerator Traitabstract value Trait new SomeDSL with JQueryExp with JSExp { self => val codegen = new JQueryGen with JSGen { val IR: self.type = self } } 22. ScalaTrait self type annotationabstract type/value JS-ScalaCake Pattern Expression Problem https://github.com/js-scala/js-scala 23. LMSJS-Scala JS MSP(Multi Staging Programming) DSL LMS Parts DSL 24. Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs by Tiark Rompf and Martin Odersky, EPFL GPCE 2010, Eindhoven, The Netherlands, October 2010 A Gentle Introduction to Multi-stage Programming by Walid Taha Matthias Zenger, Martin Odersky, Independently Extensible Solutions to the Expression Problem, I&C Technical Report No. 200433, March 2004.