Relational Parametricity and Units of Measure Andrew J. Kennedy In Proceedings of the 24th Annual ACM Symposium on Principles of Programming Languages, Paris, France, January 1997. The following copyright notice is required by the ACM (see http://www.acm.org/pubs/copyright_policy.html). Copyright c 1997 by the Association for Computing Machinery, Inc. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, to republish, to post on servers, or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from Publications Dept, ACM Inc., fax +1 (212) 869-0481, or [email protected]. Relational Parametricity and Units of Measure Andrew J. Kennedy∗ LIX, Ecole´ Polytechnique 91128 Palaiseau cedex, France Abstract work the author and other researchers have shown how the type system of a language can be extended to sup- Type systems for programming languages with numeric port the compile-time checking and automatic inference types can be extended to support the checking of units of dimensions or units of measure [7, 5, 15, 12]. In this of measure. Quantification over units then introduces article we study the semantics of such a language, and a new kind of parametric polymorphism with a cor- in particular, how the notion of parametricity (due to responding Reynolds-style representation independence Reynolds [11]) has an analogue here. principle: that the behaviour of programs is invariant The paper is structured as follows. In Section 2 we under changes to the units used. We prove this ‘dimen- introduce basic concepts from dimensional analysis and sional invariance’ result and describe four consequences. explain informally how a programming language can be The first is that the type of an expression can be used to extended with units of measure, giving illustrative ex- derive equations which describe its properties with re- amples in Standard ML. This motivates the formal defi- spect to scaling (akin to Wadler’s ‘theorems for free’ for nition in Section 3 of an explicitly-typed language whose System F). Secondly there are certain types which are denotational semantics is specified in the standard way inhabited only by trivial terms. For example, we prove using domains and continuous functions. This under- that a fully polymorphic square root function cannot lying semantics ignores the unit annotations in types; be written using just the usual arithmetic primitives. instead, units are accounted for by a binary relation Thirdly we exhibit interesting isomorphisms between over the underlying semantics defined in Section 4. Us- types and for first-order types relate these to the cen- ing the relation we prove the main result of this paper: tral theorem of classical dimensional analysis. Finally a parametricity theorem which captures the idea that we suggest that for any expression whose behaviour is the behaviour of programs is independent of the units dimensionally invariant there exists some equivalent ex- of measure used. The theorem is used in Section 5 to pression whose type reflects this behaviour, a conse- prove several properties of terms and types in the lan- quence of which would be a full abstraction result for guage, as outlined in the abstract above. In Section 6 a model of the language. we discuss whether it is possible to use the relation to construct a model of the language which is fully abstract with respect to the underlying cpo-based model. This 1 Introduction notion of relative full abstraction is believed to be new. Ever since FORTRAN, programming languages have provided a numeric type suitable for modelling physi- 2 Motivation cal quantities in scientific computations. Typically these quantities possess units of measure (such as kilograms 2.1 Units and dimensions or pounds) which belong to some class, or dimension (such as mass), but existing programming languages Physical quantities are measured with reference to a unit treat all numeric values as dimensionless. In previous of scale. When we say that something is ‘6 metres long’ we mean that six metre-lengths placed end-to-end would ∗Current address: Persimmon IT, The Westbrook Centre, Mil- have the same length. The unit ‘metre’ is acting as a ton Road, Cambridge CB4 1YG, U.K., [email protected] point of reference, not just for the purpose of comparison (X is longer than a metre), but also for measurement (X is six times as long as a metre). A single quantity can Appears in the Proceedings of the 24th Annual ACM be measured in many different systems of units, some Symposium on Principles of Programming Languages, of which may even be non-linear (such as decibels) or Paris, France, January 1997. c ACM. have an origin not at zero (such as degrees Celsius). In 1 computer science terms, these can be seen as isomorphic acceleration due to gravity g. After performing dimen- data representations; then the notion of dimension is a sional analysis it is possible to assert that the equation class of representations. must be of the form t = l/g φ(θ) for some function It is usual in science to fix a set of base dimensions φ of the angle θ. Of course it turns out that for small which cannot be defined in terms of each other such as angles φ(θ) ≈ 2π, but dimensional analysis got us a mass, length and time (abbreviated to M, L and T). long way – in particular, the period of oscillation turned Derived dimensions are products of powers of base di- out to be independent of the mass m. In general, any mensions; the dimensions of force are MLT−2, for ex- dimensionally consistent equation over several variables ample. Similarly there are base units: the SI units for can be reduced to an equation over a smaller number of mass, length and time are respectively kilograms, metres dimensionless terms which are products of powers of the and seconds. Examples of derived units include inches original variables. This is known (rather awkwardly) as (0.0254 metres) and newtons (1.0 kg m s−2). Of course, the Pi Theorem (Π = product) [3]. this division of dimensions and units into base and de- rived is arbitrary, and one could easily work with, say, 2.3 Some design choices force, acceleration and velocity instead of mass, length and time. There are a number of parameters involved in the design Dimensionless quantities are common in science. Ex- of a programming language that supports the prevention amples include refractive index, coefficient of restitution of dimension errors. We fix these now. and angle. The last should properly be considered di- First, should numeric types be parameterised on units mensionless though it is tempting to think otherwise – or dimensions? If several units with the same dimen- after all, angles are expressed in ‘units’ of radians. Nev- sion are permitted, then parameterising on units is the ertheless, it is just a dimensionless ratio of two lengths: sensible choice – it is even possible for the compiler to the distance along an arc divided by the radius it sub- insert conversions between different units automatically. tends. Semantically, too, it makes sense: the type of an expres- sion which is polymorphic in its units, denoted ∀u.τ,can 2.2 Dimensional analysis be interpreted as meaning ‘for all changes in the units of measure u’. The addition, subtraction or comparison of two quan- The second question is whether or not to allow units / tities with different dimensions is invalid dimensionally, such as kg1 2. We take the view that if such a thing whereas their product or quotient has dimensions which arose, it would suggest revision of the set of base units are the product or quotient of the corresponding dimen- rather than the use of fractional exponents, and there- sions. If a formula or equation is free of dimension errors, fore we assume that powers of units are integers. then it is said to be dimensionally consistent. For scien- Static checking of units of measure is of limited value tists and engineers, dimensional consistency is a handy without polymorphism, since even something as simple check on correctness, for dimensional inconsistency cer- as a squaring function must be polymorphic if it is ap- tainly indicates that something is amiss. Therefore the plied to values with different units. There are many va- automatic checking of numerical programs for dimension rieties of polymorphism (for example, ad hoc, subtype, errors is potentially very useful. conjunctive, parametric) but only parametric polymor- Why does dimension checking work? The answer lies phism suits units of measure, as was recognised even in in the assumption that physical laws are dimensionally an early proposal to extend Pascal with units and di- invariant [9] (or unit-free [3]): they remain the same mensions [6]. Parametric polymorphism itself comes in under changes in the units of measure used. Philo- many flavours. All of the work on type inference cited in sophically, this is profound: why should they have the the introduction is based on ML-style polymorphism, in same form at all scales? Pragmatically, it leads to a which quantifiers (over types and over units or dimen- very useful technique called dimensional analysis.The sions) appear only outermost in a type, and polymor- idea is simple: when investigating some physical phe- phism is introduced solely through the let construct and nomenon, if the equations governing the phenomenon not in lambda abstractions.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-