Higher-Order

Subtyping

Martin

Steffen

Benjamin

Piercey

February

University

of

Edinburgh

Technical

Report

and

IMMD

VII

Universit?at

Erlangen-N

urnberg

Interner

Bericht

Revised

version

with

Fun

subtyping

Abstract

System

F

is

an

extension

with

subtyping

of

Girard

's

higher-order

polymorphic

calculus

We

develop

the

fundamental

metatheory

of

this

calculus

decidability

of

conversion

on

well-kinded

types

elimination

of

the

cut-rule

of

transitivity

from

the

subtype

relation

and

the

soundness

completeness

and

termination

of

algorithms

for

subtyping

and

typechecking

Introduction

Since

the

early

's

increasing

attention

in

the

programming

language

community

has

been

devoted

to

formal

models

for

statically

typed

object-oriented

languages

Cardelli

-LSB-

-RSB-

observed

that

refinement

of

object

interfaces

can

be

modeled

by

records

and

a

simple

form

of

subtyping

To

account

for

the

types

of

message-sending

operations

Cardelli

and

Wegner

-LSB-

-RSB-

introduced

bounded

quantification

Fully

capturing

the

object

model

of

languages

like

Smalltalk

required

one

further

refinement

the

extension

to

calculi

with

higher-order

polymorphism

to

deal

properly

with

the

interaction

between

subtyping

and

object

encapsulation

A

number

of

typed

object

models

have

been

given

in

this

general

setting

Cook

Canning

Hill

Olthoff

and

Mitchell

-LSB-

CCH

-RSB-

proposed

a

variant

called

F-bounded

quantification

which

was

used

by

Bruce

-LSB-

-RSB-

to

give

the

first

full

account

of

static

typing

for

Smalltalk-style

objects

Pierce

and

Turner

-LSB-

-RSB-

gave

a

similar

model

using

existential

types

instead

of

recursive

types

to

capture

object

encapsulation

effectively

working

within

pure

F

These

two

approaches

were

generalized

by

Hofmann

and

Pierce

-LSB-

-RSB-

to

an

abstract

axiomatic

presentation

of

objects

and

subtyping

University

of

Erlangen-N

urnberg

IMMD

VII

Martensstr

Erlangen

Germany

Electronic

mail

mnsteffe@informatik.uni-erlangen.de

y

Department

of

Computer

Science

University

of

Edinburgh

The

King

's

Buildings

Edinburgh

U.K.

Electronic

mail

bcp@dcs.ed.ac.uk

for

various

object-oriented

features

have

also

been

given

using

different

techniques

-LSB-

etc.

-RSB-

The

core

calculus

underlying

all

of

these

models

is

an

extension

of

Girard

's

higher-order

polymorphic

calculus

System

F

with

subtyping

Many

of

the

ideas

behind

this

system

called

F

-LRB-

F-omegasub

-RRB-

are

due

to

Cardelli

particularly

to

his

paper

Structural

Subtyping

and

the

Notion

of

Power

Type

-LSB-

-RSB-

the

extension

of

the

subtype

relation

to

type

operators

was

developed

by

Cardelli

and

Mitchell

-LSB-

-RSB-

An

early

semantic

model

was

given

by

Cardelli

and

Longo

-LSB-

-RSB-

using

partial

equivalence

relations

Compagnoni

and

Pierce

-LSB-

-RSB-

gave

a

model

for

an

extension

of

F

with

intersection

types

A

more

powerful

model

including

recursive

types

was

given

by

Bruce

and

Mitchell

-LSB-

-RSB-

The

second

order

fragment

of

F

called

F

has

been

studied

in

detail

yielding

both

positive

-LSB-

-RSB-

and

some

surprising

negative

results

-LSB-

-RSB-

including

undecidability

Decidable

variants

of

F

have

been

proposed

-LSB-

-RSB-

-LRB-

our

formulation

of

F

is

based

on

-LSB-

-RSB-

-RRB-

But

for

the

full

order

calculus

next

to

nothing

is

known

The

analysis

of

F

is

significantly

more

challenging

than

that

of

F

principally

because

F

introduces

a

rule

of

conversion

guaranteeing

that

convertible

types

occupy

the

same

equivalence

class

in

the

subtype

relation

This

rule

interacts

with

the

rule

of

transitivity

requiring

a

substantial

generalization

of

the

standard

cut-elimination

argument

a

key

step

in

the

proof

of

decidability

where

uses

of

transitivity

are

restricted

to

a

well-behaved

form

Another

significant

difficulty

is

showing

the

termination

of

the

final

algorithm

in

decidable

variants

on

F

this

is

fairly

straightforward

here

the

proof

depends

on

the

strong

normalization

of

an

unusual

notion

of

reduction

on

types

in

which

type

variables

may

be

replaced

by

their

upper

bounds

from

the

context

Our

goal

is

to

establish

fundamental

meta-theoretic

results

for

F

leading

up

to

sound

and

complete

algorithms

for

checking

the

subtyping

and

typing

relations

We

begin

in

Section

by

introducing

F

Sections

and

develop

preliminary

results

needed

in

Section

the

core

of

the

paper

where

the

decidability

of

subtyping

is

proved

Section

extends

the

analysis

to

the

decidability

of

typing

In

the

technical

development

we

sometimes

elide

routine

proofs

Those

not

shown

are

straightforward

when

performed

in

the

order

given

Definition

of

F

Girard

's

System

F

-LSB-

-RSB-

is

a

typed

calculus

with

higher-order

polymorphism

Besides

the

term

abstraction

-LRB-

fun

-LRB-

x

T

-RRB-

t

-RRB-

and

application

-LRB-

f

a

-RRB-

of

the

simply

typed

calculus

-LSB-

-RSB-

and

the

type

abstraction

-LRB-

fun

-LRB-

A

K

-RRB-

t

-RRB-

and

application

-LRB-

t

-LSB-

T

-RSB-

-RRB-

of

the

second-order

polymorphic

calculus

-LSB-

-RSB-

it

includes

the

possibility

of

abstraction

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

and

application

-LRB-

T

U

-RRB-

within

type

expressions

To

guarantee

the

well-formedness

of

applications

within

types

an

extra

level

of

kinds

is

introduced

the

kind

classifies

ordinary

types

-LRB-

which

are

inhabited

by

terms

-RRB-

while

kinds

of

the

form

classify

type

operators

functions

mapping

types

of

kind

to

types

of

kind

The

basic

typing

judgement

for

F

is

t

T

read

term

t

has

type

T

in

context

where

records

the

type

of

each

free

term

variable

x

and

the

kind

of

each

free

type

variable

A.

To

extend

F

with

subtyping

we

introduce

an

ordering

S

T

on

the

elements

of

each

kind

K

The

declaration

of

each

type

variable

A

in

is

extended

with

an

upper

bound

written

A

T

which

constrains

A

to

range

only

over

subtypes

of

T

in

the

appropriate

kind

To

allow

new

constraints

of

this

form

to

be

introduced

-LSB-

-RSB-

has

independently

achieved

some

results

closely

related

to

ours

After

early

joint

work

on

the

formulation

of

F

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

vagaries

of

geography

led

our

efforts

onto

separate

but

parallel

tracks

Leaving

aside

inessential

technical

differences

our

reducing

system

in

Section

performs

reduction

in

the

premises

of

the

rules

where

her

analogous

normalizing

system

assumes

that

the

conclusion

is

already

normalized

she

proves

Church-Rosser

by

marking

redices

while

we

adapt

Tait

and

Martin-L

of

's

method

of

parallel

reduction

etc.

the

two

proofs

are

broadly

similar

in

structure

The

major

differences

are

as

follows

-RRB-

Compagnoni

's

results

are

for

a

more

powerful

system

F

that

includes

intersection

types

in

addition

to

the

machinery

of

F

-RRB-

Our

development

addresses

the

decidability

of

typechecking

in

addition

to

subtyping

-RRB-

Compagnoni

's

version

of

the

crucial

substitution

lemma

-LRB-

our

Lemma

-RRB-

is

phrased

more

cleverly

and

its

proof

requires

a

less

intricate

analysis

-RRB-

Our

proof

of

the

termination

of

the

subtyping

algorithm

is

based

on

showing

strong

normalization

for

an

extended

notion

of

reduction

in

which

type

variables

may

be

replaced

by

their

upper

bounds

from

the

context

the

argument

is

rather

difficult

but

introduces

techniques

that

may

be

useful

in

a

broader

ranger

of

calculi

including

for

example

type

abbreviations

Compagnoni

uses

a

more

direct

term

rewriting

technique

into

the

context

we

extend

the

universal

quantifier

All

-LRB-

A

K

-RRB-

U

to

a

bounded

quantifier

All

-LRB-

A

T

-RRB-

U

To

ensure

that

the

new

system

can

still

type

all

the

terms

of

F

we

assume

that

the

subtype

relation

in

every

kind

K

has

a

maximal

element

Top

-LRB-

K

-RRB-

The

assumption

A

Top

-LRB-

K

-RRB-

replaces

A

K.

For

kinds

of

the

form

the

subtype

relation

is

just

the

pointwise

extension

of

subtyping

for

a

function

S

is

smaller

than

a

function

T

if

S

U

T

U

for

every

U

At

the

base

kind

the

subtype

relation

also

includes

rules

for

the

type

constructors

and

All

-LRB-

A

-RRB-

The

rule

for

arrow

types

embodies

the

familiar

contravariant/covariant

inclusion

of

function

spaces

Intuitively

a

function

f

whose

results

inhabit

whenever

its

arguments

inhabit

may

safely

be

substituted

for

a

function

in

provided

that

any

element

of

that

might

be

given

as

an

argument

to

f

can

safely

be

used

as

an

element

of

and

that

f

's

result

an

element

of

can

be

used

in

place

of

the

expected

The

subtyping

rule

for

bounded

quantifiers

is

equally

simple

A

U

All

-LRB-

A

U

-RRB-

All

-LRB-

A

U

-RRB-

That

is

a

polymorphic

function

f

All

-LRB-

A

U

-RRB-

can

be

used

in

a

context

that

expects

an

element

of

All

-LRB-

A

U

-RRB-

provided

that

for

each

legal

argument

type

T

the

value

of

f

at

T

can

safely

be

used

as

an

element

of

It

would

be

semantically

sensible

to

refine

the

right-hand

premise

of

this

rule

so

that

it

only

requires

when

A

is

constrained

to

the

common

part

of

their

domain

A

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

This

indeed

is

the

form

in

which

the

rule

appears

in

most

presentations

of

second-order

bounded

quantification

-LRB-

c.f.

-LSB-

-RSB-

for

a

survey

-RRB-

However

the

extra

flexibility

offered

by

this

refinement

does

not

seem

to

be

useful

in

practice

and

it

is

very

costly

this

rule

is

responsible

for

the

failure

of

a

number

of

important

prooftheoretic

properties

in

standard

formulations

of

F

-LSB-

-RSB-

including

decidability

of

subtyping

Another

variant

of

the

quantifier

subtyping

rule

allows

the

bounds

to

differ

but

requires

that

the

bodies

be

in

the

subtype

relation

under

the

trivial

assumption

on

the

bound

variable

A

Top

-LRB-

K

-RRB-

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

-LRB-

where

K

is

the

kind

of

and

-RRB-

Indeed

an

earlier

draft

of

this

paper

used

this

rule

instead

of

the

equalbounds

variant

All

of

the

results

about

subtyping

hold

for

both

systems

-LRB-

with

nearly

identical

proofs

-RRB-

But

the

algorithm

for

synthesizing

minimal

types

works

only

for

the

equal-bounds

rule

and

not

-LRB-

as

we

erroneously

claimed

-RRB-

for

the

top-rule

Indeed

the

top-rule

actually

destroys

the

minimal

typing

property

For

example

in

the

context

Y

Top

-LRB-

-RRB-

the

term

e

fun

-LRB-

X

Y

-RRB-

fun

-LRB-

x

X

-RRB-

x

has

both

of

the

types

All

-LRB-

X

Y

-RRB-

X

X

All

-LRB-

X

Y

-RRB-

X

Y

but

these

types

are

incomparable

-LRB-

using

the

top-rule

-RRB-

and

have

no

common

lower

bound

We

are

endebted

to

Giorgio

Ghelli

for

this

example

could

also

extend

type

operators

Fun

-LRB-

A

K

-RRB-

U

to

bounded

operators

Fun

-LRB-

A

T

-RRB-

U

but

it

is

not

clear

that

this

refinement

would

be

useful

in

practice

and

it

would

complicate

the

metatheory

since

we

would

then

need

to

introduce

a

subkinding

relation

richer

definitions

of

operator

subtyping

are

possible

for

example

we

might

allow

monotone

subtyping

antimonotone

subtyping

etc.

-LSB-

-RSB-

This

extension

does

seem

useful

in

practice

-LRB-

e.g.

-LSB-

-RSB-

-RRB-

but

its

algorithmic

implications

are

unclear

Syntax

The

kinds

types

terms

and

contexts

of

F

are

defined

by

the

following

abstract

grammar

K

kind

of

types

j

K!K

kind

of

type

operators

T

A

type

variable

j

Fun

-LRB-

A

K

-RRB-

T

type

operator

j

T

T

application

of

a

type

operator

j

Top

-LRB-

K

-RRB-

maximal

type

j

T!T

function

type

j

All

-LRB-

A

T

-RRB-

T

universally

quantified

type

t

x

variable

j

fun

-LRB-

x

T

-RRB-

t

abstraction

j

t

t

application

j

fun

-LRB-

A

T

-RRB-

t

type

abstraction

j

t

T

type

application

ffl

empty

context

j

x

T

variable

binding

j

A

T

type

variable

binding

with

bound

The

inference

rules

that

follow

define

sets

of

derivable

statements

of

the

following

forms

ok

is

a

well-formed

context

T

K

type

T

has

kind

K

in

context

S

T

S

is

a

subtype

of

T

in

t

T

term

t

has

type

T

in

Terms

types

contexts

and

statements

that

differ

only

in

the

names

of

bound

variables

are

regarded

as

identical

Contexts

and

Kinding

Well-formed

contexts

are

constructed

from

the

empty

context

by

adding

well-kinded

type

and

term

variable

declarations

ffl

ok

-LRB-

C-Empty

-RRB-

T

K

A

dom

-LRB-

-RRB-

A

T

ok

-LRB-

C-TVar

-RRB-

T

x

dom

-LRB-

-RRB-

x

T

ok

-LRB-

C-Var

-RRB-

Since

the

side

conditions

guarantee

that

the

bindings

in

well-formed

contexts

are

always

for

distinct

variables

we

often

consider

them

as

finite

functions

from

variables

to

types

for

example

the

upper

bound

of

A

in

is

written

-LRB-

A

-RRB-

We

write

dom

-LRB-

-RRB-

for

the

set

of

term

and

type

variables

bound

by

If

is

a

prefix

of

we

say

that

is

an

extension

of

The

definition

of

the

kinding

relation

is

standard

Type

variables

have

the

same

kind

as

their

upper

bounds

abstraction

and

application

provide

introduction

and

elimination

forms

for

arrow-kinds

Top

-LRB-

K

-RRB-

has

kind

K

arrow

and

All-types

are

well-kinded

if

their

components

are

We

maintain

the

invariant

that

kinding

statements

are

only

derivable

in

well-formed

contexts

-LRB-

A

-RRB-

K

A

K

-LRB-

K-TVar

-RRB-

A

Top

-LRB-

-RRB-

T

Fun

-LRB-

A

-RRB-

T

-LRB-

K-Arrow-I

-RRB-

S

T

S

T

-LRB-

K-Arrow-E

-RRB-

ok

Top

-LRB-

K

-RRB-

K

-LRB-

K-Top

-RRB-

-LRB-

K-Arrow

-RRB-

A

All

-LRB-

A

-RRB-

-LRB-

K-All

-RRB-

-LRB-

The

kinding

and

context

well-formedness

judgements

are

mutually

recursive

but

the

two

main

judgement

forms

subtyping

and

typing

only

depend

non-recursively

on

other

judgements

-RRB-

Conversion

The

presence

of

abstractions

and

applications

in

type

expressions

leads

us

to

consider

conversion

within

types

For

technical

convenience

we

use

a

slight

extension

of

the

standard

conversion

relation

in

addition

to

reductions

of

the

usual

form

-LRB-

Fun

-LRB-

A

-RRB-

T

-RRB-

U

-LSB-

U

A

-RSB-

T

we

allow

reductions

of

the

form

Top

-LRB-

-RRB-

T

Top

-LRB-

-RRB-

which

relate

the

maximal

elements

of

different

kinds

We

could

achieve

the

same

effect

by

extending

the

rule

S-Top

below

but

this

way

seems

cleaner

Definition

-LSB-

reduction

-RSB-

One-step

reduction

is

the

smallest

relation

on

types

closed

under

the

following

rules

-LRB-

-RRB-

Top

-LRB-

-RRB-

S

Top

-LRB-

-RRB-

-LRB-

-RRB-

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

T

-LSB-

T

A

-RSB-

S

S

S

T

T

T

T

S

T

S

T

S

-LRB-

S!T

-RRB-

-LRB-

-RRB-

T

T

-LRB-

S!T

-RRB-

-LRB-

S!T

-RRB-

S

All

-LRB-

A

S

-RRB-

T

All

-LRB-

A

-RRB-

T

T

T

All

-LRB-

A

S

-RRB-

T

All

-LRB-

A

S

-RRB-

T

S

Fun

-LRB-

A

K

-RRB-

S

Fun

-LRB-

A

K

-RRB-

The

many-step

reduction

relation

is

the

reflexive

and

transitive

closure

of

one-step

reduction

is

its

reflexive

transitive

and

symmetric

closure

When

T

has

a

normal

form

-LRB-

it

will

necessarily

be

unique

-RRB-

we

denote

it

by

T

Reduction

to

normal

form

is

written

Subtyping

The

F

subtyping

relation

S

T

is

a

straightforward

extension

of

the

subtyping

relation

of

F

-LSB-

-RSB-

We

start

by

stipulating

that

convertible

types

always

lie

in

the

same

equivalence

class

in

the

subtype

ordering

-LRB-

S-Conv

-RRB-

and

that

the

subtype

relation

at

every

kind

is

reflexive

-LRB-

R-Refl

-RRB-

and

transitive

-LRB-

R-Trans

-RRB-

Type

assumptions

from

the

context

may

be

used

as

axioms

-LRB-

S-TVar

-RRB-

Top

-LRB-

K

-RRB-

is

maximal

in

the

ordering

for

kind

K

-LRB-

S-Top

-RRB-

Type

operators

-LRB-

S-Abs

-RRB-

and

applications

-LRB-

S-App

-RRB-

are

subtyped

pointwise

Arrow

and

All-types

have

the

rules

discussed

above

-LRB-

S-Arrow

and

S-All

-RRB-

In

several

places

in

the

definition

we

add

premises

to

ensure

that

a

proper

kinding

discipline

is

respected

-LRB-

e.g.

S

Top

-LRB-

K

-RRB-

only

when

S

K

etc.

-RRB-

But

for

readability

these

are

kept

to

a

minimum

we

maintain

the

invariant

that

whenever

the

conclusion

of

a

subtyping

judgement

is

well-kinded

the

types

on

the

right

and

left-hand

sides

of

the

will

have

the

same

kind

and

all

of

the

subderivations

will

be

similarly

well

behaved

S

U

U

K

U

T

S

T

-LRB-

S-Conv

-RRB-

T

T

-LRB-

S-Refl

-RRB-

S

U

U

T

U

K

S

T

-LRB-

S-Trans

-RRB-

A

-LRB-

A

-RRB-

-LRB-

S-TVar

-RRB-

S

K

S

Top

-LRB-

K

-RRB-

-LRB-

S-Top

-RRB-

A

Top

-LRB-

K

-RRB-

S

T

Fun

-LRB-

A

K

-RRB-

S

Fun

-LRB-

A

K

-RRB-

T

-LRB-

S-Abs

-RRB-

S

T

S

U

T

U

-LRB-

S-App

-RRB-

-LRB-

S-Arrow

-RRB-

A

U

All

-LRB-

A

U

-RRB-

All

-LRB-

A

U

-RRB-

-LRB-

S-All

-RRB-

Typing

The

typing

relation

t

T

is

standard

-LSB-

-RSB-

modulo

a

few

extra

kinding

assumptions

The

rule

T-Subsumption

captures

the

intended

interpretation

of

subtyping

as

safe

substitutability

The

others

are

straightforward

extensions

of

the

arrow

and

All-introduction

and

elimination

rules

of

pure

F

s

S

T

S

T

s

T

-LRB-

T-Subsumption

-RRB-

ok

x

-LRB-

x

-RRB-

-LRB-

T-Var

-RRB-

x

t

fun

-LRB-

x

-RRB-

t

-LRB-

T-Arrow-I

-RRB-

f

a

f

a

-LRB-

T-Arrow-E

-RRB-

A

t

fun

-LRB-

A

-RRB-

t

All

-LRB-

A

-RRB-

-LRB-

T-All-I

-RRB-

f

All

-LRB-

A

-RRB-

S

K

S

f

S

-LSB-

S

A

-RSB-

-LRB-

T-All-E

-RRB-

Properties

of

Reduction

We

now

pause

to

establish

some

technical

properties

of

the

reduction

relation

and

to

define

an

auxiliary

notion

of

parallel

reduction

that

will

simplify

some

of

the

inductive

arguments

in

later

sections

The

main

result

of

this

section

is

the

Church-Rosser

property

by

a

straightforward

adaptation

of

Tait

and

Martin-L

of

's

proof

for

ordinary

reduction

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

Definition

-LSB-

Parallel

reduction

-RSB-

Single-step

parallel

reduction

is

the

least

relation

closed

under

the

following

rules

Top

-LRB-

-RRB-

T

Top

-LRB-

-RRB-

S

T

T

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

T

-LSB-

T

A

-RSB-

S

T

T

S

T

T

T

T

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

S

T

T

S!T

S

T

T

All

-LRB-

A

S

-RRB-

T

All

-LRB-

A

-RRB-

T

T

T

Ordinary

single-step

reduction

is

a

subrelation

of

single-step

parallel

reduction

which

is

a

subrelation

of

multi-step

ordinary

reduction

The

reflexive

transitive

closures

of

the

two

relations

coincide

Fact

ae

oe

Substitution

commutes

with

parallel

and

multi-step

reduction

Lemma

If

S

and

T

T

then

-LSB-

T

A

-RSB-

S

-LSB-

T

A

-RSB-

If

S

and

T

T

then

-LSB-

T

A

-RSB-

S

-LSB-

T

A

-RSB-

If

S

and

T

T

then

-LSB-

T

A

-RSB-

S

-LSB-

T

A

-RSB-

In

the

proof

of

we

need

the

following

property

of

substitution

Fact

If

A

and

FV

-LRB-

S

-RRB-

then

-LSB-

S

A

-RSB-

-LRB-

-LSB-

T

-RSB-

U

-RRB-

-LSB-

-LSB-

S

A

-RSB-

T

-RSB-

-LRB-

-LSB-

S

A

-RSB-

U

-RRB-

One

useful

consequence

of

-LRB-

-RRB-

is

that

if

an

expression

with

an

outermost

redex

has

a

reduction

path

in

which

this

redex

is

reduced

at

some

point

then

this

reduction

can

be

performed

first

without

changing

the

result

Corollary

-LSB-

Outermost

reduction

-RSB-

If

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

T

U

where

U

-LRB-

Fun

-LRB-

A

K

-RRB-

-RRB-

T

with

S

and

T

T

then

-LSB-

T

A

-RSB-

S

U

Lemma

-LSB-

Diamond

property

for

-RSB-

For

all

types

S

and

with

S

and

S

there

is

a

type

such

that

and

S

RR

RR

Proof

Straightforward

extension

of

the

standard

argument

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

Corollary

-LSB-

Church-Rosser

for

-RSB-

For

all

types

S

and

with

S

and

S

there

is

a

type

such

that

and

The

proof

of

strong

normalization

for

has

to

be

deferred

until

after

we

have

studied

the

properties

of

the

kinding

system

since

our

proof

of

normalization

requires

that

the

types

involved

by

well

kinded

One

more

property

of

will

be

needed

for

the

induction

in

the

proof

of

Lemma

Lemma

-LSB-

and

-RSB-

If

S

and

S

then

there

is

an

with

and

S

R

R

Proof

By

induction

on

the

length

of

S

using

-LRB-

-RRB-

and

Church-Rosser

Kinding

Next

we

state

some

basic

technical

properties

of

the

context

well-formedness

and

kinding

judgements

Lemma

-LSB-

Generation

of

contexts

-RSB-

If

ok

then

-LRB-

a

-RRB-

ffl

or

-LRB-

b

-RRB-

x

T

with

ok

and

T

as

subderivations

or

-LRB-

c

-RRB-

A

T

with

ok

and

T

K

for

some

K

as

subderivations

If

S

K

then

ok

as

a

subderivation

Lemma

-LSB-

Generation

of

types

-RSB-

If

A

K

then

-LRB-

A

-RRB-

K.

If

Fun

-LRB-

A

-RRB-

T

K

then

for

some

we

have

A

Top

-LRB-

-RRB-

T

and

K

If

S

T

K

then

for

some

we

have

S

and

T

If

S!T

K

then

K

and

S

T

If

Top

-LRB-

K

-RRB-

then

K

If

All

-LRB-

A

S

-RRB-

T

K

then

K

and

A

S

T

Moreover

the

implied

derivations

are

all

subderivations

of

the

originals

We

prove

the

decidability

of

the

kinding

system

by

showing

that

it

is

equivalent

to

a

different

system

whose

decidability

is

obvious

Definition

-LSB-

Algorithmic

kinding

-RSB-

The

algorithmic

kinding

relation

A

T

K

is

the

least

relation

closed

under

the

kinding

rules

where

instead

of

rule

K-TVAR

we

use

the

following

A

T

K

A

A

T

ok

A

T

A

A

K

-LRB-

-RRB-

The

algorithmic

context

well-formedness

relation

A

ok

is

defined

as

before

using

algorithmic

kinding

Lemma

-LSB-

Context

strengthening

for

algorithmic

kinding

-RSB-

If

A

S

A

T

K

and

A

is

not

free

in

or

in

T

then

A

T

K.

If

A

A

S

ok

and

A

is

not

free

in

then

A

ok

If

x

S

A

T

K

then

A

T

K.

If

A

x

S

ok

then

A

ok

Lemma

-LSB-

Decidability

of

kinding

-RSB-

The

relations

ok

and

S

K

are

decidable

Proof

It

is

easy

to

prove

by

induction

that

the

two

kinding

systems

and

the

two

definitions

of

context

well-formedness

are

equivalent

In

each

direction

we

only

have

to

consider

the

rule

for

variables

since

all

other

rules

coincide

Case

K-TVAR

-LRB-

A

-RRB-

K

By

Lemma

-LRB-

-RRB-

A

T

ok

as

subderivation

So

by

the

induction

hypothesis

A

A

T

ok

and

A

T

A

T

K.

Repeated

application

of

Lemma

yields

A

T

K.

Case

A

T

K

and

A

A

T

ok

By

the

induction

hypothesis

T

K

and

A

T

ok

so

the

result

follows

by

K-TVAR

and

weakening

Now

the

algorithm

obtained

by

reading

the

algorithmic

kinding

rules

from

bottom

to

top

as

Horn

clauses

always

terminates

since

in

each

step

the

total

number

of

characters

in

the

conclusion

is

greater

than

the

number

of

characters

in

any

of

the

premises

Since

the

systems

are

equivalent

S

K

is

also

decidable

Lemma

-LSB-

Uniqueness

of

kinding

-RSB-

If

S

K

and

S

then

K

This

justifies

the

following

notation

Definition

The

unique

kind

of

a

well-kinded

type

S

in

a

context

is

written

Kind

-LRB-

S

-RRB-

Lemma

-LSB-

Transposition

and

weakening

for

kinding

-RSB-

Suppose

that

is

a

well-formed

extension

of

T

A

S

If

A

S

T

U

K

and

A

FV

-LRB-

T

-RRB-

then

U

K.

Lemma

-LSB-

Context

update

for

kinding

-RSB-

If

A

S

T

K

and

S

then

A

T

K.

Lemma

-LSB-

Top

reduction

-RSB-

If

Top

-LRB-

K

-RRB-

Tn

then

Top

-LRB-

K

-RRB-

Tn

Top

-LRB-

-RRB-

Lemma

-LSB-

Kinding

and

substitution

-RSB-

Suppose

T

If

A

Top

-LRB-

-RRB-

ok

then

-LSB-

T

A

-RSB-

ok

If

A

Top

-LRB-

-RRB-

S

K

then

-LSB-

T

A

-RSB-

-LSB-

T

A

-RSB-

S

K.

Proof

Both

parts

are

proved

simultaneously

by

induction

on

derivations

Lemma

-LSB-

Subject

reduction

for

types

and

contexts

-RSB-

If

S

K

and

S

T

then

T

K.

If

S

K

and

then

S

K.

Corollary

-LSB-

Kind

invariance

under

conversion

-RSB-

If

S

T

where

S

KS

and

T

KT

then

KS

KT

Strong

Normalization

of

Types

We

shall

often

need

the

fact

that

reduction

is

strongly

normalizing

for

well-kinded

types

In

fact

we

prove

the

strong

normalization

of

a

more

general

reduction

relation

called

reduction

which

will

be

used

to

prove

the

termination

of

the

subtyping

and

typing

algorithms

Besides

the

usual

and

reductions

we

allow

variables

to

be

replaced

by

their

upper

bounds

from

the

context

reduction

is

reminiscent

of

the

common

operation

in

type

checker

and

proof

checker

implementations

of

replacing

a

type

definition

by

its

expansion

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

We

begin

by

proving

the

strong

normalization

of

reduction

using

by

a

straightforward

translation

argument

Definition

Define

a

family

of

types

TK

one

for

each

kind

K

as

follows

T

All

-LRB-

A

-RRB-

A

Fun

-LRB-

A

-RRB-

Note

that

ffl

TK

K

for

each

K.

Lemma

-LSB-

Strong

normalization

-RSB-

Suppose

S

K

Then

there

is

no

infinite

reduction

from

S.

Proof

Define

a

translation

function

F

mapping

F

types

to

F

types

F

-LRB-

Top

-LRB-

K

-RRB-

-RRB-

TK

F

-LRB-

All

-LRB-

A

S

-RRB-

T

-RRB-

All

-LRB-

A

-RRB-

F

-LRB-

S

-RRB-

F

-LRB-

T

-RRB-

F

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

Fun

-LRB-

A

K

-RRB-

F

-LRB-

S

-RRB-

F

-LRB-

S

T

-RRB-

F

-LRB-

S

-RRB-

F

-LRB-

T

-RRB-

F

-LRB-

S

T

-RRB-

F

-LRB-

S

-RRB-

F

-LRB-

T

-RRB-

On

contexts

F

replaces

each

type

variable

binding

A

T

in

by

the

kinding

assumption

A

K

where

K

is

the

kind

of

T

in

It

is

easy

to

check

that

if

S

K

in

F

then

F

-LRB-

-RRB-

F

-LRB-

S

-RRB-

K

in

F

Now

any

reduction

in

F

from

S

can

be

mirrored

by

a

reduction

from

F

-LRB-

S

-RRB-

of

the

same

length

in

F

The

existence

of

an

infinite

reduction

in

F

would

thus

contradict

the

strong

normalization

of

F

-LSB-

-RSB-

Next

we

define

the

notion

of

reduction

and

establish

some

of

its

basic

properties

Definition

Single-step

reduction

is

the

least

family

of

relations

closed

under

-LRB-

A

-RRB-

Top

-LRB-

K

-RRB-

-LRB-

-RRB-

A

-LRB-

A

-RRB-

S

-LRB-

A

Top

-LRB-

K

-RRB-

-RRB-

Fun

-LRB-

A

K

-RRB-

S

Fun

-LRB-

A

K

-RRB-

S

S

T

T

T

T

S

T

S

T

S

-LRB-

S!T

-RRB-

-LRB-

-RRB-

T

T

-LRB-

S!T

-RRB-

-LRB-

S!T

-RRB-

S

All

-LRB-

A

S

-RRB-

T

All

-LRB-

A

-RRB-

T

T

-LRB-

A

S

-RRB-

T

All

-LRB-

A

S

-RRB-

T

All

-LRB-

A

S

-RRB-

T

Single-step

reduction

written

is

the

least

family

of

relations

closed

under

these

rules

and

the

rules

-LRB-

-RRB-

and

-LRB-

-RRB-

of

Definition

The

corresponding

multi-step

reductions

are

defined

as

usual

Note

that

in

a

multi-step

reduction

sequence

the

at

each

stage

remains

the

same

is

only

extended

internally

in

the

course

of

a

single

reduction

to

keep

track

of

variable

bindings

in

those

rules

that

define

reduction

under

binders

Note

that

we

are

careful

to

separate

All-bound

variables

whose

bounds

may

be

different

from

Top

and

which

may

thus

act

as

redexes

from

Fun-bound

variables

whose

bounds

are

always

Top

and

which

can

never

be

reduced

Lemma

-LSB-

Strong

normalization

-RSB-

If

S

K

then

there

is

no

infinite

reduction

from

S.

Proof

We

show

by

induction

on

the

defintion

of

that

if

S

T

in

one

step

then

T

K

by

a

shorter

derivation

Most

cases

are

straightforward

we

list

only

the

the

ones

for

type

variables

and

for

All-types

Case

S

A

and

T

-LRB-

A

-RRB-

By

the

generation

lemma

for

types

-LRB-

A

-RRB-

K

by

a

shorter

derivation

Case

S

All

-LRB-

A

-RRB-

and

T

All

-LRB-

A

-RRB-

By

the

generation

lemma

for

types

and

contexts

and

Lemma

so

by

the

induction

hypothesis

by

a

shorter

derivation

Finally

All

-LRB-

A

-RRB-

by

K-All

by

a

shorter

derivation

than

the

original

Case

S

All

-LRB-

A

-RRB-

and

T

All

-LRB-

A

-RRB-

By

the

generation

lemma

A

The

induction

hypothesis

gives

A

by

a

shorter

derivation

so

by

K-All

we

have

All

-LRB-

A

-RRB-

by

a

shorter

derivation

than

the

original

We

shall

often

use

the

subject-reduction

property

silently

in

what

follows

to

guarantee

that

a

reduction

sequence

from

a

well-kinded

term

only

contains

well-kinded

terms

Lemma

-LSB-

Subject

reduction

-RSB-

If

S

K

and

S

then

K.

Proof

By

induction

on

the

length

of

the

reduction

S

with

an

inner

induction

on

the

definition

of

single-step

reduction

Fact

If

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

then

and

A

If

Fun

-LRB-

A

K

-RRB-

Fun

-LRB-

A

K

-RRB-

then

A

Top

-LRB-

K

-RRB-

If

then

and

If

-LRB-

-RRB-

-LRB-

-RRB-

then

and

Proof

We

give

the

proof

in

detail

for

part

-LRB-

-RRB-

the

rest

are

similar

but

simpler

For

part

-LRB-

-RRB-

we

prove

the

more

refined

statement

if

and

-LRB-

A

-RRB-

and

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

then

and

-LRB-

A

-RRB-

by

induction

on

the

length

of

Case

is

empty

Immediate

Case

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

U

All

-LRB-

A

-RRB-

-LRB-

I.e.

consists

of

a

single-step

reduction

followed

by

a

multi-step

reduction

where

replaces

a

single

variable

in

by

its

upper

bound

to

yield

U

-RRB-

To

apply

the

induction

hypothesis

we

need

to

check

that

-LRB-

A

-RRB-

U

which

immediately

gives

-LRB-

A

-RRB-

U

But

if

replaces

an

occurrence

of

A

by

in

i.e.

-LSB-

A

-RSB-

-LRB-

A

-RRB-

-LSB-

-RSB-

then

we

can

build

a

reduction

-LSB-

A

-RSB-

-LRB-

A

-RRB-

-LSB-

-RSB-

-LRB-

A

-RRB-

-LSB-

-RSB-

by

replacing

this

occurrence

of

A

with

and

then

using

the

assumption

that

-LRB-

and

hence

-LRB-

A

-RRB-

-RRB-

to

develop

to

in-place

On

the

other

hand

if

replaces

some

other

variable

then

-LRB-

A

-RRB-

U

is

immediate

In

both

cases

the

induction

hypothesis

then

applies

directly

yielding

the

desired

result

Case

All

-LRB-

A

-RRB-

All

-LRB-

A

U

-RRB-

All

-LRB-

A

-RRB-

Here

the

induction

hypothesis

applies

directly

-LRB-

since

U

and

we

have

-LRB-

A

-RRB-

by

assumption

-RRB-

to

yield

the

desired

result

Lemma

-LSB-

Weak

diamond

property

for

reduction

-RSB-

T

U

V

W

Proof

By

induction

on

the

form

of

T

Case

T

A

Then

V

U

-LRB-

A

-RRB-

and

we

may

take

W

-LRB-

A

-RRB-

Case

T

All

-LRB-

A

-RRB-

We

must

find

W

All

-LRB-

A

-RRB-

such

that

the

required

diagram

commutes

this

will

follow

from

the

commutativity

of

a

smaller

diagram

for

and

and

another

diagram

for

and

There

are

three

subcases

to

consider

depending

on

whether

both

of

the

reductions

from

T

to

U

and

V

are

in

both

are

in

or

one

is

in

and

one

in

-LRB-

Since

the

last

case

is

symmetric

we

may

assume

without

loss

of

generality

that

is

reduced

to

produce

V

and

to

produce

U

-RRB-

Subcase

U

All

-LRB-

A

-RRB-

and

V

All

-LRB-

A

-RRB-

Begin

by

applying

the

induction

hypothesis

to

and

to

yield

a

common

reduct

We

must

then

show

A

A

A

A

Set

and

we

are

done

Subcase

U

All

-LRB-

A

-RRB-

and

V

All

-LRB-

A

-RRB-

We

must

find

a

such

that

A

A

A

A

The

existence

of

such

a

is

given

by

the

induction

hypothesis

Subcase

U

All

-LRB-

A

-RRB-

and

V

All

-LRB-

A

-RRB-

Set

Then

we

must

show

A

A

A

A

If

-LRB-

A

-RRB-

by

a

reduction

on

some

occurrence

of

A

in

then

we

have

-LSB-

A

-RSB-

and

-LSB-

-RSB-

set

-LSB-

-RSB-

If

-LRB-

A

-RRB-

by

a

reduction

on

some

occurrence

of

a

variable

other

than

A

in

then

we

can

set

since

-LRB-

A

-RRB-

follows

directly

from

-LRB-

A

-RRB-

in

this

case

Other

cases

Straightforward

Lemma

-LSB-

Church-Rosser

for

reduction

-RSB-

T

U

V

W

Proof

By

Newman

's

Lemma

which

states

that

the

weak

diamond

property

and

strong

normalization

together

imply

Church-Rosser

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

Lemma

-LSB-

Substitution

commutes

with

reduction

-RSB-

If

U

V

and

S

-LRB-

A

Top

-LRB-

K

-RRB-

-RRB-

T

then

-LSB-

U

A

-RSB-

S

-LSB-

V

A

-RSB-

T

Proof

Since

A

's

bound

is

Top

-LRB-

K

-RRB-

it

is

not

a

-LRB-

A

Top

-LRB-

K

-RRB-

-RRB-

redex

so

-LSB-

U

A

-RSB-

S

-LSB-

U

A

-RSB-

T

Then

-LSB-

U

A

-RSB-

T

-LSB-

V

A

-RSB-

T

by

applying

the

reduction

from

U

to

V

at

each

point

in

-LSB-

U

A

-RSB-

T

where

A

appeared

in

T

At

this

point

we

can

start

proving

properties

relating

reduction

and

reduction

First

a

technical

property

that

handles

a

key

step

of

the

following

lemma

Lemma

If

and

-LRB-

A

-RRB-

then

there

is

some

such

that

A

R

A

Proof

By

induction

on

the

length

of

the

reduction

from

to

Case

Then

set

and

we

are

done

Case

-LRB-

A

-RRB-

V

-LRB-

A

-RRB-

Apply

the

induction

hypothesis

to

find

a

W

satisfying

the

desired

property

We

must

now

show

A

R

V

A

W

A

A

If

V

-LRB-

A

-RRB-

by

contracting

a

redex

other

than

A

then

V

V

-LSB-

B

-RSB-

and

V

-LSB-

-LRB-

B

-RRB-

-RSB-

In

reducing

from

V

to

W

this

redex

may

be

copied

a

number

of

times

W

W

-LSB-

B

-RSB-

-LSB-

B

-RSB-

-LSB-

B

-RSB-

Let

be

the

result

of

contracting

the

residuals

of

this

redex

in

W

i.e.

W

-LSB-

-LRB-

B

-RRB-

-RSB-

-LSB-

-LRB-

B

-RRB-

-RSB-

-LSB-

-LRB-

B

-RRB-

-RSB-

Similarly

if

V

V

-LSB-

A

-RSB-

and

V

-LSB-

-RSB-

again

let

be

the

result

of

contracting

the

residuals

of

this

redex

in

W

Reduce

V

-LSB-

-RSB-

to

W

-LSB-

-RSB-

-LSB-

-RSB-

-LSB-

-RSB-

by

V

-LSB-

-RSB-

W

-LSB-

-RSB-

-LSB-

-RSB-

-LSB-

-RSB-

W

-LSB-

-RSB-

-LSB-

-RSB-

-LSB-

-RSB-

The

next

lemma

establishes

a

confluence

property

for

and

reductions

The

proof

is

similar

to

that

of

Lemma

This

lemma

and

Lemma

jointly

handle

the

crucial

step

in

the

strong

normalization

argument

that

follows

Lemma

-LSB-

reduction

and

reduction

-RSB-

T

U

V

W

Moreover

has

length

at

least

Proof

By

induction

on

Case

T

Top

-LRB-

-RRB-

Top

-LRB-

-RRB-

U

Any

reduction

from

T

must

reduce

only

redexes

in

so

V

has

the

form

Top

-LRB-

-RRB-

But

then

V

U

and

we

can

take

W

U

Note

that

V

W

by

a

nonempty

reduction

Case

T

-LRB-

Fun

-LRB-

A

K

-RRB-

-RRB-

-LSB-

A

-RSB-

U

Any

reduction

from

T

consists

of

a

number

of

separate

reductions

in

and

by

Fact

-LRB-

-RRB-

so

V

has

the

form

-LRB-

Fun

-LRB-

A

K

-RRB-

-RRB-

with

-LRB-

A

Top

-LRB-

K

-RRB-

-RRB-

and

By

Lemma

-LSB-

A

-RSB-

-LSB-

A

-RSB-

so

we

can

take

W

-LSB-

A

-RSB-

Note

that

V

W

by

a

nonempty

reduction

Case

T

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

U

By

Fact

-LRB-

-RRB-

V

has

the

form

All

-LRB-

A

-RRB-

with

and

-LRB-

A

-RRB-

Apply

the

induction

hypothesis

to

find

a

with

T

where

has

length

at

least

By

Lemma

there

is

some

such

that

A

A

So

W

All

-LRB-

A

-RRB-

has

the

required

property

Case

T

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

U

By

Lemma

-LRB-

-RRB-

V

has

the

form

All

-LRB-

A

-RRB-

with

and

-LRB-

A

-RRB-

We

must

show

A

A

which

follows

directly

from

the

induction

hypothesis

Other

cases

Similarly

using

parts

-LRB-

-RRB-

to

-LRB-

-RRB-

of

With

this

in

hand

we

can

proceed

to

the

main

body

of

the

strong

normalization

argument

Its

two

main

steps

are

captured

by

this

lemma

and

the

next

one

Lemma

-LSB-

postponement

-RSB-

If

T

U

X

then

there

is

some

such

that

T

For

the

proof

we

need

a

simple

fact

Fact

If

S

T

U

then

S

U

for

some

U

-LRB-

That

is

the

redex

that

is

contracted

between

T

and

U

is

a

residual

of

a

redex

already

present

in

S.

Proof

Since

reduction

can

not

create

a

redex

the

redex

appearing

in

T

must

be

a

residual

of

a

redex

already

appearing

in

S.

Proof

of

Lemma

By

Fact

there

is

some

such

that

T

U

By

Lemma

there

is

some

such

that

T

U

Since

U

X

we

can

now

apply

Lemma

T

U

X

We

can

continue

in

this

way

applying

either

or

to

successive

elements

of

the

infinite

reduction

beginning

from

X

to

obtain

an

infinite

sequence

of

multi-step

reductions

on

the

right

T

U

X

But

the

sequence

of

reductions

on

the

left

must

contain

infinitely

many

steps

-LRB-

otherwise

it

would

have

an

infinite

tail

-RRB-

so

Lemma

also

tells

us

that

infinitely

many

of

the

individual

multi-step

reductions

on

the

right

are

nonempty

The

reduction

T

is

the

desired

one

Proposition

-LSB-

Strong

normalization

-RSB-

If

S

is

well-kinded

in

then

there

is

no

infinite

reduction

from

S.

Proof

Assume

for

a

contradiction

that

R

is

an

infinite

reduction

beginning

from

S.

Let

R

Now

repeat

the

following

process

as

long

as

possible

to

construct

a

sequence

of

infinite

reductions

all

starting

from

S

If

Ri

contains

no

reduction

that

is

immediately

preceded

by

a

reduction

then

stop

Otherwise

form

Ri

from

Ri

by

using

Lemma

repeatedly

to

move

the

first

such

reduction

before

any

reduction

Note

that

all

of

the

Ri

are

infinite

and

that

the

first

i

steps

in

each

Ri

are

all

reductions

Now

there

are

two

possibilities

ffl

The

sequence

of

eventually

terminates

having

reached

some

Rn

in

which

all

reductions

precede

the

first

reduction

But

this

means

that

Rn

contains

only

reductions

contradicting

Lemma

or

has

an

infinite

tail

consisting

only

of

reductions

contradicting

Lemma

ffl

The

sequence

of

is

infinite

But

since

each

Ri

begins

with

at

least

i

reductions

we

can

use

this

to

exhibit

an

infinite

reduction

beginning

from

S

contradicting

Lemma

Subtyping

Usually

in

proof-theoretic

analyses

of

calculi

with

subtyping

the

subtyping

relation

itself

presents

the

most

challenging

problems

This

is

also

the

case

in

F

Proof

Outline

Although

the

details

of

our

development

will

be

somewhat

more

involved

it

is

helpful

to

start

by

reviewing

the

standard

argument

-LSB-

etc.

-RSB-

for

the

decidability

of

subtyping

in

the

second-order

system

F

Begin

with

an

original

presentation

of

the

subtyping

system

that

directly

expresses

its

intended

meaning

but

which

is

not

directly

implementable

Propose

an

alternative

presentation

of

the

same

relation

by

a

syntax-directed

set

of

inference

rules

in

which

the

premises

of

each

rule

contain

only

metavariables

whose

values

are

uniquely

determined

by

the

form

of

the

conclusion

and

in

which

all

the

derivations

of

any

given

subtyping

statement

S

T

must

end

with

the

same

rule

-LRB-

More

precisely

more

than

one

rule

may

be

used

to

derive

a

given

statement

as

long

as

only

one

of

them

has

premises

whose

applicability

can

not

be

checked

directly

without

making

any

recursive

calls

-RRB-

This

system

can

be

implemented

by

a

proof-search

algorithm

that

will

never

have

to

guess

or

backtrack

Check

that

this

algorithm

is

indeed

a

decision

procedure

for

the

syntax-directed

system

by

showing

that

proof

search

must

terminate

in

finite

time

when

started

with

any

statement

as

its

initial

goal

Show

that

the

syntax-directed

system

is

sound

in

the

sense

that

any

subtyping

statement

derived

by

the

algorithm

is

also

derivable

in

the

original

system

This

step

is

typically

straightforward

Finally

prove

that

the

syntax-directed

system

is

complete

that

any

statement

derivable

in

the

original

system

is

also

derivable

by

the

algorithm

This

step

is

where

a

deeper

understanding

is

required

The

syntax-directed

system

may

be

viewed

as

a

version

of

the

original

from

which

all

problematic

rules

have

been

removed

In

the

case

of

F

there

is

just

one

such

rule

S

U

U

T

S

T

-LRB-

S-Trans

-RRB-

By

analogy

with

proof

theory

this

rule

is

sometimes

called

the

cut

rule

of

the

subtyping

system

the

type

U

appearing

in

the

subderivations

is

cut

out

when

moving

to

the

conclusion

By

analogy

with

the

sequent

calculus

or

the

simply

typed

calculus

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

this

cut

rule

can

be

almost

completely

eliminated

by

rewriting

derivations

But

not

completely

In

one

situation

transitivity

is

actually

essential

Statements

with

variables

on

the

left-hand

side

can

not

in

general

be

proved

without

using

transitivity

For

example

C

Top

-LRB-

-RRB-

B

C

A

B

A

C

must

be

proved

using

two

instances

of

S-TVar

to

establish

the

connections

between

A

and

B

and

between

B

and

C

which

are

then

joined

by

a

single

instance

of

transitivity

Thus

to

eliminate

S-Trans

while

retaining

completeness

it

is

necessary

to

refine

the

treatment

of

variables

extending

each

instance

of

S-TVar

with

an

internal

use

of

transitivity

-LRB-

A

-RRB-

T

A

T

-LRB-

S-TVar-Plus-Trans

-RRB-

It

is

easy

to

see

that

replacing

S-TVar

with

S-TVar-Plus-Trans

in

the

original

system

does

not

affect

its

power

Moreover

S-Trans

can

now

be

completely

eliminated

without

losing

any

derivable

statements

The

resulting

subtyping

algorithm

-LRB-

i.e.

the

recursive

procedure

obtained

from

the

syntax-directed

system

by

ordering

overlapping

rules

so

that

the

easy

ones

come

first

-RRB-

is

check

-LRB-

S

T

-RRB-

if

T

Top

-LRB-

-RRB-

then

true

else

if

S

T

then

true

else

if

S

A

then

check

-LRB-

-LRB-

A

-RRB-

T

-RRB-

else

if

S

and

T

then

check

-LRB-

-RRB-

and

check

-LRB-

-RRB-

else

if

S

All

-LRB-

A

U

-RRB-

and

T

All

-LRB-

A

U

-RRB-

then

check

-LRB-

A

U

-RRB-

else

false

The

behavior

of

this

algorithm

reveals

a

great

deal

about

the

structure

of

the

F

subtyping

relation

The

first

cases

deal

with

the

easy

rules

for

Top

and

reflexivity

The

third

case

says

that

a

statement

of

the

form

A

T

where

A

is

not

identical

to

T

and

T

is

not

Top

can

only

be

true

if

A

's

upper

bound

is

less

that

T

In

other

words

the

region

between

A

and

its

upper

bound

is

empty

there

are

no

types

strictly

greater

than

A

and

strictly

less

than

-LRB-

A

-RRB-

Since

this

concept

of

the

smallest

proper

supertype

of

A

will

also

be

crucial

for

our

development

it

is

worth

introducing

some

special

notation

for

it

Write

A

-LRB-

A

-RRB-

for

A

promotes

to

-LRB-

S

-RRB-

We

can

then

reformulate

the

enriched

variable

subtyping

rule

S-TVar-Plus-Trans

as

A

-LRB-

A

-RRB-

S

T

A

T

-LRB-

S-Promote-TVar

-RRB-

or

more

generally

as

S

U

U

T

S

T

-LRB-

S-Promote

-RRB-

where

the

partial

function

is

undefined

except

on

variables

The

subtyping

algorithm

becomes

check

-LRB-

S

T

-RRB-

else

if

S

U

then

check

-LRB-

U

T

-RRB-

Now

let

us

generalize

these

intuitions

to

the

case

of

full

F

Here

we

encounter

one

new

kind

of

situation

in

which

transitivity

plays

an

essential

role

For

example

in

the

context

A

Top

-LRB-

-RRB-

F

-LRB-

Fun

-LRB-

B

-RRB-

B

-RRB-

the

statement

F

A

A

is

provable

as

follows

-LRB-

ignoring

kinding

-RRB-

S-TVar

F

-LRB-

Fun

-LRB-

B

-RRB-

B

-RRB-

S-App

F

A

-LRB-

Fun

-LRB-

B

-RRB-

B

-RRB-

A

S-Conv

-LRB-

Fun

-LRB-

B

-RRB-

B

-RRB-

A

A

S-Trans

F

A

A

The

instance

of

transitivity

in

this

derivation

is

again

essential

but

it

is

not

an

instance

of

the

schema

that

motivated

S-Tvar-Plus-Trans

In

fact

it

is

possible

to

construct

more

involved

examples

where

the

instance

of

S-TVar

is

separated

from

the

instance

of

S-Trans

by

arbitrarily

many

applications

of

S-App

This

suggests

the

following

generalization

of

the

promotion

relation

Definition

-LSB-

Promotion

-RSB-

The

promotion

of

a

type

A

Sn

in

a

well-formed

context

is

-LRB-

A

-RRB-

Sn

written

A

Sn

-LRB-

A

-RRB-

Sn

With

this

relation

and

S-Promote

both

examples

above

can

be

derived

without

explicitly

using

S-Trans

To

extend

the

algorithm

check

to

full

F

one

thing

obviously

missing

is

a

clause

for

type

abstraction

matching

the

pointwise

subtyping

rule

S-Abs

We

add

one

as

follows

check

-LRB-

S

T

-RRB-

else

if

S

Fun

-LRB-

A

-RRB-

and

T

Fun

-LRB-

A

-RRB-

then

check

-LRB-

A

Top

-LRB-

-RRB-

-RRB-

Surprisingly

we

do

not

need

a

similar

clause

for

application

check

-LRB-

S

T

-RRB-

else

if

S

U

and

T

U

then

check

-LRB-

-RRB-

because

its

effect

turns

out

to

be

covered

by

the

promotion

clause

But

we

do

need

to

deal

with

the

possibility

of

conversion

otherwise

for

example

the

statement

-LRB-

Fun

-LRB-

B

-RRB-

B

-RRB-

will

not

be

derivable

Clearly

we

need

to

perform

some

reduction

on

the

arguments

before

choosing

which

clause

of

the

algorithm

to

apply

We

can

make

life

easy

by

simply

normalizing

the

arguments

before

looking

at

them

Since

the

arguments

to

recursive

calls

in

all

of

the

clauses

except

promotion

will

remain

in

normal

form

if

the

original

arguments

are

given

in

normal

form

we

only

need

to

re-normalize

in

the

promotion

clause

to

preserve

normality

The

final

algorithm

then

is

check

-LRB-

S

T

-RRB-

check

-LRB-

S

T

-RRB-

check

-LRB-

S

T

-RRB-

if

T

Top

-LRB-

Kind

-LRB-

S

-RRB-

-RRB-

then

true

else

if

S

T

then

true

else

if

S

U

then

check

-LRB-

U

T

-RRB-

else

if

S

and

T

then

check

-LRB-

-RRB-

and

check

-LRB-

-RRB-

else

if

S

All

-LRB-

A

U

-RRB-

and

T

All

-LRB-

A

U

-RRB-

then

check

-LRB-

A

U

-RRB-

else

if

S

Fun

-LRB-

A

-RRB-

and

T

Fun

-LRB-

A

-RRB-

then

check

-LRB-

A

Top

-LRB-

-RRB-

-RRB-

else

false

Our

task

for

the

remainder

of

the

section

will

be

to

show

that

this

algorithm

is

sound

and

complete

for

the

rules

in

Section

Our

first

step

is

a

technical

reformulation

of

the

original

system

which

provides

a

convenient

setting

for

the

arguments

to

follow

we

remove

the

general

rule

of

conversion

and

regain

its

effect

a

real

implementation

it

is

not

desirable

to

fully

normalize

type

expressions

this

wastes

time

-LRB-

in

the

vast

majority

of

calls

to

the

subtyping

algorithm

the

types

being

compared

are

identical

-RRB-

and

results

in

the

unnecessary

expansion

of

type

abbreviations

making

the

compiler

's

diagnostic

output

difficult

for

the

programmer

to

understand

In

practice

we

reduce

types

only

to

weak

head

normal

form

exposing

only

their

outermost

constructors

at

each

step

The

completeness

of

this

modification

rests

on

the

observation

that

the

reflexivity

check

in

the

algorithm

can

be

restricted

to

type

variables

applications

and

the

left-hand

sides

of

quantifiers

by

generalizing

each

of

the

remaining

rules

to

allow

arbitrary

reduction

in

the

premises

For

example

the

rule

for

subtyping

arrow

types

becomes

S

T

S

T

It

is

not

hard

to

show

that

this

reducing

system

is

equivalent

to

the

original

Next

we

introduce

two

important

properties

of

certain

derivations

in

the

reducing

system

ffl

A

cut-free

derivation

is

one

with

no

instances

of

the

rule

of

transitivity

ffl

A

strong

derivation

is

one

in

which

every

reduction

is

actually

a

reduction

to

normal

form

Cut-free

derivations

are

close

to

the

form

of

those

discovered

by

the

algorithm

check

Strength

is

a

more

technical

property

which

reduces

the

complexity

of

the

case

analyses

required

at

some

crucial

points

Using

these

properties

the

main

facts

that

we

need

are

The

rule

of

transitivity

can

be

eliminated

from

strong

derivations

The

validity

of

subtyping

is

preserved

by

reduction

in

cut-free

derivations

From

these

we

can

show

the

central

theorem

any

derivation

in

the

reducing

system

can

be

transformed

to

a

strong

cut-free

derivation

of

the

same

statement

Finally

to

obtain

the

algorithm

we

observe

that

the

rule

of

application

can

also

be

eliminated

from

strong

cut-free

derivations

The

Reducing

System

The

main

difference

between

the

reducing

system

and

the

original

subtyping

system

presented

in

Section

is

that

we

remove

the

rule

S-Conv

and

distribute

its

effects

over

the

remaining

rules

in

form

of

extra

premises

We

also

replace

the

rule

S-TVar

by

the

more

general

rule

of

promotion

R-Promote

Definition

-LSB-

Reducing

System

-RSB-

S

U

T

U

S

T

-LRB-

R-Refl

-RRB-

S

U

U

T

U

K

S

T

-LRB-

R-Trans

-RRB-

S

U

U

U

U

T

S

T

-LRB-

R-Promote

-RRB-

T

Top

-LRB-

K

-RRB-

S

K

S

T

-LRB-

R-Top

-RRB-

S

T

S

T

-LRB-

R-Arrow

-RRB-

S

All

-LRB-

A

U

-RRB-

T

All

-LRB-

A

U

-RRB-

A

U

S

T

-LRB-

R-All

-RRB-

S

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

A

Top

-LRB-

K

-RRB-

T

S

T

-LRB-

R-Abs

-RRB-

S

U

T

T

U

T

S

T

-LRB-

R-App

-RRB-

Notation

To

avoid

confusion

we

sometimes

distinguish

derivations

in

different

systems

by

marking

the

turnstile

symbol

O

for

the

original

system

R

for

the

reducing

system

S

for

strong

derivations

in

the

reducing

system

C

for

cut-free

derivations

in

the

reducing

system

and

CS

for

strong

cut-free

derivations

in

the

reducing

system

Our

task

for

the

remainder

of

Section

is

to

establish

the

equivalence

of

the

reducing

system

and

the

original

one

We

begin

by

establishing

some

technical

properties

of

the

original

Lemma

-LSB-

Kind

invariance

under

promotion

-RSB-

If

S

K

and

S

then

K.

Lemma

-LSB-

Promotion

and

Subtyping

-RSB-

If

S

then

O

S

Lemma

-LSB-

Well-kinded

subderivations

-RSB-

Suppose

S

KS

and

T

KT

If

d

is

a

derivation

of

O

S

T

and

is

a

derivation

of

O

T

with

a

subderivation

of

d

then

T

K

for

some

K.

Suppose

S

KS

and

T

KT

If

d

is

a

derivation

of

R

S

T

and

is

a

derivation

of

R

T

with

a

subderivation

of

d

then

T

K

for

some

K.

Lemma

The

following

rules

are

derivable

S

T

K

O

U

T

U

U

S

O

S

T

-LRB-

S-Conv

-LRB-

l

-RRB-

-RRB-

S

T

K

T

O

T

S

T

T

O

S

T

-LRB-

S-Conv

-LRB-

-RRB-

-RRB-

Proof

For

S-Conv

-LRB-

-RRB-

O

S

S

S

K

S

U

S-Conv

O

S

U

O

U

T

U

S-Trans

O

S

T

For

S-Conv

-LRB-

-RRB-

the

proof

is

similar

Lemma

-LSB-

Equivalence

-RSB-

The

original

and

the

intermediate

subtyping

systems

are

equivalent

for

wellkinded

types

if

S

T

K

then

O

S

T

iff

R

S

T

Proof

By

induction

on

derivations

For

the

remainder

of

Section

we

will

work

exclusively

within

the

reducing

system

Cut

Elimination

We

begin

our

analysis

of

the

reducing

system

with

a

proof

that

R-Trans

is

inessential

any

derivation

ending

with

it

can

be

rewritten

as

a

derivation

in

the

cut-free

subsystem

To

control

the

complexity

of

the

combinatorial

analysis

we

do

not

show

this

property

for

arbitrary

uses

of

transitivity

but

only

for

uses

of

a

restricted

form

we

consider

only

cut-terms

in

normal

form

and

ask

that

the

subderivations

of

the

cut

be

strong

The

following

section

will

show

that

these

conditions

can

always

be

achieved

With

these

restrictions

the

proof

of

cut

elimination

is

a

straightforward

extension

of

standard

proofs

for

the

second-order

fragment

-LRB-

c.f.

-LSB-

-RSB-

-RRB-

We

begin

with

one

technical

lemma

Lemma

Suppose

S

KS

and

A

Tn

KT

If

CS

S

A

Tn

then

this

statement

can

be

proved

using

a

sequence

of

instances

of

R-Promote

preceded

by

a

single

instance

of

R-Refl

S

Sn

Sn

Sn

V

A

Tn

V

CS

Sn

A

Tn

CS

A

Tn

CS

S

A

Tn

Proof

By

induction

on

the

given

subtyping

derivation

The

R-Refl

case

is

immediate

R-Promote

makes

straightforward

use

of

the

induction

hypothesis

R-App

uses

the

induction

hypothesis

to

construct

a

derivation

of

the

required

shape

for

the

left-hand

sides

of

the

application

-LRB-

CS

A

T

T

n

-RRB-

it

is

then

easy

to

check

that

the

right-hand

side

-LRB-

T

n

-RRB-

can

be

adjoined

to

all

the

steps

in

this

derivation

Proposition

-LSB-

Cut

elimination

-RSB-

Suppose

S

KS

and

T

KT

and

U

KU

If

CS

S

U

and

CS

U

T

where

U

is

in

normal

form

then

CS

S

T

Proof

By

induction

on

the

combined

size

of

the

given

subderivations

Proceed

by

a

case

analysis

on

the

last

rule

in

each

Case

R-Refl

on

the

left

anything

on

the

right

S

U

U

U

CS

S

U

To

prove

CS

S

T

we

can

directly

use

the

subderivations

of

CS

U

T

since

S

U

Case

Anything

on

the

left

R-Refl

on

the

right

Similar

Case

Anything

on

the

left

R-Top

on

the

right

T

Top

-LRB-

K

-RRB-

CS

U

K

CS

U

T

By

the

well-kindedness

of

subderivations

we

obtain

CS

S

T

immediately

by

R-Top

Case

R-Promote

on

the

left

anything

on

the

right

S

CS

U

CS

S

U

By

induction

-LRB-

using

well-kindedness

of

subderivations

-RRB-

and

R-Promote

Case

Anything

on

the

left

R-Promote

on

the

right

U

A

Un

V

CS

V

T

CS

U

T

By

Lemma

we

may

assume

that

the

derivation

of

CS

S

U

consists

of

a

sequence

of

instances

of

R-Promote

preceded

by

an

instance

of

R-Refl

S

Sn

Sn

Sn

U

A

Un

U

CS

Sn

A

Un

CS

A

Un

CS

S

A

Un

Replacing

the

final

instance

of

R-Refl

by

an

additional

instance

of

R-Promote

we

obtain

S

Sn

Sn

Sn

Sn

V

CS

V

T

CS

Sn

T

CS

T

CS

S

T

as

desired

This

takes

care

of

of

the

cases

The

remaining

cases

are

listed

in

the

following

table

CS

U

T

CS

S

U

R-Arrow

R-All

R-Abs

R-App

R-Top

R-Arrow

p

R-All

p

R-Abs

p

R-App

p

The

cases

marked

p

are

dealt

with

individually

below

Those

marked

can

never

occur

since

the

two

rules

in

question

would

place

incompatible

constraints

on

the

form

of

U

-LRB-

This

can

easily

be

checked

by

inspecting

the

rules

the

assumption

that

the

given

derivations

are

strong

is

needed

in

several

places

-RRB-

Case

R-All

S

All

-LRB-

A

-RRB-

U

All

-LRB-

A

-RRB-

A

CS

S

U

U

All

-LRB-

A

-RRB-

T

All

-LRB-

A

-RRB-

A

CS

U

T

By

the

well-kindedness

of

subderivations

the

induction

hypothesis

applies

giving

S

All

-LRB-

A

-RRB-

T

All

-LRB-

A

-RRB-

A

CS

S

T

Case

R-Arrow

R-Abs

Similar

Case

R-App

S

V

U

U

V

CS

U

CS

S

U

U

U

V

T

T

V

CS

U

T

CS

U

T

By

the

well-kindedness

of

subderivations

the

induction

hypothesis

applies

giving

S

V

T

T

V

CS

T

CS

S

T

Reduction

and

Subtyping

The

main

task

of

this

subsection

is

to

show

that

for

cut-free

derivations

reduction

in

types

does

not

interfere

with

the

subtyping

judgement

The

cornerstone

of

the

argument

is

a

substitution

lemma

saying

-LRB-

informally

-RRB-

that

if

S

T

then

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

T

From

this

we

can

show

that

the

reduction

of

an

outermost

redex

on

either

the

left-hand

or

the

right-hand

side

of

a

subtyping

statement

preserves

its

derivability

As

in

the

proof

of

Church-Rosser

in

Section

we

extend

these

properties

to

a

proof

of

the

preservation

of

subtyping

under

arbitrary

multi-step

reduction

by

passing

through

an

intermediate

step

where

we

show

it

for

one-step

parallel

reduction

In

outline

then

the

major

steps

are

as

follows

reduction

-LRB-

-RRB-

parallel

reduction

-LRB-

-RRB-

I

outer

reduction

-LRB-

left

-RRB-

-LRB-

-RRB-

outer

reduction

-LRB-

right

-RRB-

-LRB-

-RRB-

I

substitution

-LRB-

-RRB-

We

begin

with

two

technical

lemmas

Lemma

-LSB-

Expansion

preserves

subtyping

-RSB-

Suppose

S

KS

and

T

KT

If

C

T

with

S

and

T

T

then

C

S

T

Proof

By

inspection

of

the

rules

using

the

properties

of

kinding

and

reduction

Lemma

-LSB-

Maximality

of

Top

-RSB-

Suppose

Top

-LRB-

K

-RRB-

Sn

KS

and

T

KT

If

C

Top

-LRB-

K

-RRB-

Sn

T

then

T

Top

-LRB-

KT

-RRB-

Proof

Straightforward

induction

using

the

properties

of

kinding

and

reduction

Now

we

come

to

the

key

result

of

this

subsection

the

preservation

of

subtyping

under

substitution

Lemma

-LSB-

Substitution

preserves

subtyping

-RSB-

Let

A

Top

-LRB-

K

-RRB-

and

-LSB-

V

A

-RSB-

Suppose

that

V

K

and

that

S

KS

and

T

KT

If

C

S

T

then

C

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

T

Proof

The

cases

other

than

R-Promote

are

straightforward

we

give

the

argument

for

R-All

as

an

example

Case

R-All

S

All

-LRB-

U

-RRB-

T

All

-LRB-

U

-RRB-

U

C

C

S

T

By

Lemma

-LRB-

-RRB-

we

have

-LSB-

V

A

-RSB-

S

All

-LRB-

-LSB-

V

A

-RSB-

U

-RRB-

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

T

All

-LRB-

-LSB-

V

A

-RSB-

U

-RRB-

-LSB-

V

A

-RSB-

By

the

well-kinded

subderivations

lemma

and

the

induction

hypothesis

-LSB-

V

A

-RSB-

U

C

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

Now

R-All

applies

as

follows

-LSB-

V

A

-RSB-

S

All

-LRB-

-LSB-

V

A

-RSB-

U

-RRB-

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

T

All

-LRB-

-LSB-

V

A

-RSB-

U

-RRB-

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

U

C

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

C

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

T

that

the

more

general

form

of

this

property

in

which

-LRB-

A

-RRB-

is

allowed

to

be

any

supertype

of

V

would

be

much

more

difficult

to

prove

Here

we

obtain

a

straightforward

proof

by

considering

only

the

form

that

will

actually

be

required

later

in

the

critical

case

the

one

for

R-Promote

the

fact

that

-LRB-

A

-RRB-

Top

-LRB-

K

-RRB-

allows

a

direct

argument

using

Lemma

Case

R-Promote

S

U

C

U

T

C

S

T

The

definition

of

promotion

gives

Un

for

some

n

and

U

-LRB-

-RRB-

Un

Now

there

are

two

subcases

to

consider

Subcase

A

By

induction

C

-LSB-

V

A

-RSB-

-LRB-

-LRB-

-RRB-

Un

-RRB-

-LSB-

V

A

-RSB-

T

Now

-LSB-

V

A

-RSB-

-LRB-

-LRB-

-RRB-

Un

-RRB-

-LRB-

-LSB-

V

A

-RSB-

-LRB-

-LRB-

-RRB-

-RRB-

-RRB-

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

Un

-LRB-

-RRB-

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

Un

and

by

Lemma

-LRB-

-RRB-

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

Un

So

by

the

definition

of

promotion

we

can

apply

R-Promote

to

obtain

the

desired

result

as

follows

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

-LSB-

V

A

-RSB-

Un

-LSB-

V

A

-RSB-

-LRB-

-LRB-

-RRB-

Un

-RRB-

C

-LSB-

V

A

-RSB-

-LRB-

-LRB-

-RRB-

Un

-RRB-

-LSB-

V

A

-RSB-

T

C

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

T

Subcase

A

Since

-LRB-

A

-RRB-

Top

-LRB-

K

-RRB-

we

have

C

Top

-LRB-

K

-RRB-

Un

T

By

the

well-kinded

subderivations

lemma

-LRB-

-RRB-

U

is

well

kinded

in

So

by

Lemma

we

have

T

Top

-LRB-

KT

-RRB-

By

Lemma

also

-LSB-

V

A

-RSB-

T

Top

-LRB-

KT

-RRB-

By

the

well-kindedness

of

subderivations

S

KT

so

by

the

fact

that

substitution

preserves

kinding

-LRB-

Lemma

-RRB-

rule

R-Top

-LSB-

T

A

-RSB-

T

Top

-LRB-

KT

-RRB-

-LSB-

V

A

-RSB-

S

KT

C

-LSB-

V

A

-RSB-

S

-LSB-

V

A

-RSB-

T

gives

us

the

desired

result

The

next

lemma

introduces

a

technical

property

needed

for

the

following

one

a

subtype

of

a

Fun-type

is

either

a

Fun-type

itself

or

can

be

promoted

to

one

in

a

finite

number

of

steps

Lemma

-LSB-

Fun-right

-RSB-

Suppose

S

KS

and

T

KT

If

C

S

Fun

-LRB-

A

K

-RRB-

T

then

this

statement

can

be

derived

by

a

sequence

of

instances

of

R-Promote

preceded

by

one

instance

of

R-Fun

S

Sn

Sn

Sn

Fun

-LRB-

A

K

-RRB-

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

A

Top

-LRB-

K

-RRB-

C

T

C

Sn

Fun

-LRB-

A

K

-RRB-

T

C

Fun

-LRB-

A

K

-RRB-

T

C

S

Fun

-LRB-

A

K

-RRB-

T

Proof

By

induction

on

a

derivation

of

C

S

Fun

-LRB-

A

K

-RRB-

T

By

the

form

of

the

right-hand

side

and

the

fact

that

the

derivation

is

cut

free

there

are

three

cases

to

consider

-LRB-

Note

in

passing

that

this

is

one

point

where

we

crucially

depend

on

the

absence

of

cut

this

argument

fails

on

arbitrary

reducing

derivations

-RRB-

Case

R-Refl

S

U

Fun

-LRB-

A

K

-RRB-

T

U

C

S

Fun

-LRB-

A

K

-RRB-

T

By

definition

of

the

type

U

must

be

of

the

form

Fun

-LRB-

A

K

-RRB-

T

with

T

T

So

by

R-Abs

and

R-Refl

S

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

A

Top

-LRB-

K

-RRB-

C

T

T

C

S

Fun

-LRB-

A

K

-RRB-

T

Case

R-Abs

Immediate

Case

R-Promote

S

U

C

U

Fun

-LRB-

A

K

-RRB-

T

C

S

Fun

-LRB-

A

K

-RRB-

T

By

the

well-kinded

subderivations

lemma

the

induction

hypothesis

and

R-Promote

Lemma

-LSB-

Outer

reduction

on

the

right

-RSB-

Suppose

S

KS

and

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

KT

If

C

S

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

then

C

S

-LSB-

U

A

-RSB-

T

Proof

By

induction

on

a

proof

of

C

S

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

The

only

difficult

case

is

R-App

which

we

give

in

full

below

R-Refl

uses

the

Church-Rosser

property

R-Promote

uses

only

the

induction

hypothesis

All

the

other

cases

follow

a

common

pattern

we

show

just

R-All

Case

R-All

S

All

-LRB-

V

-RRB-

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

All

-LRB-

V

-RRB-

V

C

C

S

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

By

the

outermost

reduction

corollary

-LRB-

-RRB-

-LSB-

U

A

-RSB-

T

All

-LRB-

V

-RRB-

The

result

follows

immediately

by

R-All

Case

R-App

There

are

two

cases

to

consider

depending

on

whether

the

redex

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

is

itself

contracted

at

some

point

in

the

reduction

sequence

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

Subcase

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

-LSB-

U

A

-RSB-

T

V

S

W

V

C

W

C

S

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

Since

-LSB-

U

A

-RSB-

T

-LSB-

U

A

-RSB-

T

V

-LRB-

by

Lemma

-LRB-

-RRB-

-RRB-

the

result

follows

immediately

from

R-App

Subcase

S

W

U

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

C

W

Fun

-LRB-

A

K

-RRB-

T

C

S

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

Here

we

can

not

directly

use

the

subderivation

of

C

W

Fun

-LRB-

A

K

-RRB-

T

Intuitively

we

must

look

inside

this

derivation

to

find

an

inner

subderivation

in

which

T

appears

by

itself

on

the

right-hand

side

and

use

this

to

rebuild

a

subderivation

ending

with

-LSB-

U

A

-RSB-

T

on

the

right

-LRB-

More

precisely

the

inner

subderivation

will

have

T

on

the

right

where

T

T

-RRB-

This

we

accomplish

as

follows

First

we

use

the

Fun-right

lemma

-LRB-

-RRB-

to

obtain

a

derivation

of

C

W

Fun

-LRB-

A

K

-RRB-

T

in

a

very

rigid

form

a

sequence

of

n

instances

of

R-Promote

ending

with

an

instance

of

R-Abs

W

Wn

Wn

Wn

Fun

-LRB-

A

K

-RRB-

X

Fun

-LRB-

A

K

-RRB-

T

Fun

-LRB-

A

K

-RRB-

T

A

Top

-LRB-

K

-RRB-

C

X

T

C

Wn

Fun

-LRB-

A

K

-RRB-

T

C

Fun

-LRB-

A

K

-RRB-

T

C

W

Fun

-LRB-

A

K

-RRB-

T

From

the

instance

of

R-Abs

at

the

top

we

obtain

C

-LSB-

U

A

-RSB-

X

-LSB-

U

A

-RSB-

T

by

the

substitution

lemma

-LRB-

-RRB-

-LRB-

To

check

that

the

lemma

applies

we

need

the

following

observations

-LRB-

-RRB-

U

K

by

the

assumption

-LRB-

Fun

-LRB-

A

K

-RRB-

T

-RRB-

U

KT

the

fact

that

U

U

the

generation

lemma

for

types

and

subject

reduction

-LRB-

-RRB-

X

and

T

are

both

well-kinded

in

A

Top

-LRB-

K

-RRB-

by

the

well-kindedness

of

subderivations

-RRB-

At

this

point

we

have

accomplished

a

substitution

operation

but

not

quite

the

one

we

need

We

must

now

work

backwards

to

the

desired

result

From

C

-LSB-

U

A

-RSB-

X

-LSB-

U

A

-RSB-

T

and

the

fact

that

-LSB-

U

A

-RSB-

T

-LSB-

U

A

-RSB-

T

-LRB-

Lemma

-RRB-

we

use

the

expansion

lemma

-LRB-

-RRB-

to

obtain

C

-LSB-

U

A

-RSB-

X

-LSB-

U

A

-RSB-

T

after

remarking

that

-LSB-

U

A

-RSB-

X

and

-LSB-

U

A

-RSB-

T

are

well-kinded

by

subject

reduction

This

gives

us

what

we

need

on

the

right-hand

side

now

we

turn

to

fixing

the

left-hand

side

Wn

U

is

well-kinded

and

Wn

U

-LRB-

Fun

-LRB-

A

K

-RRB-

X

-RRB-

U

-LSB-

U

A

-RSB-

X

so

by

the

expansion

lemma

C

Wn

U

-LSB-

U

A

-RSB-

T

We

use

this

as

the

starting

point

for

a

new

sequence

of

R-Promote

steps

where

an

application

to

U

has

been

added

to

each

intermediate

term

S

W

U

U

Wn

U

Wn

U

C

Wn

U

-LSB-

U

A

-RSB-

T

C

U

-LSB-

U

A

-RSB-

T

C

S

-LSB-

U

A

-RSB-

T

The

conclusion

of

this

derivation

is

the

desired

statement

We

proceed

for

the

left-hand

side

of

in

the

same

way

as

we

have

done

for

the

righthand

side

Lemma

-LSB-

Fun-left

-RSB-

Suppose

Fun

-LRB-

A

K

-RRB-

S

KS

and

T

KT

If

C

Fun

-LRB-

A

K

-RRB-

S

T

then

one

of

the

following

cases

holds

T

Top

-LRB-

KT

-RRB-

or

Fun

-LRB-

A

K

-RRB-

S

Fun

-LRB-

A

K

-RRB-

and

T

Fun

-LRB-

A

K

-RRB-

T

with

A

Top

-LRB-

K

-RRB-

C

T

Proof

By

inspection

of

the

rules

By

the

form

of

the

left-hand

side

of

the

statement

and

the

fact

that

we

consider

only

cut-free

derivations

there

are

only

three

cases

to

consider

Case

R-Refl

Fun

-LRB-

A

K

-RRB-

S

U

T

U

C

Fun

-LRB-

A

K

-RRB-

S

T

U

must

be

of

the

form

Fun

-LRB-

A

K

-RRB-

where

S

so

we

can

conclude

A

Top

-LRB-

K

-RRB-

C

Case

R-Top

R-Abs

By

case

and

respectively

Lemma

-LSB-

Outer

reduction

on

the

left

-RSB-

Suppose

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

U

KS

and

T

KT

If

C

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

U

T

then

C

-LSB-

U

A

-RSB-

S

T

Proof

By

induction

Again

R-App

is

the

only

difficult

case

If

the

reduction

starting

from

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

U

reduces

the

outer

redex

at

some

point

then

this

case

proceeds

as

in

the

analogous

case

on

the

right-hand

side

-LRB-

-RRB-

Otherwise

we

have

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

U

-LRB-

Fun

-LRB-

A

K

-RRB-

-RRB-

U

T

W

U

C

Fun

-LRB-

A

K

-RRB-

W

C

-LRB-

Fun

-LRB-

A

K

-RRB-

S

-RRB-

U

T

By

the

well-kindedness

of

subderivations

both

Fun

-LRB-

A

K

-RRB-

and

W

are

well

kinded

By

the

Fun-left

lemma

there

are

two

cases

to

consider

We

are

given

W

Top

-LRB-

K!KT

-RRB-

Since

-LSB-

U

A

-RSB-

S

KT

-LRB-

by

subject

reduction

and

uniqueness

of

kinding

-RRB-

and

T

W

V

Top

-LRB-

K!KT

-RRB-

V

Top

-LRB-

KT

-RRB-

we

can

conclude

C

-LSB-

U

A

-RSB-

S

T

by

R-Top

We

are

given

Fun

-LRB-

A

K

-RRB-

Fun

-LRB-

A

K

-RRB-

W

Fun

-LRB-

A

K

-RRB-

Y

A

Top

-LRB-

K

-RRB-

C

Y

Both

and

Y

are

well

kinded

in

A

Top

-LRB-

K

-RRB-

-LRB-

by

well-kindedness

of

subderivations

subject

reduction

and

the

generation

lemma

-RRB-

so

C

-LSB-

U

A

-RSB-

-LSB-

U

A

-RSB-

Y

by

the

substitution

lemma

-LRB-

-RRB-

As

in

the

analogous

case

on

the

right

we

have

now

accomplished

a

substitution

operation

but

not

quite

the

one

we

need

We

must

again

work

backwards

to

the

desired

result

From

C

-LSB-

U

A

-RSB-

-LSB-

U

A

-RSB-

Y

and

the

fact

that

-LSB-

U

A

-RSB-

S

-LSB-

U

A

-RSB-

-LRB-

Lemma

-RRB-

we

can

use

the

expansion

lemma

to

obtain

C

-LSB-

U

A

-RSB-

S

-LSB-

U

A

-RSB-

Y

-LRB-

Note

that

-LSB-

U

A

-RSB-

S

is

well

kinded

by

subject

reduction

and

-LSB-

U

A

-RSB-

Y

by

subject

reduction

and

the

generation

lemma

-RRB-

Then

we

have

T

W

V

-LRB-

Fun

-LRB-

A

K

-RRB-

Y

-RRB-

U

-LSB-

U

A

-RSB-

Y

which

yields

the

desired

result

by

the

expansion

lemma

Using

the

two

outer

reduction

lemmas

we

can

do

one

reduction

step

at

the

outside

of

a

type

while

preserving

the

subtyping

judgement

The

next

step

is

to

generalize

this

to

an

arbitrary

reduction

For

this

purpose

we

choose

the

relation

Proposition

-LSB-

Parallel

reduction

preserves

subtyping

-RSB-

Suppose

S

KS

and

T

KT

If

C

S

T

with

S

and

then

C

T

If

C

S

T

with

T

T

and

then

C

S

T

-LRB-

Parallel

reduction

of

contexts

written

is

the

pointwise

extension

of

parallel

reduction

of

types

-RRB-

Proof

By

simultaneous

induction

on

derivations

The

only

difficult

cases

are

R-App

and

R-Promote

We

show

only

the

arguments

for

part

-LRB-

-RRB-

where

S

part

-LRB-

-RRB-

is

similar

except

in

the

case

R-Promote

which

is

easier

Case

R-Refl

S

U

T

U

C

S

T

By

Lemma

there

is

a

type

U

with

U

U

and

U

as

well

as

T

U

The

result

follows

by

R-Refl

Case

R-Top

T

Top

-LRB-

K

-RRB-

S

K

C

S

T

By

subject

reduction

and

uniqueness

of

kinding

we

have

KT

So

by

subject

reduction

the

wellkindedness

of

subderivations

and

Lemma

the

result

follows

by

R-Top

Case

R-Arrow

S

T

C

C

C

S

T

By

Lemma

and

the

definition

of

parallel

reduction

-LRB-

-RRB-

there

are

types

and

with

and

and

S

Since

the

Si

and

Ti

are

well-kinded

the

result

follows

by

induction

Case

R-All

S

All

-LRB-

A

U

-RRB-

V

T

All

-LRB-

A

U

-RRB-

W

A

U

C

V

W

C

S

T

By

Lemma

and

the

definition

of

parallel

reduction

-LRB-

-RRB-

there

are

types

U

and

V

with

U

U

and

V

V

and

S

All

-LRB-

A

U

-RRB-

V

All

-LRB-

A

U

-RRB-

V

Since

the

V

and

W

are

well-kinded

in

A

U

the

result

follows

by

induction

All

-LRB-

A

U

-RRB-

V

T

All

-LRB-

A

U

-RRB-

W

A

U

C

V

W

C

T

Case

R-All

R-Abs

Similar

Case

R-Promote

S

W

W

U

C

U

T

C

S

T

By

the

definition

of

the

promotion

relation

we

have

W

A

Sn

-LRB-

A

-RRB-

Sn

U

By

Lemma

and

the

definition

of

there

are

with

Si

and

S

A

Sn

A

Let

U

-LRB-

A

-RRB-

By

well-kindedness

of

subderivations

and

subject

reduction

U

and

U

are

well

kinded

The

result

follows

by

induction

the

fact

that

-LRB-

A

-RRB-

Sn

-LRB-

A

-RRB-

and

R-Promote

Case

R-App

S

U

W

T

V

W

C

U

V

C

S

T

By

Lemma

there

is

some

X

such

that

S

U

W

X

By

subject

reduction

U

W

KS

and

V

W

KT

By

the

generation

lemma

this

implies

that

U

and

V

are

well

kinded

Continue

by

cases

on

the

form

of

U

-LRB-

The

interesting

one

is

when

U

is

a

type

operator

-RRB-

Subcase

U

A

or

U

By

the

definition

of

parallel

reduction

X

must

have

the

form

U

W

with

U

U

and

W

W

By

subject

reduction

U

is

well

kinded

The

result

then

follows

by

induction

and

R-App

U

W

T

V

W

V

W

C

U

V

C

T

Subcase

U

Top

-LRB-

K

-RRB-

S

Top

-LRB-

K

-RRB-

W

T

V

W

C

Top

-LRB-

K

-RRB-

V

C

S

T

By

Lemma

V

Top

-LRB-

-RRB-

Since

T

has

kind

KT

so

does

Top

-LRB-

-RRB-

W

and

by

Lemma

T

Top

-LRB-

-RRB-

W

Top

-LRB-

KT

-RRB-

The

result

then

follows

by

R-Top

-LRB-

using

subject

reduction

wellkindedness

of

subderivations

and

lemma

-RRB-

T

Top

-LRB-

KT

-RRB-

KT

C

T

Subcase

U

Fun

-LRB-

A

K

-RRB-

We

have

-LRB-

Fun

-LRB-

A

K

-RRB-

-RRB-

W

X.

By

the

definition

of

parallel

reduction

X

can

have

one

of

two

forms

Subsubcase

X

-LRB-

Fun

-LRB-

A

K

-RRB-

U

-RRB-

W

Fun

-LRB-

A

K

-RRB-

Fun

-LRB-

A

K

-RRB-

U

W

W

The

result

follows

by

induction

-LRB-

using

subject

reduction

and

generation

for

the

kinding

hypothesis

-RRB-

and

R-App

-LRB-

Fun

-LRB-

A

K

-RRB-

U

-RRB-

W

T

V

W

V

W

C

Fun

-LRB-

A

K

-RRB-

U

V

C

T

Subsubcase

X

-LSB-

W

A

-RSB-

U

U

W

W

C

Fun

-LRB-

A

K

-RRB-

V

-RRB-

-LRB-

by

induction

-RRB-

C

Fun

-LRB-

A

K

-RRB-

U

V

-RRB-

-LRB-

by

R-App

-RRB-

C

Fun

-LRB-

A

K

-RRB-

U

W

V

W

-RRB-

-LRB-

by

outer

reduction

on

the

left

-LRB-

-RRB-

-RRB-

C

-LSB-

W

A

-RSB-

U

V

W

-RRB-

-LRB-

by

the

expansion

lemma

-LRB-

-RRB-

-RRB-

C

T

using

subject

reduction

and

the

generation

lemma

for

the

required

kinding

hypotheses

in

the

first

step

and

subject

reduction

in

the

third

and

fourth

steps

Corollary

-LSB-

Reduction

preserves

subtyping

-RSB-

Suppose

S

KS

and

T

KT

If

S

and

T

T

with

C

S

T

then

C

T

we

can

be

more

precise

about

why

we

choose

parallel

reduction

to

carry

out

this

proof

A

commutation

lemma

similar

to

the

Church-Rosser

property

the

existence

of

a

type

for

S

and

S

where

and

-LRB-

lemma

-RRB-

is

crucial

for

the

induction

This

immediately

excludes

the

one-step

reduction

relation

Ordinary

many-step

reduction

is

another

obvious

choice

but

it

can

not

be

used

in

the

case

of

R-App

since

here

we

need

to

know

the

form

of

the

reduct

of

the

application

which

can

not

be

recovered

from

Other

even

more

deterministic

reduction

strategies

such

as

normalizing

reduction

leftmost-outermost

reduction

or

complete

development

might

work

here

but

these

all

seem

to

fail

in

the

R-Promote

case

There

we

have

S

A

Sn

-LRB-

A

-RRB-

Sn

where

-LRB-

A

-RRB-

can

contain

redices

not

present

in

A

For

the

induction

to

work

in

this

case

we

need

to

be

able

to

ignore

these

new

redices

when

reducing

further

from

-LRB-

A

-RRB-

Sn

which

we

would

not

be

not

free

to

do

if

we

were

using

a

more

deterministic

reduction

strategy

Proof

If

we

can

do

one

step

we

can

do

many

So

the

result

follows

by

the

observation

-LRB-

-RRB-

that

the

reflexive

transitive

closure

of

parallel

reduction

coincides

with

ordinary

many-step

reduction

Corollary

-LSB-

Cut-free

derivations

can

be

strengthened

-RSB-

Suppose

S

KS

and

T

KT

If

C

S

T

then

CS

S

T

Proof

By

induction

Completeness

of

Strong

Cut-free

Subtyping

Theorem

Suppose

S

KS

and

T

KT

If

R

S

T

then

CS

S

T

Proof

By

induction

on

derivations

In

all

the

cases

except

R-Trans

we

argue

as

follows

By

the

induction

hypothesis

-LRB-

using

the

well-kindedness

of

subderivations

-RRB-

we

may

assume

that

all

the

subtyping

premises

are

proved

by

cut-free

derivations

Since

the

conclusion

is

not

a

cut

the

whole

derivation

is

cut-free

and

can

be

strengthened

using

Corollary

Now

suppose

the

final

rule

is

R-Trans

R

S

U

R

U

T

U

K

R

S

T

By

induction

we

may

assume

that

the

derivations

of

the

premises

are

cut-free

Moreover

by

Corollary

we

can

put

U

in

normal

form

C

S

U

and

C

U

T

Corollary

allows

these

derivations

to

be

strengthened

CS

S

U

and

CS

U

T

The

result

now

follows

from

Proposition

The

Algorithm

We

now

show

that

the

algorithm

we

developed

informally

in

Section

is

indeed

a

decision

procedure

for

the

subtype

relation

The

first

thing

we

must

verify

is

that

this

recursively

defined

procedure

is

really

an

algorithm

that

it

halts

in

finite

time

on

all

well-kinded

inputs

Proposition

-LSB-

Termination

of

the

algorithm

-RSB-

Suppose

S

KS

and

T

KT

Then

check

halts

when

presented

with

S

T

as

input

Proof

We

use

the

fact

that

the

reduction

defined

in

Section

is

strongly

normalizing

to

define

a

simple

termination

ordering

for

the

algorithm

First

note

that

the

recursive

call

in

the

third

clause

-LRB-

the

clause

for

promotion

-RRB-

is

guaranteed

to

halt

on

the

next

step

if

U

the

promotion

of

S

reduces

to

Top

-LRB-

K

-RRB-

for

some

K.

Thus

we

need

only

consider

the

possibility

of

nontermination

in

the

case

where

the

promotion

of

S

is

different

from

Top

i.e.

where

the

variable

being

promoted

is

a

redex

Let

the

rank

of

a

well-kinded

type

V

in

a

context

be

the

pair

-LRB-

r

s

-RRB-

where

r

is

the

maximum

length

of

a

reduction

sequence

starting

from

V

and

s

is

the

number

of

characters

in

V

Let

the

rank

of

a

well-kinded

subtyping

statement

S

T

the

pairwise

sum

of

the

ranks

of

S

and

T

Order

ranks

lexicographically

Then

the

rank

of

every

recursive

call

of

check

in

its

definition

is

smaller

than

the

rank

of

the

input

-LRB-

using

the

observation

that

we

need

only

consider

interesting

promotions

in

the

third

clause

-RRB-

The

algorithm

check

does

not

include

a

case

corresponding

to

the

pointwise

application

rule

R-App

But

this

rule

can

easily

be

shown

to

be

inessential

in

strong

cut-free

derivations

Lemma

-LSB-

Eliminability

of

R-App

-RSB-

Suppose

S

KS

and

T

KT

If

CS

S

T

then

this

statement

can

also

be

proved

by

a

-LRB-

strong

cut-free

-RRB-

derivation

with

no

instances

of

R-App

Proof

Straightforward

induction

on

derivations

using

Lemma

for

the

R-App

case

In

effect

each

instance

of

the

application

rule

is

replaced

by

a

sequence

of

instances

of

the

promotion

rule

Finally

we

verify

that

the

algorithm

defines

the

same

relation

as

the

original

subtyping

rules

Theorem

The

algorithm

check

is

sound

and

complete

for

the

original

subtyping

relation

-LRB-

on

wellkinded

types

-RRB-

Proof

On

well-kinded

inputs

the

original

subtyping

relation

is

equivalent

to

the

reducing

subtyping

relation

-LRB-

-RRB-

restricted

to

strong

cut-free

derivations

-LRB-

-RRB-

with

no

uses

of

R-App

-LRB-

-RRB-

Now

each

of

the

rules

in

this

restricted

system

begins

by

normalizing

both

sides

of

the

conclusion

Nothing

changes

if

we

present

the

rules

in

a

form

where

the

conclusion

is

assumed

to

be

in

normal

form

adding

a

single

normalization

step

at

the

very

end

of

the

derivation

and

inserting

a

re-normalizing

step

at

each

premise

that

is

not

guaranteed

to

be

in

normal

form

when

the

conclusion

is

indeed

there

is

just

one

of

these

the

last

premise

of

R-Promote

We

may

now

observe

that

proof

search

for

derivations

in

the

reformulated

system

is

an

essentially

linear

process

any

given

subtyping

statement

can

match

the

conclusion

of

only

one

subtyping

rule

for

which

further

search

may

be

required

That

is

a

given

statement

may

match

R-Refl

and/or

R-Top

and/or

one

of

the

remaining

rules

R-Promote

R-Arrow

R-All

and

R-App

Since

the

premises

of

R-Refl

and

R-Top

can

be

checked

directly

the

applicability

of

these

two

rules

can

be

tested

first

Using

this

strategy

no

backtracking

is

required

Moreover

all

the

metavariables

appearing

in

the

premises

the

rules

may

be

calculated

from

the

conclusion

no

guessing

is

required

The

algorithm

check

implements

this

strategy

Typing

In

Section

we

derived

an

algorithm

for

checking

the

subtyping

relation

by

controlling

the

non-syntaxdirected

rules

of

transitivity

and

conversion

In

this

section

we

carry

out

an

analogous

exercise

for

the

typing

relation

eliminating

the

rule

of

subsumption

from

the

system

defined

in

Section

and

accounting

for

its

effects

by

extending

some

of

the

other

rules

Compared

to

what

we

had

to

do

for

subtyping

this

is

actually

a

rather

simple

task

Indeed

the

resulting

algorithm

strongly

resembles

standard

algorithms

for

typechecking

F

The

only

essential

difference

comes

from

the

fact

that

the

promotion

relation

here

must

deal

with

application

in

addition

to

the

promotion

of

type

variables

As

usual

we

obtain

the

algorithm

by

analyzing

the

shapes

of

minimal

types

First

we

check

that

the

typing

relation

guarantees

well-kindedness

of

derivable

statements

Lemma

If

t

T

then

T

The

minimal

type

of

an

expression

is

a

type

smaller

or

equal

to

all

the

other

types

of

the

expression

For

the

algorithm

we

also

need

to

talk

about

a

term

's

minimal

types

of

certain

specific

shapes

Definition

-LSB-

Minimal

arrow-minimal

and

All-minimal

types

-RSB-

A

type

S

is

minimal

for

a

term

s

in

a

context

if

s

S

and

for

all

T

with

s

T

we

have

S

T

A

type

is

arrow-minimal

for

s

in

if

s

and

for

all

arrow-types

with

s

we

have

A

type

All

-LRB-

A

-RRB-

is

All-minimal

for

s

in

if

s

All

-LRB-

A

-RRB-

and

for

all

All-types

All

-LRB-

A

-RRB-

with

s

All

-LRB-

A

-RRB-

we

have

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

Definition

Let

T

be

well

kinded

in

The

promote-normal

form

of

T

in

is

T

ae

U

if

T

U

or

T

if

T

can

not

be

promoted

We

next

show

how

All-minimal

and

arrow-minimal

types

of

a

term

can

be

calculated

from

its

minimal

type

Lemma

Suppose

S

and

are

well

kinded

If

S

then

S

and

Suppose

S

and

All

-LRB-

A

-RRB-

are

well

kinded

If

S

All

-LRB-

A

-RRB-

then

S

All

-LRB-

A

-RRB-

and

All

-LRB-

A

-RRB-

All

-LRB-

A

-RRB-

Proof

On

well-kinded

inputs

the

original

subtyping

relation

is

equivalent

to

the

reducing

subtyping

relation

-LRB-

-RRB-

restricted

to

strong

cut-free

derivations

-LRB-

-RRB-

Proceed

by

induction

on

a

derivation

of

CS

S

Similar

Corollary

If

S

is

minimal

for

s

in

and

S

has

the

form

then

is

arrow-minimal

for

s

in

If

S

is

minimal

for

s

in

and

S

has

the

form

All

-LRB-

A

-RRB-

then

All

-LRB-

A

-RRB-

is

All-minimal

for

s

in

The

typing

algorithm

can

now

be

obtained

directly

from

the

original

typing

relation

by

removing

T-Subsumption

in

effect

restricting

the

set

of

types

derivable

for

a

well-typed

term

to

one

of

its

minimal

types

and

generalizing

the

application

and

type

application

rules

to

compensate

for

this

restriction

in

their

premises

We

use

A

to

distinguish

the

typing

algorithm

from

the

original

typing

relation

Definition

-LSB-

Typechecking

algorithm

-RSB-

ok

A

x

-LRB-

x

-RRB-

-LRB-

A-Var

-RRB-

x

A

e

A

fun

-LRB-

x

-RRB-

e

-LRB-

A-Arrow-I

-RRB-

S

A

s

S

A

t

T

T

A

s

t

-LRB-

A-Arrow-E

-RRB-

A

A

e

A

fun

-LRB-

A

-RRB-

e

All

-LRB-

A

-RRB-

-LRB-

A-All-I

-RRB-

T

All

-LRB-

A

-RRB-

A

t

T

S

K

S

A

t

S

-LSB-

S

A

-RSB-

-LRB-

A-All-E

-RRB-

The

termination

of

this

algorithm

is

straightforward

given

the

decidability

of

kinding

the

termination

of

the

subroutine

for

checking

subtyping

and

the

strong

normalization

of

reduction

which

guarantees

that

T

can

be

calculated

in

finite

time

whenever

T

is

well

kinded

Fact

If

ok

then

-LRB-

x

-RRB-

is

a

minimal

type

of

x

in

If

ok

then

x

has

no

type

in

If

is

a

minimal

type

of

e

in

x

then

is

a

minimal

type

of

fun

-LRB-

x

-RRB-

e

in

If

e

has

no

type

in

x

then

fun

-LRB-

x

-RRB-

e

has

no

type

in

If

S

is

a

minimal

type

for

s

in

and

S

and

T

is

a

minimal

type

for

t

in

and

T

then

is

a

minimal

type

for

s

t

in

If

s

or

t

has

no

type

in

or

if

S

is

a

minimal

type

for

s

in

and

T

is

a

minimal

type

for

t

in

but

S

or

if

S

but

T

then

s

t

has

no

type

in

If

is

a

minimal

type

of

e

in

A

then

All

-LRB-

A

-RRB-

is

a

minimal

type

of

fun

-LRB-

A

-RRB-

e

in

If

e

has

no

type

in

A

then

fun

-LRB-

A

-RRB-

e

has

no

type

in

If

S

is

a

minimal

type

for

s

in

and

S

All

-LRB-

A

-RRB-

and

U

then

-LSB-

U

A

-RSB-

is

a

minimal

type

for

s

U

in

If

s

has

no

type

in

or

if

S

is

a

minimal

type

for

s

in

but

S

All

-LRB-

A

-RRB-

or

if

S

All

-LRB-

A

-RRB-

but

U

then

s

U

has

no

type

in

Theorem

-LSB-

Soundness

and

completeness

-RSB-

If

A

t

T

then

T

and

t

T

If

s

T

then

T

and

A

s

S

where

S

is

minimal

for

s

in

Proof

By

induction

using

the

previous

facts

This

brings

us

to

our

final

result

Corollary

-LSB-

Decidability

of

F

typing

-RSB-

The

original

typing

relation

t

T

is

decidable

Proof

To

check

whether

a

statement

t

T

is

derivable

first

check

that

T

is

well

kinded

then

calculate

the

minimal

type

S

of

t

in

using

the

algorithm

above

and

use

the

subtyping

algorithm

check

to

verify

that

S

T

Acknowledgements

This

research

was

supported

by

the

Specification

and

Verification

of

Distributed

Systems

project

funded

by

the

Deutsche

Forschungsgemeinschaft

Sonderforschungsbereich

Multiprozessor

und

Netzwerkkonfigurationen

with

assistance

from

the

DAAD

-LRB-

Deutscher

Akademischer

Austauschdienst

-RRB-

-LSB-

Steffen

-RSB-

by

the

British

Science

and

Engineering

Research

Council

-LSB-

Pierce

-RSB-

and

by

and

the

British

Council

-LSB-

Steffen

and

Pierce

-RSB-

Discussions

with

Ulf

Becker

Luca

Cardelli

Adriana

Compagnoni

Randy

Pollack

Terry

Stroup

and

David

Turner

helped

deepen

our

understanding

of

F

David

Aspinall

and

Adriana

Compagnoni

gave

us

useful

comments

on

a

late

draft

Giorgio

Ghelli

pointed

out

a

bug

in

an

earlier

version

of

the

proof

which

Adriana

Compagnoni

helped

correct

References

-LSB-

-RSB-

Mart

n

Abadi

Baby

and

a

theory

of

objects

Research

Report

Digital

Equipment

Corporation

Systems

Research

Center

Palo

Alto

California

February

To

appear

in

Journal

of

Functional

Programming

-LSB-

-RSB-

Mart

n

Abadi

and

Luca

Cardelli

A

theory

of

primitive

objects

Second-order

systems

In

European

Symposium

on

Programming

-LRB-

ESOP

-RRB-

Edinburgh

Scotland

-LSB-

-RSB-

Mart

n

Abadi

and

Luca

Cardelli

A

theory

of

primitive

objects

Untyped

and

first-order

systems

In

Theoretical

Aspects

of

Computer

Software

-LRB-

TACS

-RRB-

Sendai

Japan

-LSB-

-RSB-

Hendrik

Barendregt

The

Lambda

Calculus

Its

Syntax

and

Semantics

volume

of

Studies

in

Logic

and

the

Foundations

of

Mathematics

North-Holland

revised

edition

-LSB-

-RSB-

Val

Breazu-Tannen

Thierry

Coquand

Carl

Gunter

and

Andre

Scedrov

Inheritance

as

implicit

coercion

Information

and

Computation

-LCB-

-LSB-

-RSB-

Kim

B.

Bruce

and

Giuseppe

Longo

A

modest

model

of

records

inheritance

and

bounded

quantification

Information

and

Computation

-LCB-

Also

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

Object-Oriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

An

earlier

version

appeared

in

the

proceedings

of

the

IEEE

Symposium

on

Logic

in

Computer

Science

-LSB-

-RSB-

Kim

Bruce

and

John

Mitchell

PER

models

of

subtyping

recursive

types

and

higher-order

polymorphism

In

Proceedings

of

the

Nineteenth

ACM

Symposium

on

Principles

of

Programming

Languages

Albequerque

NM

January

-LSB-

-RSB-

Kim

B.

Bruce

Safe

type

checking

in

a

statically

typed

object-oriented

programming

language

In

Proceedings

of

the

Twentieth

ACM

Symposium

on

Principles

of

Programming

Languages

January

To

appear

in

Journal

of

Functional

Programming

-LSB-

-RSB-

Luca

Cardelli

A

semantics

of

multiple

inheritance

In

G.

Kahn

D.

MacQueen

and

G.

Plotkin

editors

Semantics

of

Data

Types

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer-Verlag

Full

version

in

Information

and

Computation

-LCB-

-LSB-

-RSB-

Luca

Cardelli

Structural

subtyping

and

the

notion

of

power

type

In

Proceedings

of

the

ACM

Symposium

on

Principles

of

Programming

Languages

pages

-LCB-

San

Diego

CA

January

-LSB-

-RSB-

Luca

Cardelli

Notes

about

F

Unpublished

manuscript

October

-LSB-

-RSB-

Luca

Cardelli

Extensible

records

in

a

pure

calculus

of

subtyping

Research

report

DEC

Systems

Research

Center

January

Also

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

Object-Oriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

-LSB-

-RSB-

Giuseppe

Castagna

Strong

typing

in

object-oriented

paradigms

Rapport

de

Recherche

Ecole

Normale

Sup?erieure

Paris

May

-LSB-

CCH

-RSB-

Peter

Canning

William

Cook

Walter

Hill

Walter

Olthoff

and

John

Mitchell

F-bounded

quantification

for

object-oriented

programming

In

Fourth

International

Conference

on

Functional

Programming

Languages

and

Computer

Architecture

pages

-LCB-

September

-LSB-

-RSB-

Pierre-Louis

Curien

and

Giorgio

Ghelli

Subtyping

extensionality

Confluence

of

reductions

in

F

In

Ito

and

Meyer

-LSB-

-RSB-

pages

-LCB-

-LSB-

-RSB-

Pierre-Louis

Curien

and

Giorgio

Ghelli

Coherence

of

subsumption

Minimum

typing

and

type-checking

in

F

Mathematical

Structures

in

Computer

Science

-LCB-

Also

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

Object-Oriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

-LSB-

-RSB-

G.

Castagna

G.

Ghelli

and

G.

Longo

A

calculus

for

overloaded

functions

with

subtyping

In

ACM

conference

on

LISP

and

Functional

Programming

pages

-LCB-

San

Francisco

July

ACM

Press

Also

available

as

Rapport

de

Recherche

Ecole

Normale

Sup?erieure

Paris

-LSB-

-RSB-

William

R.

Cook

Walter

L.

Hill

and

Peter

S.

Canning

Inheritance

is

not

subtyping

In

Seventeenth

Annual

ACM

Symposium

on

Principles

of

Programming

Languages

pages

-LCB-

San

Francisco

CA

January

Also

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

ObjectOriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

-LSB-

-RSB-

Alonzo

Church

A

formulation

of

the

simple

theory

of

types

Journal

of

Symbolic

Logic

-LCB-

-LSB-

-RSB-

Luca

Cardelli

and

Giuseppe

Longo

A

semantic

basis

for

Quest

Journal

of

Functional

Programming

-LRB-

-RRB-

-LCB-

October

Preliminary

version

in

ACM

Conference

on

Lisp

and

Functional

Programming

June

Also

available

as

DEC

SRC

Research

Report

Feb.

-LSB-

-RSB-

Luca

Cardelli

Simone

Martini

John

C.

Mitchell

and

Andre

Scedrov

An

extension

of

system

F

with

subtyping

In

Ito

and

Meyer

-LSB-

-RSB-

pages

-LCB-

-LSB-

-RSB-

Adriana

B.

Compagnoni

Subtyping

in

F

is

decidable

Draft

technical

report

January

-LSB-

-RSB-

Adriana

B.

Compagnoni

and

Benjamin

C.

Pierce

Multiple

inheritance

via

intersection

types

Technical

Report

LFCS

University

of

Edinburgh

August

Also

available

as

Catholic

University

Nijmegen

computer

science

technical

report

-LSB-

-RSB-

Giuseppe

Castagna

and

Benjamin

Pierce

Decidable

bounded

quantification

In

Proceedings

of

TwentyFirst

Annual

ACM

Symposium

on

Principles

of

Programming

Languages

Portland

Oregon

ACM

January

-LSB-

-RSB-

Luca

Cardelli

and

Peter

Wegner

On

understanding

types

data

abstraction

and

polymorphism

Computing

Surveys

-LRB-

-RRB-

December

-LSB-

-RSB-

Jean

H.

Gallier

On

Girard

's

candidats

de

reductibilit?e

In

Piergiorgio

Odifreddi

editor

Logic

and

Computer

Science

number

in

APIC

Studies

in

Data

Processing

pages

-LCB-

Academic

Press

-LSB-

-RSB-

Giorgio

Ghelli

Proof

Theoretic

Studies

about

a

Minimal

Type

System

Integrating

Inclusion

and

Parametric

Polymorphism

PhD

thesis

Universit?a

di

Pisa

March

Technical

report

TD

-LCB-

Dipartimento

di

Informatica

Universit?a

di

Pisa

-LSB-

-RSB-

G.

Ghelli

Divergence

of

F

type

checking

Technical

Report

Dipartimento

di

Informatica

Universit?a

di

Pisa

-LSB-

-RSB-

Giorgio

Ghelli

Recursive

types

are

not

conservative

over

F

In

Typed

Lambda

Calculus

and

Applications

March

-LSB-

-RSB-

Jean-Yves

Girard

Interpr?etation

fonctionelle

et

elimination

des

coupures

de

l'arithm

etique

d'ordre

sup?erieur

PhD

thesis

Universit?e

Paris

VII

-LSB-

-RSB-

Jean-Yves

Girard

Yves

Lafont

and

Paul

Taylor

Proofs

and

Types

volume

of

Cambridge

Tracts

in

Theoretical

Computer

Science

Cambridge

University

Press

Cambridge

-LSB-

-RSB-

Giorgio

Ghelli

and

Benjamin

Pierce

Bounded

existentials

and

minimal

typing

Draft

report

June

-LSB-

-RSB-

Martin

Hofmann

and

Benjamin

Pierce

A

unifying

type-theoretic

framework

for

objects

In

Symposium

on

Theoretical

Aspects

of

Computer

Science

Extended

version

available

as

An

Abstract

View

of

Objects

and

Subtyping

-LRB-

Preliminary

Report

-RRB-

University

of

Edinburgh

LFCS

technical

report

ECS

-LSB-

-RSB-

T.

Ito

and

A.

R.

Meyer

editors

Theoretical

Aspects

of

Computer

Software

-LRB-

Sendai

Japan

-RRB-

number

in

Lecture

Notes

in

Computer

Science

Springer-Verlag

September

-LSB-

-RSB-

Dinesh

Katiyar

and

Sriram

Sankar

Completely

bounded

quantification

is

decidable

In

Proceedings

of

the

ACM

SIGPLAN

Workshop

on

ML

and

its

Applications

June

-LSB-

-RSB-

Simone

Martini

Bounded

quantifiers

have

interval

models

In

Proceedings

of

the

ACM

Conference

on

Lisp

and

Functional

Programming

pages

-LCB-

Snowbird

Utah

July

ACM

-LSB-

-RSB-

John

C.

Mitchell

Furio

Honsell

and

Kathleen

Fisher

A

lambda

calculus

of

objects

and

method

specialization

In

IEEE

Symposium

on

Logic

in

Computer

Science

June

-LSB-

-RSB-

John

C.

Mitchell

Toward

a

typed

foundation

for

method

specialization

and

inheritance

In

Proceedings

of

the

ACM

Symposium

on

Principles

of

Programming

Languages

pages

-LCB-

January

Also

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

Object-Oriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

-LSB-

-RSB-

Benjamin

C.

Pierce

Bounded

quantification

is

undecidable

In

Proceedings

of

the

Nineteenth

Annual

ACM

Symposium

on

Principles

of

Programming

Languages

-LRB-

Albequerque

New

Mexico

-RRB-

January

To

appear

in

Information

and

Computation

and

in

Carl

A.

Gunter

and

John

C.

Mitchell

editors

Theoretical

Aspects

of

Object-Oriented

Programming

Types

Semantics

and

Language

Design

-LRB-

MIT

Press

to

appear

-RRB-

-LSB-

-RSB-

Benjamin

C.

Pierce

and

David

N.

Turner

Simple

type-theoretic

foundations

for

object-oriented

programming

Journal

of

Functional

Programming

To

appear

a

preliminary

version

appeared

in

Principles

of

Programming

Languages

and

as

University

of

Edinburgh

technical

report

under

the

title

Object-Oriented

Programming

Without

Recursive

Types

-LSB-

-RSB-

Benjamin

C.

Pierce

and

David

N.

Turner

Statically

typed

friendly

functions

via

partially

abstract

types

Technical

Report

University

of

Edinburgh

LFCS

April

Also

available

as

INRIA-Rocquencourt

Rapport

de

Recherche

No.

-LSB-

-RSB-

John

Reynolds

Towards

a

theory

of

type

structure

In

Proc

Colloque

sur

la

Programmation

pages

-LCB-

New

York

Springer-Verlag

LNCS

-LSB-

-RSB-

Paula

Severi

and

Erik

Poll

Pure

type

systems

with

definitions

Computing

science

note

Eindhoven

University

of

Technology

September

-LSB-

-RSB-

Mitchell

Wand

Complete

type

inference

for

simple

objects

In

Proceedings

of

the

IEEE

Symposium

on

Logic

in

Computer

Science

Ithaca

NY

June

-LSB-

-RSB-

Mitchell

Wand

Corrigendum

Complete

type

inference

for

simple

objects

In

Proceedings

of

the

IEEE

Symposium

on

Logic

in

Computer

Science

-LSB-

-RSB-

Mitchell

Wand

Type

inference

for

record

concatenation

and

multiple

inheritance

In

Fourth

Annual

IEEE

Symposium

on

Logic

in

Computer

Science

pages

-LCB-

Pacific

Grove

CA

June

bounded quantification
polymorphism
subtyping
type systems
typechecking