Adding Dynamically-Typed Language Support to a Statically-Typed ...
Arithmetic with measures on dynamically typed object oriented languages
-
Upload
hernan-wilkinson -
Category
Software
-
view
52 -
download
1
Transcript of 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)
Máximo Prieto** Lifia – Facultad de Informática
Universidad Nacional de La Plata cc11, 1900, La Plata, Argentina
+54 221 422-8252 (ext. 215)
Luciano Romeo Mercap Software Architect
Tacuarí 202, 7mo Piso C1071AAF, Buenos Aires, Argentina
54-11-4878-1118
* Also Universidad de Buenos Aires, UBA, Argentina ** Also Universidad Nacional de la Patagonia Austral, Unidad Académica Caleta Olivia (UNPA-UACO)
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
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
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?
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!!!
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”
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
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
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 …
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
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
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
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
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
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
Measure Bags+measurements++ anArithmeticObject+* anArithmeticObject
MeasureBag
10 dollars + 20 euros
10 dollar
20 euros
++ anArithmeticObject+< anObject+amount+unit
Measure
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
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!
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
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
Questions