Mathematical Structures for Data Types with Restricted - - PowerPoint PPT Presentation

mathematical structures for data types with restricted
SMART_READER_LITE
LIVE PREVIEW

Mathematical Structures for Data Types with Restricted - - PowerPoint PPT Presentation

Mathematical Structures for Data Types with Restricted Parametricity Trends in Functional Programming, 12-14th June, St. Andrews Dominic Orchard Alan Mycroft Mathematically structured programming e.g. monoids, groups, functors , monads ,


slide-1
SLIDE 1

Mathematical Structures for Data Types with Restricted Parametricity

Dominic Orchard Alan Mycroft

Trends in Functional Programming, 12-14th June, St. Andrews

slide-2
SLIDE 2

Mathematically structured programming

  • e.g. monoids, groups, functors, monads,

comonads, monoidal functors (idioms), etc.

  • Design pattern
  • Abstraction (detail hiding)
  • Generalisation (e.g. for all functors...)
  • Eases writing, reading, and reasoning
slide-3
SLIDE 3

Parametric data types in Haskell as functors

class Functor f where fmap :: (a → b) → f a → f b

  • fmap is polymorphic in a, b i.e. ∀a,b
  • f is a parametric data type, meaning

∀a . a ∈ Type ⇒ f a ∈ Type

instance [ ] where fmap = map

slide-4
SLIDE 4

... but some data types are restricted in their parametricity

  • e.g. sets implemented by balanced binary trees
  • e.g. unboxed arrays of primitive (fixed size) types
  • Efficient data types often restricted

i.e. ∀a . a ∈ Type ∧ a is ordered ⇒ Set a ∈ Type i.e. ∀a . a ∈ Type ∧ a is primitive ⇒ UArray a ∈ Type

slide-5
SLIDE 5

class Functor f where fmap :: (a → b) → f a → f b

Is Set a functor?

Set.map :: (Ord a, Ord b) ⇒ (a → b) → Set a → Set b

No instances for (Ord b, Ord a) arising from a use of `Set.map' In the expression: Set.map In an equation for `fmap': fmap = Set.map In the instance declaration for `Functor Set'

instance Functor Set where fmap = Set.map fmap has no constraints i.e. polymorphic; Set.map is constrained i.e. restricted polymorphic class Ord a where ...

( )

slide-6
SLIDE 6

This paper...

  • Data types with restricted parametricity:
  • usually real-world, efficient data types, but
  • do not fit the language abstractions used;
  • do not fit the mathematics used.
  • This paper, for functors, monads, and comonads:
  • fixes the mathematics;
  • shows a language approach.
  • Essential insight:

restricted parametricity = subcategories

slide-7
SLIDE 7

Parametric polymorphism

fmap :: ∀a,b . (a → b) → (f a → f b)

  • Uniform behaviour at any parameter type

∀α . α

  • Universal quantification
slide-8
SLIDE 8

Set.map :: ∀a,b . (Ord a, Ord b) ⇒ (a → b) → (f a → f b)

Restricted parametric polymorphism

  • Quantification over a subset of types

∀α . α ∈ A ⇒ α

  • Described by ad-hoc polymorphism
  • Behaviour may vary at parameter type
  • In Haskell, type classes and class constraints
slide-9
SLIDE 9

What is the categorical interpretation of restricted parametricity?

slide-10
SLIDE 10

Categorical interpretation of programs

  • Programs make definitions in Hask
  • objects = types

f g

g ◦ f

idInt idBool idString

  • morphisms (maps between objects) = functions

Int Bool String

....

slide-11
SLIDE 11

Functors

  • Map between objects & morphisms of categories

f g

g ◦ f

A C B

C F(A) F(C) F(B) F(f) F(g)

F(g ◦ f)

D F

slide-12
SLIDE 12

data F a = ...

  • Data type:

∴ Functor describes endofunctors on Hask

∀a,b . a → b

Hask morphisms

∀a . a

Hask objects

Categorical interpretation: Functor class

  • Instance of Functor

class Functor f where fmap :: (a → b) → f a → f b

  • object-mapping on Hask i.e. F : |Hask| → |Hask|
  • morphism-mapping on Hask via fmap

(from a category back to the same category)

slide-13
SLIDE 13

f g

g ◦ f

A C B

Hask Hask [a] [A] [C] [B]

map (g ◦ f)

map f map g

Categorical interpretation: Functor class

slide-14
SLIDE 14

Categorical interpretation of class constraints

  • Instances of a type class define a subset of all types

class Eq a where (==) :: a → a → Bool instance Eq Int where ... instance Eq Bool where ...

i.e. Eq ⊆ |Hask|

  • A type class C defines a subcategory of Hask
  • objects: ∀a . C a ⇒ a
  • morphisms: ∀a,b . (C a, C b) ⇒ a → b
slide-15
SLIDE 15

Set is a functor

Set.map :: (Ord a, Ord b) ⇒ (a → b) → Set a → Set b

∴ Set is a functor from Ord-subcategory to Hask

Set : Ord → Hask

  • but Functor describes endofunctors on Hask

i.e. F : Hask → Hask

  • Type error manifests the mathematical mismatch
  • How then to describe this in Haskell?

maps morphisms of Ord-subcategory

slide-16
SLIDE 16

class ExoFunctor f where constraint SubCat f a fmap :: (SubCat f a, SubCat f b) ⇒ (a → b) → f a → f b instance ExoFunctor Set where constraint SubCat Set a = Ord a fmap = Set.map

Language solution: use constraint families*

  • Allows constraint per instance of the Functor class

*[Orchard, Schrijvers, 2010]

slide-17
SLIDE 17

class ExoFunctor f where type SubCat f a :: Constraint fmap :: (SubCat f a, SubCat f b) ⇒ (a → b) → f a → f b instance ExoFunctor Set where type SubCat Set a = Ord a fmap = Set.map

Language solution (current): constraint-kinded type families*

  • Allows constraint per instance of the Functor class

*[implemented by Bolingbroke, 2011, http://blog.omega-prime.co.uk/?p=127]

slide-18
SLIDE 18

Generalisation...

class ExoFunctor f where type SubCat f a b :: Constraint fmap :: (SubCat f a b) ⇒ (a → b) → f a → f b

e.g. ∀a,b . (Show a) ⇒ a → b

  • Allows more interesting structures
  • Pre-proceeding: (non-full) subcategory of Hask with all

Hask objects but only morphisms from Show objects

  • Actually slightly more subtle (see post-proceeding)
slide-19
SLIDE 19

Conclusion

  • Slogan: restricted parametricity = subcategories
  • Extended to relative monads (Altenkirch et. al) and

relative comonads (with underlying non-endofunctor)

  • All details in paper
  • Provides elegant mathematical structuring to real-world,

efficient data types

Thank you. http://dorchard.co.uk

slide-20
SLIDE 20
slide-21
SLIDE 21

Additional slides

slide-22
SLIDE 22

Haskell

eq :: Eq t ⇒ t → t → Bool

SML

eq : ‘‘t → ‘‘t → bool

Java

‹T extends Comparable› boolean eq(T x, T y)

∀α . α ∈ A ⇒ α

  • Restricted quantification
  • Behaviour may vary at parameter type

Ad-hoc polymorphism