Polymoprhic Typing Rule


We will say a monomorphic type τ is an instance of a polymorphic type σ if there exists a monomorphic type τ, (a possibly empty) list of type variables α1,,αn, and a corresponding list of monomorphic types τ1,,τn such that σ=α1,,αn. τ and τ=τ[τ1/α1,,τn/αn], the type gotten by replacing each occurence of αi in τ by τi. When using the rule below that require one type to be an instance of another, you should give the instantiation: [τ1/α1,,τn/αn].

In simpler terms, think of a polymorphic type like a template and a monomorphic type like a filled-out form of that template. The process described is like saying, “To prove that this filled-out form is based on a specific template, show me how you substituted the placeholders in the template with actual information to get this form.

Signatures

Polymorphic constant signatures:

sig(true) = bool sig(true) = bool
sig(n)=int n an integer constant sig(f)=float f a floating point (real) constant
sig(s)=string s a string constant sig([])=α.αlist

Polymorphic Unary Primitive Operators:

sig(fst)=αβ.(αβ)α sig(snd)=αβ.(αβ)β
sig(hd)=α.αlistα sig(tl)=α.αlistα list
sig()=int  int sig(print_string)=string  unit
sig(not)=bool  bool

Polymorphic Binary Primitive Operators:

sig()=int int int for {+,,,mod,/} sig()=string  stirng string
sig()=float float float for {+.,.,.,/.,} sig((_,_))=αβ.αβαβ
sig()=bool bool bool for {||,&&} sig(::)=α.αα listα list
sig()=α.ααbool for {<,>,=,<=,>=,<>}

Rules

Constants:

Γc:τ CONST where c is a constant listed above, and τ is an instance of sig(c)

Variables:

Γx:τ VAR  where x is a variable and τ is an instance of Γ(x)

Unary Primitive Operators:

Γe:τ1Γe:τ2 MONOP τ1τ2 an instance of sig()

Binary Prmitive Operators:

Γe1:τ1Γe2:τ2Γe1e2:τ3 BINOP τ1τ2τ3 an instance of sig()

If then else rule:

Γec:boolΓet:τΓee:τΓifecthenetelseee:τ IF

Application rule:

Γe1:τ1τ2Γe2:τ1Γe1e2:τ2 APP