Pickler Combinators Explained Benedikt Grundmann - - PowerPoint PPT Presentation

pickler combinators explained
SMART_READER_LITE
LIVE PREVIEW

Pickler Combinators Explained Benedikt Grundmann - - PowerPoint PPT Presentation

Motivation Pickler Combinator Sharing The End Pickler Combinators Explained Benedikt Grundmann benedikt-grundmann@web.de Software Engineering Chair (Prof. Zeller) Saarland University Programming Systems Lab (Prof. Smolka) Saarland


slide-1
SLIDE 1

Motivation Pickler Combinator Sharing The End

Pickler Combinators – Explained

Benedikt Grundmann benedikt-grundmann@web.de

Software Engineering Chair (Prof. Zeller) Saarland University Programming Systems Lab (Prof. Smolka) Saarland University

Advanced Functional Programming – WS 2005/2006

slide-2
SLIDE 2

Motivation Pickler Combinator Sharing The End

Martin Elsman. Type-specialized serialization with sharing. In Sixth Symposium on Trends in Functional Programming (TFP’05), September 2005. Andrew Kennedy. Pickler combinators.

  • J. Funct. Program., 14(6):727–739, 2004.

Guido Tack, Leif Kornstaedt, and Gert Smolka. Generic pickling and minimization. Electronic Notes in Theoretical Computer Science, 148(2):79–103, March 2006.

slide-3
SLIDE 3

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-4
SLIDE 4

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-5
SLIDE 5

Motivation Pickler Combinator Sharing The End

Example

  • primitive Spellchecker application
slide-6
SLIDE 6

Motivation Pickler Combinator Sharing The End

Example

  • primitive Spellchecker application
  • words stored in binary search tree
slide-7
SLIDE 7

Motivation Pickler Combinator Sharing The End

Example

  • primitive Spellchecker application
  • words stored in binary search tree

Example

type Word = String data Tree = N (Word, Tree, Tree) | E

slide-8
SLIDE 8

Motivation Pickler Combinator Sharing The End

Problem

How to store a tree? createFile :: String -> String -> IO () loadFile :: String -> IO String

slide-9
SLIDE 9

Motivation Pickler Combinator Sharing The End

Problem

How to store a tree? createFile :: String -> String -> IO () loadFile :: String -> IO String Therefore we need: toString :: Tree -> String fromString :: String -> Tree

slide-10
SLIDE 10

Motivation Pickler Combinator Sharing The End

Writing those by hand is NO fun

  • Synchronize
  • Type declaration
  • toString implementation
  • fromString implementation
slide-11
SLIDE 11

Motivation Pickler Combinator Sharing The End

Writing those by hand is NO fun

  • Synchronize
  • Type declaration
  • toString implementation
  • fromString implementation
  • extensibility?
slide-12
SLIDE 12

Motivation Pickler Combinator Sharing The End

Writing those by hand is NO fun

  • Synchronize
  • Type declaration
  • toString implementation
  • fromString implementation
  • extensibility?
  • Implementation is not declarative
slide-13
SLIDE 13

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-14
SLIDE 14

Motivation Pickler Combinator Sharing The End

Solution: Pickling Combinators

word :: PU String word = string tree :: PU Tree tree = alt tag [ wrap (Node, \(Node d) -> d) (triple word tree tree) , lift E ] where tag (N _) = 0 tag E = 1 str = pickle tree (N ("foo", E, E)) N ("foo", E, E) = unpickle tree str

slide-15
SLIDE 15

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-16
SLIDE 16

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
slide-17
SLIDE 17

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
  • We know what a combinator library is
slide-18
SLIDE 18

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
  • We know what a combinator library is
  • Idea: Primitive functions + Combinator Functions =

Powerful Functions

slide-19
SLIDE 19

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
  • We know what a combinator library is
  • Idea: Primitive functions + Combinator Functions =

Powerful Functions

  • “Higher-Order Functions for Parsing”
slide-20
SLIDE 20

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
  • We know what a combinator library is
  • Idea: Primitive functions + Combinator Functions =

Powerful Functions

  • “Higher-Order Functions for Parsing”
  • “Embedding an interpreted language using higher-order

functions and types”

slide-21
SLIDE 21

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator Library?

  • A combinator library to create picklers
  • We know what a combinator library is
  • Idea: Primitive functions + Combinator Functions =

Powerful Functions

  • “Higher-Order Functions for Parsing”
  • “Embedding an interpreted language using higher-order

functions and types”

  • So what is a pickler?
slide-22
SLIDE 22

Motivation Pickler Combinator Sharing The End

What is a Pickler?

A pair of a pickling and an unpickling function for values of a certain type.

slide-23
SLIDE 23

Motivation Pickler Combinator Sharing The End

What is a Pickler?

A pair of a pickling and an unpickling function for values of a certain type.

Definition (Pickling)

Value → Byte*

slide-24
SLIDE 24

Motivation Pickler Combinator Sharing The End

What is a Pickler?

A pair of a pickling and an unpickling function for values of a certain type.

Definition (Pickling)

Value → Byte*

Definition (Unpickling)

Byte* → Value

slide-25
SLIDE 25

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator?

It is a pickler...

Definition (Pickling)

Value → Byte*

Definition (Unpickling)

Byte* → Value

slide-26
SLIDE 26

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator?

It is a pickler extended to be composable.

Definition (Pickling)

Value → Byte*

Definition (Unpickling)

Byte* → Value

slide-27
SLIDE 27

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator?

It is a pickler extended to be composable.

Definition (Pickling)

Value × Byte* → Byte*

Definition (Unpickling)

Byte* → Value

slide-28
SLIDE 28

Motivation Pickler Combinator Sharing The End

What is a Pickler Combinator?

It is a pickler extended to be composable.

Definition (Pickling)

Value × Byte* → Byte*

Definition (Unpickling)

Byte* → Value × Byte*

slide-29
SLIDE 29

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-30
SLIDE 30

Motivation Pickler Combinator Sharing The End

API

data PU α

slide-31
SLIDE 31

Motivation Pickler Combinator Sharing The End

API

data PU α = PU { appP :: (a, [Char]) -> [Char] , appU :: [Char] -> (a, [Char]) }

slide-32
SLIDE 32

Motivation Pickler Combinator Sharing The End

API

data PU α pickle :: PU α -> α -> String unpickle :: PU α -> String -> α

slide-33
SLIDE 33

Motivation Pickler Combinator Sharing The End

API

data PU α pickle :: PU α -> α -> String unpickle :: PU α -> String -> α

Example

True = unpickle bool (pickle bool True)

slide-34
SLIDE 34

Motivation Pickler Combinator Sharing The End

API

data PU α pickle :: PU α -> α -> String unpickle :: PU α -> String -> α

Standard types

unit :: PU () bool :: PU Bool char :: PU Char string :: PU String nat :: PU Int zeroTo :: Int -> PU Int

slide-35
SLIDE 35

Motivation Pickler Combinator Sharing The End

Basic Picklers & Combinators

  • Constant values

lift :: α -> PU α lift x = PU snd (\s -> (x, s)) unit = lift ()

  • Small numbers

smallInt :: PU Int smallInt = PU (\(c,s) -> (toEnum c : s)) (\(c,s) -> (fromEnum c, s))

slide-36
SLIDE 36

Motivation Pickler Combinator Sharing The End

Sequential Composition

sequ :: (β->α) -> PU α -> (α->PU β) -> PU β

  • pickles A followed by B
  • A can be created from B
  • pickled representation of B can depend on A

Example

pair :: PU α -> PU β -> PU (α, β) pair pa pb = sequ fst pa (\ a -> sequ snd pb (\ b -> lift (a, b)))

slide-37
SLIDE 37

Motivation Pickler Combinator Sharing The End

More Combinators

  • map on picklers

wrap :: (α-> β, β -> α) -> PU α -> PU β bool = wrap (toEnum,fromEnum) (zeroTo 1)

  • wrap & recursion

zeroTo :: Int -> PU Int zeroTo 0 = lift 0 zeroTo n = wrap (\(h,l) -> h * 256 + l, (‘divMod‘ 256)) (pair (zeroTo (n ‘div‘ 256)) smallInt)

slide-38
SLIDE 38

Motivation Pickler Combinator Sharing The End

Wrapping datatypes

alt :: (α -> Int) -> [PU α] -> PU α wrap :: (α -> β, β -> α) -> PU α -> PU β

Example

tree = alt tag [ wrap (N, \(N d) -> d) (triple word tree tree) , lift E ] where tag (N _) = 0 tag E = 1

slide-39
SLIDE 39

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-40
SLIDE 40

Motivation Pickler Combinator Sharing The End

Sharing

xs d b g a c f h

  • We want sharing for

efficiency

  • Remember “Fun with

binary heap trees”

slide-41
SLIDE 41

Motivation Pickler Combinator Sharing The End

Sharing

xs d ys d’ b g a c f h g’ f’ e

  • We want sharing for

efficiency

  • Remember “Fun with

binary heap trees”

  • Example ys = insert (e, xs)
slide-42
SLIDE 42

Motivation Pickler Combinator Sharing The End

Sharing

xs d ys d’ b g a c f h b’ g’ a’ c’ f’ h’ e

  • We want sharing for

efficiency

  • Remember “Fun with

binary heap trees”

  • Example ys = insert (e, xs)
  • (xs,ys) = unpickle

(pickle (xs, ys))

slide-43
SLIDE 43

Motivation Pickler Combinator Sharing The End

Sharing

xs d ys d’ b g a c f h b’ g’ a’ c’ f’ h’ e

  • We want sharing for

efficiency

  • Remember “Fun with

binary heap trees”

  • Example ys = insert (e, xs)
  • (xs,ys) = unpickle

(pickle (xs, ys))

  • This is BAD!!
slide-44
SLIDE 44

Motivation Pickler Combinator Sharing The End

Sharing

xs d ys d’ b g a c f h g’ f’ e

  • We want sharing for

efficiency

  • Remember “Fun with

binary heap trees”

  • Example ys = insert (e, xs)
  • (xs,ys) = unpickle

(pickle (xs, ys))

  • This is BAD!!
  • We want sharing!
slide-45
SLIDE 45

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-46
SLIDE 46

Motivation Pickler Combinator Sharing The End

Sharing Implementation Idea

On pickling

  • Remember all values we pickled
  • If we want to pickle it again store a reference

On unpickling

  • Remember unpickled values
  • On a reference return corresponding value

⇒ We need a dictionary!

slide-47
SLIDE 47

Motivation Pickler Combinator Sharing The End

Sharing Pickler Combinator

Need to memorize pickled values

Definition (Pickling)

Value × Byte* → Byte* Need to memorize unpickled values

Definition (Unpickling)

Byte* → Value

slide-48
SLIDE 48

Motivation Pickler Combinator Sharing The End

Sharing Pickler Combinator

Need to memorize pickled values

Definition (Pickling)

Value × Byte* × Dict → Byte* × Dict Need to memorize unpickled values

Definition (Unpickling)

Byte* → Value

slide-49
SLIDE 49

Motivation Pickler Combinator Sharing The End

Sharing Pickler Combinator

Need to memorize pickled values

Definition (Pickling)

Value × Byte* × Dict → Byte* × Dict Need to memorize unpickled values

Definition (Unpickling)

Byte* × Dict → Value × Dict

slide-50
SLIDE 50

Motivation Pickler Combinator Sharing The End

Sharing continued

share :: Eq α => PU α [α] -> PU α [α] share p = memorizing logic as outlined before tree = share $ alt tag ...

  • Sharing limited to values of one type
slide-51
SLIDE 51

Motivation Pickler Combinator Sharing The End

Sharing continued

share :: Eq α => PU α [α] -> PU α [α] share p = memorizing logic as outlined before tree = share $ alt tag ...

  • Sharing limited to values of one type
  • Normal equality test maximizes sharing
slide-52
SLIDE 52

Motivation Pickler Combinator Sharing The End

Sharing continued

share :: Eq α => PU α [α] -> PU α [α] share p = memorizing logic as outlined before tree = share $ alt tag ...

  • Sharing limited to values of one type
  • Normal equality test maximizes sharing
  • Cyclic values
slide-53
SLIDE 53

Motivation Pickler Combinator Sharing The End

Sharing continued

share :: Eq α => PU α [α] -> PU α [α] share p = memorizing logic as outlined before tree = share $ alt tag ...

  • Sharing limited to values of one type
  • Normal equality test maximizes sharing
  • Cyclic values
  • equality test diverges
slide-54
SLIDE 54

Motivation Pickler Combinator Sharing The End

Sharing continued

share :: Eq α => PU α [α] -> PU α [α] share p = memorizing logic as outlined before tree = share $ alt tag ...

  • Sharing limited to values of one type
  • Normal equality test maximizes sharing
  • Cyclic values
  • equality test diverges
  • pointer based test would work
slide-55
SLIDE 55

Motivation Pickler Combinator Sharing The End

Outline

Motivation Spellchecker Solution preview Pickler Combinator Introduction API & Implementation Sharing Problem Solution The End Wrap-Up Pickler Combinator

slide-56
SLIDE 56

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use

Contra

slide-57
SLIDE 57

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved

Contra

slide-58
SLIDE 58

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions

Contra

slide-59
SLIDE 59

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype

Contra

slide-60
SLIDE 60

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype
  • Extensible

Contra

slide-61
SLIDE 61

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype
  • Extensible
  • Language implementation independent

Contra

slide-62
SLIDE 62

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype
  • Extensible
  • Language implementation independent

Contra

  • either no cycles
slide-63
SLIDE 63

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype
  • Extensible
  • Language implementation independent

Contra

  • either no cycles
  • or no minimization
slide-64
SLIDE 64

Motivation Pickler Combinator Sharing The End

Pickler Combinator

Pro

  • Declarative syntax – easy to use
  • Synchronization problem solved
  • only one code for both directions
  • Type checker checks consistency of pickler and datatype
  • Extensible
  • Language implementation independent

Contra

  • either no cycles
  • or no minimization
  • sharing only values of one type
slide-65
SLIDE 65

More Samples

list :: PU α -> PU [α] pair :: PU α -> PU β -> PU (α, β) triple :: PU α -> PU β -> PU γ -> PU (α, β, γ) maybe :: PU α -> PU (Maybe α)

slide-66
SLIDE 66

More Samples

list :: PU α -> PU [α] pair :: PU α -> PU β -> PU (α, β) triple :: PU α -> PU β -> PU γ -> PU (α, β, γ) maybe :: PU α -> PU (Maybe α)

Example

type URL = (String, String, Maybe Int, String) type Bookmark = (String, URL) string = list char url = quad string string (maybe nat) string bookmark = pair string url