



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




David Evans http://www.cs.virginia.edu/evans
CS150: Computer Science University of Virginia Computer Science
Lecture 32: Truthiness 2
Lecture 32: Truthiness 3
Scheme
Python
Charme
LazyCharme
StaticCharme
Java
Lecture 32: Truthiness 4
Charme StaticCharme
change grammar, represent types
typecheck expressions before eval
Lecture 32: Truthiness 5
Lecture 32: Truthiness 6
Lecture 32: Truthiness 7
Changed parameters grammar rule: Parameter ::= Name : Type
CType ::= CPrimitiveType | CProcedureType | CProductType CPrimitiveType ::= Number | Boolean CProcedureType ::= ( CProductType -> Type) CProductType ::= ( CTypeList) CTypeList ::= CType CTypeList CTypeList ::=
Lecture 32: Truthiness 8
CType ::= CPrimitiveType | CProcedureType | CProductType CPrimitiveType ::= Number | Boolean CProcedureType ::= ( CProductType -> Type) CProductType ::= ( CTypeList) CTypeList ::= CType CTypeList CTypeList ::=
CType
CPrimitiveType
CProcedureType CProductType
CErrorType
superclass
subclasses
Lecture 32: Truthiness 9
class CType: @staticmethod def fromString(s):
tparse = parse(s) return CType.fromParsed(tparse[0])
@staticmethod def fromParsed(typ): ... # create type from parsed type
def isPrimitiveType(self): return False def isProcedureType(self): return False def isProductType(self): return False def isError(self): return False
No self object
Lecture 32: Truthiness 10
class CPrimitiveType(CType): def init(self, s): self._name = s def str(self): return self._name def isPrimitiveType(self): return True def matches(self, other): return other.isPrimitiveType() and self._name == other._name
class X(Y): means X is a subclass of Y
Lecture 32: Truthiness 11
class CProcedureType(CType): def init(self, args, rettype): self._args = args self._rettype = rettype def str(self): return "(" + str(self._args) + " -> " \
Lecture 32: Truthiness 12
class CProductType(CType): def init(self, types): self._types = types def str(self): ... def isProductType(self): return True def matches(self, other): if other.isProductType(): st = self._types ot = other._types if len(st) == len(ot): for i in range(0, len(st)): if not st[i].matches(ot[i]): return False
return True return False
Lecture 32: Truthiness 19
def typeDefinition(expr, env): assert isDefinition(expr) if len(expr) != 5: evalError ("Bad definition: %s" % str(expr)) name = expr[1] if isinstance(name, str): if expr[2] != ':': evalError ("Definition missing type: %s" % str(expr)) typ = CType.fromParsed(expr[3]) etyp = typecheck(expr[4], env) if not typ.matches(etyp): evalError("Mistyped definition: ..." % (name, typ, etyp)) elif isinstance(name, list): evalError ("Procedure definition syntax not implemented") else: evalError ("Bad definition: %s" % str(expr))
Lecture 32: Truthiness 20
def typecheck(expr, env): if isPrimitive(expr): return typePrimitive(expr) elif isConditional(expr): return typeConditional(expr, env) elif isLambda(expr): return typeLambda(expr, env) elif isDefinition(expr): typeDefinition(expr, env) elif isName(expr): return typeName(expr, env) elif isApplication(expr): return typeApplication(expr, env) else: evalError ("Unknown expression: " + str(expr))
Lecture 32: Truthiness 21
class Procedure: def init(self, params, typ, body, env): self._params = params self._body = body self._typ = typ self._env = env def getParams(self): return self._params def getParamTypes(self): return self._typ def getBody(self): return self._body def getEnvironment(self): return self._env def str(self): return "" % (str(self._params), str(self._body))
Add type to Procedure
Lecture 32: Truthiness 22
def evalLambda(expr, env): assert isLambda(expr) if len(expr) != 3: evalError ("Bad lambda expression: %s" % (str(expr))) params = expr[1] paramtypes = [] paramnames = [] assert len(params) % 3 == 0 for i in range(0, len(params) / 3): name = params[i3] assert params[(i3)+1] == ':' paramnames.append(name) typ = CType.fromParsed(params[(i*3)+2]) paramtypes.append(typ) return Procedure(paramnames, paramtypes, expr[2], env)
Lecture 32: Truthiness 23
def typeLambda(expr, env): assert isLambda(expr) if len(expr) != 3: evalError ("Bad lambda expression: %s" % str(expr))
newenv = Environment(env) params = expr[1] paramnames = [] paramtypes = [] assert len(params) % 3 == 0 for i in range(0, len(params) / 3): name = params[i3] assert params[(i3)+1] == ':' typ = CType.fromParsed(params[(i*3)+2]) paramnames.append(name) paramtypes.append(typ) newenv.addVariable(name, typ, None) resulttype = typecheck(expr[2], newenv) return CProcedureType(CProductType(paramtypes), resulttype)
Lecture 32: Truthiness 24
def typecheck(expr, env): if isPrimitive(expr): return typePrimitive(expr) elif isConditional(expr): return typeConditional(expr, env) elif isLambda(expr): return typeLambda(expr, env) elif isDefinition(expr): typeDefinition(expr, env) elif isName(expr): return typeName(expr, env) elif isApplication(expr): return typeApplication(expr, env) else: evalError ("Unknown expression: " + str(expr))
Lecture 32: Truthiness 25
def typeApplication(expr, env): proctype = typecheck(expr[0], env) if not proctype.isProcedureType(): evalError("Application of non-procedure: " + str(expr[0])) optypes = map (lambda op: typecheck(op, env), expr[1:]) optype = CProductType(optypes) if not optype.matches(proctype.getParameters()): evalError("Parameter type mismatch: ..." % (proctype.getParameters(), optype)) return proctype.getReturnType()
Lecture 32: Truthiness 26
def typecheck(expr, env): if isPrimitive(expr): return typePrimitive(expr) elif isConditional(expr): return typeConditional(expr, env) elif isLambda(expr): return typeLambda(expr, env) elif isDefinition(expr): typeDefinition(expr, env) elif isName(expr): return typeName(expr, env) elif isApplication(expr): return typeApplication(expr, env) else: evalError ("Unknown expression: " + str(expr))
Lecture 32: Truthiness 27
def typePrimitive(expr): if isNumber(expr): return CPrimitiveType('Number') elif isinstance(expr, bool): return CPrimitiveType('Boolean') elif callable(expr): return findPrimitiveProcedureType(expr) else: assert False This is a kludgey procedure that looks through the global environment to find the matching procedure, and returns its type
Lecture 32: Truthiness 28
def typecheck(expr, env): if isPrimitive(expr): return typePrimitive(expr) elif isConditional(expr): return typeConditional(expr, env) elif isLambda(expr): return typeLambda(expr, env) elif isDefinition(expr): typeDefinition(expr, env) elif isName(expr): return typeName(expr, env) elif isApplication(expr): return typeApplication(expr, env) else: evalError ("Unknown expression: " + str(expr))
Left as possible Exam 2 question!
Lecture 32: Truthiness 29
StaticCharme> (+ 1 #t) Error: Parameter type mismatch: expected (Number Number), given (Number Boolean) StaticCharme> (define square:((Number) -> Number) (lambda (x:Number) (* x x))) StaticCharme> (square #t) Type error: Parameter type mismatch: expected (Number), given (Boolean) StaticCharme> (define badret:((Number) -> Number) (lambda (x: Number) (> x 3))) Error: Mistyped definition: badret declared type ((Number) -> Number), actual type ((Number) -> Boolean)
Lecture 32: Truthiness 30
(define compose:((((Number) -> Number) ((Number) -> Number)) -> ((Number) -> Number)) (lambda (f:((Number) -> Number) g:((Number) -> Number)) (lambda (x:Number) (f (g x)))))
Is it possible to define compose in StaticCharme?
But, no way to define a compose that works for any functions.