Tool Support for Refactoring Functional Programs Huiqing Li Claus Reinke Simon Thompson Computing Lab, University of Kent

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

Why Do We Need a (Plan) Portability API? Gerd Breiter Frank Leymann Thomas Spatzier.
HaRe The Haskell Refactorer Huiqing Li Claus Reinke Simon Thompson Computing Lab, University of Kent
Progress on ‘HaRe: The Haskell Refactorer’ Huiqing Li, Claus Reinke, Simon Thompson Computing Laboratory, University of Kent Refactoring is the process.
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Introducing JavaScript
Software Engineering Session 14 INFM 603. Software Software represents an aspect of reality –Input and output represent the state of the world –Software.
Framework is l Reusable Code, often domain specific (GUI, Net, Web, etc) l expressed as l a set of classes and l the way objects in those classes collaborate.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
1 CS 501 Spring 2003 CS 501: Software Engineering Lecture 2 Software Processes.
1/18 CS 693/793 Lecture 09 Special Topics in Domain Specific Languages CS 693/793-1C Spring 2004 Mo, We, Fr 10:10 – 11:00 CH 430.
The Formalisation of Haskell Refactorings Huiqing Li Simon Thompson Computing Lab, University of Kent
Generic Haskell Where to start from. Issues Touched Language Ideas Tools Pitfalls.
CS 501: Software Engineering
Refactoring Haskell Programs Huiqing Li Computing Lab, University of Kent
WRT 2007 Refactoring Functional Programs Huiqing Li Simon Thompson Computing Lab Chris Brown Claus Reinke University of Kent.
The Haskell Refactorer, HaRe, and its API Huiqing Li Claus Reinke Simon Thompson Computing Lab, University of Kent
Building software from reusable components.
Software Issues Derived from Dr. Fawcett’s Slides Phil Pratt-Szeliga Fall 2009.
Generative Programming. Generic vs Generative Generic Programming focuses on representing families of domain concepts Generic Programming focuses on representing.
Course Instructor: Aisha Azeem
1 Integrated Development Environment Building Your First Project (A Step-By-Step Approach)
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 14Slide 1 Design with Reuse l Building software from reusable components.
Xactium xDSLs Run Models Not Code Tony Clark
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
CS 360 Lecture 3.  The software process is a structured set of activities required to develop a software system.  Fundamental Assumption:  Good software.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
DIY Refactorings in Wrangler Huiqing Li Simon Thompson School of Computing University of Kent.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
A Novel Approach to Architectural Recovery in Evolving Object- Oriented Systems PhD thesis Koen De Hondt December 11, 1998.
Hyper/J and Concern Manipulation Environment. The need for AOSD tools and development environment AOSD requires a variety of tools Life cycle – support.
Supported by ELTE IKKK, Ericsson Hungary, in cooperation with University of Kent Erlang refactoring with relational database Anikó Víg and Tamás Nagy Supervisors:
A Little Language for Surveys: Constructing an Internal DSL in Ruby H. Conrad Cunningham Computer and Information Science University of Mississippi.
Generative Programming. Automated Assembly Lines.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Supported by ELTE IKKK, Ericsson Hungary, in cooperation with University of Kent Erlang refactoring with relational database Anikó Víg and Tamás Nagy Supervisors:
Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ.
Programming Languages Seminar, Spring Grammar Adaptation (Ralf Lämmel, CWI) Presentation and slides by: Faizan Javed March 9 th, 2004.
Sheet 1 DocEng’03, Grenoble, November 2003 Model Driven Architecture based XML Processing Ivan Kurtev, Klaas van den Berg University of Twente, the Netherlands.
Towards the better software metrics tool motivation and the first experiences Gordana Rakić Zoran Budimac.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
1 CS 501 Spring 2004 CS 501: Software Engineering Lecture 2 Software Processes.
AP-1 4. Agile Processes. AP-2 Agile Processes Focus on creating a working system Different attitude on measuring progress XP Scrum.
Chapter 5: Software Re-Engineering Omar Meqdadi SE 3860 Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Demeter Aspects We study techniques for the emerging area of Aspect-Oriented Software Development and focus on the following areas:  Aspectual Collaborations.
LanguageLab A Meta-modelling Environment Terje Gjøsæter and Andreas Prinz, University of Agder, Norway SDL Forum 2015, Berlin, Germany.
Scalable Clone Detection and Elimination for Erlang Programs Huiqing Li, Simon Thompson University of Kent Canterbury, UK.
Topic 4 - Database Design Unit 1 – Database Analysis and Design Advanced Higher Information Systems St Kentigern’s Academy.
The Spoofax Language Workbench Rules for Declarative specification of language and IDEs Lennart C. L. Kats and Eelco Visser Delft University of Technology.
Concepts and Realization of a Diagram Editor Generator Based on Hypergraph Transformation Author: Mark Minas Presenter: Song Gu.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
CS223: Software Engineering
Software Development Process CS 360 Lecture 3. Software Process The software process is a structured set of activities required to develop a software.
Lectures 2 & 3: Software Process Models Neelam Gupta.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
SOFTWARE DESIGN AND ARCHITECTURE
Software Processes (a)
Compiler Lecture 1 CS510.
IDE and Visualisation of Abstract Syntax Trees for Coco/R
Objects First with Java
Implementing Language Extensions with Model Transformations
Parsing & Scanning Lecture 2
CSE401 Introduction to Compiler Construction
CHAPTER 10 METHODOLOGIES FOR CUSTOM SOFTWARE DEVELOPMENT
On the notion of Variability in Software Product Lines
System Reengineering Restructuring or rewriting part or all of a system without changing its functionality Applicable when some (but not all) subsystems.
Implementing Language Extensions with Model Transformations
Presentation transcript:

Tool Support for Refactoring Functional Programs Huiqing Li Claus Reinke Simon Thompson Computing Lab, University of Kent

Refactoring Functional Programs 3 year EPSRC-funded project at the University of Kent: –explore the prospects for refactoring in functional languages –catalogue useful refactorings and prototype tool support –look into differences between OO and FP refactoring –concrete focus: Haskell refactoring (would like to add Erlang) –collect and document Haskell design patterns (each refactoring changes from one design option to an alternative, implicitly documenting the pros and cons for both) –we’d like a real life refactoring tool for Haskell programming Now at end of year one; have focussed on: 1.refactoring case studies, initial catalogue 2.securing suitable infra-structure for building a refactorer for H98 3.first prototype exists (simple, local refactorings to stress-test 2)

btw, what is this “Refactoring”? Refactoring, the process: –is about “improving the design of existing code” (Fowler) –systematically changing program structure, without changing program functionality –representation-level implementation of design changes Refactorings, individual steps: –meaning-preserving program transformations.. Refactoring, context: –software maintenance (separate functional and structural changes, simplify the former by supporting the latter) –agile development processes: continuous design improvement and adaptation favoured over monolithic upfront design  Bug =  Feature = 0

Transformations, transformations,.. operational semantics, reduction to whnf program optimisation, source-to-source transformations to get more efficient code program derivation, calculating efficient code from obviously correct specifications refactoring, transforming code structure.. related themes, with substantial overlap, and common theory, but with different intentions

specification program result (program) core program derive refine, transform de-sugar optimise reduce optimise code+initial state code+final state compile de-compile execute Development by transformation where does Refactoring come in?

common result common specification refactor program core program derive de-sugar reduce Between development variants program’ core program’ derive de-sugar reduce variant Avariant B

refactor program program A modify program B modify common result common specification specification Aspecification B result A The better the support for refactoring, The less look-ahead guess-work needed to anticipate functional changes Refactoring vs modification

sum [] = 0 sum (h:t) = h + sum t main = print $ sum [1..4] A harmless little program

fold c n [] = n fold c n (h:t) = h `c` fold c n t sum = fold (+) 0 main = print $ sum [1..4] sum [] = 0 sum (h:t) = h + sum t main = print $ sum [1..4] But: shouldn’t you write it like this?

sum [] = 0 sum (h:t) = h + sum t main = print $ sum [1..4] fold c n [] = n fold c n (h:t) = h `c` fold c n t sum = fold (+) 0 main = print $ sum [1..4] fold c n l ¦ null l = n fold c n l ¦ otherwise = head l `c` fold c n (tail l) sum = fold (+) 0 main = print $ sum (cons 1 (cons 2 (cons 3 (cons 4 nil)))) Or like this?

… … … Or like what?

… … … Do not try to guess ahead: design minimally now, refactor if neccessary

Soft Ware design for now, refactor later (if necessary) traditional assumptions: –code freezes into first form –any change is expensive and error prone –you have to get everything right first time  substantial investment in upfront analysis & design needed refactoring changes those assumptions: –code remains malleable –structural changes can be inexpensive and safe –functional changes can be less expensive and safer –what is “right” can emerge and evolve after coding has started  incremental analysis & continuous adaptive redesign Sounds nice, but can that work?

mini demo

module Sum where sum [] = 0 sum (h:t) = h + sum t main = sum [1..4]

generalise definition module Sum where sum [] = 0 sum (h:t) = h + sum t main = sum [1..4]

generalise definition module Sum where sum [] = 0 sum (h:t) = h + sum t main = sum [1..4] name for new parameter?

generalise definition module Sum where sum [] = 0 sum (h:t) = h + sum t main = sum [1..4] name for new parameter? n

generalise definition module Sum where sum n [] = n sum n (h:t) = h + sum n t main = sum 0 [1..4]

generalise definition module Sum where sum n [] = n sum n (h:t) = h + sum n t main = sum 0 [1..4]

generalise definition module Sum where sum n [] = n sum n (h:t) = h + sum n t main = sum 0 [1..4] name for new parameter?

generalise definition module Sum where sum n [] = n sum n (h:t) = h + sum n t main = sum 0 [1..4] name for new parameter? c

generalise definition module Sum where sum c n [] = n sum c n (h:t) = h `c` sum c n t main = sum (+) 0 [1..4]

rename definition module Sum where sum c n [] = n sum c n (h:t) = h `c` sum c n t main = sum (+) 0 [1..4]

rename definition module Sum where sum c n [] = n sum c n (h:t) = h `c` sum c n t main = sum (+) 0 [1..4] new name?

rename definition module Sum where sum c n [] = n sum c n (h:t) = h `c` sum c n t main = sum (+) 0 [1..4] new name? fold

rename definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = fold (+) 0 [1..4]

introduce definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = fold (+) 0 [1..4]

introduce definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = fold (+) 0 [1..4] name of new definition?

introduce definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = fold (+) 0 [1..4] name of new definition? sum

introduce definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = sum [1..4] where sum = fold (+) 0

lift definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = sum [1..4] where sum = fold (+) 0

lift definition module Sum where fold c n [] = n fold c n (h:t) = h `c` fold c n t main = sum [1..4] sum = fold (+) 0

demo end

Tool support for refactoring recap: –source-level representation of design changes –meaning-preserving program transformations  need to manipulate source-code, but not as text (semantic editing) 1.Gathering semantic info –lexical/syntactic/static/type analyses 2.Editing I: analyses/program transformations –conditional rewrite rules + rewrite strategies 3.Editing II: interaction/integration –retranslation/faithful presentation at source-level –navigation/interaction/simple editing

Tool support for Haskell Tools (1) gathering semantic information Ideal: standard interface to semantic info in your favourite Haskell implementation?  not there yet.. Reuse code from one of the implementations/hack your own tool-specific frontend?  common practice Write or find reusable Haskell-in-Haskell frontend for meta-programming and Haskell tool development –parser/pretty printer: hsparser (haskell 98) –Type analysis: thih (haskell 98 + some variations) –p/pp+ta: hatchet (haskell 98, somewhat in limbo) –p/pp+ta+static analysis: programatica frontend (haskell 98 + some first extensions, under active development; see Thomas’ demo this afternoon)

Tool support for Haskell Tools (2) program analyses/transformation you’ve got your annotated AST (scopes,types,..) what about tool-specific analyses/transformations? –idea from optimiser implementations: combine rewrite rules and rewrite strategies in strategic programming dsl; implement your own traversals on top Stratego, Strafunski (the latter provides a Haskell library) –abstract Haskell grammar is complex and many-typed: if handwritten, the essence of traversals disappears in an unmaintainable deluge of boilerplate code  – Strafunski already addresses this problem, providing a generic strategy library as well as pre-processor support to instantiate it

Tool support for Haskell Tools (3) user interaction/integration in development environment Refactoring is a form of semantic editing, and needs to be integrated with standard development tools and processes Write-your-own Haskell editor/browser: –full control –zero acceptance –substantial extra work Interface to standard editor (Emacs/Vim): –restricted control, divergent standards –easier acceptance –reduced extra work

Tool support for Haskell Refactorer 1.Gathering semantic info –Programatica’s Haskell-in-Haskell frontend 2.Editing I: program transformations/analyses –Strafunski: strategy library and generic programming support (currently pre-processor-based) 3.Editing II: interaction/integration –Text interface to refactorer proper, used via shallow script bindings/GUI elements from Emacs and Vim … retranslation/faithful presentation at source-level

Theory vs practice, an example retranslation/faithful presentation at source-level initial (bad) idea: no problem –parse/analyse  transform  pretty print –most frontends throw away aspects of your code that you’d find quite essential (comments, layout) revised idea: that needs some thinking –preserve layout in annotated AST? –extract layout “style” and imitate that in pretty-printer?  use abstract syntax for abstract tasks, concrete syntax for concrete tasks; AST auxiliary, not intermediate representation; concrete updates on token stream

Conclusions Refactoring Functional Programs: –3-year project at U of Kent; at the end of first year –Prototype Haskell Refactorer, initial release after PLI (don’t use on production sources just yet, but try it out and give us feedback) –Over the next 2 years, prototype should develop into real-life tool – neither perfect, nor complete, but in daily use –Think about refactoring: it’ll change your programming, and we’d welcome your suggestions (we have our own unbounded list of more complex refactoring candidates, though;-) –Practice makes the difference (implementing ideas is important!) Connections to non-refactoring transformations –Should we provide an API for extensions (so you can extend our tool for program derivation, optimisation, or...)? Infrastructure for Haskell tool development is improving –time to undust your good ideas and implement them?