Docsity

Static Type Checking in Charme Programming Language - Prof. David Evans, Study notes of Computer Science

Toggle sidebar
bg1
1
David Evans
http://www.cs.virginia.edu/evans
CS150: Computer Science
University of Virginia
Computer Science
Lecture 32:
Lecture 32:
Truthiness
Truthiness
“It would appear that we have reached the
limits of what it is possible to achieve with
computer technology, although one should be
careful with such statements, as they tend to
sound pretty silly in five years.”
John Von Neumann, 1949
2
Lecture 32: Truthiness
Problem Set 8
• Understand and modify a dynamic web
application
• Will be posted on the web site before
midnight tonight
• Due next Friday (April 13)
• Team requests and ideas due Monday
(email me before midnight)
Problem Set 9
3
Lecture 32: Truthiness
Programming Language Design Space
Expressiveness
“Truthiness”
Scheme
Python
Charme
LazyCharme
StaticCharme
Java
C++
4
Lecture 32: Truthiness
Types of Types
Latent
Dynamically Checked
Manifest
Statically Checked
Charme StaticCharme
change grammar, represent types
typecheck expressions before eval
5
Lecture 32: Truthiness
Manifest Types
Need to change the grammar rules to
include types in definitions and parameter
lists
Definition
::= (define
Name
:
Type Expression
)
Parameters
::= ε |
Parameter Parameters
Parameter
::=
Name
:
Type
Type ::=
??
6
Lecture 32: Truthiness
Types in Charme
CType
::=
CPrimitiveType
CType
::=
CProcedureType
CType
::=
CProductType
CPrimitiveType
::= Number | Boolean
CProcedureType
::= (
CProductType
->
Type
)
CProductType
::= (
CTypeList
)
CTypeList
::=
CType CTypeList
CTypeList
::=
pf3
pf4
pf5

Partial preview of the text

Download Static Type Checking in Charme Programming Language - Prof. David Evans and more Study notes Computer Science in PDF only on Docsity!

David Evans http://www.cs.virginia.edu/evans

CS150: Computer Science University of Virginia Computer Science

Lecture 32:Lecture 32:

Truthiness Truthiness

“It would appear that we have reached the

limits of what it is possible to achieve with

computer technology, although one should be

careful with such statements, as they tend to

sound pretty silly in five years.”

John Von Neumann, 1949

Lecture 32: Truthiness 2

Problem Set 8

• Understand and modify a dynamic web

application

• Will be posted on the web site before

midnight tonight

• Due next Friday (April 13)

• Team requests and ideas due Monday

(email me before midnight)

Problem Set 9

Lecture 32: Truthiness 3

Programming Language Design Space

Expressiveness

“Truthiness”

Scheme

Python

Charme

LazyCharme

StaticCharme

Java

C++

Lecture 32: Truthiness 4

Types of Types

Latent

Dynamically Checked

Manifest

Statically Checked

Charme StaticCharme

change grammar, represent types

typecheck expressions before eval

Lecture 32: Truthiness 5

Manifest Types

Need to change the grammar rules to

include types in definitions and parameter

lists

Definition ::= (define Name : Type Expression)

Parameters ::= ε | Parameter Parameters

Parameter ::= Name : Type

Type ::= ??

Lecture 32: Truthiness 6

Types in Charme

CType ::= CPrimitiveType

CType ::= CProcedureType

CType ::= CProductType

CPrimitiveType ::= Number | Boolean

CProcedureType ::= ( CProductType -> Type)

CProductType ::= ( CTypeList)

CTypeList ::= CType CTypeList

CTypeList ::=

Lecture 32: Truthiness 7

Number

((Number Number) -> Number)

Number

(lambda (x:Number y:Number) (> x y))

((Number Number) -> Boolean)

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

Representing Types

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):

create type from string

tparse = parse(s) return CType.fromParsed(tparse[0])

@staticmethod def fromParsed(typ): ... # create type from parsed type

These methods are overridden by subclasses

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

CPrimitiveType

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

CProcedureType

class CProcedureType(CType): def init(self, args, rettype): self._args = args self._rettype = rettype def str(self): return "(" + str(self._args) + " -> " \

  • str(self._rettype) + ")" def isProcedureType(self): return True def getReturnType(self): return self._rettype def getParameters(self): return self._args def matches(self, other): return other.isProcedureType() and self.getParameters().matches(other.getParameters()) and self.getReturnType().matches(other.getReturnType())

Lecture 32: Truthiness 12

CProductType

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

reached end of loop ==> all matched

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

Static Type Checking

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))

this is a bit tricky - we need to "partially" apply it

to find the type of the body

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

Static Type Checking

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

Type Application

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

Static Type Checking

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

Type Primitives

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

Static Type Checking

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

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

(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.