4 ms·
"One obvious use of types in physics that we have not explored in this work is the expression of physical dimensions (length, mass, time) and units (meter, kilo
by jaked89 12y ago
"One obvious use of types in physics that we have not explored in this work is the expression of
physical dimensions (length, mass, time) and units (meter, kilogram, second).
...This is not trivial to do with Haskell’s type
system because one wants multiplication to “multiply the units” as well as the numbers."
F# supports units:
https://msdn.microsoft.com/en-us/library/dd233243.aspx https://msdn.microsoft.com/en-us/library/dd233243.aspx
- Guvante 12y agoAnd note that there is no way to resolve this without some fundamental changes as Haskell requires that the two operands and the resultant type share a type. (Otherwise you could do some tricks with recursive types to accomplish this)
- wyager 12y ago>Haskell requires that the two operands and the resultant type share a type. Most physical unit libraries simply describe a "unit-ed" multiplication/division/etc operator, so you write code like let v = (5 ~* meter) / (1 *~ second) This division operator would replace the standard one. This requires using Haskell extensions that support dependent types, but the libraries certainly exist. I've used "Dimensional" before.
- Guvante 12y agoCertainly avoiding using naked `*` avoids the problem, I was talking specifically about that operator.
- JadeNB 12y agoI think that this is true only if you want to be an instance of `Num`, which makes sense: the collection of, say, lengths is not such an instance, because you cannot multiply two lengths and get a length. Nothing stops us from defining (simplified) data Unit a = Unit a [String] (*) (Unit a as) (Unit b bs) = Unit (a Main.* b) (as ++ bs) Of course, we will then have to disambiguate `` when we use it in the code. Another option would be to give it another name, like `unit` (or something less awful).
- Guvante 12y agoI didn't think there was a way to set `*` without losing `Num` (which is required for raw numbers in your code).
- spott 12y agoThere is also a haskell package for it: https://hackage.haskell.org/package/units https://hackage.haskell.org/package/units and the corresponding definitions for types: https://hackage.haskell.org/package/units-defs https://hackage.haskell.org/package/units-defs Though I haven't used them.
- pseudonom- 12y agoThere are several options for physical units of that sort in Haskell: https://wiki.haskell.org/Physical_units https://wiki.haskell.org/Physical_units. I remember being fairly satisfied with unittyped: https://bitbucket.org/xnyhps/haskell-unittyped/ https://bitbucket.org/xnyhps/haskell-unittyped/. Though, enabled by GHC 7.8, units may now be nicer: https://github.com/goldfirere/units https://github.com/goldfirere/units