

















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!


















and Dec = Valdec of (stringMLtype)Exp | Fundec of (stringMLtypeMLtype)stringExp | Mutdec of Dec list
information about their types (as is true in Mini-Java)
TCExp:Exp ->(string * MLtype)list ->MLtype TCDec:Dec ->(string * MLtype)list ->(string * MLtype) list
x: int f: int -> int
even: int -> bool odd: int -> bool
fun (even:int ->bool) (n:int) = if n= then true else odd (n-1) and (odd:int -> bool) (n:int) = if n= then false else if n= then true else even (n-1);
| TCDec (Mutdec ds) cntxt =
let fun pass1 [] cntxt = cntxt | pass1 (Valdec(p,b)::ds) cntxt = pass1 ds (p::cntxt) | pass1 (Fundec((f,d,r),x,b)::ds) cntxt = pass1 ds ((f,Arrow(d,r))::cntxt) val temp = pass1 ds cntxt val pass2 = map (fn d => TCDec d temp) ds in List.concat pass2 end;
x: int f: int -> int
odd: int->bool even: int->bool x: int f: int -> int
even: int->bool odd: int->bool
even: int->bool odd: int->bool
cntxt temp^ pass2^ result
nested scope.
type ('name,'value)table = ('name * 'value) list; type ('name,'value)Scope = (('name,'value)table) list ;
fun initialize scope = [ ] :: scope fun insert name value (table::scope) = ((name,value)::table)::scope fun Lookup name (table::scope) = case List.find (fn (x,y) => x=name) table of NONE => error | SOME (x,y) => y fun finalize (table::scope) = scope
More sophisticated kinds of tables can be used here rather than a linked list of pairs. Hash Tables, Balanced trees, etc.
Th, C, TE |- exp : type
Th, R, C, TE |= stmt
C |~ t 1 ≤ t 2
t 1 = t 2
C defines x
In C p has method f
datatype CTab
= Node of Id * (* Class Name ) (Id * Type)list * ( Class Variables ) (Id * Type list * Type)list * ( Methods ) CTab ref * ( Parent Class ) CTab list ( Sub Classes *) | NullClass;
val root = Node("object",[],[],ref NullClass,[]);
CTab for class table is an n-way branching tree.
object
point numeric
int double
boolean
colorpoint
void
val t1 = newClass "point" [] [] "object" root;
Node(“object”, [ ], [ ], ref NullClass,[ ])
Node(“point”, [ ], [ ], ref _ ,[ ])
root
new
Node(“object”, [ ], [ ], ref NullClass, [Node(“point”, [ ], [ ], ref _ ,[ ])])
newP
val t1 = newClass "point" [] [] "object" root;
val t2 = newClass "colorpoint" [] [] "point" t1;
Node(“point”, [ ], [ ], ref _ ,[ _ ])
Node(“object”, [ ], [ ], ref NullClass ,[ _ ])
Node(“colorpoint”, [ ], [ ], ref _ ,[ ])
t
fun basiceq (x,y) =
case (x,y) of
(Real,Real) => true
| (Int,Int) => true
| (Bool,Bool) => true
| (,) => false
fun typeeq (x,y) =
case (x,y) of
(BasicType x,BasicType y) => basiceq(x,y)
| (ArrayType x,ArrayType y) => basiceq(x,y)
| (ObjType x,ObjType y) => x=y
| (VoidType,VoidType) => true
| (,) => false Docsity.com