- Review Homework
-
We spent a lot of time reviewing the HW in painful detail!
- Problems with no type checking
-
Right now, our simple SPL interpreter does no type checking at
all. This has implications from the merely annoying:
write 4 < 5;
1 # Why don't I get "true" as a result?
new sqr := lambda x { res := x*x; };
write sqr;
134849776 # Why don't I get "function" as a result?
write sqr = 11612^2 + 11232;
1 # Functions and numbers shouldn't be equal!
... to the truly catastophic:
new sqr := 9;
write sqr(3);
Segmentation fault # The interpreter crashes and never knows why!
We like the idea that systems know how to print or read or
otherwise manipulate objects based on their type, rather than
having the programmer specify everything. Moreover, we like
interpreters to recognize illegal operations and do the
print-error-and-return-to-interpreter thing rather than
crash. In our compiled code, we like errors like type
mismatches to be found so that we don't have mysterious
crashes once the code gets run. So type checking is important
stuff.
-
- What is "type"
- A "Type System" provides:
- mechanism for defining types and associating them
with basic program elements, e.g. variable declarations.
- rules for type inference - i.e. given an expression
and the types of its subexpressions, infer the type
of the expression.
- mechanism for determining "type equivalence"
- mechanism for determining "type compatability" -
i.e. in a given context can two types be used interchangably
- Do functions have types? Depends on whether functions
are 1st/2nd/3rd class objects.
- Do types have type? Dependes on whether types are 1st/2nd/3rd class objects.
- Static vs. Dynamic
-
There are two basic ways to do type checking: static or
dynamic. You should recognize those words by now!
- Dynamic Typing
-
every object gets tagged with a type
each op has to check types of operands
P1: class Obj { int otype; int oival; bool obval; Node *ofval; };
P2: class Obj { int otype; union { int oival; bool obval; Node *ofval; }; };
New symbol table?
Show how it works.
Only finds errors when code is run!
Makes sense for interpreted languages.
- Static Typing
-
Every name must have a type declaration.
We must do type inference!
Rules for SPL?