phantom type in scala

32
Phantom Type in Scala ファントムタイプ社の社長にみんなでPhatom Typeを教える会 2012.10.06 前田康行(@maeda_)

Upload: yasuyuki-maeda

Post on 27-Jun-2015

3.240 views

Category:

Technology


4 download

DESCRIPTION

Phantom Type自体の説明はほとんどありません。Implicit parameterを使って、コンパイル時にごにょごにょやる方法を通し、型の表現によっていろいろできることを説明しています。

TRANSCRIPT

  • 1. Phantom Type in Scala Phatom Type2012.10.06 (@maeda_)

2. (@maeda_ ) Scala Smalltalk DyNagoya (Dynamic language + Nagoya) http://dynagoya.info 3. About Phatom Type A phantom type is a parametrised type whoseparameters do not all appear on the right-handside of its denition- Haskell wiki -http://www.haskell.org/haskellwiki/Phantom_type 4. Tcase class case class A[T](x : Int)scala> A[String](3)res0: A [String] = A(3)scala> A [List[Char]](3)res1: A [List[Char]] = A(3)scala> res0 == res1res2: Boolean = true ScalaType Erasure 5. Phantom Type Phantom Type() 6. Phatom Type 1Type Safe Expr[T]Ttrait Expr[T]case object True extends Expr[Boolean]case object False extends Expr [Boolean]case class Number(x: Int) extendsExpr [Int]case class Plus(x1: Expr [Int], x2:Expr [Int]) extends Expr [Int]scala> Plus(Plus(Number(3),Number(4)),Number(5))res4: Plus = Plus(Plus(Number(3),Number(4)),Number(5))scala> Plus(Plus(Number(3), Number(4)), True) // PlusBoolean:14: error: type mismatch; found : True.type required: Expr[Int]Plus(Plus(Number(3), Number(4)), True) 7. Phatom Type 2 () sealed trait Stateclass Empty private () extends Stateclass Ready private () extends Stateclass Dinner[Oeuvre Dinner.start.cookSalad.cookCake.serve()Now you can eat!scala> Dinner.start.cookSalad.cookSteak.cookSoup.serve()Now you can eat! 9. 10. Implicit Parameter 11. implicit parameter // implicit parameterscala> def func(implicit ev: Int) = m// implicit parameterscala> func:9: error: could not nd implicit value for parameterm: Int// implicitscala> implicit val v = 3scala> func // 3 implicit 12. implicit parameter scala> def implicitly[T](implicit ev: T) = mscala> implicit val v = 3scala> implicitly[Int] // 3scala> implicitly[String]// implicitlyPredef implicit 13. class Tag[A]()def func[A](implicit ev: Tag[A]) = "OKimplicit val intAllowed = new Tag[Int]scala> func[Int] // OKscala> func[String] // implicit implicit 14. class Tag[A, B] ()def func[A, B](implicit ev: Tag[A, B]) = "OK"implicit val intAllowed = new Tag[Int, Int]implicit val stringAllowed = new Tag[String, String]scala> func[Int, Int]// OKscala> func[Int, String] // implicit 15. implicit val x[A] = new Tag[A, A] implicit def f[A] = new Tag[A, A] 16. class Tag[A, B] ()implicit def equalAllowed[A] = new Tag[A, A]def func[A, B](implicit ev: Tag[A, B]) = "OKscala> func[Int, Int]// OKscala> func[Int, String] // 17. class Tag[A, B] ()val singletonTag = new Tag[Any, Any]implicit def equalAllowed[A]: Tag[A, A] = singletonTag.asInstanceOf[Tag[A, A]]def func[A, B](implicit ev: Tag[A, B]) = "OK" implicit implicit nullOK 18. scala> class Op[A, B] ()scala> val x: Op[Int, String] = new Opx: Op[Int,String] = Op@ab612f8scala> val x: Int Op String = new Opx: Op[Int,String] = Op@165e6c89 19. Scala 2.10-M7 sealed abstract class =:=[From, To]extends (From => To) with Serializableprivate[this] nal val singleton_=:= =new =:=[Any,Any] { def apply(x: Any): Any = x }object =:= {implicit def tpEquals[A]: A =:= A =singleton_=:=.asInstanceOf[A =:= A]} From => To def func[A](x1: A)(implicit w: A =:= B ) = {val x2 = w(x1) // x2B...} Serializable 20. generalized type constraints scala> implicitly[Int =:= Int]res7: =:=[Int,Int] = scala> implicitly[Int =:= String]:9: error: Cannot prove that Int =:= String. implicitly[Int =:= String] ^ 21. Prove? 22. A =:= Bimplicit AB 23. Coq http://d.hatena.ne.jp/yoshihiro503/20100119 Coq 24. Scala http://apocalisp.wordpress.com/ 2010/06/08/type-level-programming-in- scala/ http://www.chuusai.com/2011/06/09/scala- union-types-curry-howard/ 25. Phantom Type() Scalaimplicit parameter Coq 26. ProofCafe(4) http://proofcafe.org/wiki/ TAPL-nagoya () ScalaF#SML# TAPL http://proofcafe.org/tapl/ 27. 28. BOKscala> implicitly[ Int %> implicitly[ Int implicitly[ Int => Long ]res5: Int => Long = 29. Dinner.start.cookSalad.cookCake.serve():8: error: Cannot prove that Dinner.Empty =:=Dinner.Ready. Dinner.start.cookSalad.cookCake.serve()^scala> Dinner.start.cookSalad.cookCake.cookSalad:8: error: Cannot prove that Dinner.Ready =:=Dinner.Empty. Dinner.start.cookSalad.cookCake.cookSalad ^scala> Dinner.start.cookSalad.cookCake.cookSteak.serve()Now you can eat! 31. 2 ABscala> trait Ascala> trait B with Ascala> implicitly[ B