modular masses extensible genericschapter 1 extensible and modular generics for the masses bruno c....

39
Chapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function that is defined on the structure of data types: with a single definition, we obtain a function that works for many data types. In contrast, an ad-hoc p olymorphic function requires a separate im- plementation for each data type. Previous work b y Hinze on lightweight generic programming has introduced techniques that allow the definition of generic func- tions directly in Haskell. A severe drawback of these approaches is that generic functions, once defined, cannot be extended with ad-hoc behaviour for new data types, precluding the design of an extensible and modular generic programming

Upload: others

Post on 27-Sep-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

Chapte

r1

Exte

nsib

lean

dM

odul

arGe

neric

sfo

rthe

Mass

esB

run

oC

.d.

S.

Olive

ira1,

Ralf

Hinze

2,A

nd

res

L o¨h2

Abs

trac

t:A

gene

ricfu

nctio

nis

afu

nctio

nth

atis

defin

edon

the

stru

ctur

eof

data

type

s:w

itha

sing

lede

finiti

on,

we

obta

ina

func

tion

that

wor

ksfo

rm

any

data

type

s.In

cont

rast

,an

ad-h

ocp

olym

orph

icfu

nctio

nre

quire

sa

sepa

rate

im-

plem

enta

tion

for

each

data

type

.P

revi

ous

wor

kb

yH

inze

onlig

htw

eigh

tge

neric

prog

ram

min

gha

sin

trodu

ced

tech

niqu

esth

atal

low

the

defin

ition

of

gene

ricfu

nc-

tions

dire

ctly

inH

aske

ll.A

seve

redr

awba

ckof

thes

eap

proa

ches

isth

atge

neric

func

tions

,on

cede

fined

,ca

nnot

beex

tend

edw

ithad

-hoc

beha

viou

rfo

rne

wda

taty

pes,

prec

ludi

ngth

ede

sign

of

anex

tens

ible

and

mod

ular

gene

ricpr

ogra

mm

ing

Page 2: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

libra

ryb

ased

onth

ese

tech

niqu

es.

Inth

ispa

per,

we

pres

ent

are

vise

dve

rsio

nof

Hin

ze’s

Gen

eric

sfo

rth

em

asse

sap

proa

chth

atov

erco

mes

this

limita

tion.

Us-

ing

our

new

tech

niqu

e,w

ritin

gan

exte

nsib

lean

dm

odul

arge

neric

prog

ram

min

glib

rary

inH

aske

ll98

ispo

ssib

le.

1.1

INT

RO

DU

CT

ION

Age

neric

,or

p ol

ytyp

ic,

func

tion

isa

func

tion

that

isde

fined

over

the

stru

ctur

eof

type

s:w

itha

sing

lede

finiti

on,

we

obta

ina

func

tion

t hat

wor

ksfo

rm

any

data

type

s.St

anda

rdex

ampl

esin

clud

eth

efu

nctio

nsth

atca

nbe

deriv

edau

tom

atic

ally

inH

aske

ll[1

4],

such

ass

how

,re

ad,

and

‘==’

,bu

tth

ere

are

man

ym

ore.

By

cont

rast

,an

ad-h

ocp

olym

orph

icfu

nctio

n[1

5]re

quire

sa

sepa

rate

impl

e-m

enta

tion

for

each

data

type

.In

Has

kell,

we

impl

emen

tad

-hoc

p ol

ymor

phic

func

tions

usin

gty

pecl

asse

s.H

ere

isan

exam

ple,

ab

inar

yen

code

r:

1Oxf

ord

Uni

vers

ityC

ompu

ting

L ab

orat

ory,

Wol

fson

B ui

ldin

g,Pa

rks

Roa

d,O

xfor

dO

X1

3QD

,U

K,b

runo

@co

mla

b.o

x.a

c.u

k2I

nstit

utf u¨

rIn

form

atik

III,

Unive

rsita

¨tB

onn,

R ¨o

mer

straß

e16

4,53

117

Bonn

,G

erm

any,

{ra

lf,lo

eh

}@in

form

at

ik.u

ni-b

on

n.d

e

Page 3: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

1cl

ass

E nc

ode

twhe

reen

code

::t→

[Bit]

inst

ance

Enc

ode

Cha

rw

here

enco

de=

enco

deC

har

inst

ance

Enc

ode

Int

whe

reen

code

=en

code

Int

inst

ance

Enc

ode

a⇒

Enc

ode

[a]

whe

resetn

acnocd

ee E[n]

=a

⇒[0E]

enco

de(x

:xs)

=1:(

enco

dex

++en

code

xs)

The

clas

sco

nstru

ctin

trodu

ces

anov

erlo

aded

func

tion

with

aty

pepa

ram

eter

t,an

dth

ein

stan

cest

atem

ents

prov

ide

impl

emen

tatio

nsfo

ra

num

ber

of

spec

ific

type

s.A

nin

stan

cefo

rlis

tsof

type

[a]

can

only

begi

ven

ifan

inst

ance

for

aex

ists

alre

ady.

The

func

tion

enco

deth

usw

orks

onch

arac

ters

,in

tege

rs,

and

lists

,an

don

data

type

sth

atar

ebu

iltfr

omth

ese

type

s.If

we

call

enco

de,

the

com

pile

rfig

ures

out

the

corr

ect

impl

emen

tatio

nto

use,

or,if

nosu

itabl

ein

stan

ceex

ists

,re

ports

aty

peer

ror.

We

assu

me

that

prim

itive

bit

enco

ders

for

inte

gers

and

char

acte

rsar

epr

ovid

edfr

omso

mew

here

.Li

sts

are

enco

ded

byre

plac

ing

anoc

curr

ence

ofth

eem

pty

list

[]

Page 4: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

with

the

bit

0,an

doc

curre

nces

of

the

list

cons

truct

or(:

)w

ithth

ebi

t1

follo

wed

byth

een

codi

ngof

the

h ea

del

emen

tan

dth

een

codi

ngof

the

r em

aini

nglis

t.Th

efo

llow

ing

exam

ple

sess

ion

dem

onst

rate

sth

eus

eof

enco

deon

alis

tof

strin

gs(w

here

strin

gsar

elis

tsof

char

acte

rsin

Has

kell)

.

Main

ien

code

["xy

","x

"]

[MM1,a

1,0i,e

e0n,c0

o,d1e,

1[,1x,

1y,"1,,

"1x,0"

,]0,1

,1,1

,1,0

,1,1

,0,0

,0,1

,1,1

,1,0

,0]

The

func

tion

enco

deca

nb e

exte

nded

atan

ytim

eto

wor

kon

addi

tiona

lda

taty

pes.

All

we

have

todo

isw

rite

anot

her

inst

ance

of

the

Enc

ode

clas

s.H

owev

er,

each

time

we

add

ane

wda

taty

pean

dw

ew

ant

toen

code

valu

esof

that

data

type

,w

ene

edto

supp

lya

spec

ific

impl

emen

tatio

nof

enco

defo

rit.

In“G

ener

ics

for

the

Mas

ses”

(GM

)[4

]a

parti

cula

rlylig

htw

eigh

tap

proa

chto

gene

ricpr

ogra

mm

ing

isp

rese

nted

.U

sing

the

tech

niqu

esde

scrib

edin

that

p ap

erw

eca

nw

rite

gene

ricfu

nctio

nsdi

rect

lyin

Has

kell

98.

This

cont

rast

sw

ithot

her

appr

oach

esto

gene

ricp

rogr

amm

ing,

whi

chus

ually

requ

iresi

gnifi

cant

com

pile

rsu

ppor

tor

lang

uage

exte

nsio

ns.

InFi

gure

1.1,

we

pres

ent

age

neric

b in

ary

enco

der

impl

emen

ted

usin

gth

eG

Mte

chni

que.

W e

will

desc

ribe

the

tech

nica

lde

tails

,su

chas

the

shap

eof

clas

sG

ener

ic,

inS

ectio

n1.

2.Le

tus

,for

now

,fo

cus

onth

eco

mpa

rison

with

the

ad-h

ocpo

lym

orph

icfu

nctio

ngi

ven

abov

e.Th

edi

ffere

ntm

etho

dsofc

lass

Gen

eric

defin

e

Page 5: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

diffe

rent

case

sof

the

gene

ricfu

nctio

n.Fo

rch

arac

ters

and

inte

gers

,w

eas

sum

eag

ain

stan

dard

defin

ition

s.B

utth

eca

sefo

rlis

tsis

now

subs

umed

b y

thre

ege

neric

case

sfo

run

it,su

man

dpr

oduc

tty

pes.

By

view

ing

all

data

type

sin

aun

iform

way

,th

ese

thre

eca

ses

are

suffi

cien

tto

call

the

enco

der

onlis

ts,

tupl

es,

trees

,an

dse

vera

lm

ore

com

plex

data

stru

ctur

es–

ane

win

stan

cede

clar

atio

nis

not

requ

ired.

2

new

type

E nc

ode

a=

E nc

ode

{en

code

0::

a→

[Bit]

}

inst

ance

Gen

eric

E nc

ode

whe

reun

it=

E nc

ode

(con

st[]

)pl

usa

b=

E nc

ode

(λx→

case

xof In

ll

→0

:enc

ode0

al

IInnrl

r→

→10:e

enncco

oddee0

br)

prod

ab

=E

ncod

e(λ

(x×y)→encoIndre0rax+1 + :eennccooddee0by )

char

==EE

nnccoo

ddee

(eλnc(

ox×dey

C)h→ a

rin

t=

Enc

ode

enco

deIn

tvi

ewis

oa

=E

ncod

e(λ

x→

enco

de0

a(f

rom

iso

x))

FIG

UR

E1.

1.A

gene

ricbi

nary

enco

der

Page 6: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

How

ever

,th

ere

are

situ

atio

nsin

whi

cha

spec

ific

case

for

asp

ecifi

cda

taty

pe–

calle

dan

ad-h

occa

se–

isde

sira

ble.

For

exam

ple,

lists

can

been

code

dm

ore

effic

ient

lyth

ansh

own

abov

e:in

stea

dof

enco

ding

each

cons

truct

or,

we

can

enco

deth

ele

ngth

oft

helis

tfo

llow

edb

yen

codi

ngs

of

the

elem

ents

.O

r,su

ppos

eth

atse

tsar

ere

pres

ente

das

trees

:Th

esa

me

set

can

b er e

pres

ente

dby

mul

tiple

trees

,so

age

neric

equa

lity

func

tion

shou

ldno

tco

mpa

rese

tsst

ruct

ural

ly,

and

ther

efor

ew

ene

edan

ad-h

occa

sefo

rse

ts.

Def

inin

gad

-hoc

case

sfo

rad

-hoc

p ol

ymor

phic

func

tions

istr

ivia

l:w

ej u

stad

dan

inst

ance

decl

arat

ion

with

the

desi

red

impl

emen

tatio

n.F

orth

ege

neric

vers

ion

of

the

bina

ryen

code

r,th

ead

ditio

nof

ane

wca

seis

,how

ever

,ve

rydi

fficu

lt.E

ach

case

of

the

func

tion

defin

ition

isim

plem

ente

da

met

hod

of

clas

sG

ener

ic,

and

addi

nga

new

case

late

rre

quire

sth

em

odifi

catio

noft

hecl

ass.

We

say

that

gene

ricfu

nctio

nsw

ritte

nin

this

styl

ear

eno

tex

tens

ible

,an

dth

atth

eG

Map

proa

chis

not

mod

ular

,be

caus

eno

n-ex

tens

ibili

typr

eclu

des

writ

ing

age

neric

prog

ram

min

glib

rary

.G

ener

icfu

nctio

nsar

em

ore

conc

ise,

but

ad-h

ocpo

lym

orph

icfu

nctio

nsar

em

ore

flexi

ble.

Whi

lepr

evio

usfo

unda

tiona

lw

ork

[2,

7,

3,9]

prov

ides

ave

ryst

rong

basi

sfo

rge

neric

p ro

gram

min

g,m

ost

ofi

ton

lyco

nsid

ered

non-

exte

nsib

lege

neric

func

-tio

ns.

Itw

asre

aliz

edby

man

yau

thor

s[5

,4,8

]th

atth

isw

asa

seve

relim

itatio

n.Th

ispa

per

mak

esth

efo

llow

ing

cont

ribut

ions

:

Page 7: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

•In

Sec

tion

1.3,

we

give

anen

codi

ngof

exte

nsib

lege

neric

func

tions

dire

ctly

Iwnit S

hienc

iHonas

k1 .e3

ll,w 98

eth g

iavte

eiasnm

e ondc

uoladirn

,ov

erco

min

gtlhe

e gelin

meirtia

cti ofun

nscotio

fnGsM

dwre

chtillye

reta

inin

gits

adva

ntag

es.

An

exte

nsib

lege

neric

p re

ttypr

inte

ris

p re

sent

edin

Sec

tion

1.4.

•In

Sec

tion

1.5,

we

show

that

usin

ga

type

clas

sw

ithtw

opa

ram

eter

s,a

smal

leIxn

teS ne

cstioion

nt1o

H5,a sw

keel slh

o98w,

tthhea

tnu ost

iantgion

a at yl

opveec

rhlaesa

sdwca

nt bw

esi

gnifi

cant

ly,rea

ds um

ceadll

furth

er.

•Th

efa

ctth

atan

exte

nsib

lean

dm

odul

arge

neric

prog

ram

min

glib

rary

requ

ires

tThhee

abili

tyato

taand

d ex bt

eotnhs

new

gene

ricufu

lanrctg

ieonnes

riacn

dprne

wma

dm-hio

ncgca

ses

yisrerq

eluaitre

eds

3to

the

expr

essi

onpr

oble

m[1

8].

W e

esta

blis

hth

isre

latio

nan

dpr

esen

tot

her

rela

ted

wor

kin

Sec

tion

1.6.

But

let

usst

art

with

the

fund

amen

tals

of

the

GM

appr

oach

,an

dw

hyex

tens

ibili

tyin

this

fram

ewor

kis

not

easy

toac

hiev

e.

1.2

GE

NE

RIC

SF

OR

TH

EM

AS

SE

S

Inth

isse

ctio

nw

ew

illsu

mm

aris

eth

eke

ypo

ints

of

the

GM

appr

oach

.

Page 8: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

1.2.

1A

clas

sfo

rge

neric

f unc

tions

Inth

eG

Map

proa

chto

gene

ricp

rogr

amm

ing,

each

gene

ricfu

nctio

nis

anin

stan

ceof

the

clas

sG

ener

ic:

clas

sG

ener

icg

whe

reun

it::

g1

plus

::g

a→

gb

→g

(a+b

)pr

od::

ga

→→

gbb

→→

g((aa

×+bb)

)co

nstr

::Ng

aam→

e→ g

bA

rity

( →a

ga

)→g

aco

nstr

=::N

idach

ar::

gC

har

int

::g

Int

view

::Is

ob

a→

ga

→g

b

Our

gene

ricbi

nary

enco

der

inFi

gure

1.1

ison

esu

chin

stan

ce.

The

idea

ofG

ener

icis

that

gre

pres

ents

the

type

of

the

gene

ricfu

nctio

nan

dea

chm

etho

dof

the

type

clas

sre

pres

ents

aca

seof

the

gene

ricfu

nctio

n.H

aske

ll98

seve

rely

rest

ricts

the

type

term

sth

atca

nap

pear

inin

stan

cede

clar

atio

ns.

Tofu

lfil

thes

ere

stric

tions

,w

eha

veto

pack

the

type

of

the

enco

der

ina

data

type

Enc

ode.

For

conv

enie

nce,

we

defin

eE

ncod

eas

ar e

cord

type

with

one

field

and

anac

cess

orfu

nctio

nen

code

0::

Page 9: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

Enc

ode

a→

(a→

[Bit]

).Tod

heeaf i

r →stt

h (are→

em[

Betiht

]o)d.s

of c

lass

Gen

eric

are

for

the

unit,

sum

and

prod

uct

type

sth

atar

ede

fined

asfo

llow

s:

data

1=

1

data

a+b

=In

la|I

nr

b

data

a×b=a×b

The

type

soft

hecl

ass

met

hods

follo

wth

eki

nds

oft

heda

taty

pes

[3],

whe

rek

inds

are

type

sof

type

-leve

lte

rms.

Type

sw

ithva

lues

such

asI n

t,C

har,

and

1ha

veki

nd∗

.Th

ep

aram

eter

ized

type

s+

and

×havekind∗→∗→∗,toreflectthefka

inctd

dth∗ a

.tth

eyar

eb

inar

yop

erat

ors

onty

pes

×ofha

kivnedk

∗.

Tdh∗ e

→fun∗

ct→ ion

∗ s,

p lu

san

dp ro

dco

rres

pond

ingl

ybt a

iknea

rayddo

ipteiroa

ntoalr

argu

men

tstfhk

atica

ptur

eethf

eunr e

ctciuor

nssivp

elucs

a lalsn

dopfr

tohed

gene

ricfu

nctio

non

the

p ar

amet

ers

of

the

data

type

.Th

eb

inar

yen

code

ris

defin

edto

enco

deth

eun

itty

peas

the

empt

yse

quen

ceof

b its

.In

the

sum

case

,a

0or

1is

gene

rate

dde

pend

ing

onth

eco

nstru

ctor

of

4

the

inpu

tva

lue.

Inth

epr

oduc

tca

se,

we

conc

aten

ate

the

enco

ding

sof

the

left

and

Page 10: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

right

com

pone

nt.

Ifou

rge

neric

func

tions

requ

irein

form

atio

nab

out

the

cons

truct

ors

(suc

has

the

nam

ean

dar

ity),

we

can

optio

nally

prov

ide

ade

finiti

onfo

rth

efu

nctio

nco

nstr.

Oth

erw

ise

–su

chas

for

the

bina

ryen

code

r–

we

can

j ust

use

the

defa

ult

impl

e-m

enta

tion,

whi

chig

nore

sth

eex

train

form

atio

n.C

ases

for

the

p rim

itive

type

sC

har

and

Int

are

defin

edb

ypr

ovid

ing

the

func

-tio

nsch

aran

din

t,re

spec

tivel

y.Th

evi

ewfu

nctio

nis

the

key

toge

neric

ity:

give

nan

isom

orph

ism

(bet

wee

nth

eda

taty

pean

da

sum

of

prod

ucts

)an

da

repr

esen

tatio

nfo

rth

eis

omor

phic

type

,re

turn

sa

r epr

esen

tatio

nfo

rth

eor

igin

alda

taty

pe.

Let

uslo

okat

lists

asan

exam

-pl

e.A

list

of

type

[a]

can

beco

nsid

ered

asa

bina

rysu

m(it

has

two

cons

truct

ors)

,w

here

the

first

argu

men

tis

aun

it(th

e[]

cons

truct

orha

sno

argu

men

ts)

and

the

seco

ndar

gum

ent

isa

p ai

r(th

e(:

)co

nstru

ctor

has

two

argu

men

ts)

of

anel

emen

tof

type

aan

dan

othe

rlis

toft

ype

[a].

This

mot

ivat

esth

efo

llow

ing

defin

ition

s:

data

Iso

ab

=Is

o{f

rom

::a

→b,

to::

b→

a}

isoL

ist

::Is

o[a

](1

+(a

×[a]))iiss

ooLLiis

stt=:

:IIss

oo [af

r]o m(

1L+ist(

tao×L

i[sat

from

List

::[a

]→

1+

(a×[a])

from

List

:[::]→=

1I +

nl(1a

from

List

(x:x

s)=

Inr

(x×xs)

Page 11: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

toLi

st::1

+(a

×[a])→[a]ttoo

LLiisst

t:(:I1

nl++1)

()=

→→[→]

toLi

st(I

nr

(x×xs))=x:xs

Inor

der

tous

ege

neric

func

tions

ona

data

type

,th

epr

ogra

mm

erm

ust

defin

esu

chan

isom

orph

ism

once

.A

fterw

ards

,al

lgen

eric

func

tions

can

beus

edon

the

data

type

bym

eans

oft

hevi

ewca

se.

The

func

tion

rLis

t–

also

with

inth

epr

ogra

mm

er’

sre

spon

sibi

lity

–ca

ptur

esho

wto

appl

yvi

ewin

the

case

of

lists

:

rLis

t::

Gen

eric

g⇒

ga→

g[a

]rrL

Liisstt

aG=

evnie

erwic

ig so ⇒

Lisg t

a(u→ n

itg‘p

[ lau]s‘

(a‘p

rod‘

rLis

ta

))

The

first

argu

men

tofv

iew

isth

eis

omor

phis

mfo

rlis

tsde

fined

abov

e.Th

ese

cond

argu

men

tre

flect

sth

elis

t-is

omor

phic

type

1+(

a×[a]).UsingrList,wecanapply

any

gene

riclfeu

nctcst

t ihoen

ltiosa

sloismt

type

,b

yvi

ewin

gan

yali]

)st.as

asu

most

f,pro

duct

sapn

ldyth

enus

ing

the

gene

ricde

finiti

ons

for

the

unit,

sum

and

prod

uct

type

s.Th

evi

ewca

seof

the

enco

der

appl

ies

thef

rom

part

of

the

isom

orph

ism

toco

n-ve

rtth

ety

peof

the

inpu

tva

lue

and

then

calls

the

enco

der

recu

rsiv

ely

onth

atva

lue.

Gen

eral

ly,

func

tions

such

asth

een

code

rw

here

the

type

varia

ble

appe

ars

only

inth

ear

gum

ent

posi

tion

are

calle

dge

neric

cons

umer

san

dre

quire

only

thef

rom

p ar

tof

the

isom

orph

ism

.F

unct

ions

that

prod

uce

or

trans

form

valu

esge

neric

ally

mak

eal

sous

eof

the

toco

mpo

nent

.

Page 12: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

We

have

dem

onst

rate

dth

ata

fixed

amou

ntof

code

(the

isom

orph

ism

and

the

view

invo

catio

n)is

suffi

cien

tto

adap

tal

lgen

eric

f unc

tions

tow

ork

ona

new

data

5ty

pe.

This

isa

h ug

eim

prov

emen

tov

erad

-hoc

p ol

ymor

phic

func

tions

,w

hich

have

tobe

exte

nded

one

b y

one

tow

ork

onan

addi

tiona

lda

taty

pe.

1.2.

2U

sing

gene

ricfu

nctio

ns

Inor

der

toca

lla

gene

ricfu

nctio

nsu

chas

enco

de0,

we

have

tosu

pply

asu

itabl

eva

lue

of

type

Enc

ode.

As

asi

mpl

eex

ampl

e,su

ppos

eth

atw

ew

ant

toca

llth

efu

nctio

nen

code

0on

apa

irofa

nin

tege

ran

da

char

acte

r.W

eth

enus

ep

rod

int

char

tobu

ildth

ede

sire

dva

lue

of

type

E nc

ode

(Int

×Char),anduseencode0toextractttho

e bfuu

inldctt

hioend

:

Main

ien

code

0(p

rod

int

char

)(1

×’x’)[MM

1,0i,n

0i,ee0n

,c0o,d

0e,0,(

p0,ro

0d,0i n

,0t,c

c0h,aa0r,

))0(,1

0,××0,

’’0x

,’0,)0

,1,

1,1,

1]

Sim

ilarly

,if

we

wan

tto

enco

dea

list

of

strin

gs,

we

can

mak

eus

eof

func

tion

rLis

tto

build

the

argu

men

tto

enco

de0:

Page 13: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

Main

ien

code

0(r

List

(rLi

stch

ar))

["xy

","x

"][MM

1,1i,n

0i,ee0n,c

c0o,d1

e,1,

1,1,

1,1,

0,0

,1,1

,1,1

,0,1

,1,0

,0,0

,1,1

,1,1

,0,0

]

The

argu

men

tto

enco

de0

isdi

ctat

edb

yth

ety

peat

whi

chw

eca

llth

ege

neric

func

tion.

W e

can

ther

efor

eus

eth

et y

pecl

ass

R ep

,sh

own

inFi

gure

1.2,

t oin

fer

this

so-c

alle

dty

pere

pres

enta

tion

auto

mat

ical

lyfo

rus

.W

eca

llsu

cha

type

clas

sa

disp

atch

er,

beca

use

itse

lect

sth

eco

rrec

tca

seof

age

neric

func

tion

depe

ndin

gon

the

type

cont

ext

i nw

hich

itis

used

.N

ote

that

the

disp

atch

erw

orks

for

any

gth

atis

anin

stan

ceof

Gen

eric

.Th

eref

ore,

itne

eds

tobe

defin

edon

lyon

cefo

ra

llge

neric

func

tions

.W

ithth

edi

spat

cher

,w

eca

nde

fine

enco

deas

follo

ws:

enco

de::

Rep

t⇒

t→

[Bit]

eenncc

ooddee

= ::e

Rnecpo

t d⇒e 0

rep

Her

e,th

ety

pere

pres

enta

tion

isim

plic

itly

pass

edvi

ath

et y

pecl

ass.

The

func

tion

enco

deca

nb e

used

with

the

sam

eco

nven

ienc

eas

any

ad-h

ocov

erlo

aded

func

tion,

but

itis

trul

yge

neric

.

1.3

EX

TE

NS

IBL

EG

EN

ER

ICF

UN

CT

ION

S

This

sect

ion

cons

ists

of

two

p ar

ts:

inth

efir

stp

art,

we

dem

onst

rate

how

the

non-

Page 14: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

exte

nsib

ility

of

GM

func

tions

lead

sto

non-

mod

ular

ity.

Inth

ese

cond

part,

we

show

how

toov

erco

me

this

limita

tion.

1.3.

1T

hem

odul

arity

prob

lem

Supp

ose

t hat

we

wan

tto

enco

delis

ts,

and

that

we

wan

tto

use

adi

ffere

nten

codi

ngof

lists

than

the

one

deriv

edge

neric

ally

:a

list

can

b een

code

dby

enco

ding

itsle

ngth

,fo

llow

edb

yth

een

codi

ngs

of

all

the

list

elem

ents

.Fo

rlo

nglis

ts,

this

6cl

ass

Rep

aw

here

rep

::(G

ener

icg

)⇒

ga

inst

ance

Rep

1w

here

rep

=un

it

inst

ance

Rep

Cha

rw

here

rep

=ch

ar

inst

ance

Rep

Int

whe

rere

p=

int

inst

ance

(Rep

a,R

epb)

⇒R

ep(a

+b)

whe

rere

pc=

eep (

lRuse

rep

rep

Page 15: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

inst

ance

(Rep

a,R

epb)

⇒R

ep(a

×b)wherere

pc=e

e p( Rro

edpre

pre

p

inst

ance

Rep

a⇒

Rep

[a]

whe

rere

pc=e

rR Lei

pstre

p

FIG

UR

E1.

2.A

gene

ricdi

spat

cher

enco

ding

ism

ore

effic

ient

than

tose

para

tean

ytw

osu

bseq

uent

elem

ents

of

the

lists

and

tom

ark

the

end

oft

helis

t.Th

ecl

ass

Gen

eric

isth

eb

ase

clas

sofa

llge

neric

func

tions

,an

dits

met

hods

are

limite

d.If

we

wan

tto

desi

gna

gene

ricpr

ogra

mm

ing

libra

ry,

itis

man

dato

ryth

atw

eco

nstra

inou

rsel

ves

toa

limite

dse

tof

frequ

ently

used

t ype

s.S

till,

we

mig

htho

peto

add

anex

traca

seb

yi n

trodu

cing

subc

lass

es:

clas

sG

ener

icg

⇒G

ener

icLi

stg

whe

realsis

stG::

ga

c→g

g⇒[aG

]lliis

stt= :

:grLa

is→ t

Thi

sde

clar

atio

nin

trodu

ces

acl

ass

Gen

eric

List

asa

subc

lass

of

Gen

eric

:w

eca

n

Page 16: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

only

inst

antia

teG

ener

icLi

stfo

rt y

peg

that

are

also

inst

ance

sof

clas

sG

ener

ic.

The

subc

lass

cont

ains

asi

ngle

met

hod

list.

By

defa

ult,

list

isde

fined

tob e

j ust

rLis

t.H

owev

er,

the

defa

ult

defin

ition

of

list

can

beov

errid

den

inan

inst

ance

decl

arat

ion.

For

exam

ple,

h er

eis

how

tode

fine

the

mor

eef

ficie

nten

codi

ngfo

rlis

ts: in

stan

ceG

ener

icLi

stE

ncod

ew

here

list

a=

E nc

ode

(λx

→en

code

Int

(leng

thx

)++c

onca

tMap

(enc

ode0

a)x

)

Our

exte

nsio

nbr

eaks

dow

n,ho

wev

er,

once

we

try

toad

apt

the

disp

atch

er:

the

met

hod

rep

of

clas

sR

epha

sth

ety

peG

ener

icg

⇒g

a,an

dw

eca

nnot

easi

lyre

plac

eht h

oedcr o

epnteo

xftc

Glaesn

seR rie

cpwh

aiths

som

ethi

ngm

ore

spec

ific

,wa itnh

dou wte

mod

ifyin

gstih

leyR

epcl

ass.

Ther

efor

e,th

eon

lym

etho

dsof

aty

pecl

ass

depe

ndin

gon

the

type

7

clas

sR

epE

ncod

etw

here

repE

ncod

e::

E nc

ode

t

inst

ance

R e

pEnc

ode

1w

here

repE

ncod

e=

E nc

ode

(enc

ode0

unit)

inst

ance

R e

pEnc

ode

Int

whe

rere

pEnc

ode

=E

ncod

e(e

ncod

e0in

t)

Page 17: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

inst

ance

R e

pEnc

ode

Cha

rw

here

repE

ncod

e=

E nc

ode

(enc

ode0

char

)

inst

ance

(Rep

Enc

ode

a ,R

epEn

code

b)⇒

R e

pEnc

ode

(a+b

)w

here

srteapn

Ecnec (

oRdeep

=E

nE con

cdoeda

e,(Re

enpcEo

dnec0o

(dpel u

bs)⇒re

pR Een

pcEond

ecore

depE(

an+cod

be))w

)

inst

ance

(Rep

Enc

ode

a ,R

epEn

code

b)⇒

R e

pEnc

ode

(a×b)where

srteapn

Ecnec (

oRdeep

=E

nE con

cdoeda

e,(Re

enpcEo

dnec0o

(depr b

o)d⇒

⇒repR

EenpcE

ondceo

dreep

(Ean×c

obd)ew

) )

FIG

UR

E1.

3.A

nad

-hoc

disp

atch

erfo

rbi

nary

enco

ders

varia

ble

gth

atw

eca

nus

eat

the

defin

ition

sof

rep

are

thos

eof

Gen

eric

–an

yus

esof

met

hods

from

subc

lass

esof

Gen

eric

will

r esu

ltin

type

erro

rs.

Inpa

rticu

lar,

we

cann

otad

apt

the

inst

ance

of R

epfo

rlis

tsto

mak

eus

eof

list

rath

erth

anrL

ist.

Con

sequ

ently

,ge

neric

func

tions

inth

eG

Map

proa

char

eno

tex

tens

ible

.Th

isru

les

out

mod

ular

ity:

allc

ases

that

can

appe

arin

age

neric

func

tion

mus

tb e

turn

edin

tom

etho

dsof

clas

sG

ener

ic,

and

asw

eha

veal

read

yar

gued

,th

isis

impo

ssib

le:

itm

ayb e

nece

ssar

yto

add

spec

ific

b eh

avio

uron

user

-def

ined

orab

stra

ctty

pes

that

are

sim

ply

not

know

nto

the

libra

ryw

riter

.

Page 18: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

1.3.

2A

d-ho

cdi

spat

cher

s

The

prob

lem

with

the

GM

appr

oach

isth

atth

ege

neric

disp

atch

erfo

rces

asp

ecifi

cdi

spat

chin

gbe

havi

our

ona

llge

neric

func

tions

.A

sim

ple

solu

tion

toth

ispr

oble

mis

tosp

ecia

lize

the

disp

atch

erR

epto

the

gene

ricfu

nctio

nin

ques

tion.

This

mea

nsth

atw

eno

wne

edon

edi

spat

cher

for

each

gene

ricfu

nctio

n,bu

tit

also

mea

nsth

atex

tens

ibili

tyis

nolo

nger

apr

oble

m.

Figu

re1.

3sh

ows

wha

tw

eob

tain

b y

spec

ializ

ing

Rep

toth

eb

inar

yen

code

r.In

the

inst

ance

s,w

eus

een

code

0to

extra

ctth

eva

lue

from

the

new

type

and

redi

rect

the

call

toth

eap

prop

riate

case

inG

ener

ic.

The

spec

ializ

eddi

spat

cher

can

beus

edj u

stas

the

gene

ral

disp

atch

erbe

fore

,to

defin

ea

trul

yge

neric

b in

ary

enco

der:

enco

de::

R e

pEnc

ode

t⇒

t→[B

it]een

nccood

dee= :

:Rene

cpoEdn

ec0ore

depEt ⇒

ncot d

→ e

Itis

now

triv

ial

toex

tend

the

disp

atch

erto

new

type

s.C

onsi

der

once

mor

eth

ead

-hoc

case

for

enco

ding

lists

,de

fined

b y

prov

idin

gan

inst

ance

decl

arat

ion

for

Gen

eric

List

Enc

ode.

The

corr

espo

ndin

gdi

spat

cher

exte

nsio

nis

perfo

rmed

asfo

l-lo

ws:

8

Page 19: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

inst

ance

R e

pEnc

ode

a⇒

R e

pEnc

ode

[a]

whe

resrte

apnEcn

ec Roed

peE=

nc Eo

ndceod

a e ⇒

⇒(eR nce

opdEen

0c(oli

dset[

reap]E

w nhce

ordee)

)

Let

ussu

mm

ariz

e.B

ysp

ecia

lizin

gdi

spat

cher

sto

spec

ific

gene

ricfu

nctio

ns,

we

obta

inan

enco

ding

of

gene

ricfu

nctio

nsin

Has

kell

that

isj u

stas

expr

essi

veas

the

GM

appr

oach

and

shar

esth

ead

vant

age

that

the

code

isp

ure

Has

kell

98.

Add

ition

ally

,ge

neric

func

tions

with

spec

ializ

eddi

spat

cher

sar

eex

tens

ible

:w

eca

npl

ace

the

type

clas

sG

ener

icto

geth

erw

ithfu

nctio

nssu

chas

enco

dein

alib

rary

that

isea

syto

use

and

exte

ndby

p ro

gram

mer

s.

1.4

EX

AM

PL

E:

A N

EX

TE

NS

IBL

EG

EN

ER

ICP

RE

TT

YP

RIN

TE

R

Inth

isse

ctio

nw

esh

owho

wto

defin

ea

exte

nsib

lege

neric

p re

ttypr

inte

r.Th

isex

ampl

eis

base

don

the

non-

mod

ular

vers

ion

p re

sent

edin

GM

(orig

inal

lyba

sed

onW

adle

r’s

wor

k[1

9]).

1.4.

1A

gene

ricpr

etty

prin

ter

InFi

gure

1.4

we

pres

ent

anin

stan

ceof

Gen

eric

that

defin

esa

gene

ricpr

etty

prin

ter.

The

pret

typr

inte

rm

akes

use

of

Wad

ler’

spr

etty

prin

ting

com

bina

tors

.Th

ese

com

bina

tors

gene

rate

ava

lue

of

type

Doc

that

can

bere

nder

edin

toa

strin

g

Page 20: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

afte

rwar

ds.

For

the

stru

ctur

alca

ses,

the

unit

func

tion

j ust

retu

rns

anem

pty

docu

-m

ent;

p lu

sde

com

pose

sth

esu

man

dpr

etty

prin

tsth

eva

lue;

for

prod

ucts

,w

ep

retty

prin

tth

efir

stan

dse

cond

com

pone

nts

sepa

rate

dby

alin

e.Fo

rba

sety

pes

char

and

int

we

assu

me

exis

ting

pret

typr

inte

rsp

retty

Cha

ran

dp re

ttyIn

t.Th

evi

ewca

sej u

stus

esth

eis

omor

phis

mto

conv

ert

betw

een

the

user

defin

edty

pean

dits

stru

ctur

alre

pres

enta

tion.

Fin

ally

,si

nce

pret

typr

inte

rsre

quire

extra

cons

truct

orin

form

atio

n,th

efu

nctio

nco

nstr

calls

p re

ttyC

onst

r,w

hich

pret

typr

ints

cons

truct

ors.

Supp

ose

that

we

add

ane

wda

taty

peTr

eefo

rre

pres

entin

gla

belle

db

inar

ytre

es.

Furth

erm

ore,

the

node

sha

vean

auxi

liary

inte

ger

valu

eth

atca

nbe

used

totra

ckth

em

axim

umde

pth

of

the

subt

rees

.

data

Tree

a=

E m

pty

|For

kIn

t(T

ree

a)a

(Tre

ea)

Now

,w

ew

ant

tous

eou

rge

neric

func

tions

with

Tree

.A

sw

eha

veex

plai

ned

befo

re,

wha

tw

ene

edto

dois

t oad

da

subc

lass

of

Gen

eric

with

aca

sefo

rth

ene

wda

taty

pean

dpr

ovid

ea

suita

ble

view

.

clas

sG

ener

icg

⇒G

ener

icTr

eeg

whe

retrse

se G::

ga

i→c g

g⇒(T

G reeen

nae)r

ttrreeee

ag=

aav

→iewg

i s(ToTr

ereee a

)(con

str

"Em

pty

"0

unit

‘plus

‘co

nstr

"Fork

"4

(int

‘prod

‘(r

Tree

a‘p

rod‘

(a‘p

rod‘

rTre

ea

))))

Page 21: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

(We

omit

the

b oi

lerp

late

defin

ition

of

isoT

ree)

.P

rovi

ding

apr

etty

prin

ter

for

Tree

amou

nts

tode

clar

ing

anem

pty

inst

ance

of

Gen

eric

Tree

–th

atis

,usi

ngth

ede

faul

tde

finiti

onfo

rtre

e.

9

new

type

P re

ttya

=P

retty

{pre

tty0

::a

→D

oc}

inst

ance

Gen

eric

P re

ttyw

here

unit

=P

retty

(con

stem

pty)

char

=P

retty

(pre

ttyC

har)

int

=P

retty

(pre

ttyIn

t)pl

usa

b=

P re

tty(λ

x→

case

xo

f In

ll

→pr

etty

0al

IInnrl

r→

→ppr

reetttty

y0b

r)pr

oda

b=

P re

tty(λ

(x×y)→p retItynr0ax→?pl irneett?ypretty0by )

view

iso

a==

P re

tty((λ

p(rextt×

y0ya

→◦ fr o

pmre

titsyo)a

cons

trn

ar

a=

P re

tty(p

retty

Caon

◦fsrt

orn

ar

oa))

pret

tyC

onst

rn

a r

ax

=le

ts=

text

nin

if a

r==

0th

ens

else

g ro

up(n

est

1(te

xt"

("?s

?lin

e?p

retty

0a

x ?

text

")

"))

Page 22: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

FIG

UR

E1.

4.A

gene

ricpr

ettie

rpr

inte

r

Main

ile

tt=

Fork

1(F

ork

0E

mpt

y’h

’E

mpt

y)’i

’(F

ork

0E

mpt

y’

!’E

mpt

y)MM

aaiinn

iilree

tndt =

er8Fo

0r(kp

1 re( ttF

yo0(ktr

0 eeE

cmhpa

tyr)’t)h

(MFaoi

rnki

i1re(F

ndorek

r80

E m

pty

’yh’

E m

pty)

’i

’(F

ork

0E

mpt

y’!’

Em

pty)

)M

ain

ile

ti=

Fork

1(F

ork

0E

mpt

y10

4E

mpt

y)10

5(F

ork

0E

mpt

y33

E m

pty)

MMaai

innii

lreetnd

i = er

8Fo0r

(kp1 re(

ttFyo0

r(ktr

0 eeE

(mPprte

ytt1 y0

(4λEx

m→p

ttye)x1

t[0c5

h r(F

F xo])r

)k) 00iE)

(MFaoi

rnki

i1re(F

ndorek

r80

E m

pty

hyE(tm

repety

( )Pir

(eFttoy

r( kλ

0xE

mpt

yt![E

cmhrp

x ty])))

)M

ain

ire

nder

80(p

retty

t)(MF

aoirnk

ii1r

e(Fndo

rekr

80E

mpt

y’yt

h)’

E m

pty)

’i

’(F

ork

0E

mpt

y’!’

Em

pty)

)

FIG

UR

E1.

5.A

sam

ple

inte

ract

ive

sess

sion

inst

ance

Gen

eric

Tree

Pre

tty

We

now

dem

onst

rate

the

use

of

gene

ricfu

nctio

ns,

and

the

pret

typr

inte

rin

parti

cula

r,by

show

ing

the

outc

ome

of

aco

nsol

ese

ssio

nin

Figu

re1.

5.

Page 23: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

The

first

use

ofp

retty

0pr

ints

the

t ree

tus

ing

the

gene

ricfu

nctio

nalit

ygi

ven

bytre

ean

dch

ar.

Mor

ein

tere

stin

gly,

the

seco

ndex

ampl

e(o

na

tree

of

inte

gers

),sh

ows

that

we

can

over

ride

the

gene

ricbe

havi

our

for

the

inte

ger

para

met

erb

ypr

ovid

ing

aus

er-d

efin

edfu

nctio

nin

stea

dof

int

–in

this

case

,w

ein

terp

ret

anin

tege

ras

the

code

of

ach

arac

ter

usin

gth

efu

nctio

nch

r.W

hene

ver

the

extra

flexi

bilit

ypr

ovid

edby

the

p os

sibi

lity

of

over

ridin

gth

ege

neric

b eh

avio

uri s

not

requ

ired

(as

inth

efir

stca

llof

p re

tty0)

,w

eca

npr

ovid

ea

disp

atch

ersu

chas

the

one

p re

sent

edi n

F ig

ure

1.6

and

j ust

use

the

conv

enie

ntpr

etty

func

tion.

10cl

ass

R e

pPre

ttya

whe

rere

pPre

tty::

P re

ttya

repP

retty

List

::P

retty

[a]

repP

retty

List

=P

retty

(pre

tty0

(list

repP

retty

))

inst

ance

R e

pPre

tty1

whe

rere

pPre

tty=

P re

tty(p

retty

0re

pPre

tty)

inst

ance

R e

pPre

ttyC

har

whe

rere

pPre

tty=

P re

tty(p

retty

0ch

ar)

repP

retty

List

=P

retty

p re

ttyS

trin

g

Page 24: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

inst

ance

R e

pPre

ttyIn

tw

here

repP

retty

=P

retty

(pre

tty0

int)

inst

ance

(Rep

Pre

ttya,

Rep

Pre

ttyb)

⇒R

epP

retty

(a+

b)w

here

repP

retty

=et

P re

tty(pp

Prertet

tyt0y

( bp)l⇒u

sre

pPre

ttyyr(e

paP+r

ebt)tyw

))h

inst

ance

(Rep

Pre

ttya,

Rep

Pre

ttyb)

⇒R

epP

retty

(a×b)where

repP

retty

=et

P re

tty(pp

Prertet

tyt0y

( bp)r⇒

o dre

pPre

ttyr (e

ap×Pre

bt)t yw

))h

inst

ance

R e

pPre

ttya⇒

R e

pPre

tty(T

ree

a)w

here

repP

retty

=tt

P re

ttyR(e

prpePttr

ye0t

t(ytr (eT

erere

ep aP)

rw ethty

e)r)e

pret

ty::

Rep

Pre

ttyt⇒

t→D

ocpr

etty

=::R

R perpe

Pttyre0

repP

retty

FIG

UR

E1.

6.A

nad

-hoc

disp

atch

erfo

rpr

etty

prin

ters

1.4.

2S

how

ing

lists

For

user

-def

ined

type

slik

eTr

ee,

our

gene

ricpr

etty

prin

ter

can

j ust

reus

eth

ege

neric

func

tiona

lity

and

the

resu

ltsw

illbe

very

sim

ilar

toth

eon

esw

ege

tif

Page 25: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

we

j ust

appe

ndde

rivin

gSh

owto

our

data

type

defin

ition

s.H

owev

er,

this

does

not

wor

kfo

rbu

ilt-in

lists

.Th

epr

oble

mw

ithlis

tsis

that

they

use

asp

ecia

lm

ix-

fixno

tatio

nin

stea

dof

the

usua

lal

phab

etic

and

pref

ixco

nstru

ctor

s.Fo

rtuna

tely

,w

eha

vese

enin

Sec

tion

1.3

that

we

can

com

bine

ad-h

ocp

olym

orph

icfu

nctio

nsw

ithge

neric

func

tions

.W

esh

all

doth

esa

me

h er

e:w

ede

fine

anin

stan

ceof

Gen

eric

List

P re

ttybu

t,de

viat

ing

from

Gen

eric

Tree

Pre

tty,

we

over

ride

the

de-

faul

tde

finiti

on.

inst

ance

Gen

eric

List

P re

ttyw

here

listp

=P

retty

(λx

case

xorf

e[t]y

→→

text

"[]

"(a

:as)

→→

grou

p[(]n

e"st

1(te

xt"

["?p

retty

0p

a?

rest

as))

)w

here

res(ta

a[::]→=

gtreo

xutpp"(

]n"e

rest

(x:x

s)=

text

","

?lin

e?p

retty

0p

x ?

rest

x s

11W

eca

nno

wex

tend

the

disp

atch

erin

F ig

ure

1.6

with

anin

stan

cefo

rlis

tsth

atus

esHa

skel

l’s

stan

dard

nota

tion.

inst

ance

R e

pPre

ttya

⇒R

epP

retty

[a]

whe

re

Page 26: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

pret

ty=

Re pp

rePtrtey

t0t(yla

ist⇒⇒re

Rp ePp

rPetrty

e)tt

Unf

ortu

nate

ly,

we

are

not

done

yet.

InH

aske

llth

ere

ison

em

ore

spec

ial

nota

-tio

nin

volv

ing

lists

:st

rings

are

j ust

lists

of

char

acte

rs,

but

we

wan

tt o

p rin

tth

emus

ing

the

conv

entio

nal

strin

gno

tatio

n.S

o,no

ton

lydo

we

need

totre

atlis

tsin

asp

ecia

lm

anne

r,bu

tw

eal

sone

edto

hand

lelis

tsof

char

acte

rssp

ecia

lly.

We

thus

h av

eto

impl

emen

ta

n es

ted

case

anal

ysis

onty

pes.

W e

antic

ipat

edth

ispo

s-si

bilit

yin

F ig

ure

1.6

and

incl

uded

afu

nctio

nre

pPre

ttyLi

st.

The

basi

cid

eais

that

repP

retty

List

beha

ves

asex

pect

edfo

ral

llis

tsex

cept

the

ones

with

char

ac-

ters

,w

here

itus

espr

etty

Str

ing.

Thi

sis

the

sam

eas

Has

kell

does

inth

eSh

owcl

ass.

Fin

ally

,w

em

odify

R e

pPre

tty[a

]t o

redi

rect

the

call

to p

retty

List

and

we

are

done

.

inst

ance

R e

pPre

ttya⇒

R e

pPre

tty[a

]w

here

repP

retty

p=P

repP

retty

List

Inth

epr

etty

prin

ter

p re

sent

edin

GM

,su

ppor

ting

the

list

nota

tion

invo

lved

addi

ngan

extra

case

toG

ener

ic,

whi

chre

quire

dus

toha

veac

cess

toth

eso

urce

code

whe

reG

ener

icw

asor

igin

ally

decl

ared

.In

cont

rast

,w

ithou

rso

lutio

n,th

ead

ditio

nof

asp

ecia

lca

sefo

rlis

tsdi

dno

tin

volv

ean

ych

ange

toou

ror

igin

alG

ener

iccl

ass

or

even

itsin

stan

cefo

rP

retty

.Th

ead

ditio

nal

flexi

bilit

yof

ad-h

ocdi

spat

cher

sco

mes

ata

pric

e:w

hile

inG

Mth

ere

spon

sibi

lity

of

writ

ing

the

code

for

the

disp

atch

ers

was

onth

elib

rary

Page 27: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

writ

ersi

de,

now

this

resp

onsi

bilit

yis

onth

eus

erof

the

libra

ry,

who

has

tow

rite

addi

tiona

lbo

ilerp

late

code

.S

till,

itis

certa

inly

pref

erab

leto

defin

ean

ad-h

ocdi

spat

cher

than

tode

fine

the

func

tion

asan

ad-h

ocp

olym

orph

icfu

nctio

n,be

ing

forc

edto

give

anac

tual

impl

emen

tatio

nfo

rea

chda

taty

pe.

Yet

,it

wou

ldb e

even

bette

rif

we

coul

dso

meh

owre

turn

toa

sing

ledi

spat

cher

that

wor

ksfo

ral

lgen

eric

func

tions

and

rest

ore

the

defin

ition

of

the

disp

atch

erto

the

libra

ryco

de.

Inth

ene

xtse

ctio

nw

ew

illse

ean

alte

rnat

ive

enco

ding

that

requ

ires

asi

ngle

gene

ricdi

spat

cher

only

and

still

allo

ws

for

mod

ular

and

exte

nsib

lefu

nctio

ns.

The

pric

eto

pay

for

this

isth

atth

eco

dere

quire

sa

smal

lex

tens

ion

toH

aske

ll98

.

1.5

MA

KIN

GA

D-H

OC

D IS

PA

TC

HE

RS

LES

SA

D-H

OC

Inth

isse

ctio

nw

epr

esen

tan

othe

rw

ayto

writ

eex

tens

ible

gene

ricfu

nctio

ns,

whi

chre

quire

son

lyon

ege

neric

disp

atch

erj u

stlik

eth

eor

igin

alG

Map

proa

ch.

Itre

lies,

how

ever

,on

anex

tens

ion

toH

aske

ll98

:m

ulti-

para

met

erty

pecl

asse

s,w

hich

are

wid

ely

used

and

supp

orte

db

yth

em

ajor

Has

kell

impl

emen

tatio

ns.

Rec

all

the

disc

ussi

onat

the

end

of

Sec

tion

1.3.

1.T

here

,w

eha

vesh

own

that

the

prob

lem

with

GM

’sdi

spat

cher

isth

atit

fixes

the

cont

ext

of

met

hod

rep

toth

ecl

ass

Gen

eric

.T

his

happ

ens

beca

use

the

t ype

varia

ble

g ,w

hich

abst

ract

sov

erth

e“g

ener

icfu

nctio

n”,

isu

nive

rsal

lyqu

antif

ied

inth

ecl

ass

met

hod

rep.

How

ever

,

Page 28: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

12in

stan

ceG

ener

icg⇒

GR

epg1

w h

ere

grep

e=G

Gunenit

e

inst

ance

Gen

eric

g⇒

GR

epg

Int

whe

regr

epe=

GGin

etn

inst

ance

Gen

eric

g⇒

GR

epg

Cha

rw

here

grep

e=G

Gchena

rer

inst

ance

(Gen

eric

g ,G

Rep

ga

,GR

epg

b)⇒

GR

epg

(a+b

)w

here

grep

e=(

(pG leu

sng

rep

grep

inst

ance

(Gen

eric

g ,G

Rep

ga

,GR

epg

b)⇒

GR

epg

(a×b)where

grep

e=(

(p Greo

dnegr

epg

rep

inst

ance

(Gen

eric

List

g,G

Rep

ga)⇒

GR

epg

[a]

whe

regr

epe=

(lGi

setgr

ep

inst

ance

(Gen

eric

Tre

eg

,GR

epg

a )⇒

GR

epg

(Tre

ea)

whe

regr

epe=

(tGr

eeen

g re

p

FIG

UR

E1.

7.A

less

ad-h

ocdi

spat

cher

.

Page 29: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

sinc

ew

ew

ant

tous

esu

bcla

sses

of

Gen

eric

toad

dad

ditio

nal

case

sto

gene

ricfu

nctio

ns,

the

cont

ext

of

rep

mus

tbe

flexi

ble.

W e

ther

efor

em

ust

beab

leto

ab-

stra

ctfr

omth

esp

ecifi

cty

pecl

ass

Gen

eric

.O

urso

lutio

nfo

rth

ispr

oble

mis

toch

ange

the

quan

tific

atio

nof

g :

inst

ead

of

univ

ersa

llyqu

antif

ying

gat

the

met

hod

rep

we

can

quan

tify

iton

the

type

clas

sits

elf.

clas

sG

Rep

ga

whe

regr

ep::

ga

The

type

clas

sG

Rep

ga

isa

varia

tion

of R

epa

with

the

prop

osed

chan

geof

quan

-tif

icat

ion.

The

fact

that

goc

curs

atth

eto

p-le

vel

give

sus

the

extra

flexi

bilit

yth

atw

ene

edto

prov

ide

mor

ere

fined

cont

exts

t oth

em

etho

dg

rep

(whi

chco

rresp

onds

toth

em

etho

dre

p).

InF

igur

e1.

7w

ese

eho

wto

use

this

idea

toca

ptur

eal

lad

-hoc

disp

atch

ers

ina

sing

lede

finiti

on.

The

inst

ance

sof

GR

eplo

okj u

stlik

eth

ein

stan

ces

of

Rep

exce

ptth

atth

eyha

veth

eex

trap

aram

eter

gat

the

t op-

leve

l.Th

est

ruct

ural

case

s1

,+

and

×togetherwiththeb asecasesintandcharareallhandledinGeneric,t1h

,er+ efa

onred

we

orgeqe

uthiree

rg

titohbt h

ean

aisnseta

c nasce

esoi n

ftGa

ennder

c ihca.

rH aore

wa evll

eh r,a

fnodrl

e[ad]

annG d

Tnreer

eia

the

argu

men

tg

mus

tbe

cons

train

edb

yG

ener

icLi

stan

dG

ener

icTr

ee,

r esp

ectiv

ely,

sinc

eth

ese

are

the

type

clas

ses

that

h an

dle

thos

ety

pes.

The

r em

aini

ngco

nstra

ints

,of

the

form

GR

epg

a ,c

onta

inth

ene

cess

ary

info

rmat

ion

tope

rfor

mth

ere

curs

ive

Page 30: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

calls

.N

ow,

we

can

j ust

use

this

disp

atch

erto

obta

inan

exte

nsib

leen

code

b y

spec

ializ

ing

the

argu

men

tg

toE

ncod

e:

enco

de::

GR

epE

ncod

et⇒

t→[B

it]een

nccood

dee=:

:GGeR

nceopd

E en0

grep

13Fo

rpr

etty

prin

ters

we

can

j ust

use

the

sam

edi

spat

cher

,bu

tth

istim

eus

ing

Pre

ttyin

stea

dofE

ncod

e:

pret

ty::

GR

epP

retty

t⇒

t→D

ocpr

etty

=::G

G pRre

etptyP 0

grep

This

appr

oach

avoi

dsth

eex

trabo

ilerp

late

of

the

solu

tion

with

ad-h

ocdi

s-pa

tche

rsp

rese

nted

inSe

ctio

ns1.

3an

d1.

4r e

quiri

nga

sim

ilar

amou

ntof

wor

kto

the

orig

inal

GM

tech

niqu

e.S

till

itis

mod

ular

,an

dal

low

sus

tow

rite

age

neric

prog

ram

min

glib

rary

.In

apr

evio

usve

rsio

nof

this

p ap

er[1

3]w

eus

eda

tric

kpr

opos

edb

yH

ughe

s[6

]an

dal

sous

edby

L ¨a

mm

elan

dP

eyto

nJ o

nes

[8]

that

sim

ulat

edab

stra

ctio

nov

erty

pecl

asse

sus

ing

acl

ass

of

the

form

:

clas

sO

ver

twhe

re

Page 31: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

over

::t

Whi

leth

esa

me

effe

ctco

uld

beac

hiev

edus

ing

Ove

rin

stea

dof

GR

ep,

this

wou

ldbe

mor

ede

man

ding

onth

ety

pesy

stem

sinc

eth

ein

stan

ces

wou

ldno

tbe

lega

lin

Has

kell

98:

For

exam

ple,

the

inst

ance

for

GR

epg

Int

wou

ldb

ecom

eO

ver

(gI n

t).Th

efo

rmer

isle

gal

inH

aske

ll98

,th

ela

tter

isno

t.M

oreo

ver,

GR

epis

also

mor

efin

ely

type

dal

low

ing

usto

p re

cise

lysp

ecify

the

kind

of

the

“typ

ecla

ss”

that

we

are

abst

ract

ing

from

.S

ince

the

publ

icat

ion

of

the

orig

inal

vers

ion

of

this

p ap

er,

Sul

zman

nan

dW

ang

[16]

have

show

nho

wto

add

exte

nsib

lesu

perc

lass

esto

the

Has

kell

lan-

guag

e,w

hich

wou

ldco

nstit

ute

anot

her

solu

tion

toth

eex

tens

ibili

typr

oble

mfo

rG

Mge

neric

func

tions

.

1.6

DIS

CU

SS

ION

AN

DR

ELA

TE

DW

OR

K

Inth

isse

ctio

nw

ebr

iefly

rela

teou

rte

chni

que

toth

eex

pres

sion

prob

lem

[18]

and

disc

uss

som

eot

her

clos

ely

rela

ted

wor

k.

1.6.

1E

xpre

ssio

npr

oble

m

Wad

ler

[18]

iden

tifie

dth

ene

edfo

rex

tens

ibili

tyin

two

dim

ensi

ons

(add

ing

new

varia

nts

and

new

func

tions

)as

apr

oble

man

dca

lled

itth

eex

pres

sion

prob

lem

.

Page 32: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

Acc

ordi

ngto

him

,a

solu

tion

for

the

prob

lem

shou

ldal

low

the

defin

ition

of

ada

taty

pe,

the

addi

tion

of

new

varia

nts

tosu

cha

data

type

asw

ell

asth

ead

ditio

nof

new

func

tions

over

that

data

type

.A

solu

tion

shou

ldno

tre

quire

r eco

mpi

latio

nof

exis

ting

code

,an

dit

shou

ldbe

stat

ical

lyty

pesa

fe:

appl

ying

afu

nctio

nto

ava

riant

for

whi

chth

atfu

nctio

nis

not

defin

edsh

ould

r esu

ltin

aco

mpi

le-ti

me

erro

r.O

urso

lutio

nac

com

plis

hes

all

of

thes

efo

rth

epa

rticu

lar

case

of

gene

ricfu

nctio

ns.

Itsh

ould

bepo

ssib

leto

gene

raliz

eou

rte

chni

que

insu

cha

way

that

itca

nb e

appl

ied

toot

her

inst

ance

sof

the

expr

essi

onpr

oble

m.

For

exam

ple,

the

wor

kof

Oliv

eira

and

Gib

bons

[11]

,whi

chge

nera

lizes

the

GM

tech

niqu

eas

ade

sign

patte

rn,

coul

dbe

reca

stus

ing

the

tech

niqu

esof

this

p ap

er.

14Le

tus

anal

yze

the

role

of

each

type

clas

sof

our

solu

tion

inth

eco

ntex

tof

the

expr

essi

onpr

oble

m.

The

clas

sG

ener

icpl

ays

the

role

of

ada

taty

pede

finiti

onan

dde

clar

esth

eva

riant

sth

ata

llfu

nctio

nssh

ould

b ede

fined

for.

The

subc

lass

esof

Gen

eric

r epr

esen

tex

trava

riant

sth

atw

ead

d:no

ta

llfu

nctio

nsne

edto

b ede

fined

for

thos

eva

riant

s,bu

tif

we

wan

tto

use

afu

nctio

nw

ithon

eoft

hose

,th

enw

ene

edto

prov

ide

the

resp

ectiv

eca

se.

The

inst

ance

sof

Gen

eric

and

subc

lass

esar

eth

ebo

dies

of

our

exte

nsib

lefu

nctio

ns.

Fin

ally

,th

edi

spat

cher

allo

ws

u sto

enco

deth

edi

spat

chin

gbe

havi

our

for

the

exte

nsib

lefu

nctio

ns:

ifw

ead

da

new

varia

ntan

dw

ew

ant

tous

eit

with

our

func

tions

,w

em

ust

add

ane

win

stan

cefo

rth

atva

riant

.

Page 33: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

1.6.

2O

ther

rela

ted

wor

k

Gen

eric

Has

kell

(GH

)[9

]is

aco

mpi

ler

for

ala

ngua

geex

tens

ion

of

Has

kell

that

supp

orts

gene

ricp

rogr

amm

ing.

The

com

pile

rca

nge

nera

teH

aske

llco

deth

atca

nth

enb e

used

with

aH

aske

llco

mpi

ler.

Like

our

appr

oach

,G

Hus

essu

ms

ofp

rod-

ucts

for

view

ing

user

defin

edty

pes.

GH

can

gene

rate

the

b oi

lerp

late

code

requ

ired

for

new

data

type

sau

tom

atic

ally

.W

ith

our

appr

oach

we

need

tom

anua

llypr

ovid

eth

isco

de,b

utw

eco

uld

empl

oyan

addi

tiona

lto

olto

faci

litat

eits

gene

ratio

n.H

ow-

ever

,ou

rge

neric

func

tions

are

exte

nsib

le;

atan

ypo

int

we

can

add

anex

traad

-hoc

case

for

som

ege

neric

func

tion.

We

belie

veth

isis

of

maj

orim

porta

nce

sinc

e,as

we

have

been

argu

ing,

exte

nsib

lefu

nctio

nsar

ecr

ucia

lfo

ra

mod

ular

gene

ricpr

o-gr

amm

ing

libra

ry.

This

isno

tth

eca

sefo

rG

Hsi

nce

allt

hesp

ecia

lca

ses

need

tobe

defin

edat

once

.A

lso,

sinc

eG

His

anex

tern

alto

olit

isle

ssco

nven

ient

tous

e.W

ithou

rap

proa

ch,

all

we

h av

eto

dois

toim

port

the

mod

ules

with

the

gene

riclib

rary

.“D

eriv

able

Type

Cla

sses

”(D

TCs)

[5]

isa

prop

osed

exte

nsio

nto

Has

kell

that

allo

ws

usto

writ

ege

neric

defa

ult

case

sfo

rm

etho

dsof

aty

pecl

ass.

Inth

isap

-pr

oach

,da

taty

pes

are

view

edas

ifco

nstru

cted

b y

bina

rysu

ms

and

bina

ryp

rod-

ucts

,w

hich

mak

esit

acl

ose

rela

tive

ofb

oth

our

appr

oach

and

GM

.Th

em

ain

ad-

vant

age

of

DTC

sis

that

itis

triv

ial

toad

dad

-hoc

case

sto

gene

ricfu

nctio

ns,

and

Page 34: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

the

isom

orph

ism

sbe

twee

nda

taty

pes

and

thei

rst

ruct

ural

r epr

esen

tatio

ns(s

eeS

ec-

tion

1.2.

1)ar

eau

tom

atic

ally

gene

rate

dby

the

com

pile

r.H

owev

er,

the

appr

oach

perm

itson

lyge

neric

func

tions

onu

npar

amet

eriz

edty

pes

(type

so

fki

nd?)

,an

dth

eD

TC

impl

emen

tatio

nla

cks

the

abili

tyto

acce

ssco

nstru

ctor

info

rmat

ion,

pre-

clud

ing

the

defin

ition

of

gene

ricpa

rser

sor

pret

typr

inte

rs.

The

gene

ricex

tens

ion

toC

lean

[1]

u se

sth

eid

eas

ofD

TCs

and

allo

ws

the

defin

ition

ofg

ener

icfu

nctio

nson

type

so

fan

yki

nd.

L ¨a

mm

elan

dP

eyto

nJ o

nes

[8]

pres

ent

anot

her

appr

oach

toge

neric

prog

ram

-m

ing

b as

edon

type

clas

ses.

The

idea

issi

mila

rto

DTC

sin

the

sens

eth

aton

ety

pecl

ass

isde

fined

for

each

gene

ricfu

nctio

nan

dth

atde

faul

tm

etho

dsar

eus

edto

prov

ide

the

gene

ricde

finiti

on.

Ove

rrid

ing

the

gene

ricbe

havi

our

isas

sim

ple

asp

rovi

ding

anin

stan

cew

ithth

ead

-hoc

defin

ition

.Th

eap

proa

chsh

ares

DTC

’slim

itatio

nto

gene

ricfu

nctio

nson

type

so

fk in

d?

.O

nedi

ffere

nce

toou

rap

proa

chis

that

data

type

sar

eno

tm

appe

dto

aco

mm

onst

ruct

ure

cons

istin

go

fsu

ms

and

prod

ucts

.In

stea

d,ge

neric

defin

ition

sm

ake

use

of

asm

all

set

of c

ombi

nato

rs.

An-

15ot

her

diffe

renc

eis

that

thei

rap

proa

chre

lies

onso

me

adva

nced

exte

nsio

nsto

the

type

clas

ssy

stem

,w

hile

our

appr

oach

requ

ires

only

am

ulti-

para

met

erty

pecl

ass

or

even

j ust

Has

kell

98.

L o¨h

and

Hin

ze[1

0]pr

opos

ean

exte

nsio

nto

Has

kell

that

allo

ws

the

defin

ition

Page 35: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

of

exte

nsib

leda

taty

pes

and

exte

nsib

lefu

nctio

ns.

With

the

h el

pof

this

exte

nsio

n,it

isal

sopo

ssib

let o

defin

eex

tens

ible

gene

ricfu

nctio

ns,

onty

pes

of

any

kind

,in

Has

kell.

W h

ileth

eir

prop

osed

lang

uage

mod

ifica

tion

isr e

lativ

ely

smal

l,ou

rso

lutio

nha

sth

ead

vant

age

of

b ei

ngus

able

r igh

tno

w.

Furth

erm

ore,

we

can

give

mor

esa

fety

guar

ante

es:

inou

rse

tting

,a

call

toan

unde

fined

case

of

age

neric

func

tion

isa

stat

icer

ror;

with

open

data

type

s,it

resu

ltsin

apa

ttern

mat

chfa

ilure

.V

ytin

iotis

and

othe

rs[1

7]pr

esen

ta

lang

uage

whe

reit

ispo

ssib

leto

defin

eex

tens

ible

gene

ricfu

nctio

nson

type

sof

any

kind

,w

hile

guar

ante

eing

stat

icsa

fety

.W

hile

itis

not

ano

velty

that

we

can

defin

esu

chfle

xibl

ege

neric

func

tions

,w

ebe

lieve

itis

the

first

time

that

aso

lutio

nw

itha

llth

ese

feat

ures

ispr

esen

ted

inH

aske

ll,re

lyin

gso

lely

onim

plem

ente

dla

ngua

geco

nstru

cts

orev

enso

lely

onH

aske

ll98

.

1.7

CO

NC

LUS

ION

S

Inth

eG

Map

proa

chde

finin

gge

neric

func

tions

inH

aske

ll98

ispo

ssib

lebu

tit

isim

poss

ible

toex

tend

them

ina

mod

ular

way

.Th

eab

ility

tode

fine

exte

nsi-

ble

gene

ricfu

nctio

nis

very

impo

rtant

sinc

e,in

prac

tice,

mos

tge

neric

func

tions

have

ad-h

occa

ses.

Inth

isp

aper

we

p re

sent

edtw

ova

riatio

nsof

GM

that

allo

wth

ede

finiti

onof

gene

ricfu

nctio

nsth

atar

eb

oth

exte

nsib

lean

dm

odul

ar.

The

first

varia

tion,

like

the

orig

inal

GM

,ca

nb e

enco

ded

u si

ngH

aske

ll98

only

butr

equi

res

Page 36: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

extra

b oi

lerp

late

code

not

pres

ent

inth

eor

igin

alap

proa

ch.

The

seco

ndva

riatio

nre

quire

sa

mul

ti-pa

ram

eter

type

clas

s,an

exte

nsio

nto

Has

kell

98th

atis

supp

orte

dby

the

maj

orH

aske

llim

plem

enta

tions

.Th

isva

riatio

nst

illal

low

sex

tens

ibili

tyan

ddo

esno

tad

dan

ysi

gnifi

cant

b oi

lerp

late

over

the

orig

inal

GM

.O

neim

porta

ntas

pect

of

the

GM

and

our

enco

ding

isth

atdi

spat

chin

gge

neric

func

tions

isre

-so

lved

stat

ical

ly:

calli

nga

gene

ricfu

nctio

non

aca

seth

atis

not

defin

edfo

rit

isa

com

pile

-tim

eer

ror.

Base

don

the

resu

ltsof

this

p ap

er,

we

are

curr

ently

inth

epr

oces

sof

asse

m-

blin

ga

libra

ryof

frequ

ently

used

gene

ricfu

nctio

ns.

For

the

inte

rest

edre

ader

,th

eH

aske

llso

urce

code

for

this

pape

rca

nbe

foun

don

line

[12]

.

AC

KN

OW

LE

DG

EM

EN

TS

We

wou

ldlik

eto

than

kJe

rem

yG

ibbo

nsan

dFe

rm ı

´nR

eig

for

valu

able

sugg

es-

tions

and

disc

ussi

ons

abou

tth

isw

ork.

W e

wou

ldal

solik

eto

than

kth

ean

onym

ous

refe

rees

who

sere

view

sgr

eatly

cont

ribut

edfo

rim

prov

ing

the

pres

enta

tion

of

this

pape

r.Th

isw

ork

was

parti

ally

fund

edb

yth

eE

PS

RC

D a

taty

pe-G

ener

icP

rogr

am-

min

gan

dth

eD

FG

“Age

neric

f unc

tiona

lp

rogr

amm

ing

lang

uage

”pr

ojec

ts.

Page 37: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

16

RE

FE

RE

NC

ES

[1]

Art

emA

limar

ine

and

Mar

inus

J.P

lasm

eije

r.A

gene

ricpr

ogra

mm

ing

exte

nsio

nfo

rC

lean

.In

I mpl

emen

tatio

nof

Fun

ctio

nal

L an

guag

es,

p ag

es16

8–18

5,20

01.

[2]

Ric

hard

Bird

,O

ege

deM

oor,

and

Pau

lHoo

gend

ijk.

Gen

eric

func

tiona

lpr

ogra

mm

ing

with

type

san

dr e

latio

ns.

Jour

nal

of F

unct

iona

lP

rogr

amm

ing,

6(1)

:1–2

8,19

96.

[3]

Ral

fH

inze

.P

olyt

ypic

valu

espo

sses

spo

lyki

nded

type

s.In

Rol

and

Bac

khou

sean

dJ.

N.

Oliv

eira

,ed

itors

,P

roce

edin

gsoft

heF

ifth

I nte

rnat

iona

lC

onfe

renc

eon

M a

the-

mat

ics

ofP

rogr

amC

onst

ruct

ion,

J ul

y3–

5,20

00,

volu

me

1837

of

Lect

ure

N o

tes

inC

ompu

ter

Sci

ence

,pa

ges

2–27

.S

prin

ger-

Ver

lag,

2000

.

[4]

Ral

fH

inze

.G

ener

ics

for

the

mas

ses.

InI C

FP’0

4:P

roce

edin

gsof

the

nint

hA

CM

SIG

PLA

Nin

tern

atio

nal

conf

eren

ceon

F un

ctio

nal

p ro

gram

min

g,p

ages

236–

243.

AC

MP

ress

,20

04.

[5]

Ral

fH

inze

and

Sim

onP

eyto

nJo

nes.

Der

ivab

lety

pecl

asse

s.In

Gra

ham

Hut

ton,

edito

r,P

roce

edin

gsof

the

2 00

0A

CM

SIG

PLA

NH

aske

llW

orks

hop,

volu

me

41.

1o

fE

lect

roni

cN

ote

sin

The

oret

ical

Com

pute

rS

cien

ce.

Els

evie

rS

cien

ce,

Aug

ust

2001

.Th

ep

relim

inar

yp

roce

edin

gsap

pear

edas

aU

nive

rsity

of

Not

tingh

amte

chni

cal

re-

port

.

Page 38: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

[6]

J .H

ughe

s.R

estri

cted

data

type

sin

Has

kell.

InE

.Mei

jer,

edito

r,P

roce

edin

gsof

the

1999

Has

kell

Wor

ksho

p,nu

mbe

rU

U-C

S-1

999-

28,

1999

.

[7]

Pat

rikJa

nsso

n.F

unct

iona

lP

olyt

ypic

P ro

gram

min

g.P

hDth

esis

,C

halm

ers

Uni

vers

ityofT

echn

olog

y,M

ay20

00.

[8]

Ral

fL

¨am

mel

and

Sim

onP

eyto

nJo

nes.

Scr

apyo

urb

oile

rpla

tew

ithcl

ass:

exte

nsib

lege

neric

func

tions

.In

P ro

ceed

ings

oft

heA

CM

SIG

PLA

NIn

tern

atio

nal

Con

fere

nce

onF

unct

iona

lP

rogr

amm

ing

(IC

FP

2005

),pa

ges

204–

215.

AC

MPr

ess,

Sept

embe

r20

05.

[9]

And

res

L o¨h.

E x

plor

ing

Gen

eric

Has

kell.

PhD

thes

is,

Utre

cht

Uni

vers

ity,

2004

.

[10]

And

res

L¨o h

and

Ral

fH

inze

.O

pen

data

type

san

dop

enfu

nctio

ns.

Tech

nica

lR

epor

tIA

I-T

R-2

006-

3,In

stitu

tf u

¨rIn

form

atik

III,

Unive

rsita

¨tB

onn,

Febr

uary

2006

.

[11]

B.

Oliv

eira

and

J .G

ibbo

ns.

Type

case

:A

desi

gnpa

ttern

for

type

-inde

xed

func

tions

.In

Has

kell

Wor

ksho

p,p

ages

98–1

09,

2005

.

[12]

Bru

noO

livei

ra,

Ral

fH in

ze,

and

And

res

L¨o h.

Sou

rce

code

acco

mpa

nyin

g“E

xten

sibl

ean

dm

odul

arge

neric

sfo

rth

em

asse

s”.

Ava

ilabl

efr

omht

tp://

web

.com

lab.

ox.a

c.uk

/ouc

l/wor

k/br

uno.

oliv

eira

/.

[13]

Bru

noO

livei

ra,

Ral

fH

inze

,an

dA

ndre

sL o

¨h.G

ener

ics

asa

libra

ry.

InSe

vent

hS

ympo

sium

onTr

ends

inF

unct

iona

lP

rogr

amm

ing,

2006

.

[14]

Sim

onP

eyto

nJo

nes,

edito

r.H

aske

ll98

Lang

uage

andL

ibra

ries:

The

R e

vise

dR e

port.

Cam

brid

geU

nive

rsity

Pre

ss,

2003

.

Page 39: Modular Masses Extensible GenericsChapter 1 Extensible and Modular Generics for the Masses Bruno C. d. S. Oliveira1, Ralf Hinze2, Andres L o¨h2 Abstract: A generic function is a function

[15]

Chr

isto

pher

Stra

chey

.F

unda

men

tal

conc

epts

inp

rogr

amm

ing

lang

uage

s.Le

ctur

eN

otes

,In

tern

atio

nal

Sum

mer

Sch

ool

inC

ompu

ter

P ro

gram

min

g,C

open

hage

n,A

u-gu

st19

67.

Rep

rinte

din

H ig

her-

Ord

eran

dS

ymbo

licC

ompu

tatio

n,13

(1/2

),pp

.1–

49,

2000

.

[16]

Mar

tinS

ulzm

ann

and

M e

ngW

ang

.M

odul

arge

neric

p ro

gram

min

gw

ithex

tens

ible

supe

rcla

sses

.In

WG

P’0

6:P

roce

edin

gsof

the

2006

AC

MS

IGP

LAN

wor

ksho

pon

Gen

eric

p ro

gram

min

g,p

ages

55–6

5.A

CM

Pres

s,20

06.

17[1

7]D

imitr

ios

Vyt

inio

tis,

Geo

ffrey

W a

shbu

rn,

and

Step

hani

eW

eiric

h.A

nop

enan

dsh

utty

peca

se.

InT

LD

I’0

5:P

roce

edin

gsof

the

2 00

5A

CM

SIG

PLA

Nin

tern

atio

nal

wor

k-sh

opon

Type

sin

lang

uage

sd

esig

nan

dim

plem

enta

tion,

page

s13

–24,

New

Yor

k,N

Y,

US

A,

2005

.A

CM

Pres

s.

[18]

Phi

lipW

adle

r.Th

eex

pres

sion

prob

lem

.Ja

vaG

ener

icity

Mai

ling

list,

Nov

embe

r19

98.

[19]

Phi

lipW

adle

r.A

pret

tier

prin

ter.

InJe

rem

yG

ibbo

nsan

dO

ege

deM

oor,

edito

rs,

The

Fun

of P

rogr

amm

ing,

page

s22

3–24

4.P

algr

ave

M a

cmill

an,

2003

.