Deriving Generic Functions by Example Neil Mitchell - - PowerPoint PPT Presentation

deriving generic functions by example
SMART_READER_LITE
LIVE PREVIEW

Deriving Generic Functions by Example Neil Mitchell - - PowerPoint PPT Presentation

Deriving Generic Functions by Example Neil Mitchell www.cs.york.ac.uk/~ndm/derive Generic Functions Operates on many data types Think of equality Comparing two integers, booleans, lists, trees Usually, must give each version


slide-1
SLIDE 1

Deriving Generic Functions by Example

Neil Mitchell

www.cs.york.ac.uk/~ndm/derive

slide-2
SLIDE 2

“Generic” Functions

Operates on many data types Think of equality

Comparing two integers, booleans, lists, trees

Usually, must give each version separately

True in Ada, Java, Haskell...

But usually they follow a pattern!

slide-3
SLIDE 3

A Haskell data type

dat a dat a Li st = Cons I nt Li st | Ni l Cons 1 ( Cons 2 ( Cons 3 Ni l ) ) 1 2 3

slide-4
SLIDE 4

Generic Equality on Lists

i nst ance i nst ance Eq Li st wher e wher e Cons a b ≡ Cons x y = a ≡ x ∧ b ≡ y Ni l ≡ Ni l = Tr ue _ ≡ _ = Fal se

1.

They have the same constructor

2.

All fields are equal

slide-5
SLIDE 5

Generic Equality on Trees

dat a dat a Tr ee = B Tr ee I nt Tr ee | L

i nst ance i nst ance Eq Tr ee wher e wher e B a b c ≡ B x y z = a≡x ∧ b≡y ∧ c≡z L ≡ L = Tr ue _ ≡ _ = Fal se

slide-6
SLIDE 6

Generic Equality on anything?

Always follows the same simple pattern But highly dependent on the data type

If data type changes, updates required Could “miss” a field doing it by hand

Solution: Have it automatically generated

The DrIFT and Derive tools allows this

slide-7
SLIDE 7

The Problem

Need to state to the computer the

relationship between data and code

Must be 100% precise

I explained mainly through examples Requires learning an API, working at a

meta-level, testing etc.

slide-8
SLIDE 8

Specifying the Relationship

Dat aType → St r i ng

eq' dat = si m pl e_i nst ance " Eq" dat [ f unN " ==" body] wher e wher e body = m ap r ul e ( dat aCt or s dat ) ++ [ def cl ause 2 f al se] r ul e ct or = scl ause [ ct p ct or ' a' , ct p ct or ' b' ] ( and_ ( zi pW i t h ( ==: ) ( ct v ct or ' a' ) ( ct v ct or ' b' ) ) )

YUK!

slide-9
SLIDE 9

Generic Functions by Example

What if we provide only an example

The computer can infer the rules

Uses concepts the user understands Guaranteed to work on at least 1 example Guaranteed to be type correct Quicker to write

slide-10
SLIDE 10

Giving an example

Needs to be on an interesting data type

Complex enough to have variety

dat a dat a Dat aNam e = Fi r st | Second Any | Thi r d Any Any | Four t h Any Any

slide-11
SLIDE 11

And the example…

i nst ance i nst ance Eq Dat aNam e wher e wher e Fi r st ≡ Fi r st = Tr ue Second x 1 ≡ Second y 1 = x 1 ≡ y 1 ∧ Tr ue Thi r d x 1 x 2 ≡ Thi r d y 1 y 2 = x 1 ≡ y 1 ∧ x 2 ≡ y 2 ∧ Tr ue Four t h x 1 x 2 ≡ Four t h y 1 y 2 = x 1 ≡ y 1 ∧ x 2 ≡ y 2 ∧ Tr ue _ ≡ _ = Fal se

Now y1 and y2 instead of x and y Redundant True at the end

slide-12
SLIDE 12

Notation for Substitution

York ⇒ Hello [#] Hello York YDS ⇒ [date] 2007/10/26

Parameter Substitute

Tom ⇒ Hello [#] Hello Tom

slide-13
SLIDE 13

Assign Parameters

3rd constructor ⇒ [name] 1 ⇒ y[#] 2 ⇒ y[#]

Thi r d y 1 y 2

Idea: Move from the specific example, to a generalised version

slide-14
SLIDE 14

Group lists (MAP)

3rd constructor ⇒ [name] 1 ⇒ y[#] 2 ⇒ y[#]

Thi r d y 1 y 2

2 ⇒ MAP 1..# y[#] Only if:

  • 1. Consecutive parameters
  • 2. Same generator
slide-15
SLIDE 15

The meaning of MAP

  • 2 ⇒ MAP 1..# y[#]
  • MAP 1..2 y[#]
  • (1 ⇒ y[#]) (2 ⇒ y[#])
  • y1 y2
slide-16
SLIDE 16

Generalise Numbers

3rd constructor ⇒ [name]

Thi r d y 1 y 2

2 ⇒ MAP 1..# y[#] 3rd constructor ⇒ MAP 1..arity y[#]

slide-17
SLIDE 17

Combine elements

3rd constructor ⇒ [name]

Thi r d y 1 y 2

3rd constructor ⇒ MAP 1..arity y[#] 3rd constructor ⇒ [name] (MAP 1..arity y[#])

slide-18
SLIDE 18

Applying to other constructors

Fi r st Fi r st Second Second y1 Thi r d Thi r d y1 y 2 Four t h Four t h y1 y 2

3rd constructor ⇒ [name] (MAP 1..arity y[#])

slide-19
SLIDE 19

The Complete Generalisation

i nst ance i nst ance Eq [ dat anam e] wher e wher e [ M

AP ct or s (

( [ nam e] [ M

AP 1. . ar i t y ( x[ #] ) ) ≡

( [ nam e] [ M

AP 1. . ar i t y ( y[ #] ) ) =

[ FO

LDR ( ∧) Tr ue

[ M

AP 1. . ar i t y ( x[ #] ≡ y[ #] ) ]

] ) ] _ ≡ _ = Fal se

slide-20
SLIDE 20

Limitations: Non-inductive

Example: Binary serialisation Write out a tag (which constructor) then

the fields

If only one constructor, no need for a tag

There is no general pattern

slide-21
SLIDE 21

Limitations: Type Based

Example: Monoid The instance for a Monoid is based on the

types of the fields

Equal types have one value, different another

The DataName type does not have

different types

slide-22
SLIDE 22

Limitations: Records

Example: Show dat a Pai r = Pai r { f st : : I nt , snd: : I nt } show ( Pai r 1 2) = “ Pai r { f st =1, snd=2} ” Show includes the record field names DataName does not have record fields

slide-23
SLIDE 23

Success Rate

Success Failure

15 9

  • Eq
  • Ord
  • Data
  • Serial
  • Arbitrary
  • Enum
slide-24
SLIDE 24

Future Work

Extend the data type with more variety

Allows more classes to be specified But more work to specify each class

New uses for the information

Can derive classes at runtime

Implement in other languages (Java?)

slide-25
SLIDE 25

Conclusion

Writing generic functions is cumbersome Writing generic relationships is hard Writing a single example is much easier

Works well in practice Enables new contributors

slide-26
SLIDE 26

Example 2

x 1 ≡ y 1 ∧ x 2 ≡ y 2 ∧ Tr ue

⇒ True 1 ⇒ x[#] 1 ⇒ y[#] 1 ⇒ x[#] ≡ y[#] 2 ⇒ x[#] ≡ y[#]

slide-27
SLIDE 27

Generalising to a FOLDR

x 1 ≡ y 1 ∧ x 2 ≡ y 2 ∧ Tr ue

⇒ True 1 ⇒ x[#] ≡ y[#] 2 ⇒ x[#] ≡ y[#] FOLDR (∧) True (1 ⇒ x[#] ≡ y[#], 2 ⇒ x[#] ≡ y[#])

slide-28
SLIDE 28

Generalising to a MAP

x 1 ≡ y 1 ∧ x 2 ≡ y 2 ∧ Tr ue

FOLDR (∧) True (1 ⇒ x[#] ≡ y[#], 2 ⇒ x[#] ≡ y[#]) 2 ⇒ FOLDR (∧) True (MAP 1..# (x[#] ≡ y[#]))