SacalaZa #1

1,943 views

Published on

  • Be the first to comment

SacalaZa #1

  1. 1. Scala ocaml-nagoya / mzp 2010-09-04 ScalaZa #1 1
  2. 2. • / mzp • http://twitter.com/mzp • SE(not Scala) • 2
  3. 3. 10% love hate 45% neither 45% ( ) 3
  4. 4. 10% Yes No 90% ( ) 4
  5. 5. Scala Java Integer x = inc(42) Scala val x = inc(42) 5
  6. 6. .. 6
  7. 7. def inc(n : Int ) : Int = n + 1 ( ) 7
  8. 8. def id[T](x : T) = x 8
  9. 9. 9
  10. 10. def inc(x) = x + 1 def inc(x : Int) : Int 10
  11. 11. def id(x) = x def id[T](x : T): T 11
  12. 12. Scala 3.0 ! http://d.hatena.ne.jp/kmizushima/20100401/p1 12
  13. 13. Scala3.0 13
  14. 14. Hindley-Milner • • ( ) • • OCaml Haskell 14
  15. 15. ← Types and Programming Laungages(TAPL) • 2 • 15
  16. 16. Scalet • Scala + let = Scalet • http://bitbucket.org/mzp/scalet/ • : 376 , : 262 • Scala • implicit conversion . ... 16
  17. 17. 17
  18. 18. • • int bool • let if fn 3 • 18
  19. 19. 19
  20. 20. AST AST 20
  21. 21. • case sealed abstract class Term // case class Var(name : String) extends Term // λ fn x => x + 1 case class Lambda(arg : String,body : Term) extends Term // f x case class Apply(f : Term,arg : Term) extends Term ... 21
  22. 22. • // def varref : Parser[Term] = ident ^^ (Var((_ : String))) // λ def lambda : Parser[Term] = (keyword("fn") ~> ident <~ keyword("->")) ~ term ^^ { case x~body => Lambda(x,body) } // def term : Parser[Term] = (lambda | varref | .. ) 22
  23. 23. • def eval(term : Term) : Term = term match { // case Var(_) | Lambda(_,_) | ... => term // f x case Apply(Lambda(x,body),y @ Value()) => body.subst(x -> y).eval // f x f x case Apply(x @ Value(),y) => Apply(x,y eval).eval // f x f case Apply(x,y) => Apply(x eval,y).eval .... } 23
  24. 24. • case • (ry sealed abstract class Type // Int case class Int() extends Type // Bool case class Bool() extends Type // case class TVar(name : String) extends Type // case class ->:(from : Type,to : Type) extends Type 24
  25. 25. : plus1 : int -> int : x :α let plus2 x = → plus1 : α -> β plus1 (plus1 x) → plus1 : β ->γ plus2 : α -> γ ( ): plus2 : int -> int 25
  26. 26. def constrain(term : Term, env : Map[String,Type]) : (Type,TypeConstrain) = term match { case Var(name) => // (env(name),TypeConstrain()) case Lambda(arg,body) => // λ val x = TypeInfer gensym val (t,c) = body.constrain(env + (arg -> x)) (x ->: t,c) case Apply(f,arg) => // val (t1,c1) = f constrain env val (t2,c2) = arg constrain env val x = TypeInfer gensym val c = c1 ++ c2 + (t1 -> (t2 ->: x)) (x,c) ... 26 }
  27. 27. ( ) def unify() : Map[String,Type] = types match { case List() => Map() case x::xs => val ys = new TypeConstrain(xs) x match { case (s,t) if(s == t) => ys unify .... } } 27
  28. 28. 28
  29. 29. Hindley-Milner • Hindley Milner • (Principal Type ) • • 29
  30. 30. Scala ( ) • • Scala 3.0 .. def f(x) = x.close def f[A <: { def close(): Unit }](x : A): Unit 30
  31. 31. ( ) • Java • ? • Java Scala ? • Java Scala ? 31
  32. 32. • • • Scala • Scala3.0 ( ) 32
  33. 33. • Benjamin-Pierce, Types and Programming Laungages • Scala by Example • http://www.scala-lang.org/docu/files/ ScalaByExample.pdf 33
  34. 34. One more thing.... 34
  35. 35. ! • • http://github.com/mzp/lambda/ 35

×