Type Checker
In this assignment, you will work with a typed language that includes numbers, booleans, conditionals, functions, and numeric lists (ie lists of numbers). The concrete syntax for the language is given by the following BNF grammars:
<expr> ::= <num>
| true
| false
| {+ <expr> <expr>}
| {- <expr> <expr>}
| {* <expr> <expr>}
| {iszero <expr>}
| {bif <expr> <expr> <expr>}
| <id>
| {with {<id> <expr>} <expr>}
| {fun {<id> : <type>} : <type> <expr>}
| {<expr> <expr>}
| nempty
| {ncons <expr> <expr>}
| {nempty? <expr>}
| {nfirst <expr>}
| {nrest <expr>}
<type> ::= number
| boolean
| nlist
| (<type> -> <type>)
In the surface syntax for types, base types are represented by
symbols, and the arrow type by a Scheme list of three elements: the
type of the argument, the symbol ->, and the type of the
result.
You have not implemented some of these constructs yet, but they should be familiar:
-
iszeroconsumes a number, and returnstrueif it is0,falseotherwise -
the test expression of
bif("boolean if") must evaluate totrueorfalse -
nconsconsumes a number and a numeric list, and produces a numeric list
-
Define the function
parse, which consumes the concrete representation of a program, and returns its abstract syntax tree. You may assume that the concrete representation conforms to the above grammer. -
Write down type judgments for the five numeric list constructs:
nempty,ncons,nempty?,nfirst, andnrest. You should typeset them in ASCII comments at part of your Scheme code, eg:; E |- lhs : number E |- rhs : number ; ----------------------------------------- ; E |- (+ lhs rhs) : number -
Implement the function
type-of, which consumes the abstract representation of a program (i.e. the result ofparse). If the program has no type errors,type-ofreturns the type of the program, using the names of the types given in the grammar above. If the program does have a type error,type-ofraises an exception(raise msg), where msg is an appropriate error string. For example:> (type-of (parse '{3 4})) uncaught exception: "Number is not a function"