HaRe: Haskell Refactorer Advanced Functional Programming - mini-talk - - PowerPoint PPT Presentation

hare haskell refactorer
SMART_READER_LITE
LIVE PREVIEW

HaRe: Haskell Refactorer Advanced Functional Programming - mini-talk - - PowerPoint PPT Presentation

HaRe: Haskell Refactorer Advanced Functional Programming - mini-talk Tom Lokhorst 25 `feb` 2008 HaRe Haskell Refactorer 0.3 Refactoring Functional Programs project Computing Laboratory, University of Kent Simon


slide-1
SLIDE 1

HaRe: Haskell Refactorer

Advanced Functional Programming - mini-talk

λTom Lokhorst → 25 `feb` 2008

slide-2
SLIDE 2

HaRe

  • Haskell Refactorer 0.3
  • ‘Refactoring Functional Programs’ project
  • Computing Laboratory, University of Kent
  • Simon Thomson, Claus Reinke, Huiqing Li
  • From October 2003 to April 2005
  • Emacs & Vi bindings
slide-3
SLIDE 3

Refactoring

  • “Controlled technique for

improving the design of an existing code base”

  • Series of small, behavior-

preserving transformations, each of which “too small to be worth doing”

  • Popular in OO / XP worlds
slide-4
SLIDE 4

Refactoring demo

slide-5
SLIDE 5

Generalize definition

slide-6
SLIDE 6

Generalize definition

slide-7
SLIDE 7

Generalize definition

slide-8
SLIDE 8

Generalize definition

slide-9
SLIDE 9

Rename identifier

slide-10
SLIDE 10

Rename identifier

slide-11
SLIDE 11

Rename identifier

slide-12
SLIDE 12

Introduce new definition

slide-13
SLIDE 13

Introduce new definition

slide-14
SLIDE 14

Introduce new definition

slide-15
SLIDE 15

Lift to top level

slide-16
SLIDE 16

Lift to top level

slide-17
SLIDE 17

Common issues

  • More than indentation control or textual

search-and-replace

  • Preserve program functionality
  • Refactorings might only be valid under

certain side-conditions

slide-18
SLIDE 18

Common issues

  • More than indentation control or textual

search-and-replace

  • Preserve program functionality
  • Refactorings might only be valid under

certain side-conditions

showAll = table . map show where format = ... table = concat . format

slide-19
SLIDE 19

Common issues

  • More than indentation control or textual

search-and-replace

  • Preserve program functionality
  • Refactorings might only be valid under

certain side-conditions

showAll = table . map show where format = ... table = concat . format showAll = table format . map show where format = ... table format = concat . format

slide-20
SLIDE 20

Requirements

  • Tool needs to be:
  • aware of static semantics of the program
  • have knowledge of free variables
  • have access to type information
  • able to access multiple files
slide-21
SLIDE 21

Requirements (con’d)

  • Integration with existing development

environments

  • Layout style must not be changed, and

maintained after transformations

  • Comments need to be retained (and maybe

even updated)

slide-22
SLIDE 22

HaRe architecture

  • Gathering semantic information:
  • Programatica [extended]
  • Analyses / program transformations:
  • Strafunski
  • Interaction / integration:
  • Text based: Emacs & Vi
  • API to build specific refactorings
slide-23
SLIDE 23

“Use abstract syntax for abstract tasks, concrete syntax for concrete tasks; AST auxiliary, not intermediate representation; concrete updates on token stream”

slide-24
SLIDE 24

HaRe architecture old

Program source Prettyprinter AST Refactorer AST Parser

slide-25
SLIDE 25

HaRe architecture new

Lexer (pass1) Refactorer+prettyprinter AST Program source AST Token Stream Printer AST Updater Token Stream AST Token Stream Lexer Token Stream Program source Lexer (pass1) Parser + Lexer

slide-26
SLIDE 26

Building a refactoring

if isApproaching monster then runAway else investigate

slide-27
SLIDE 27

Building a refactoring

if isApproaching monster then runAway else investigate case isApproaching monster of True -> runAway False -> investigate

slide-28
SLIDE 28

module RefacCase (ifToCase) where import RefacUtils ifToCase fileName beginPos endPos = do (_, _, mod,ts) <- parseSourceFile fileName

  • - mod: the AST; ts: the token stream.

let exp = locToExp beginPos endPos mod ts case exp of (Exp (HsIf _ _ _))

  • > do

r <- applyRefac (worker exp) (Just(mod,ts)) fileName writeRefactoredFiles False [r] _ -> error "You haven’t selected a conditional expr!" where worker exp = applyTP (once_buTP (failTP ‘adhocTP‘ inExp)) where inExp exp1@((Exp (HsIf e e1 e2))::HsExpP) | sameOccurrence exp exp1 = let newExp = Exp (HsCase e [HsAlt loc0 (nameToPat "True") (HsBody e1) [], HsAlt loc0 (nameToPat "False")(HsBody e2) []]) in update exp1 newExp exp1 inExp _ = mzero

slide-29
SLIDE 29

Conclussions

  • Lots of refactorings available
  • Lots of papers pulished
  • Interesting architecture
  • Too bad it doesn’t work...
slide-30
SLIDE 30

More about HaRe

  • http://www.cs.kent.ac.uk/projects/refactor-fp/
  • Paper: “Tool Support for Refactoring

Functional Programs”