Levity Polymorphism Richard A. Eisenberg Simon Peyton Jones Bryn - - PowerPoint PPT Presentation

levity polymorphism
SMART_READER_LITE
LIVE PREVIEW

Levity Polymorphism Richard A. Eisenberg Simon Peyton Jones Bryn - - PowerPoint PPT Presentation

Levity Polymorphism Richard A. Eisenberg Simon Peyton Jones Bryn Mawr College Microsoft Research Cambridge rae@cs.brynmawr.edu simonpj@microsoft.com Tuesday, 20 June 2017 PLDI Barcelona, Spain How can we compile polymorphism without


slide-1
SLIDE 1

Levity Polymorphism

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu

Tuesday, 20 June 2017 PLDI Barcelona, Spain

Simon Peyton Jones Microsoft Research Cambridge simonpj@microsoft.com

slide-2
SLIDE 2

How can we compile polymorphism ? without losing performance

slide-3
SLIDE 3

Polymorphism

  • choose :: ∀ a. Bool ! a ! a ! a

choose True t _ = t choose False _ f = f (+) :: ∀ a. Num a ⇒ a ! a ! a

slide-4
SLIDE 4

How can we compile polymorphism ?

slide-5
SLIDE 5

Answer:
 
 Our novel approach:
 kind-directed compilation Many ways

slide-6
SLIDE 6

Design Criteria

  • High performance
  • Type erasure
  • Support for fancy types
  • -"
slide-7
SLIDE 7

Compiling Polymorphism

  • Uniform representation

✦ Examples: Java, OCaml ✦ All polymorphic values

represented by pointers

✦ For OCaml: machine ints

also work

✦ Not performant

slide-8
SLIDE 8
  • Uniform representation
  • Monomorphization

✦ Examples: C++, MLton, Rust ✦ Polymorphic definitions are

instantiated

✦ No fancy types ✦ Separate compilation is hard

Compiling Polymorphism

slide-9
SLIDE 9
  • Uniform representation
  • Monomorphization
  • Run-time specialization

✦ C#: On-demand instantiation ✦ TIL compiler for ML: runtime

type analysis

✦ No type erasure

Compiling Polymorphism

slide-10
SLIDE 10
  • Uniform representation
  • Monomorphization
  • Run-time specialization
  • “Kinds are calling

conventions” Compiling Polymorphism

✦ Cyclone, TALT, Haskell/GHC

slide-11
SLIDE 11

Kinds are calling conventions

choose :: Bool ! a ! a ! a let b = ... in choose b 3 4 let b = ... in choose b 3# 4#

slide-12
SLIDE 12

Kinds are calling conventions

choose :: Bool ! a ! a ! a ∀ (a :: Type). 3 :: Int Int :: Type 3# :: Int# Int# :: # let b = ... in choose b 3# 4#

slide-13
SLIDE 13

Problems lurk

  • What is the kind of (!)?
  • Old solution: sub-kinding
  • But that causes more problems

notType ! Type ! Type Type # OpenKind

slide-14
SLIDE 14

Our innovation: Levity Polymorphism

slide-15
SLIDE 15

Levity Polymorphism

TYPE :: Rep ! Type data Rep = LiftedRep
 | IntRep
 | DoubleRep
 | ... type Type = TYPE LiftedRep

slide-16
SLIDE 16

Examples

Int :: Type Int :: TYPE LiftedRep Int# :: TYPE IntRep Double# :: TYPE DoubleRep Maybe :: Type Type !

slide-17
SLIDE 17

Examples

(+) :: ∀ (r :: Rep). ∀ (a :: TYPE r). Num a ⇒ a ! a ! a 3 + 4 3# + 4# With levity polymorphism, performant code is easier to write.

slide-18
SLIDE 18

Examples

choose :: ∀ (r :: Rep). ∀ (a :: TYPE r). Bool ! a ! a ! a choose True t _ = t choose False _ f = f

Counter-

This cannot be compiled. choose has to store its arguments.

slide-19
SLIDE 19

Restrictions Never store a levity- polymorphic value

➡ No levity-polymorphic variables ➡ No levity-polymorphic function

arguments --

slide-20
SLIDE 20

What can have L.P.?

($) :: ∀ (r :: Rep). ∀ (a :: Type) (b :: TYPE r). (a ! b) ! a ! b f $ x = f x

slide-21
SLIDE 21

What can have L.P.?

error :: ∀ (r :: Rep) (a :: TYPE r). String ! a error msg = <throw exception>

slide-22
SLIDE 22

What can have L.P.? class methods

class Num (a :: TYPE r) where (+) :: a ! a ! a (-) :: a ! a ! a (*) :: a ! a ! a ...

34 of 76 standard classes can be generalized

slide-23
SLIDE 23

What can have L.P.?

(!) :: ∀ (r1 :: Rep) (r2 :: Rep). TYPE r1 ! TYPE r2 ! Type

slide-24
SLIDE 24

Kind-directed compilation x = f y

How does GHC compile this function call? Lazily or strictly? It depends on the kind

  • f the type of y.

The proof is in the paper.

slide-25
SLIDE 25

Levity Polymorphism

Lazy types are lifted. (They have an extra element.) Levity polymorphism permits polymorphism over laziness, hence "liftedness". Not liftedness, but levity.

slide-26
SLIDE 26

With levity polymorphism, performant code is easier to write.

slide-27
SLIDE 27

Levity Polymorphism

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu

Tuesday, 20 June 2017 PLDI Barcelona, Spain

Simon Peyton Jones Microsoft Research Cambridge simonpj@microsoft.com