48
Framework Design Using Framework Design Using Function Generalization Function Generalization H. Conrad Cunningham H. Conrad Cunningham 1 Pallavi Tadepalli Pallavi Tadepalli 1 1 Yi Liu Yi Liu 2 1 Computer & Information Science Computer & Information Science University of Mississippi University of Mississippi 2 Electrical Engineering & Computer Electrical Engineering & Computer Science Science South Dakota State University South Dakota State University

Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

Embed Size (px)

Citation preview

Page 1: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

Framework Design Using Framework Design Using Function GeneralizationFunction Generalization

H. Conrad Cunningham H. Conrad Cunningham 11 Pallavi Tadepalli Pallavi Tadepalli 1 1

Yi Liu Yi Liu 22

11 Computer & Information Science Computer & Information Science University of MississippiUniversity of Mississippi

22 Electrical Engineering & Computer Science Electrical Engineering & Computer Science South Dakota State UniversitySouth Dakota State University

Page 2: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

22

OutlineOutline

Software frameworkSoftware framework MotivationMotivation Function generalizationFunction generalization Cosequential processingCosequential processing Binary tree traversalBinary tree traversal ConclusionConclusion Future workFuture work

Page 3: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

33

Software FrameworkSoftware Framework

Generic application allowing creation Generic application allowing creation of members of family of related of members of family of related programsprograms

Reusable design expressed as set of Reusable design expressed as set of abstract classes and way they abstract classes and way they collaboratecollaborate

Common and variable aspects known Common and variable aspects known as as frozen spotsfrozen spots and and hot spotshot spots

FrameworkFramework

library

User-supplied code(application specific)

Hot spots

Frozen spots

Page 4: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

44

MotivationMotivation

Nontrivial to identify needed hot spot Nontrivial to identify needed hot spot abstractionsabstractions

Difficult to specify hot spot behaviorsDifficult to specify hot spot behaviors

Need systematic generalization methodology Need systematic generalization methodology ExploreExplore function generalization function generalization

– incrementally generalize functional structure of incrementally generalize functional structure of specification to produce general applicationspecification to produce general application

– executable specification expressed as set of executable specification expressed as set of functions in Haskellfunctions in Haskell

Page 5: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

55

HaskellHaskell

Purely functional languagePurely functional language– forces explicit consideration of computational forces explicit consideration of computational

effects (no implicit state)effects (no implicit state) Polymorphic, higher-order, first-class Polymorphic, higher-order, first-class

functions & user-defined algebraic data functions & user-defined algebraic data typestypes– enables generic programming enables generic programming

Concise, equational notationConcise, equational notation– allows convenient mathematical manipulationallows convenient mathematical manipulation

Page 6: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

66

Function GeneralizationFunction Generalization

Create executable specification for a Create executable specification for a concrete application as Haskell programconcrete application as Haskell program

Define scope of familyDefine scope of family Identify frozen spots and hot spotsIdentify frozen spots and hot spots Analyze and design each hot spot systemAnalyze and design each hot spot system

– generalize Haskell program for hot spotgeneralize Haskell program for hot spot– transform simple function to generalized function transform simple function to generalized function

(e.g., with higher-order parameters)(e.g., with higher-order parameters) Transform generalized Haskell program to Transform generalized Haskell program to

Java frameworkJava framework

Page 7: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

77

ExamplesExamples

1.1. Cosequential processingCosequential processing

2.2. Binary tree traversalBinary tree traversal

Page 8: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

88

Cosequential ProcessingCosequential Processing

Coordinates processing of two input sequences Coordinates processing of two input sequences ordered by same total orderingordered by same total ordering

Create third sequence in incremental steps by Create third sequence in incremental steps by merging and matching input elementsmerging and matching input elements

Includes set operations and sequential file update Includes set operations and sequential file update applicationsapplications

A1…An

B1…Bn

CosequentialProcessing C1…Cn

Input sequences

Output sequence

Page 9: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

99

Binary Tree TraversalBinary Tree Traversal

procedure preorder(t)procedure preorder(t)

{ if t null, then return;{ if t null, then return;

perform visit action for root of tree t;perform visit action for root of tree t;

preorder(left subtree of t);preorder(left subtree of t);

preorder(right subtree of t);preorder(right subtree of t);

}}

21

18

3 20

50

30

1

Preorder 21 18 3 1 20 50 30

Page 10: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1010

Cosequential Cosequential ProcessingProcessing H. C. Cunningham and P. Tadepalli. “Using H. C. Cunningham and P. Tadepalli. “Using

Function Generalization to Design a Function Generalization to Design a Cosequential Processing Framework,” In Cosequential Processing Framework,” In Proceedings of the 39th Hawaii Proceedings of the 39th Hawaii International Conference on System International Conference on System Sciences (HICSS)Sciences (HICSS), 10 pages, IEEE, January , 10 pages, IEEE, January 2006.2006.

Page 11: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1111

Process A1 and B1

Cosequential ProcessingCosequential Processing

Coordinates processing of two input sequences Coordinates processing of two input sequences ordered by same total orderingordered by same total ordering

Create third sequence in incremental steps by Create third sequence in incremental steps by merging and matching input elementsmerging and matching input elements

Includes set operations and sequential file update Includes set operations and sequential file update applicationsapplications

A1…An

B1…Bn

CosequentialProcessing C1…Cn

Input sequences

Output sequence

Write result C1 to

output

Process Ai and BjWrite C2 to

output

Page 12: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1212

Executable SpecificationExecutable Specification(Merging two ascending integer (Merging two ascending integer sequences)sequences)

merge0 :: [Int]->[Int]->[Int]merge0 :: [Int]->[Int]->[Int]

merge0 [] ys = ysmerge0 [] ys = ys

merge0 xs [] = xsmerge0 xs [] = xs

merge0 xs@(x:xs’) ys@(y:ys’)merge0 xs@(x:xs’) ys@(y:ys’)

| x < y = x : merge0 xs’ ys| x < y = x : merge0 xs’ ys

| x == y = x : merge0 xs’ ys’| x == y = x : merge0 xs’ ys’

| x > y = y : merge0 xs ys’| x > y = y : merge0 xs ys’

Page 13: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1313

Framework ScopeFramework Scope Process two ordered sequences of Process two ordered sequences of

values to produce third ordered values to produce third ordered sequencesequence

Use ordering to restrict to a few Use ordering to restrict to a few current values from sequencescurrent values from sequences

Include classic examples:Include classic examples:– sequential file update programssequential file update programs– set and bag operationsset and bag operations

Page 14: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1414

Frozen SpotsFrozen Spots

1.1. Input sequences have same total orderingInput sequences have same total ordering2.2. Input sequences are immutableInput sequences are immutable3.3. Output sequence has same ordering as input Output sequence has same ordering as input

sequencessequences4.4. Incremental processingIncremental processing

– current element from each sequence examined current element from each sequence examined – at least one input sequence advanced by one at least one input sequence advanced by one

elementelement5.5. Appropriate action taken after examining Appropriate action taken after examining

current elements from each sequence current elements from each sequence

Represented by merge function in HaskellRepresented by merge function in Haskell

Page 15: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1515

Hot SpotsHot Spots

1.1. Variability in total orderingVariability in total ordering2.2. Variability in record formatVariability in record format3.3. Variability of input and output sequencesVariability of input and output sequences4.4. Variability of transformations Variability of transformations 5.5. Variability of source/destinationVariability of source/destination

Represented as additional functions, types, Represented as additional functions, types, and class definitions to and class definitions to mergemerge function function

Page 16: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1616

Hot Spot #1Hot Spot #1(Variability in total ordering)(Variability in total ordering)

Generalizes element type of sequences and Generalizes element type of sequences and associated comparison operatorsassociated comparison operators

Restricts polymorphic type of elements to class Restricts polymorphic type of elements to class OrdOrd ( (having usual relational operations)having usual relational operations)

Results in generalized comparisonsResults in generalized comparisons

merge1 :: merge1 :: Ord aOrd a => [ => [aa] -> [] -> [aa] -> [] -> [aa]]merge1 [] ys = ysmerge1 [] ys = ysmerge1 xs [] = xsmerge1 xs [] = xsmerge1 xs@(x:xs’) ys@(y:ys’)merge1 xs@(x:xs’) ys@(y:ys’)| x < y = x:merge1 xs’ ys| x < y = x:merge1 xs’ ys| x == y = x:merge1 xs’ ys’| x == y = x:merge1 xs’ ys’| x > y = y:merge1 xs ys’| x > y = y:merge1 xs ys’

Page 17: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1717

Hot Spot #1Hot Spot #1(Initial prototype as special (Initial prototype as special case)case)

If If merge1merge1 type variable type variable aa restricted restricted

to to Int, Int, thenthen

merge1 xs ys == merge0 xs ysmerge1 xs ys == merge0 xs ys

Page 18: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1818

Hot Spot #2Hot Spot #2(Variability in record format)(Variability in record format)

Allows elements of sequences to be records with Allows elements of sequences to be records with keyskeys

Adds Adds keykey extraction function as higher order extraction function as higher order parameterparameter

Results in generalized record formatResults in generalized record format

merge2 :: Ord b =>merge2 :: Ord b =>(a -> b) (a -> b) -> [a] -> [a]-> [a] -> [a] -> [a]-> [a]merge2 merge2 keykey [] ys = ys [] ys = ysmerge2 merge2 keykey xs [] = xs xs [] = xsmerge2 merge2 keykey xs@(x:xs’) ys@(y:ys’) xs@(x:xs’) ys@(y:ys’) | | key xkey x < < key ykey y = x:merge2 key xs’ ys = x:merge2 key xs’ ys | | key xkey x == == key ykey y = x:merge2 key xs’ ys’ = x:merge2 key xs’ ys’ | | key xkey x > > key ykey y = y:merge2 key xs ys’ = y:merge2 key xs ys’

Page 19: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

1919

Hot Spot #2Hot Spot #2(Keyless version is special (Keyless version is special case)case)

If If idid is the identity function, is the identity function, thenthen

merge2 id xs ys merge2 id xs ys == merge1 xs ys== merge1 xs ys

Page 20: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2020

Hot Spot #3Hot Spot #3(Variability of input and output (Variability of input and output sequences) sequences) Allows different element format in each sequenceAllows different element format in each sequence Requires separate Requires separate keykey extraction functions for each extraction functions for each Introduces transformation functions Introduces transformation functions Results in independent sequence formatsResults in independent sequence formats

merge3 merge3 kx ky tx tykx ky tx ty xs ys = mg xs ys xs ys = mg xs ys wherewhere mg [] ys = mg [] ys = map ty ysmap ty ys mg xs [] = mg xs [] = map tx xsmap tx xs mg xs@(x:xs’) ys@(y:ys’)mg xs@(x:xs’) ys@(y:ys’) | | kx xkx x < < ky yky y = = tx xtx x : mg xs’ ys : mg xs’ ys | | kx xkx x == == ky yky y == tx xtx x : mg xs’ ys’: mg xs’ ys’ || kx xkx x >> ky yky y = = ty yty y : mg xs ys’ : mg xs ys’

Page 21: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2121

Hot Spot #3Hot Spot #3(Multikey version is special (Multikey version is special case of case of single key version) single key version)

If If xsxs and and ysys have the same have the same type, thentype, then

merge3 key key id id xs ys merge3 key key id id xs ys

== merge2 key xs ys== merge2 key xs ys

Page 22: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2222

Hot Spot #4Hot Spot #4(Variability of transformations)(Variability of transformations)

Enables use of more general Enables use of more general transformations on inputtransformations on input

Introduces explicit Introduces explicit state state to record to record ongoing computation ongoing computation

Adds accumulating parameter to Adds accumulating parameter to maintain local state throughout maintain local state throughout processingprocessing

Transforms Transforms statestate to output at end of to output at end of input sequence processinginput sequence processing

Page 23: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2323

Variable Sequence Variable Sequence TransformationsTransformationsmerge4b kx ky merge4b kx ky tl te tg nex ney ttx ttytl te tg nex ney ttx tty

res sres s xs ys = mg xs ys = mg ss xs ys xs ys

wherewhere

mg mg ss [] ys = [] ys = res (foldl tty s ys)res (foldl tty s ys)

mg mg ss xs [] = xs [] = res (foldl ttx s xs)res (foldl ttx s xs)

mg mg ss xs@(x:xs’) ys@(y:ys’) xs@(x:xs’) ys@(y:ys’)

| kx x < ky y = mg | kx x < ky y = mg (tl s x y)(tl s x y) xs’ ys xs’ ys

| kx x == ky y = mg | kx x == ky y = mg (te s x y)(te s x y)

(nex s xs) (ney s ys)(nex s xs) (ney s ys)

| kx x > ky y = mg | kx x > ky y = mg (tg s x y)(tg s x y) xs ys xs ys

Page 24: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2424

Hot Spot #4Hot Spot #4(Progress requirement)(Progress requirement)

For each call of For each call of mgmg if (kx x == ky y) thenif (kx x == ky y) then

(length (nex s xs) < length xs) || (length (nex s xs) < length xs) ||

(length (ney s ys) < length ys)(length (ney s ys) < length ys)

else Trueelse True

Page 25: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2525

Hot Spot #4Hot Spot #4(Backward recursive version (Backward recursive version special special case of forward recursive case of forward recursive version)version)merge4b kx ky merge4b kx ky (\ss x y -> ss ++ [tx x])(\ss x y -> ss ++ [tx x]) -- tl-- tl (\ss x y -> ss ++ [tx x])(\ss x y -> ss ++ [tx x]) -- te-- te (\ss x y -> ss ++ [ty y])(\ss x y -> ss ++ [ty y]) -- tl-- tl (\ss xs -> tail xs)(\ss xs -> tail xs) -- nex-- nex (\ss ys -> tail ys)(\ss ys -> tail ys) -- ney-- ney (\ss x -> ss ++ [x])(\ss x -> ss ++ [x]) -- ttx-- ttx (\ss y -> ss ++ [y])(\ss y -> ss ++ [y]) -- tty-- tty ss xs ys ss xs ys

== ss ++ merge3 kx ky tx ty xs ys== ss ++ merge3 kx ky tx ty xs ys

Page 26: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2626

Hot Spot #5Hot Spot #5(Variability of (Variability of source/destination)source/destination) Allows diverse sources for inputs and Allows diverse sources for inputs and

destination for outputdestination for output No changes to No changes to merge4bmerge4b except its use except its use

– sequences already represented as sequences already represented as pervasive polymorphic list data typepervasive polymorphic list data type

– supply different input sequence argumentssupply different input sequence arguments– use result by different functionuse result by different function

Page 27: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2727

Transformation to Java Transformation to Java FrameworkFramework Drive using shape of Haskell program Drive using shape of Haskell program Use design patterns (Template, Strategy, Use design patterns (Template, Strategy,

etc.)etc.) Construct cosequential frameworkConstruct cosequential framework

– recursive legs become main while looprecursive legs become main while loop– nonrecursive legs become post-loop codenonrecursive legs become post-loop code– interfaces and classes represent various hot interfaces and classes represent various hot

spot generalizationsspot generalizations java.lang.Comparablejava.lang.Comparable for for OrdOrd KeyedKeyed for key extraction functions for key extraction functions

Page 28: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2828

Cosequential Processing Cosequential Processing Framework in JavaFramework in Java

public final void merge() // template methodpublic final void merge() // template method{ { advXs(); advYs();advXs(); advYs(); // uses Interators and Keyed // uses Interators and Keyed while(xsNotEmpty && ysNotEmpty)while(xsNotEmpty && ysNotEmpty) { int cmpxy = xKey.{ int cmpxy = xKey.compareTo(yKey);compareTo(yKey); // Ord as Comparable // Ord as Comparable if (cmpxy < 0) if (cmpxy < 0) { { transLt();transLt(); advXs(); } // tl advXs(); } // tl else if (cmpxy == 0)else if (cmpxy == 0) { { transEq(); advEqXs(); advEqYs();transEq(); advEqXs(); advEqYs(); } // te, nex, ney } // te, nex, ney elseelse { { transGt();transGt(); advYs(); } // tg advYs(); } // tg }} while (xsNotEmpty) { while (xsNotEmpty) { transYsEmpty();transYsEmpty(); advXs(); } // ttx advXs(); } // ttx while (ysNotEmpty) { while (ysNotEmpty) { transXsEmpty();transXsEmpty(); advYs(); } // tty advYs(); } // tty finish();finish(); // res // res}}

Page 29: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

2929

Applications of Applications of Cosequential FrameworkCosequential Framework

Implemented master-transaction file Implemented master-transaction file update program (bank account)update program (bank account)

Page 30: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3030

Binary Tree Binary Tree TraversalTraversal H. C. Cunningham, Y. Liu, and P. Tadepalli. H. C. Cunningham, Y. Liu, and P. Tadepalli.

“Framework Design Using Function “Framework Design Using Function Generalization: A Binary Tree Traversal Generalization: A Binary Tree Traversal Case Study,” In Case Study,” In Proceedings of the ACM Proceedings of the ACM SouthEast ConferenceSouthEast Conference, pp. 312-318, March , pp. 312-318, March 2006.2006.

Page 31: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3131

Binary Tree TraversalBinary Tree Traversal

procedure preorder(t)procedure preorder(t)

{ if t null, then return;{ if t null, then return;

perform visit action for root of tree t;perform visit action for root of tree t;

preorder(left subtree of t);preorder(left subtree of t);

preorder(right subtree of t);preorder(right subtree of t);

}}

21

18

3 20

50

30

1

Preorder 21 18 3 1 20 50 30

Page 32: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3232

Executable SpecificationExecutable Specification

data BinTree a data BinTree a

= Nil | Node(BinTree a) a BinTree a= Nil | Node(BinTree a) a BinTree a

preorder :: BinTree a -> [a]preorder :: BinTree a -> [a]

preorder Nil = []preorder Nil = []

preorder (Node l v r) preorder (Node l v r)

= v : preorder l ++ preorder r= v : preorder l ++ preorder r

Page 33: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3333

Framework ScopeFramework Scope

Standard kinds of depth-first Standard kinds of depth-first traversalstraversals

Flexible visit actions that are Flexible visit actions that are functions of accumulated state functions of accumulated state along traversalalong traversal

Other traversals orders (e.g. level Other traversals orders (e.g. level by level)by level)

Binary search trees, but not Binary search trees, but not multiway trees or graphs multiway trees or graphs

Page 34: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3434

Frozen SpotsFrozen Spots

1.1. Structure of tree (Structure of tree (BinTreeBinTree) not ) not redefined by clientsredefined by clients

2.2. Traversal accesses every node once Traversal accesses every node once (unless stopped early) (unless stopped early)

3.3. Traversal performs one or more visit Traversal performs one or more visit actions on access to node of treeactions on access to node of tree

Represented by a traversal function Represented by a traversal function in Haskellin Haskell

Page 35: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3535

Hot SpotsHot Spots

1.1. Variability in the visit operation’s Variability in the visit operation’s actionaction

2.2. Variability in ordering of visit action Variability in ordering of visit action with respect to subtree visitswith respect to subtree visits

3.3. Variability in tree navigation technique Variability in tree navigation technique (not just left-to-right, depth first)(not just left-to-right, depth first)

Represented as additional functions, Represented as additional functions, types, and class definitions to traversal types, and class definitions to traversal functionfunction

Page 36: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3636

Hot Spot #1Hot Spot #1(Generalizing the visit action)(Generalizing the visit action)

Represent visit action by update-state Represent visit action by update-state function (function (usus) passed into traversal ) passed into traversal functionfunction

Accumulate state along traversal pathAccumulate state along traversal path

gaPre :: gaPre :: (a -> b -> b) -> (b -> b) -> b(a -> b -> b) -> (b -> b) -> b

-> BinTree a -> b-> BinTree a -> b

gaPre gaPre us un isus un is t = po t is t = po t is

where po Nil s = un swhere po Nil s = un s

po (Node l v r) s po (Node l v r) s

= po r (po l (us v s))= po r (po l (us v s))

Page 37: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3737

Hot Spot #1Hot Spot #1(Initial prototype as special (Initial prototype as special case)case)

The following identity holds:The following identity holds:

gaPre (\x y -> y ++ [x]) id [] tgaPre (\x y -> y ++ [x]) id [] t== preorder t== preorder t

Page 38: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3838

Hot Spot #2Hot Spot #2(Generalizing the visit order)(Generalizing the visit order)

Allow visit actions at three points (Euler Allow visit actions at three points (Euler tour traversal)tour traversal)– first arrival (left)first arrival (left)– between subtree traversals (bottom)between subtree traversals (bottom)– before final departure (right)before final departure (right)

gvTraverse :: gvTraverse :: (a -> b -> b) -> (a -> b -> b) -> (a -> b -> b) -> (a -> b -> b)(a -> b -> b) -> (a -> b -> b) -> -> (b -> b) -> b -> BinTree a -> b(b -> b) -> b -> BinTree a -> bgvTraverse gvTraverse ul ub urul ub ur un is t = tr t is un is t = tr t iswherewhere tr Nil s = un str Nil s = un s tr (Node l v r) s tr (Node l v r) s = ur v (tr r (ub v (tr l (ul v s))))= ur v (tr r (ub v (tr l (ul v s))))

Page 39: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

3939

Hot Spot #2Hot Spot #2(Single visit action as special (Single visit action as special case)case)

The following identity holds:The following identity holds:

gvTraverse us id id un is t gvTraverse us id id un is t

== gaPre us un is t == gaPre us un is t

Page 40: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4040

Hot Spot #3Hot Spot #3(Generalizing the tree (Generalizing the tree navigation)navigation) Pass in tree navigation function (Pass in tree navigation function (navnav)) Navigation function generates a list of Navigation function generates a list of

update functions update functions Folding (composition) of list from initial Folding (composition) of list from initial

state generates traversalstate generates traversal

traverse traverse nav nav ua ub ud un is tua ub ud un is t

= compose = compose (nav ua ub ud un t)(nav ua ub ud un t) is is

where compose fs s where compose fs s

= foldl (flip (.)) id fs s= foldl (flip (.)) id fs s

Page 41: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4141

Hot Spot #3Hot Spot #3(Euler tour traversal as special (Euler tour traversal as special case)case)

euler ua ub ud un t = doEuler teuler ua ub ud un t = doEuler t

where doEuler Nil = [un]where doEuler Nil = [un]

doEuler (Node l v r)]doEuler (Node l v r)]

= [(ua v)] ++ doEuler l = [(ua v)] ++ doEuler l

++ [(ub v)] +++ [(ub v)] ++ doEuler r + doEuler r

++ [(ud v)]++ [(ud v)]

The following identity holds:The following identity holds: traverse euler ua ub ud un is t traverse euler ua ub ud un is t

=== gvTraverse ua ub ud un is t= gvTraverse ua ub ud un is t

Page 42: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4242

Transformation to Java Transformation to Java FrameworkFramework Drive using shape of Haskell program Drive using shape of Haskell program Use design patterns (Composite, Strategy, Use design patterns (Composite, Strategy,

Template, Visitor, etc.)Template, Visitor, etc.) Construct binary tree traversal frameworkConstruct binary tree traversal framework

– use Composite pattern to represent treeuse Composite pattern to represent tree– use Strategy pattern to encapsulate higher use Strategy pattern to encapsulate higher

order parameters of functions (i.e. state order parameters of functions (i.e. state update functions)update functions)

– use Visitor pattern to separate tree use Visitor pattern to separate tree navigation from the application of state navigation from the application of state updatesupdates

Page 43: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4343

Transformation to Java Transformation to Java FrameworkFramework

public class Node extends BinTreepublic class Node extends BinTree{ public Node(Object v, BinTree l, BinTree r){ public Node(Object v, BinTree l, BinTree r) { value = v; left = l; right = r; }{ value = v; left = l; right = r; } … … // accept a Visitor object// accept a Visitor object public void accept(BinTreeVisitor v) public void accept(BinTreeVisitor v) { v.visit(this); }{ v.visit(this); } … … private Object value; // instance dataprivate Object value; // instance data private BinTree left, right;private BinTree left, right;}}public interface BinTreeVisitorpublic interface BinTreeVisitor{ abstract void visit(Node t); { abstract void visit(Node t); abstract void visit(Nil t);abstract void visit(Nil t);}}

Page 44: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4444

Transformation to Java Transformation to Java FrameworkFramework

public class EulerTourVisitor implements BinTreeVisitorpublic class EulerTourVisitor implements BinTreeVisitor{ public EulerTourVisitor(EulerStrategy es, Object ts){ public EulerTourVisitor(EulerStrategy es, Object ts) { this.es = es; this.ts = ts; }{ this.es = es; this.ts = ts; } public void setVisitStrategy(EulerStrategy es) public void setVisitStrategy(EulerStrategy es) { this.es = es; }{ this.es = es; } public void visit(Node t) // Visitor hook implementationspublic void visit(Node t) // Visitor hook implementations { ts = es.visitLeft(ts,t); // upon first arrival{ ts = es.visitLeft(ts,t); // upon first arrival t.getLeft().accept(this);t.getLeft().accept(this); ts = es.visitBottom(ts,t); // upon return from leftts = es.visitBottom(ts,t); // upon return from left t.getRight().accept(this);t.getRight().accept(this); ts = es.visitRight(ts,t); // upon completion of nodets = es.visitRight(ts,t); // upon completion of node }} public void visit(Nil t) { ts = es.visitNil(ts,t); }public void visit(Nil t) { ts = es.visitNil(ts,t); } public Object getResult() { return ts; } public Object getResult() { return ts; } private EulerStrategy es; // encapsulate state change opsprivate EulerStrategy es; // encapsulate state change ops private Object ts; // traversal stateprivate Object ts; // traversal state}}

Page 45: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4545

ConclusionConclusion

Framework construction followed Framework construction followed function function generalization generalization

Each transformation produced an executable Each transformation produced an executable specificationspecification

Appropriate hooks (hot spot abstractions) Appropriate hooks (hot spot abstractions) defineddefined

Constructed cosequential processing Constructed cosequential processing framework with better understanding of hot framework with better understanding of hot spot behaviorsspot behaviors

Constructed general binary tree traversal Constructed general binary tree traversal framework with better understanding of hot framework with better understanding of hot spot behaviorsspot behaviors

Page 46: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4646

Future WorkFuture Work

Develop better guidelines for Develop better guidelines for generalizing Haskell programsgeneralizing Haskell programs

Investigate usage of Haskell features like Investigate usage of Haskell features like modules, classes, and monadsmodules, classes, and monads

Develop better guidelines for creating Develop better guidelines for creating Java frameworks from Haskell programsJava frameworks from Haskell programs

Conduct case studies of larger programsConduct case studies of larger programs Investigate usage of Scala instead of Investigate usage of Scala instead of

Haskell and Java Haskell and Java ☺☺

Page 47: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4747

AcknowledgmentsAcknowledgments

Cuihua Zhang (Northeast Lakeview Cuihua Zhang (Northeast Lakeview College)College)

Acxiom Corporation Acxiom Corporation University of MississippiUniversity of Mississippi South Dakota State University South Dakota State University

Page 48: Framework Design Using Function Generalization H. Conrad Cunningham 1 Pallavi Tadepalli 1 Yi Liu 2 1 Computer & Information Science University of Mississippi

4848

DiscussionDiscussion

Any questions or comments?Any questions or comments?