HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A...
Transcript of HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A...
![Page 1: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/1.jpg)
HaReThe Haskell Refactorer
Huiqing LiClaus Reinke
Simon Thompson
Computing Lab, University of Kentwww.cs.kent.ac.uk/projects/refactor-fp/
![Page 2: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/2.jpg)
2
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe
• Current Work
• Future Work
![Page 3: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/3.jpg)
3
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe
• Current Work
• Future Work
![Page 4: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/4.jpg)
4
Refactoring
• What? Changing the structure of existing code …… without changing its meaning.
• Essential part of the functional programming process.
• Where? Development, maintenance, …-- to make the code easier to understand and modify-- to improve code reuse, quality and productivity.
• Not just programming … also proof, presentation, …
![Page 5: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/5.jpg)
5
A Simple Example
module Main where
pow = 2
f [] = 0f (h:t) = h^pow + f t
main = print $ f [1..4]
• The original code
• Refactoring 1: rename f to sumSquares to make thepurpose of the function clearer.
![Page 6: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/6.jpg)
6
A Simple Example (cont.)
• Code after renaming
• Refactoring 2: demote the definition of pow to makeits scope narrower.
module Main where
pow = 2
sumSquares [] = 0sumSquares (h:t) = h^pow + sumSquares t
main = print $ sumSquares [1..4]
![Page 7: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/7.jpg)
7
A Simple Example (cont.)
module Main where
sumSquares [] = 0sumSquares (h:t) = h^pow + sumSquares t
wherepow = 2
main = print $ sumSquares [1..4]
• Code after demoting
![Page 8: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/8.jpg)
8
Refactoring vs Program Optimisation
• Refactoring-- source-to-source
-- functionality-preserving
-- improve the design ofa program
-- diffuse and bureaucratic
-- bi-directional
• Program optimisation-- source-to-source
-- functionality-preserving
-- improve the efficiency ofa program
-- focused
-- unidirectional
![Page 9: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/9.jpg)
9
How to apply refactoring?
• By handTediousError-proneDepends on extensive testing
• With machine supportReliableLow cost: easy to make and un-make large changesExploratory: a full part of the programmers’ toolkit
![Page 10: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/10.jpg)
10
Refactoring Functional Programs
• 3-year EPSRC-funded project? Explore the prospects of refactoring functional programs
? Catalogue useful refactorings
? Look into the difference between OO and FP refactoring
? A real life refactoring tool for Haskell programming
? A formal way to specify refactorings, and a set of formal proofs that theimplemented refactorings are correct.
• Currently end of second year: the second HaRe is module-aware.
![Page 11: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/11.jpg)
11
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe
• Current Work
• Future Work
![Page 12: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/12.jpg)
12
HaRe – The Haskell Refactorer
-- A prototype tool for refactoring Haskell programs
-- Driving concerns: usability and solid basis for extensions.
-- Implemented in Haskell, using Programatica’s frontends andStrafunski’s generic programming technique.
-- Full Haskell 98 coverage
-- Integrated with the two program editors: Emacs and Vim
-- Preserves both comments and layout style of the source
![Page 13: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/13.jpg)
13
Refactorings in HaRe: Move Definition
e.g. demote/promote the definition of f
module Main where
f [] = 0f (h:t) = h^2 + f t
main = print $ f [1..4]
module Main where
main = print $ f [1..4]
where
f [] = 0
f (h:t) = h^2 + f t
<=>
• Move a definition--Demote a definition: move a definition down in the scopehierarchy to make its scope narrower.--Promote a definition: move a definition up in the scope hierarchyto widen its scope.
![Page 14: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/14.jpg)
14
Refactorings in HaRe: Generalise
module Main where
f [] = 0f (h:t) = h^2 + f t
main = f [1..4]
=>
e.g. generalise definition f on sub-expression 0 with new parametername n.
module Main where
f n [] = nf n (h:t) = h^2 + f n t
main = f 0 [1..4]
• Generalise a definition-- select a sub-expression of the rhs of the definition and introducethat sub-expression as a new argument to the function at each ofits call sites.
![Page 15: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/15.jpg)
15
Refactorings in HaRe … others
Released:• Rename• Introduce definition• unfold• Duplicate definition• Delete definition• Add/Remove an argument
Not yet released:• Move definition to another module• Clean imports• Make imports explicit• Add/Remove entity to/from exports• From algebraic data type to ADT (in progress)
![Page 16: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/16.jpg)
16
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe (hosted in Emacs)
• The Implementation of HaRe
• Current Work
• Future Work
![Page 17: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/17.jpg)
17
Demo
module Demo(sumSquares) where
sq x = x ^ 2
sumSquares [] = 0sumSquares (x:xs) = sq x + sumSquares xs
anotherFun = sumSquares [1..4]
![Page 18: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/18.jpg)
18
Generalise Definition
module Demo(sumSquares) where
sq x = x ^ 2
sumSquares [] = 0sumSquares (x:xs) = sq x + sumSquares xs
anotherFun = sumSquares [1..4]
![Page 19: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/19.jpg)
19
Generalise Definition
module Demo(sumSquares) where
sq x = x ^ 2
sumSquares [] = 0sumSquares (x:xs) = sq x + sumSquares xs
anotherFun = sumSquares [1..4]
name of new parameter?
![Page 20: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/20.jpg)
20
Generalise Definition
module Demo(sumSquares) where
sq x = x ^ 2
sumSquares [] = 0sumSquares (x:xs) = sq x + sumSquares xs
anotherFun = sumSquares [1..4]
name of new parameter? f
![Page 21: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/21.jpg)
21
Generalise Definition
module Demo(sumSquares, sumSquares_gen) where
sq x = x ^ 2
sumSquares f [] = 0sumSquares f (x:xs) = f x + sumSquares f xs
sumSquares_gen = sq
anotherFun = sumSquares sq [1..4]
![Page 22: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/22.jpg)
22
Generalise Definition
module DemoMain where
import Demo
ints = [1..10]
main = print $ sumSquares ints
![Page 23: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/23.jpg)
23
Generalise Definition
module DemoMain where
import Demo
ints = [1..10]
main = print $ sumSquares sumSquares_gen ints
![Page 24: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/24.jpg)
24
Move definition to another module
module DemoMain where
import Demo
ints = [1..10]
main = print $ sumSquares sumSquares_gen ints
Destination module name?
![Page 25: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/25.jpg)
25
Move definition to another module
module DemoMain where
import Demo
ints = [1..10]
main = print $ sumSquares sumSquares_gen ints
Destination module name? Demo
![Page 26: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/26.jpg)
26
Move definition to another module
module DemoMain where
import Demo
main = print $ sumSquares sumSquares_gen ints
module Demo(ints,sumSquares, sumSquares_gen) where
ints = [1..10]
sq x = x ^ 2sumSquares f [] = 0sumSquares f (x:xs) = f x + sumSquares f xs
sumSquares_gen = sq
anotherFun = sumSquares sq [1..4]
![Page 28: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/28.jpg)
28
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe
• Current Work
• Future Work
![Page 29: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/29.jpg)
29
The Implementation of HaRe
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq x + sq y
where sq :: Int->Intsq x = x ^ pow
pow = 2 :: Int
main = sumSquares 10 20
![Page 30: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/30.jpg)
30
The Implementation of HaRe
Step 1 : Identify the definition to be promoted.
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq x + sq y
where sq :: Int->Intsq x = x ^ pow
pow = 2 :: Int
main = sumSquares 10 20
![Page 31: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/31.jpg)
31
The Implementation of HaRe
Step 2: Is sq defined at top level here or in importing modules? Issq imported from other modules?
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq x + sq y
where sq :: Int->Intsq x = x ^ pow
pow = 2 :: Int
main = sumSquares 10 20
![Page 32: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/32.jpg)
32
The Implementation of HaRe
Step 3: does sq use any identifiers locally defined in sumSquares?
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq x + sq y
where sq :: Int->Intsq x = x ^ pow
pow = 2 :: Int
main = sumSquares 10 20
![Page 33: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/33.jpg)
33
The Implementation of HaRe
Step 4: If so, generalise to add these parameters and change typesignature.
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq pow x + sq pow y
where sq :: Int->Int->Intsq pow x = x ^ pow
pow = 2 :: Int
main = sumSquares 10 20
![Page 34: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/34.jpg)
34
The Implementation of HaRe
Step 5: Move sq to top level.
• An example: Promote the definition of sq to top level.-- This is an example
module Main where
sumSquares x y = sq pow x + sq pow y
where pow = 2 :: Int
sq :: Int->Int->Intsq pow x = x ^ pow
main = sumSquares 10 20
![Page 35: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/35.jpg)
35
The Implementation of HaRe
Informationgathering
Pre-conditionchecking
Programrendering
Programtransformation
• Basic steps
![Page 36: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/36.jpg)
36
The Implementation of HaRe
Informationgathering
Pre-conditionchecking
Programrendering
Programtransformation
• Basic steps
![Page 37: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/37.jpg)
37
The Implementation of HaRe
• Information required
-- Abstract Syntax Tree (AST): for finding syntax phrases, e.g.the definition of sq. (need parser & lexer)
-- Static semantics: for the scope of identifiers.
-- Type information: for type-aware refactorings.(need type-checker)
-- Module information: for module-aware refactorings.(need module analysis system)
![Page 38: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/38.jpg)
38
• Project at OGI to build a Haskell system …
• … with integral support for verification at various levels:assertion, testing, proof etc.
• The Programatica project has built a Haskell front end inHaskell, supporting syntax, static, type and moduleanalysis, and a lexer that preserves location info.
• … freely available under BSD licence.
![Page 39: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/39.jpg)
39
The Implementation of HaRe
Informationgathering
Pre-conditionchecking
Programrendering
Programtransformation
• Basic steps
![Page 40: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/40.jpg)
40
The Implementation of HaRe
-- Our initial experience-- A large amount of boilerplate code for each refactoring-- Tiresome to write and error prone.
-- Why?-- The large size of the Haskell grammar: about 20 algebraic
data types and the sum of 110 data constructors.-- Both program analysis and transformation involve traversing
the syntax tree frequently.
• Pre-condition checking and program transformation
![Page 41: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/41.jpg)
41
The Implementation of HaRe
• Example: code for renaming an identifierinstance Rename HsExp whererename oldName newName (Exp (HsId id))= Exp (HsId (rename oldName newName id))
rename oldName newName (Exp (HsLit x)) = Exp(HsLit x)rename oldName newName (Exp (HsInfixApp e1 op e2))= Exp (HsInfixApp (rename oldName newName e1)
(rename oldName newName op)(rename oldName newName e2))
rename oldName newName (Exp (HsApp f e))= Exp (HsApp (rename oldName newName f)
(rename oldName newName e))rename oldName newName (Exp(HsNegApp e))= Exp (HsNegApp (rename oldName newName e))
rename oldName newName (Exp(HsLambda ps e))=Exp (HsLambda (rename oldName newName ps)
(rename oldName newName e))
. . . (about 200 lines)
![Page 42: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/42.jpg)
42
• Programatica’s support for generic programming
-- A small selection of generic traversal operators.-- Defined as type class instances.-- 2-level scheme data type definitions.-- Sensitive to changes in grammars or traversals.
The Implementation of HaRe
![Page 43: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/43.jpg)
43
• Strafunski’s support for generic programming-- A Haskell library developed for supporting generic programming
in application areas that involve term traversal over large ASTs.
-- Allow users to write generic function that can traverse intoterms with ad hoc behaviour at particular points.
-- Offers a strategy combinator library StrategyLib and apre-processor based on DrIFT.
The Implementation of HaRe
• DrIFT – a generative tool .
… Strafunski: Lämmel and Visser
… DriFT: Winstanley, Wallace
![Page 44: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/44.jpg)
44
• Example: renaming an identifier using Strafunski
rename:: (Term t)=>PName->HsName->t->Maybe trename oldName newName = applyTP worker
whereworker = full_tdTP (idTP ‘adhocTP‘ idSite)
idSite :: PName -> Maybe PNameidSite v@(PN name orig)
| v == oldName = return (PN newName orig)idSite pn = return pn
The Implementation of HaRe
![Page 45: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/45.jpg)
45
• Our experience of using Strafunski-- Traversal combinators are extensively used during the
development of refactorings.-- Strafunski-style of programming makes the code concise.
(average 200 lines per primitive refactoring). Much of thecode lies on comment&layout preservation.
-- A combinator which combines TP(type-preserving) andTU(type-unifying) would be helpful.
-- Generic zipping is helpful too. (supported by the boilerplateapproach).
The Implementation of HaRe
![Page 46: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/46.jpg)
46
The Implementation of HaRe
Informationgathering
Pre-conditionchecking
Programrendering
Programtransformation
• Basic steps
![Page 47: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/47.jpg)
47
-- A real-life useful refactoring tool should preserve programlayout and comments.
but,
-- layout information and comments are not preserved in AST
-- the layout produced by pretty-printer may not be satisfactoryand comments are still missing
• Program rendering
The Implementation of HaRe
![Page 48: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/48.jpg)
48
• Program rendering -- example
The Implementation of HaRe
-- This is an example
module Main where
sumSquares x y = sq x + sq y
where sq :: Int->Intsq x = x ^ pow
pow = 2 :: Int
main = print $ sumSquares 10 20
-- program source before promoting definition sq to top level.
![Page 49: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/49.jpg)
49
• Program rendering -- example
The Implementation of HaRe
module Main wheresumSquares x y
= sq pow x + sq pow y where pow = 2 :: Int
sq :: Int->Int->Intsq pow x = x ^ pow
main = print $ sumSquares 10 20
-- program source from pretty printer after promoting .
![Page 50: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/50.jpg)
50
• Program rendering -- example
The Implementation of HaRe
-- program source using our approach after promoting .
-- This is an example
module Main where
sumSquares x y = sq pow x + sq pow y
where pow = 2 :: Int
sq :: Int->Int->Intsq pow x = x ^ pow
main = print $ sumSquares 10 20
![Page 51: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/51.jpg)
51
-- make use of the white space & comments in the token stream(the lexer output)
-- the refactorer takes two views of the program: the token streamand the AST
-- the modification in the AST guides the modification of thetoken stream.
-- after a refactoring, the program source is extracted from thetoken stream instead of from the AST
-- use heuristics for associating comments and semantics entities.
• Program rendering -- our approach
The Implementation of HaRe
![Page 52: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/52.jpg)
52
• The current implementation architecture
The Implementation of HaRe
PS: program source ; TS: token stream;AAST: annotated abstract syntax tree; MI: module information ;
Programatica(lexer, parser,type checker,
module analysis)
TS
AAST+ MI
PS TS
analysis andtransformation
usingStrafunski
AAST
extractprogram
from tokenstream
PS
![Page 53: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/53.jpg)
53
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe
• Current Work
• Future Work
![Page 54: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/54.jpg)
54
-- A refactoring may have effects in several modules
-- Effects and constraints can be subtle, choices have to be made.
-- A refactoring succeeds only if it succeeds on all affectedmodules in the project.
-- Built on top of Programatica’s module analysis system
-- Information needed: module graph, entities imported by amodule, entities exported by a module
-- What if the module is used by modules outside theproject? Notify the user or create a wrapper?
Making refactorings module-aware
![Page 55: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/55.jpg)
55
Making refactorings module-aware• Example: move a top-level definition f from module A to B.
-- Conditions:-- Is f defined at the top-level of B?-- Are the free variables in f accessible within module B?-- Will the move require recursive modules?
-- The transformation:-- Remove the definition of f from module A.-- Add the definition to module B.-- Modify the import/export in module A, B and the client
modules of A and B if necessary.-- Change the uses of A.f to B.f or f in all affected modules.-- Resolve ambiguity.
![Page 56: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/56.jpg)
56
From Algebraic Data Type to ADT
• A large-scale refactoring.• Can be decomposed into a series of primitive refactorings:
-- Introduce field labels-- Create discriminators-- Create constructors-- Remove nested patterns-- Remove patterns-- Move a set of declarations to a new module
• Need to compose primitive refactorings into one compositerefactoring.
![Page 57: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/57.jpg)
57
Outline
• Introduction
• HaRe: The Haskell Refactorer
• Demo of HaRe
• The Implementation of HaRe• Current Work
• Future Work
![Page 58: HaRe The Haskell Refactorer - Department of Computer ... fileHaRe – The Haskell Refactorer-- A prototype tool for refactoring Haskell programs-- Driving concerns: usability and solid](https://reader034.fdocuments.net/reader034/viewer/2022050718/5e18ddc89674c028aa019536/html5/thumbnails/58.jpg)
58
-- Other kinds of refactorings: type-aware, interface, structural, …
-- ‘Not quite refactorings’ and transformations …
-- An API for do-it-yourself refactoring.
-- A language for composing refactorings.
-- More complex interactions between the refactorer and the user.
-- Use HaRe in teaching.
Future work