[swift-evolution] Epic: Typesafe calculations

Árpád Goretity arpad.goretity at gmail.com
Tue Dec 29 02:45:04 CST 2015


@Howard: While we are at implementation details: I think this is the
typical case of what type theorists call "structural equivalence". You can
think of Newton as a typedef (alias) for kg * m / s^2. Hence, if the
compiler can look this info up, it can most definitely determine that they
are the same and so is m * kg / s / s.

Yes, care needs to be taken about the associativity and commutativity of
multiplication of units, but that's not too hard either, it basically
amounts to some pattern matching along with symbolic computations
respecting algebraic identities (think of it as a compile-time "unit
interpreter") and maybe some post-normalization.



On Tue, Dec 29, 2015 at 9:12 AM, Austin Zheng via swift-evolution <
swift-evolution at swift.org> wrote:

> +1; first-class affordances to make dimensional analysis possible would be
> incredibly useful.
>
> Austin
>
> > On Dec 29, 2015, at 12:11 AM, Howard Lovatt via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > +1 for the ability to unit check expressions. It is harder to do than it
> sounds because there are many equivalent units, for example force N = mass
> kg * acceleration m/s^2. Therefore N, kg m/s^2, m/s^2 kg, etc. are all
> equal.
> >
> > Sent from my iPad
> >
> >> On 28 Dec 2015, at 9:33 AM, Greg Titus via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>
> >>
> >>> On Dec 27, 2015, at 2:56 AM, Tino Heth <2th at gmx.de> wrote:
> >>>
> >>>
> >>>> There’s some unfortunate extra boilerplate here, which could be
> better handled with newtype support in the language, but when compiled with
> optimizations the resulting code is nearly identical to using plain Ints.
> >>>
> >>> Cool — have you checked the generated assembler for this conclusion?
> But I guess there is some knowledge on how to build an optimizing compiler
> in the core team ;-), so I'd expect little to no penalty (I guess the
> memory footprint of plain Ints is still better).
> >>
> >> Yes, I have, and actually, the memory footprint is no different! These
> are value-types that are exactly word-sized, and so get passed around in
> registers and stored inline in larger structs.
> >>
> >>   - Greg
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org
> >> https://lists.swift.org/mailman/listinfo/swift-evolution
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>



-- 
Author of the Sparkling language
http://h2co3.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151229/7c59d8c7/attachment.html>


More information about the swift-evolution mailing list