unifying kind and type inference remko van beusekom & jeroen gordijn

21
Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Upload: layla-timm

Post on 31-Mar-2015

221 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Unifying Kind and Type Inference

Remko van Beusekom

&

Jeroen Gordijn

Page 2: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Overview

• Type inferencing

• Kind inferencing

• Example

• Similarities kind and type inferencing

Page 3: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Overview (2)

• Implementation problem in uhc

• Our solution to the problem

• Conclusion

Page 4: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Approach

• Investigate type and kind inferencing

• Check the similarities

• Investigate the implementations in UHC

• Check whether unification is possible

Page 5: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Type inferencing

• Check if an (untyped) term can be typede.g.:

(\x.x+1) true can’t be typed

(\x.x+1) 2 can be typed.

Page 6: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Kind inferencing

• Kinds are “the types of types”

• Check if a type definition can be kinded

e.g.:

(Bool)(Nat) Can’t be kinded

(Nat->Bool)(Nat) Can be kinded

Page 7: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Type Inferencing

example

Page 8: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

\x:X.\z:Z.(x z) 0 : S | x C

x:X z:Z (CT-VAR)(CT-VAR)

0 : Nat |ø {}(CT-ZERO)

x : _ |_ _ z : _ |_ _ (CT-APP)

(x z) : _ |_ _ 0 : _ |_ _ (CT-APP)

X |ø {} Z |ø {}

Nat |ø {}V1 |{V1} C1

C1 = {X = ZV1} = x:X,z:Z

(x z) 0 : _ |_ _(CT-ABS)

C2 = C1 {V1 = Nat V2}

V2 |{V1,V2} C2

X Z V2{V1,V2}

C2

Page 9: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Unifying the constraints

• Unify({X = Z V1}, {V1 = Nat V2})

• Unification fails or succeeds

• If succeeds then typeable = [X Z V1, V1 Nat V2]

(X Z V2) = (Z (Nat V2)) Z V2

Page 10: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Kind Inferencing

example

Page 11: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

X Y :: _(K-ABS) 2x

= X:: _ ,Y:: _

\X.\Y.X Y :: K

(K-APP) X :: _ _ Y :: _

(K-TVAR) X :: _ _

(K-TVAR) Y :: _

*

(* *) * *

** *

** *

* * *

Page 12: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Similarities

• Walk over the tree. (BOTH)

• Introduce placeholders for types/kinds (BOTH)

• Introduce placeholders for Constraints (TYPE)

• Fill the environment (BOTH)

• Fill in the placeholders (BOTH)

Page 13: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

The challenge

• Type inferencing implemented first

• Kind inferencing added by copying parts

from type inferencing

• Duplicate code

• Q: Can we unify these implementations?

Page 14: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Solution to the problem

• Generalize the AST– put common constructors in general data type– extra general constructor

• Move inferencing code into general code

• Problem: extra node in the generalised AST

Page 15: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Implementation in UHC

• Different structure

DATA KindExpr

| KVar

| KStar

| KCon

| KApply

DATA TypeExpr

| TVar

| TCon

| TConProduct

| TProduct

| TPred

| TQuant

| TApply

Page 16: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Generalized AST

• Unify structure

DATA GenExpr

| GVar

| GCon

| GApply

DATA KindExpr

| KGenExpr

gExpr :: GenExpr

| KStar

DATA TypeExpr

| TGenExpr

gExpr :: GenExpr

| TConProduct

| TProduct

| TPred

| TQuant

Page 17: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Kind abstract treeKindExpr

KVar KApply KCon KStar KParen

KindExpr

KGenExpr KStar KParen

GenExpr

GVar GApply GCon

GenExpr

before

unified

Page 18: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

KindExpr

KVar

TypeExpr

TVar TApplyKApply

Unified tree

GenExpr

GVar

KindExpr

KGenExpr

TypeExpr

TGenExprGApply

before

unified

GenExpr GenExpr

Page 19: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

-- Pass 1, patterns/placeholders

SEM KindExpr

| KVar

loc . (kpuniq,tai,kgam)

= samefun @lhs.patTpTpConstrGam

lhs . patTpTpConstrGam = @kgam

SEM TypeExpr

| TVar

loc . (_,tai,tcgam)

= samefun @lhs.patImTpConstrGam

lhs . patImTpConstrGam = @tcgam

Page 20: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

ATTR GenExpr [ | patImGenGam: {TypeAssumptions} | ]

SEM GenExpr

| GVar

loc . (gpuniq,tai,ggam) = samefun @lhs.patImGenGam

lhs . patImGenGam = @ggam

SEM TypeExpr

| TGenExpr

gExpr . patImGenGam = @lhs.patImTpConstrGam

lhs . patImTpConstrGam = @gExpr.patImGenGam

SEM KindExpr

| KGenExpr

gExpr . patImGenGam = @lhs.patTpTpConstrGam

lhs . patTpTpConstrGam = @gExpr.patImGenGam

Page 21: Unifying Kind and Type Inference Remko van Beusekom & Jeroen Gordijn

Conclusion

• Not tested, but this should work

• Draw back: Lot of work now

• Improvement: Future additions/fixes easier