Determining type in a functional language

106 Views Asked by At

In a small Program users insert multiple typed formulas and wire them together in order to create a system of simultaneous equations. And I'd like to introduce runtime type checking, so as to report an error if dimensions do not match

Basical the types can be anything, e.g. Persons, Money, Money/Period, m^2/s^2 etc, and are entered as strings at runtime

I read about Hindley-Milner type inference, but thought that it's a bit overblown.

My idea was to rely on two rules:

first tokenize, then

If (a + b) or (a-b) the dimensions must match! [a] = [b]

if a*b then the dimensions are "expanded" or "contracted" in the sense that you keep track of the count of individual dimensions, e.g

Person/Year * Money/Year * Money*Year^2 ===> Money(2) Person(1) Year(2) / Year(1)

then subtract individual counts, thus leaving Money(2) Person(1) Year(1) as final type

this could be implemented as a tree walker, where the dimensional contraints are tested per (+/-)-node then simplifying tho yield the final answer

Is this a good way to solve this issue?

0

There are 0 best solutions below