Refactoring Erlang Programs



Similar documents
Microservices and Erlang/OTP. Christoph Iserlohn

Install Java Development Kit (JDK) 1.8

Visualization of C++ Template Metaprograms

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Unboxed Polymorphic Objects for Functional Numerical Programming

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

What's new in 3.0 (XSLT/XPath/XQuery) (plus XML Schema 1.1) Michael Kay Saxonica

Moving from CS 61A Scheme to CS 61B Java

6.170 Tutorial 3 - Ruby Basics

Visual Basic. murach's TRAINING & REFERENCE

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Programming Languages in Artificial Intelligence

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

The Needle Programming Language

Distributed Systems / Middleware Distributed Programming in Erlang

Some programming experience in a high-level structured programming language is recommended.

Regression Verification: Status Report

Semester Review. CSC 301, Fall 2015

Chapter 15 Functional Programming Languages

Lecture 1: Introduction

Chapter 7: Functional Programming Languages

Regular Expressions and Automata using Haskell

Glossary of Object Oriented Terms

THE BASIC SERVER IN ERLANG

Using Eclipse CDT/PTP for Static Analysis

Real SQL Programming 1

MAPREDUCE Programming Model

Lecture Data Types and Types of a Language

The C Programming Language course syllabus associate level

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

Lesson 4 Web Service Interface Definition (Part I)

Chapter 2: Remote Procedure Call (RPC)

Big Data looks Tiny from the Stratosphere

Java EE Web Development Course Program

Using Linux Clusters as VoD Servers

Building highly available systems in Erlang. Joe Armstrong

CS346: Database Programming.

Change Impact Analysis

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Eventia Log Parsing Editor 1.0 Administration Guide

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Erlang a platform for developing distributed software systems

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Erlang in Production. I wish I'd known that when I started Or This is nothing like the brochure :-(

Databases 2011 The Relational Model and SQL

2) Write in detail the issues in the design of code generator.

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Jedd: A BDD-based Relational Extension of Java

Getting Started with Erlang. version 5.4

Microsoft' Excel & Access Integration

The Cool Reference Manual

Chapter 5 Names, Bindings, Type Checking, and Scopes

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Functional Programming

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Intro to Embedded SQL Programming for ILE RPG Developers

Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0

irods and Metadata survey Version 0.1 Date March Abhijeet Kodgire 25th

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Programming Language Pragmatics

Fundamentals of Java Programming

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Messaging with Erlang and Jabber

Java SE 8 Programming

Architectural Analysis of Microsoft Dynamics NAV

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Data Tool Platform SQL Development Tools

Cover. SEB SIMOTION Easy Basics. Collection of standardized SIMOTION basic functions. FAQ April Service & Support. Answers for industry.

Some Scanner Class Methods

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Object-Oriented Databases

Formal Engineering for Industrial Software Development

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Experience from Developing the Dialyzer: A Static Analysis Tool Detecting Defects in Erlang Applications

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

PL / SQL Basics. Chapter 3

PostgreSQL Functions By Example

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Making Standard ML a Practical Database Programming Language

Apache Cassandra Query Language (CQL)

Using UML Part Two Behavioral Modeling Diagrams

8 Tutorial: Using ASN.1

SDMX technical standards Data validation and other major enhancements

Module: Software Instruction Scheduling Part I

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Oracle Database: SQL and PL/SQL Fundamentals

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Parameter passing in LISP

Japan Communication India Skill Development Center

EVALUATION. WA1844 WebSphere Process Server 7.0 Programming Using WebSphere Integration COPY. Developer

Transcription:

Erlang programs 1 Zoltán Horváth, László Lövei, Zoltán Csörnyei, Tamás Kozsik, Anikó Víg, Tamás Nagy, Roland Király, Melinda Tóth, Róbert Kitlei Dept. Programming Languages and Compilers Eötvös Loránd University, Budapest, Hungary Central-European Functional Programming School (CEFP 2007) 1 Supported by ELTE IKKK (GVOP-3.2.2.-2004-07-0005/3.0) and Ericsson Hungary.

Contents Erlang programs 1 Data Evaluation Program structure Concurrency and distribution 2 3 Erlang programs Transformations Case study Erlang refactorer

Erlang Erlang programs Data Evaluation Program structure Concurrency and distribution Target area: telecommunication software Concurrent and distributed Fault tolerant Soft real-time Other key features: Functional (high-level declarative syntax) Dynamic type system Light-weight processes Extensive support libraries (OTP)

Examples Erlang programs Data Evaluation Program structure Concurrency and distribution fact(1) -> 0; fact(n) -> N * fact(n-1). gcd(a,0) -> A; gcd(a,b) -> gcd(b, A rem B). gcd(a,b) -> if B==0 -> A; true -> gcd(b, A rem B) end.

Erlang programs Data in Erlang: Terms Data Evaluation Program structure Concurrency and distribution Number: integers and floats 0 42-9 3.14 2.3e-4 Atom: string-like data used as literals alphanumeric, begins with lowercase letter Variable: terms can be bound to variables Pi = 3.14 Label = yellow Variable names begin with an uppercase letter Variables are not variable!

Compound terms Erlang programs Data Evaluation Program structure Concurrency and distribution List Tuple Record String

Tuple Erlang programs Data Evaluation Program structure Concurrency and distribution Fixed-sized, immutable array of terms Enclosed in braces Tup = {date, 2007, 6, 22} size(tup) == 4 element(2, Tup) == 2007

List Erlang programs Data Evaluation Program structure Concurrency and distribution Variable-sized collection of terms Enclosed in brackets Head plus tail Short = [1,2] Long = [ 0 Short ] Long == [0, 1, 2] length(short) == 2 hd(short) == 1 tl(short) == [2] Short ++ [3] == [1, 2, 3]

String Erlang programs Data Evaluation Program structure Concurrency and distribution List of ASCII codes Usual special syntax "Bar" == [$B, $a, $r] == [66, 97, 114]

Record Erlang programs Data Evaluation Program structure Concurrency and distribution Relatively new feature Similar to tuple Fields are given names -record(date,[year,month,day]). DateOfBirth = #date{month=4,day=28,year=1970} NextBirthday = DateOfBirth#date{year=2008}

Control flow Erlang programs Data Evaluation Program structure Concurrency and distribution Sequence: evaluated for side effects distance( {X1,Y1}, {X2,Y2} ) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy). Branching on patterns: length(l) -> case L of [] -> 0; [Hd Tl] -> 1 + length(tl) end. Branching on conditions: if X > Y -> greater; X < Y -> less end

Recursion / loop Erlang programs Data Evaluation Program structure Concurrency and distribution Recursive function definitions length(l) -> case L of [] -> 0; [Hd Tl] -> 1+length(Tl) end. Loops by tail-recursion

Binding variables Erlang programs Data Evaluation Program structure Concurrency and distribution Each variable can be bound only once in its lifetime Multiple binding occurrences are possible gcd(x,x) -> X; gcd(x,y) -> if X > Y -> {NewX,NewY} = {X-Y,Y}; true -> {NewX,NewY} = {X,Y-X} end, gcd(newx,newy).

Functions Erlang programs Data Evaluation Program structure Concurrency and distribution Name is an atom, arguments are patterns Result is computed by clauses The first clause with a matching pattern is executed fact(1) -> sum([]) -> 0; 0; fact(n) -> sum([hd Tl]) -> N * fact(n-1). Hd + sum(tl).

Pattern matching Erlang programs Data Evaluation Program structure Concurrency and distribution Formal arguments of function clauses sum([]) -> 0; sum([hd Tl]) -> Hd + sum(tl). Branching expressions length(l) -> case L of [] -> 0; [Hd Tl] -> 1+length(Tl) end. Binding variables Long = [ 0, 1, 2 ], [0, Short] = Long

Pattern matching Erlang programs Data Evaluation Program structure Concurrency and distribution Multiple roles Decomposes terms Looks up values Binds variables Same syntax as a term, but may contain unbound variables Puts an implicit condition on the matched term Match failures generate runtime errors

Modules Erlang programs Data Evaluation Program structure Concurrency and distribution Functions are compiled in modules Module name: atom (same as the file name) Export list: functions callable from outside -module(factorial). -export([fact/1]). fact(1) -> 0; fact(n) -> N * fact(n-1). factorial:fact(5) == 120

Types Erlang programs Data Evaluation Program structure Concurrency and distribution No types declared for functions and variables Function signature: module, name, arity Dynamic typing Pattern matching might result in run-time error Flexibility, polymorphism Branching on patterns: case get_value() of {X, Y} -> tuple; [X, Y] -> list end

Erlang programs Standard environment Data Evaluation Program structure Concurrency and distribution Built-in functions (BIFs): implemented in the runtime system length, element, is_tuple, atom_to_list etc. STDLIB: a set of useful modules math:sqrt(25) == 5.0000 lists:seq(3,6) == [3,4,5,6] string:substr("foobar", 2, 3) == "oob" io:format("~b: ~s~n", [42, answer])

Erlang programs Lambda expressions Data Evaluation Program structure Concurrency and distribution Unnamed functions stored/passed as data F(3) == 4 F = fun(x) -> X+1 end Useful for writing or using generic functions lists:map(f, [1,2,3]) == [2,3,4] lists:filter( fun(x) -> X > 0 end, [2, -3, 4, -1] ) == [2,4]

Concurrency Erlang programs Data Evaluation Program structure Concurrency and distribution Based on light-weight processes Communicate by message passing The spawn BIF starts a new process Pid = spawn(fun() -> main() end) A process can be assigned a name (an atom) register(johnny_b_good, Pid) Distribution: processes on different Erlang nodes can communicate transparently

Communication Erlang programs Data Evaluation Program structure Concurrency and distribution Processes can use message passing Any term can be sent as a message ping() -> Pid = spawn(fun() -> pong() end), Pid! {ping, self()}, receive pong -> ok; after 1000 -> timeout end. pong() -> receive {ping, From} -> From! pong end.

Erlang programs Change the structure of the code Rename a variable/function/module... Extract function, inline function Pull up method Goals Increase quality Prepare for further development or for subsequent transformations Preserve semantics

Erlang programs tools is hard (error-prone) to to by hand Simultaneous changes (often global) Conditions for admissibility Tool support Mostly for OOP Less work on FP Haskell (HaRe, Univ. Kent) Clean (prototype, ELU) Erlang (ELU, Univ. Kent)

Erlang programs in Erlang Transformations Case study Erlang refactorer Set of transformations differs from that for OOP FP, so no(t much) side effects Not much statically available information Dynamically typed Rules (e.g. scoping) in RM are based on operational semantics Higher-order functions Reflective programs Communication

How to do Erlang programs Transformations Case study Erlang refactorer Reformulate the semantic rules Perform static analysis Assume guidelines and conventions Refactor well-written programs Focus on Erlang/OTP Specify clearly the capabilities

Erlang programs Implemented transformations Transformations Case study Erlang refactorer Rename variable Rename function Merge subexpression duplicates Eliminate variable Extract function Reorder function arguments Tuple function arguments Currently working on: Introduce records

Rename variable Erlang programs Transformations Case study Erlang refactorer Affects a single function clause Variable scoping rules sum_pos(x) -> sum(lists:filter(fun(x) -> X>0 end, X)). sum_pos(l) -> sum(lists:filter(fun(x) -> X>0 end, L)).

Rename function Erlang programs Transformations Case study Erlang refactorer Exported functions called from other modules Higher-order functions, reflective calls max(a,b,c) -> max({ A, max({b,c}) }). max({a,b}) -> if A>B -> A; true -> B end. max(a,b,c) -> maxt({ A, maxt({b,c}) }). maxt({a,b}) -> if A>B -> A; true -> B end.

Erlang programs Transformations Case study Erlang refactorer Merge subexpression duplicates Finding occurrences of the same expression Variables should have the same bindings No side-effects sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy)

Eliminate variable Erlang programs Transformations Case study Erlang refactorer Inverse of Merge subexpression duplicates Unique binding at every elimination position Each variable occurring in the bound term is visible at every elimination position No side-effects DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy) sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

Extract function Erlang programs Transformations Case study Erlang refactorer Variables with possible binding occurrences should not appear outside of extracted sequence of expression gcd(x,x) -> X; gcd(x,y) -> {A,B} = if X > Y -> {X,Y}; true -> {Y,X} end, gcd(a-b,b). sort(x,y) = if X>Y -> {X,Y}; true->{y,x} end. gcd(x,x) -> X; gcd(x,y) -> {A,B} = sort(x,y), gcd(a-b,b).

Erlang programs Reorder function arguments Transformations Case study Erlang refactorer perimeter({x1,y1},{x2,y2},{x3,y3}) -> dist(x1,x2,y1,y2) + dist(x1,x3,y1,y3) + dist(x3,x2,y3,y2). dist(x1,x2,y1,y2) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy). perimeter({x1,y1},{x2,y2},{x3,y3}) -> dist(x1,y1,x2,y2) + dist(x1,y1,x3,y3) + dist(x3,y3,x2,y2). dist(x1,y1,x2,y2) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy).

Erlang programs Tuple function arguments Transformations Case study Erlang refactorer perimeter({x1,y1},{x2,y2},{x3,y3}) -> dist(x1,y1,x2,y2) + dist(x1,y1,x3,y3) + dist(x3,y3,x2,y2). dist(x1,y1,x2,y2) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy). perimeter({x1,y1},{x2,y2},{x3,y3}) -> dist({x1,y1},{x2,y2}) + dist({x1,y1},{x3,y3}) + dist({x3,y3},{x2,y2}). dist({x1,y1},{x2,y2}) -> % signature change DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy).

Introduce record Erlang programs Transformations Case study Erlang refactorer perimeter({x1,y1},{x2,y2},{x3,y3}) -> dist({x1,y1},{x2,y2}) + dist({x1,y1},{x3,y3}) + dist({x3,y3},{x2,y2}). dist({x1,y1},{x2,y2}) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy). -record(point,[x,y]). perimeter(p1,p2,p3) -> dist(p1,p2) + dist(p1,p3) + dist(p3,p2). dist(#point{x=x1,y=y1},#point{x=x2,y=y2}) -> DX=X1-X2, DY=Y1-Y2, sqrt(dx*dx+dy*dy).

Chat server Erlang programs Transformations Case study Erlang refactorer Client-server application Distributed Follow the slides! -module(srv). -export([start/1, stop/0]). -export([connect/2, disconnect/1, send/2]). -define(client, cli). % ------------------ Interface functions % -------------------- Client interface % --------------- Implementation functions

Interface functions Erlang programs Transformations Case study Erlang refactorer -module(srv). -export([start/1, stop/0]). -export([connect/2, disconnect/1, send/2]). -define(client, cli). % ------------------ Interface functions start(max) -> register(chatsrv, spawn(fun()->init(max) end)). stop() -> chatsrv! stop.

Client interface Erlang programs Transformations Case study Erlang refactorer connect(srv, Nick) -> {chatsrv, Srv}! {connect, self(), Nick}, receive ok -> ok; deny -> deny after 1000 -> timeout end. disconnect(srv) -> {chatsrv, Srv}! {disconnect, self()}. send(srv, Text) -> {chatsrv, Srv}! {text, self(), Text}.

Erlang programs Implementation functions 1 Transformations Case study Erlang refactorer init(max) -> loop([], Max, 0). loop(users, Max, Nr) ->... send(_, _, []) -> ok; send(msg, Nr, [Pid Users]) -> (?CLIENT):send(Pid, "[" ++ (integer_to_list(nr) ++ ("]" ++ Msg)) ), send(msg, Nr, Users).

Erlang programs Transformations Case study Erlang refactorer loop(users, Max, Nr) -> receive stop -> ok; {connect, Pid, Nick} -> if length(users) >= Max -> Pid! deny, loop(users, Max, Nr); true -> {link(pid),pid}! ok, NewUsers = [{Nick, Pid} Users], loop(newusers, Max, Nr) end; {text, From, Text} -> [{Nick, _}] = lists:filter(fun({_, Pid}) -> Pid == From end,users), Msg = Nick ++ (": " ++ Text), send(msg, Nr + 1, Users), loop(users, Max, Nr + 1); {disconnect, Pid} -> NewUsers = lists:filter(fun({_, P}) -> P /= Pid end, Users), loop(newusers, Max, Nr) end.

Erlang refactorer Erlang programs Transformations Case study Erlang refactorer Univ. Kent: traversal of annotated AST ELU Graph representation of programs Semantic information is represented as labeled edges Graphs are stored in DB Transformations implemented using SQL User interface: integrated into Emacs Cooperation

Erlang programs is useful for industry in Erlang is difficult, but possible Tool being developped by ELU and Univ. Kent Seven transformations are already implemented Try them out at the lab!