Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Type system for dimensional units

Name: Anonymous 2012-02-25 15:48

Are there any programming languages that let you specify dimensional units as part of the type information? It would add to the analyzability of code at compile time.
Something like this:
type Second;
type Minute = 60 Second;
type Hour = 60 Minute;
type Meter;
type Mile = 1609.344 Meter;
type DegreeC;
type DegreeF = DegreeC * 9/5 + 32;
float(Meter/Second) mps = (Mile/Hour)60; // converts Miles/Hour into Meters/Second
float(Meter/Second) x = (Hour)60; // type error

Name: Anonymous 2012-02-25 16:07

Name: Anonymous 2012-02-25 16:19

>>1
these are all derived classes of a unit-less number. And the definitions you gave correspond to constructor definitions.

Name: Anonymous 2012-02-25 17:16

Probably be able to do this using constructors, type classes, and (I don't know if haskell has this but) where you can make new datatypes of type classes. So:


data Second = Second Float
data Minute = Minute Float
-- etc.
class Time a where
-- i guess code to convert between the units or sth
instance Time Second where
--- ...


data Meter = Meter Float

data Time a, Distance b => Speed = Speed b a

c = Speed (Meter 3e8) (Second 1)


IDK, lol. I don't even know haskell can do this but probably this is how it would be done. Though it looks to be a pain. I'd rather just use matrices for everything.

On second though, nvm, haskell doesn't automatically coerce between types.

Name: Anonymous 2012-02-25 18:10

Name: Anonymous 2012-02-25 18:10

sorry that doesn't answer your question. i'm not sure if the units are part of an actual type system in frink

Name: Anonymous 2012-02-25 19:46

Doesn't Ada allow you to define types like this?

Name: Anonymous 2012-02-25 19:59

>>7
sweet binary triples bro

Name: Anonymous 2012-02-25 20:17

Sarah moaned as she slowly pushed the erect eggplant into her front port. Paralysed by terror, the eggplant tried to escape but its resistance only added to Sarah's pleasure.

Name: 3 2012-02-25 20:26

Although it is tricky. There are technically infinitely many types here. There is m, m/s, m/s^2. m/s^3. m/s^4... Infinitely many types. You couldn't hope to handle something like this in seeples. Maybe haskal can do it.

Name: Anonymous 2012-02-25 22:56

This can easily be done with C++ templates, and the new user defined literals in C++0x. ECMAScript 5 should also allow this style with user defined get/set implementations.

Name: Anonymous 2012-02-26 2:11

You can do it in GNU-C if you abuse the extensions.

Don't change these.
Name: Email:
Entire Thread Thread List