arithmetic with measures on dynamically typed object oriented languages

21
Arithmetic with Measures on Dynamically Typed OO Languages H ernán W ilkinson* M ercap D evelopm entM anager Tacuarí202,7m o Piso C 1071AAF,Buenos Aires,Argentina 54-11-4878-1118 (ext.120) [email protected] M áxim o Prieto** Lifia – Facultad de Inform ática U niversidad N acional de La Plata cc11,1900,La Plata,Argentina +54 221 422-8252 (ext.215) m axim o.prieto@ lifia.info.unlp.edu.ar Luciano R om eo M ercap Softw are Architect Tacuarí202,7m o Piso C 1071AAF,Buenos Aires,Argentina 54-11-4878-1118 l.romeo@ mercapsoftware.com * Also Universidad de Buenos Aires, UBA, Argentina ** Also Universidad Nacional de la Patagonia Austral, Unidad Académica Caleta Olivia (UNPA-UACO)

Upload: hernan-wilkinson

Post on 16-Jan-2017

52 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Arithmetic with measures on dynamically typed object oriented languages

Arithmetic with Measures on Dynamically Typed OO

Languages

Hernán Wilkinson* Mercap Development Manager

Tacuarí 202, 7mo Piso C1071AAF, Buenos Aires, Argentina

54-11-4878-1118 (ext. 120)

[email protected]

Máximo Prieto** Lifia – Facultad de Informática

Universidad Nacional de La Plata cc11, 1900, La Plata, Argentina

+54 221 422-8252 (ext. 215)

[email protected]

Luciano Romeo Mercap Software Architect

Tacuarí 202, 7mo Piso C1071AAF, Buenos Aires, Argentina

54-11-4878-1118

[email protected]

* Also Universidad de Buenos Aires, UBA, Argentina ** Also Universidad Nacional de la Patagonia Austral, Unidad Académica Caleta Olivia (UNPA-UACO)

Page 2: Arithmetic with measures on dynamically typed object oriented languages

Presentation of the Problem

Common mistakes in Financial Software Adding amounts of different currencies Applying interest rates with different time

units Confusing Nominal and Residual quantities

Mars Climate Orbiter Confused English and Metric Units

Star Wars laser-beam experiment Confused Miles with Feet

Page 3: Arithmetic with measures on dynamically typed object oriented languages

Why? We are NOT used to creating the right

ABSTRACTIONS We are accustomed to using “primitive

objects” Java/.Net still lacks a Fraction class! Java/.Net do not have a “DayOfMonth” class!

Therefore, we have: Duplicated Code all over Maintenance Problems Communication and Design Problems

Page 4: Arithmetic with measures on dynamically typed object oriented languages

Motivating Example Simple Interest Investment FormulafinalCapital:=initialCapital+(initialCapital*interestRate*investmentTime)

Algorithm to invest 100 dollars just with Numbers: Interest Rate: 10% per Year Investment Time: 6 MonthsinitialCapital := 100.

interestRate := 0.1. investmentTime := 6. finalCapital:=initialCapital+(initialCapital*interestRate*investmentTime) = 100 +( 100 * 0.1 * 6 ) = 160 Is it right?

Page 5: Arithmetic with measures on dynamically typed object oriented languages

Motivating Example Investing 100 dollars just with Measures:

Interest Rate: 10% per Year Investment Time: 6 Months

initialCapital := 100 dollars.interestRate := 0.1 / 1 year.investmentTime := 6 months.finalCapital:=initialCapital+(initialCapital*interestRate*investmentTime) = 100 dollars +( 100 dollars *0.1 / 1 year* 6 months ) = 105 dollars

We got a Measure!!! Not only a Number!!!

Page 6: Arithmetic with measures on dynamically typed object oriented languages

What have we done? We created new abstractions!

Our System has objects to represent 100 dollars, just like in real life

There are objects to represent 1 year and 6 months, JLIRL

We let those abstractions fulfill their responsibilities

We enriched our “Programming Language” with new “words”

Page 7: Arithmetic with measures on dynamically typed object oriented languages

Why do we need to do it? Numbers are too generic

2 ¿What does it mean? Units give numbers a particular meaning

2 dollars, 2 months, 2 miles Operations with numbers are context free

2 + 3 = 5 Operations with measures are context aware

3 days + 1 week = 10 days 2 dollars + 3 euros = ?... It depends

Page 8: Arithmetic with measures on dynamically typed object oriented languages

What do we want? Do arithmetic on Measures like in

physics The Measures to take care of unit

Simplification Validation Conversion

We want to relieve the programmer from those responsibilities

Page 9: Arithmetic with measures on dynamically typed object oriented languages

How did we do it? Definition by National Institute of Standards

and Technology (NIST) “The value of a measure is its magnitude expressed

as the product of a number and a unit, and the number multiplying the unit is the numerical value of the quantity expressed in that unit.”

We already have numbers… We need Units and we need to make them

collaborate with numbers to have Measures So …

Page 10: Arithmetic with measures on dynamically typed object oriented languages

Units+baseUnit (A)+sameDomainAs: aUnit+* aUnit (A)+/ aUnit (A)+convertToBaseUnit: aMeasure (A)

UnitBehavior

+* aUnit+/ aUnit

SimpleUnit

+baseUnit+convertToBaseUnit: aMeasure

BaseUnit+convertToBaseUnit: aMeasurement+convertFromBaseUnit: aMeasure (A)

DerivedUnit

+baseUnit+convertionFactor+convertFromBaseUnit: aMeasure

ProportionalDerivedUnit+baseUnit+convertionBlock+convertFromBaseUnit: aMeasure

NotProportionalDerivedUnit

+baseUnit+convertToBaseUnit: aMeasure

CompoundUnit

+* aUnit+/ aUnit

DividedUnit+* aUnit+/ aUnit

MultipliedUnit

day week

kelvin

faren heit

Page 11: Arithmetic with measures on dynamically typed object oriented languages

Measures++ anArithmeticObject (A)+- anArithmeticObject+* anArithmeticObject (A)+/ anArithmeticObject (A)

ArithmeticObject

+convertTo: aUnit using: aConverter (A)

MeasureBehavior

+amount (A)+unit (A)+< anObject (A)+convertTo: aUnit

ComparableMeasure+measurements++ anArithmeticObject+* anArithmeticObject

MeasureBag

++ anArithmeticObject+< anObject+amount+unit

Measure++ anArithmeticObject+< anObject+amount+unit

NullMeasure

dayweek

7 days

1 week

+baseUnit+convertToBaseUnit: aMeasure

BaseUnit+baseUnit+convertionFactor+convertFromBaseUnit: aMeasure

ProportionalDerivedUnit

Page 12: Arithmetic with measures on dynamically typed object oriented languages

Behavior Arithmetic operations

1 year + 6 months 18 months 5 days + 3 weeks 26 days 3 miles * 4 12 miles

Comparison 3 miles < 1 kilometer false

Conversion 1 day convertTo: hour 24 hours

Page 13: Arithmetic with measures on dynamically typed object oriented languages

Dynamically Typed Languages

Since Variables are not statically typed then…

We did not need to create a common interface or superclass

We could reuse exactly the same code for existing arithmetic expressions

Smalltalk is designed for supporting unexpected behavior so…

We did not change the Number implementation Therefore

We did not change a single line of code We just added new functionality

Page 14: Arithmetic with measures on dynamically typed object oriented languages

Other Languages Java:

+ is not a message, it is hardcoded in the VM, numbers are not objects

It is statically typed .Net:

operator + (Integer operand) Statically typed

Can not be changed

Page 15: Arithmetic with measures on dynamically typed object oriented languages

Compound Units

+baseUnit+convertToBaseUnit: aMeasure

CompoundUnit

+* aUnit+/ aUnit

DividedUnit+* aUnit+/ aUnit

MultipliedUnit

100 dollars / 10 hours

10 dollars/

hora

10 dollar/hora

dollar hora

100 miles * 10 miles

1000 miles2

1000mile*mile

mile

Page 16: Arithmetic with measures on dynamically typed object oriented languages

Measure Bags+measurements++ anArithmeticObject+* anArithmeticObject

MeasureBag

10 dollars + 20 euros

10 dollar

20 euros

++ anArithmeticObject+< anObject+amount+unit

Measure

Page 17: Arithmetic with measures on dynamically typed object oriented languages

Singularities of the Measure Domain

Equality 24 hours = 24 hours true 24 hours = 1 day true

Immutability Just like numbers

Zero 0 miles = 0 miles 0 miles = 0 feet 0 miles = 0 dollars ? But, by definition: 0 A = 0 B 0 miles = 0 dollars = 0 kelvin = 0

Page 18: Arithmetic with measures on dynamically typed object oriented languages

Financial Measures Interest Rate:

0.1 / 1 year – We can do it better!! (InterestRate yearlyOf: 10 %) value 0.1 /

1 year Price:

10 dollars/2 apples – We can do it better!! (Price of: 2 apples is: 10 dollars) value

10 dollars/2 apples Abstraction for Valuations!

Page 19: Arithmetic with measures on dynamically typed object oriented languages

Conclusions Generic Measure Model that:

Add vocabulary from the Problem Domain Supports Arithmetic Operations Encapsulates Unit Conversion

A Better Programming Language We believe this model should be part

of any programming environment

Page 20: Arithmetic with measures on dynamically typed object oriented languages

Future Work To reify the measure dimensions (distance,

force, etc.) Named composed units

Joule equivalent to m2*Kg*s-2 Pascal equivalent to m-1*Kg*s-2

To have units understand some messages to facilitate the creation of exponential units meter pow: 2 to represent square meter.

To restrict the amount of some measures to be valid

Page 21: Arithmetic with measures on dynamically typed object oriented languages

Questions