int in Java. Set of numbers between
Integer.MIN_VALUE and Integer.MAX_VALUE.
Operations +, -, *, etc.int short long byte char float double
booleanIntBool(ParamType1,...,ParamTypeN) =>
ReturnType(Int, Int) => Boolval x = 3 * 4; // is an Int val y = 3 < 4; // is a Bool
val x = 3; val y = 4; val z = x * y;
Know types of x and y, therefore type of
z.
{ x, y => x * y }
Don't have initial values for x, y
* is
(Int,Int)=>Int, x and y must be
int{ x : Int, y : Int => x * y }
{ x : Int, y : Int => x < y }
Return type is Bool
def fac = { x : Int => if (x == 0) 1 else x * fac(x - 1) };
Flow analysis doesn't work—doesn't know type of
fac
def must specify return type (like in Scala)
def fac : Int = { x : Int => if (x == 0) 1 else x * fac(x - 1) };
val twice = { f : (Int)=>Int, a : Int => f(f(a)) };
val triple = { x : Int => 3 * x };
twice(triple, 2)
http://ActiveLecture.org
val mystery = { n : Int => { x : Int => n * x } };
val enigma = mystery(3);
enigma(2)
http://ActiveLecture.org
twice?
val twice = { f : (Int)=>Int, a : Int => f(f(a)) };
http://ActiveLecture.org
mystery?
val mystery = { n : Int => { x : Int => n * x } };
http://ActiveLecture.org
class SL3Type
case class SL3Int extends SL3Type
case class SL3Bool extends SL3Type
case class SL3Fun(paramTypes : List[SL3Type], returnType : SL3Type) extends SL3Type
def typeof(expr : ExprTree, types : List[(String, SL3Type)]) : SL3Type
expr is Operator(Variable(x), Number(2),
<function>) and types contains the pair
("x", SL3Int())
expr match {
case Number(_) => SL3Int()
case Variable(name) => lookup(name, types)
case Operator(left, right, _) => {
assert(typeof(left, types) == SL3Int())
assert(typeof(right, types) == SL3Int())
SL3Int()
}
2 * (x < 0)
Function class now holds parameter names and types
case class Function(params : List[(String, SL3Type)], body : Block) extends ExprTree
(paramTypes)=>returnType
case Function(params, body) => SL3Fun(
params.map({_._2}), typeofBlock(body, params ::: types))
typeof applied to { x : Int, y : Int => x
< y } (i.e. Function(List("x", SL3Int()), ("y", SL3Int())),
body) yields SL3Fun(List(SL3Int(), SL3Int()),
SL3Bool())eval/evalDefs/evalBlock, we have
typeof/typeofDefs/typeofBlockparams.map({_._2}) do? http://ActiveLecture.org
val twice = { f : (Int)=>Int, a : Int => f(f(a)) };
val triple = { x : Int => 3 * x };
twice(triple, 2)
What type do you get for the result?
triple? Hint: Just modify
the program to evaluate triple instead of twice(triple,
2). SLFun expression. Translate
it back into a SL3 type of the form
(paramTypes)=>returnTypetwice? Translate it into a SL3 typeval x = 1; if (x < 2) x else x < 2
What happens when you run the SL3 interpreter with this input?
def fac : Bool = { x : Int => if (x == 0) 1 else x * fac(x - 1) };
fac(3)
You'll get an exception when you run the program, but the stack trace
doesn't tell you much about the cause of the error. Run the debugger
instead. When the stack trace hits, the program will stop on the line
assert(typeof(right, types) == SL3Int()). What is the value of
right?
(String, SL3Type).
They lead to messy expressions such as _._2. A better way is
to use a case class
case class TypedIdent(name : String, type3 : SL3Type)
(type is a reserved word in Scala.)
List[(String,
SL3Type)] into a List[TypedIdent]. Make the other
necessary changes and check that the correct version of the
fac function still works. What changes did you make?