Top Banner
Type Specialisation John Hughes Chalmers University
23

Type Specialisation John Hughes Chalmers University.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Type Specialisation John Hughes Chalmers University.

Type Specialisation

John Hughes

Chalmers University

Page 2: Type Specialisation John Hughes Chalmers University.

Type Specialisation(With thanks to Neil Jones)

John Hughes

Chalmers University

Page 3: Type Specialisation John Hughes Chalmers University.

1985: A Weird Program Called MIX!

The Mix Equation

(mix prog x) y = prog x y

The Futamura Projections

Specialised program

mix interp prog = codebecause

code data = interp prog data

Page 4: Type Specialisation John Hughes Chalmers University.

1985: A Weird Program Called MIX!

The Mix Equation

(mix prog x) y = prog x y

The Futamura Projections

Specialised program

mix mix interp = compilerbecause

compiler prog = mix interp prog = code

Page 5: Type Specialisation John Hughes Chalmers University.

1985: A Weird Program Called MIX!

The Mix Equation

(mix prog x) y = prog x y

The Futamura Projections

Specialised program

mix mix mix = compiler-generatorbecause

compiler-generator interp = mix mix interp = compiler

Page 6: Type Specialisation John Hughes Chalmers University.

Why Did He Use Scheme?

Why not:

•A language with lazy evaluation?

•A language with types?

Page 7: Type Specialisation John Hughes Chalmers University.

1987: Partial Evaluation and Mixed Computation

Page 8: Type Specialisation John Hughes Chalmers University.

1987: Partial Evaluation and Mixed Computation

A ”Hughes”

•a measure of the irrelevance of a talk to the workshop topic.

Page 9: Type Specialisation John Hughes Chalmers University.

1987: Partial Evaluation and Mixed Computation

Challenging Problems

•eliminating tags from typed interpreters

data V = N Int | F (V -> V)

eval (Const n) = N neval (Lam x e) = F (v. eval (bind x v ) e)…

Lam ”x” (Const 3)

F (v. N 3)

mix

Page 10: Type Specialisation John Hughes Chalmers University.

1987: Partial Evaluation and Mixed Computation

Challenging Problems

•eliminating tags from typed interpreters

•type specialisation

data V = N Int | F (V -> V)…

Int

Int->Int

(Int->Int)->Int

Page 11: Type Specialisation John Hughes Chalmers University.

1987: Partial Evaluation and Mixed Computation

Challenging Problems

•eliminating tags from typed interpreters

•type specialisation

•optimal specialisation of typed interpreters

mix self-interp prog prog x. 3

F (x. N 3)

Page 12: Type Specialisation John Hughes Chalmers University.

1995: Neil’s Seminar @ Chalmers

•How can we recognise an interpreter for a strongly typed language?

•How can we derive a type-checker from an interpreter?

•An approach for a first-order language.

Page 13: Type Specialisation John Hughes Chalmers University.

1995: Neil’s Seminar @ Chalmers

•How can we recognise an interpreter for a strongly typed language?

•How can we derive a type-checker from an interpreter?

•An approach for a first-order language.

Surely one can dobetter?

Page 14: Type Specialisation John Hughes Chalmers University.

1995: Neil’s Seminar @ Chalmers

•How can we recognise an interpreter for a strongly typed language?

•How can we derive a type-checker from an interpreter?

•An approach for a first-order language.

Invitation to Dagstuhl Seminar on Partial Evaluation!

Page 15: Type Specialisation John Hughes Chalmers University.

Key Idea: Offline Partial Evaluation

Annotations in the source code classify operations as

•static -- performed by mix

•dynamic -- built into residual program

eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y

Underlinedoperations are

dynamic

Page 16: Type Specialisation John Hughes Chalmers University.

Key Idea: Offline Partial Evaluation

Annotations in the source code classify operations as

•static -- performed by mix

•dynamic -- built into residual program

eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y

Lam ”x” (Const 3)

F (v. N 3)

mix

Page 17: Type Specialisation John Hughes Chalmers University.

Key Idea: Offline Partial Evaluation

Annotations in the source code classify operations as

•static -- performed by mix

•dynamic -- built into residual program

eval (Const n) = N (lift n)eval (Lam x e) = F (v. eval (bind x v ) e)… bind x v = y. if x=y then v else y

Don’t underlinethe tags!

But we have to!

Page 18: Type Specialisation John Hughes Chalmers University.

Interesting Properties

N (z+lift 1) N Int

x. case x of N n -> N (n+lift 1) F f -> …

N Int -> N Int

f @ N (z+lift 1) N Int

Page 19: Type Specialisation John Hughes Chalmers University.

Keep Track of Tags by Type Inference!

e e´ : ´ ´ ::= Int | ´-> ´ | C ´

Residual types

|- e e´ : ´

|- C e e´ : C ´

|- e e´ : Ck 1´

, xk e´ : 1´ |- ek ek´ : 2´

|- case e of {Cj xj -> ej} ek´ : 2´

Page 20: Type Specialisation John Hughes Chalmers University.

Reformulate Partial Evaluation in Terms of Type Inference

e e´ : ´ ´ ::= Int | ´-> ´ | C ´ | n | …

Residual types

x. x+1 x. : 2 -> 3

x. x+z <z> : close <z : Int> in x. x+z

Page 21: Type Specialisation John Hughes Chalmers University.

Real Increase in Power

Type specialisation=

Type-based analysis + type-directed transformation

Examples

•Monomorphisation

•Typed defunctionalisation

•Constructor specialisation for -calculus

By type-specialising a suitable interpreter.

Page 22: Type Specialisation John Hughes Chalmers University.

Continuing Work

•Simple type specialiser as a back-end for PE to achieve optimality (PADO II).

•Principal type specialisations.

•Strategies for controlling polyvariance (type-based cloning)

•…

Page 23: Type Specialisation John Hughes Chalmers University.

Continuing Work

•Simple type specialiser as a back-end for PE to achieve optimality (PADO II).

•Principal type specialisations & polymorphism.

•Strategies for controlling polyvariance (type-based cloning)

•…

Thank you Neil!