Constrained Type Families J. Garrett Morris Richard A. Eisenberg - - PowerPoint PPT Presentation

constrained type families
SMART_READER_LITE
LIVE PREVIEW

Constrained Type Families J. Garrett Morris Richard A. Eisenberg - - PowerPoint PPT Presentation

Constrained Type Families J. Garrett Morris Richard A. Eisenberg University of Edinburgh Bryn Mawr College University of Kansas rae@cs.brynmawr.edu garrett@ittc.ku.edu Wednesday, 6 September 2017 ICFP Oxford, UK Two main contributions:


slide-1
SLIDE 1

Constrained Type Families

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu Wednesday, 6 September 2017 ICFP Oxford, UK

  • J. Garrett Morris

University of Edinburgh University of Kansas garrett@ittc.ku.edu

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4

Two main contributions:

1. Discovering the problem: GHC assumes all type families are total. 2. First type safety proof with non-termination and non- linear patterns.

  • .
slide-5
SLIDE 5

But first: An introduction to type families

slide-6
SLIDE 6

In Haskell, type families = type functions

.....C ....

slide-7
SLIDE 7

class Collects c where type Elem c empty :: c insert :: Elem c ! c ! c

slide-8
SLIDE 8

instance Collects [a] where type Elem [a] = a ... instance Collects Word where type Elem Word = Bool ...

=..

slide-9
SLIDE 9

type family Elem c class Collects c where empty :: c insert :: Elem c ! c ! c

..

slide-10
SLIDE 10

data Z data S n type family Pred n type instance Pred (S n) = n type instance Pred Z = Z

.. ..

slide-11
SLIDE 11

data Z data S n type family Pred n where Pred (S n) = n Pred n = n

.

slide-12
SLIDE 12

(originally suggested by Chakravarty et al., ICFP ’05)

Our new old idea: Constrained Type Families

slide-13
SLIDE 13

A ground type has no type families. A total type family, when applied to ground types, always equals some ground type. Definitions

slide-14
SLIDE 14

Constrained Type Families

  • All partial type families are

associated

  • Class constraint necessary to use

an associated type family

slide-15
SLIDE 15

Example

thwack :: thwack = ... F a type family F a ! Maybe a

slide-16
SLIDE 16

Example

thwack :: thwack = ... F a type F a ! Maybe a class CF a where CF a ⇒

slide-17
SLIDE 17

The Totality Trap

slide-18
SLIDE 18

Wat #1 ..!

Ok, modules loaded: Wat. x = fst (5, ⊥ :: F Int) F a type family

slide-19
SLIDE 19

Wat #1 ..!

x = fst (5, ⊥ :: F Int) error: No instance for (CF Int) F a type class CF a where

slide-20
SLIDE 20

Wat #2

type family EqT a b where EqT a a = Char EqT a b = Bool Wat.hs: error: ... f :: a ! EqT a (Maybe a) f _ = False

  • .!

.!


  • C


..!

slide-21
SLIDE 21

Wat #2

type family EqT a b where EqT a a = Char EqT a b = Bool f :: a ! EqT a (Maybe a) f _ = False Ok, modules loaded: NoWat.

slide-22
SLIDE 22

Why Wat #2?

type family Maybes a type instance Maybes a = Maybe (Maybes a) f :: a ! EqT a (Maybe a)

C.↦,.$ .',..!

slide-23
SLIDE 23

Wat #3

justs = Just justs Wat.hs: error:

  • Cannot construct the


infinite type:
 a ~ Maybe a

  • .

type family Maybes a type instance Maybes a = Maybe (Maybes a)

slide-24
SLIDE 24

Red herring: “Just ban Maybes!” Sometimes we need loopy type families.

slide-25
SLIDE 25

Wat #3

justs = Just justs Wat.hs: error:

  • Cannot construct the


infinite type:
 a ~ Maybe a

  • .

instance CMaybes a ⇒ CMaybes a where type Maybes a = Maybe (Maybes a)

slide-26
SLIDE 26

The fundamental problem: GHC today assumes all type families are total. Constrained type families fix this.

slide-27
SLIDE 27

Why does this fix the wats? The class constraint restricts the type family domain.

slide-28
SLIDE 28

First known proof

  • f consistency with

non-linear patterns and non-termination.

slide-29
SLIDE 29

Wrinkle: Total type families Total type families need not be associated. .

slide-30
SLIDE 30

Wrinkle: Backward compatibility

  • Infer constraints
  • New feature:


Closed type classes

  • Details in paper
slide-31
SLIDE 31

Open question: Forward compatibility

  • Dependent types
  • Termination checking
  • Is Girard's paradox encodable?
slide-32
SLIDE 32

Constrained type families:

  • let us escape the totality trap
  • prevent the usage of bogus types
  • make closed type families more powerful
  • simplify injective type families
  • remove an unnecessary feature
  • simplify the metatheory
  • allow us to prove type safety
slide-33
SLIDE 33

Constrained Type Families

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu Wednesday, 6 September 2017 ICFP Oxford, UK

  • J. Garrett Morris

University of Edinburgh University of Kansas garrett@ittc.ku.edu