






























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!































Types for mini-Java
Class Hierarchy object
point numeric
int double
boolean
colorpoint
void
C |~ int ≤ object
C |~ colorpoint ≤ point
C |~ boolen ≤ object
C defines point
Type Checkers in ML
Representing Programs 1 - Auxillaries
(* A slot of type (`x TC) is an option type. The ) ( parser places NONE there and the type-checker ) ( fills it in with (SOME x) when "x" is known *)
type 'x TC = 'x option;
(******** Representing Programs *******)
datatype Op (* infix operators ) = ADD | SUB | MUL | DIV ( Arithmetic ) | AND | OR ( logical ) | EQ | NE | LT | LE | GT | GE ( relational *)
datatype Constant (* Literal constants *) = Cint of int | Creal of string | Cbool of bool
Representing Programs 2 - expressions
datatype Exp = Literal of Constant (* 5, 6.3, true ) | Binop of Op * Exp * Exp ( x + 3 ) | Relop of Op * Exp * Exp ( x < 7.7 ) | Not of Exp (! x ) | ArrayElm of Exp * Exp * (Basic TC) ( x[3] ) | ArrayLen of Exp ( x.length() ) | Call of Exp * Id (Id TC) Exp list ( x.f(1,z) ) | NewArray of Basic * Exp ( new int[3] ) | NewObject of Id ( new point() ) ( Coerce is used only in type checking *) | Coerce of Exp
Representing programs 4 - declarations datatype VarDecl = VarDecl of Type * Id * Exp option;
datatype Formal = Formal of Type * Id;
datatype MetDecl = MetDecl of Type * Id *
Formal list * VarDecl list * Stmt list;
datatype ClassDec
= ClassDec of Id * Id * VarDecl list * MetDecl list;
datatype Program = Program of ClassDec list;
Type equality
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
Subtyping fun subtype classH (x,y) =
case (x,y) of (x,ObjType “object”) => true | (BasicType Int,ObjType “numeric”) => true | (BasicType Real,ObjType “numeric”) => true | (ObjType x,ObjType y) => useTree classH (x,y) | (,) => typeeq(x,y);
Fun useTree class (x,y) =...
Type rules for mini-java
Th, C, TE |- exp : type Th, R, C, TE |= stmt C |~ t 1 ≤ t 2 t 1 = t 2 C defines x Th, C, TE |- Y.f : (s 1 , … ,sn) → t
Th, C, TE |- Y.x : t [ ]
Op <+> : (t 1 ,t 2 ) -> t 3 t is basic x ∉ TE and TE (x) = t
Expression has type
Statement is well formed t 1 is a subtype of t (^2)
Class Y has method f with domain and range
Class Y has variable x with type
Simple Expressions
Operators
Th, C, TE |- e 1 : t 1
Th, C, TE |- e 2 : t 2
Op <+> : (t 1 ,t 2 ) -> t 3
Th, C, TE |- e 1 <+> e 2 : t 3
Th, C, TE |- e 1 : boolean
Th, C, TE |-! e 1 : boolean Docsity.com
Arrays
Th, C, TE |- e 1 : t [ ]
Th, C, TE |- e 2 : int
Th, C, TE |- e 1 [ e 2 ] : t
Th, C, TE |- e : t [ ]
Th, C, TE |- e.length() : int