Inspiration

The lack of dimensional analysis software in most programming languages always caused me to make mostly preventable mistakes in my code. Also, the fact that NASA has crashed satellites into Mars in the past due to errors in dimensional analysis agreement kinda worries me, especially when you consider that we write code that continues to ignore the different dimensional units used all over the world and with the civilian and defense industry.

I wanted to be able to make a simple tool to be able to catch these errors while coding, as opposed to finding them out after the fact. In short, I wanted a program that gave me errors while I compile my code AND give me errors dynamically when used in a scripting language.

Dimensional Analysis also has a slight aesthetic appeal in my programs - when I write programs that compile properly and ensure that my answer is dimensionally correct, I can be a little more sure that my program is correct. Traditional, non-dimensionally typed code does not ensure that the answer 'makes sense' in physical reality and by constraining the code I write to be dimensionally correct, I can make my code better to work with.

What it does

The system takes in quantities any dimension possible and processes it. When given a number of quantities, the program returns an error if the expression is dimensionally incorrect or returns a final result that is independent of the dimensional units that were inputted. Also, the system has the ability to convert any given unit into any other unit. Which is pretty handy.

A few examples of how my parser can be used in scripting languages:

fromString "25 m/s"
-> Right 25.0 meter / second

fromString "25 m + 5 in + 12 ft + 60 m"
-> Right 88.7846 meter

fromString "25 m + 5 in + 12 ft + 60 m + 2 mile => mile"
-> Right 2.055168192754315 mile

fromString "25m + 5s"
-> Left DimensionError

As a dimensional aid in its own right, Dimensionality can be used within Haskell to provide functions that take into account dimensionality. This adds both the benefit of dimensional checks and the avoidance of manually typing units after each quantity. However, this feature is likely to be excluded to Haskell alone - most other languages do not have a type system that is comparable to Haskell's.

How I built it

I used Haskell's type system & DataKinds to implement a unit system that could take care of both elementary and complex dimensional units. The main focus behind the program is the parser, which allows you to input text in the form of a number and a unit, and returns a coherent quantity. The program is pretty much a hodgepodge of unit definitions, parsers and operations on quantities.

Challenges I ran into

Finding people who knew Haskell. People found: 0

Figuring out how to write a parser in 2 hours.

I worked mostly alone for the entirety of the project, so it was a challenge for me to be able to complete an entire utility in the span of the hackathon by myself.

Accomplishments that I'm proud of

Realizing that Haskell's type system is very well suited for dimensional analysis. This is the first hackathon I've used Haskell extensively for a project, with decent success!

Porting the dimensional analysis to other programming languages such as C and Python. Using a parser to no longer worry about dimensional analysis is a pretty big boon in my book.

What I learned

Writing parsers in Haskell is fun, interesting, and painful. Not an experience I would attempt again willingly.

What's next for Dimensionality

Porting the code to other function languages to other functional languages such as R or Scala. Providing conversion into LaTeX formulas for easy use in papers. The reverse conversion might be feasible with the SIUnits package.

Built With

Share this project:
×

Updates