From Parametricity to Conservation Laws, via Noether’s Theorem

Robert Atkey [email protected]

Abstract Such programs take lists of αs, for any type α, and return natu- Invariance is of paramount importance in programming languages ral numbers. Using Reynolds’ theory of relational parametricity, and in physics. In programming languages, John Reynolds’ theory Wadler showed that any f with this type satisfies the following of relational parametricity demonstrates that parametric polymor- property: phic programs are invariant under change of data representation, a ∀α, β, g : α → β, l : List α. f [α] l = f [β] (map g l) property that yields “free” theorems about programs just from their types. In physics, Emmy Noether showed that if the action of a Thus, any f with the type given above is invariant under mapping physical system is invariant under change of coordinates, then the some arbitrary function g over its input. Thinking in terms of ab- physical system has a conserved quantity: a quantity that remains stract data types and change of data representation, this free theo- constant for all time. Knowledge of conserved quantities can reveal rem states that f is invariant under change of data representation deep properties of physical systems. For example, the conservation from an arbitrary type α to another arbitrary type β, via g. of is by Noether’s theorem a consequence of a system’s in- The use of invariance under change to derive useful conse- variance under time-shifting. quences is a technique much older than programming languages. In In this paper, we link Reynolds’ relational parametricity with physics, Noether’s theorem [14] provides a general way to derive Noether’s theorem for deriving conserved quantities. We propose conservation laws for physical systems from invariance properties. an extension of System Fω with new kinds, types and term con- Informally speaking, Noether’s theorem states that every continu- stants for writing programs that describe classical mechanical sys- ous symmetry of a physical system implies the existence of a cor- tems in terms of their Lagrangians. We show, by constructing a re- responding conserved quantity. A conserved quantity is a function lationally parametric model of our extension of Fω, that relational of the state of the system that is constant for all time. We illustrate parametricity is enough to satisfy the hypotheses of Noether’s the- the use of Noether’s theorem with a simple example of a classical orem, and so to derive conserved quantities for free, directly from system consisting of two particles of equal mass m connected by a the polymorphic types of Lagrangians expressed in our system. spring with spring constant k, such that the particles are constrained to move one-dimensionally. Categories and Subject Descriptors D.3.1 [Programming Lan- Noether’s theorem applies to systems described in terms of guages]: Formal Definitions and Theory; J.2 [Physical Sciences Lagrangians. For classical mechanics, a Lagrangian is a function and Engineering]: Physics of time and the positions and velocities of all the particles in General Terms Languages, Theory, Types, Physics the system, to the difference between the kinetic energy and the potential energy of the system. For our system with two particles at Keywords classical mechanics; relational parametricity; higher- positions x1 and x2 with velocities x˙1 and x˙2 the Lagrangian is: kinded types; invariance; conservation laws 1 2 2 1 2 L(t, x1, x2, x˙1, x˙2) = m(x ˙1 +x ˙2 ) − k(x1 − x2) (1) 1. Introduction 2 2 Reynolds’ theory of relational parametricity [18] tells us that para- The first summand represents the total kinetic energy of the system metrically polymorphic programs automatically satisfy invariance in terms of the mass and velocities, while the second summand properties. Such invariance properties are often called Free Theo- represents the potential energy contained within the spring due to rems, after Wadler [22], since they follow “for free” from the types the distance between the particles. of programs, rather than through detailed study of the program text. From the Lagrangian (1), using the principle of stationary ac- An illustrative example is the free theorem for programs f with the tion, we can derive the following two equations of motion for this following type: system. (We describe the principle of stationary action, and the pro- cess for deriving the equations of motion fully in Section 2.) For f : ∀α. List α → Nat this system, the equations of motion are a pair of ordinary differen- tial equations (ODEs) that describe how the positions and velocities Permission to make digital or hard copies of all or part of this work for personal or of the particles evolve over time (the second derivative with respect classroom use is granted without fee provided that copies are not made or distributed to time, x¨i, denotes the acceleration of the ith particle): 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 the mx¨1 = −k(x1 − x2) mx¨2 = −k(x2 − x1) author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission We could now proceed to solve these ODEs to further analyse the and/or a fee. Request permissions from [email protected]. behaviour of this system. However, Noether’s theorem gives us a POPL ’14, January 22–24, 2014, San Diego, CA, USA. Copyright is held by the owner/author(s). Publication rights licensed to ACM. powerful way of gaining insight into properties of these ODEs “for ACM 978-1-4503-2544-8/14/01. . . $15.00. free”, without necessarily having to find solutions to them. This is http://dx.doi.org/10.1145/2535838.2535867 accomplished by finding invariance properties of the Lagrangian. The Lagrangian (1) does not refer to any fixed point in space; their type system by embedding it within System Fω. The benefits only the relative distance between the two particles, along with their of this embedding are twofold. Firstly, by using the richer type- velocities, is relevant. Therefore, the Lagrangian (1) is invariant level structure of System Fω, we can easily add useful indexed under translation in space by some arbitrary displacement y: types like length indexed vectors and smooth function types, both of which would have required special treatment in the special pur- L(t, x , x , x˙ , x˙ ) = L(t, x + y, x + y, x˙ , x˙ ) (2) 1 2 1 2 1 2 1 2 pose type system presented by Atkey et al.. Secondly, by using Sys- By Noether’s theorem, invariance under spatial translation implies tem Fω as our base, we are able to reuse the reflexive graph-based that the linear of the whole system is constant for all relationally parametric semantics presented by Atkey [3], extend- time. For this system, conservation of linear momentum is stated ing it as appropriate for our geometric setting. mathematically as the vanishing of the derivative with respect to time of the total linear momentum of the system: Contributions Our core contributions are threefold: d 1. We reformulate the type system for geometric invariance of m(x ˙ +x ˙ ) = 0 (3) dt 1 2 Atkey, Johann and Kennedy [5] as an extension of System Fω. This shows that the special status of the group-indexed In general, Noether’s theorem gives us a way of deriving conserved types in Atkey et al.’s work can be incorporated into a standard properties like (3) from invariance properties like (2). In this case, framework for indexed types. we have used invariance under translation in space to derive con- servation of linear momentum. Other common examples include 2. We present a relationally parametric semantics of our extension invariance under translation in time, yielding conservation of en- of System Fω. In this model, following Atkey’s model for ba- ergy, and invariance under rotation, yielding conservation of an- sic System Fω [3], each kind is interpreted as a reflexive graph. gular momentum. We will see examples of each of these kinds of Our key technical contribution is to note that the many geo- invariance, and their consequent conservation laws, in Section 5. metric groups that we wish to incorporate are expressible as The invariance property stated in Equation (2) is highly remi- groupoids, and hence as reflexive graphs. We therefore show niscent of the free theorem we stated for the polymorphic program that Reynolds’ theory of relational parametricity, suitably gen- at the start of this introduction. A change in representation in the eralised to indexed types by considering reflexive graphs, al- input, whether a change in data representation from a type α to a ready accommodates geometric invariance properties. type β, or a change in where time zero is counted from, results in 3. Finally, we connect the free theorems derivable from our rela- no change in the output. Since Reynolds’ relational parametricity tionally parametric semantics with Noether’s theorem for deriv- allowed us to derive the invariance properties for the program f just ing conservation laws. We present many examples of invariant by looking at its type, we ask the following question: Lagrangians expressible in our type system, along with the con- Question: Is it possible to use Reynolds’ theory of relational para- sequent conservation laws for physical systems. In each case, metricity to derive the invariance properties of Lagrangians re- the necessary invariance property is derived from the type of quired for Noether’s theorem to derive conservation laws? the Lagrangian. In this paper, we answer this question positively. We show that Outline it is indeed the case that we can use a generalised version of • In the next section, Section 2, we describe the necessary back- Reynolds’ theory of relational parametricity to prove the geometric ground to understand Noether’s theorem and the conservation invariance properties required for the hypotheses of Noether’s the- laws it generates. We briefly introduce the Lagrangian formula- orem. We construct an extension of System Fω suitable for writing tion of classical mechanics, define precisely what it means for a invariant Lagrangians, with a relationally parametric model that al- physical system to be invariant under transformations, and state lows us to prove invariance properties as free theorems. In our sys- Noether’s theorem. tem, the Lagrangian (1) we gave above for describing a system of two particles coupled by a spring will have the type: • Having shown how invariance generates conservation laws in Section 2, in Section 3 we show how to derive invariance prop- ∀y:T(1). erties from indexed types. We present a relationally parametric C∞( h1, 0i × h1, yi × h1, yi × h1, 0i × h1, 0i, h1, 0i) R R R R R R semantics for System Fω, and show how geometric invariance We explain this type by breaking it down from left to right. The properties can be accomodated within this semantics. quantifier ∀y:T(1) indicates that we are quantifying over all trans- • In Section 4 we construct a type system for classical mechan- lations y in one-dimensional space, just as we did implicitly in ∞ ics by extending System Fω with the necessary constants for Equation (2). The notation C (−, −) denotes the type of smooth smooth function types and combinators for constructing smooth functions between spaces. (A smooth function is a function that is functions. Programming directly with our combinators in raw infinitely differentiable.) All the functions that we wish to use as System Fω is awkward, due to the point-free style required, so Lagrangians must be smooth (at least up to second order) for the in Section 4.2 we define a surface syntax for defining smooth theory to work. The type Rhg, xi denotes real numbers that vary functions that is translated into our applied System Fω. with some linear transformation g and translation x. In this case, all the linear transformations are 1, indicating no transformation. • In Section 5 we present several examples of using our extension Likewise, when the translation component is 0, there is no transla- of System Fω for writing invariant Lagrangians that describe tion. Thus a value of type Rh1, 0i is invariant, but a value of type physical systems. Rh1, yi varies with the translation y. In the smooth function type • Section 6 concludes, and offers directions for further work. above, the domain space consists of vectors of five real numbers: the time, the two particle positions, which vary with the translation y, and the two particle velocities. 2. Conservation Laws from Invariance Atkey, Johann and Kennedy [5], building on work by Kennedy Noether’s theorem applies to systems described using the formal- [11], have already presented a polymorphic type system for ex- ism of , a reformulation of Newtonian clas- pressing geometric invariance properties similar to the translation sical mechanics. In this section we can only offer a very brief in- invariance property in Equation (2). In the present work, we extend troduction to the concepts of Lagrangian mechanics. The reader is referred to other sources, such as Landau and Lifschitz [12] or such as Gelfand and Fomin [7]. The key point for our purposes is Arnol’d [2] for the necessary background in the mathematical the- that a path q satisfies the principle of stationary action if and only if ory of classical mechanics. Lagrangian mechanics, and Noether’s it satisfies a system of ODEs derived from the Lagrangian L, called theorem, make crucial use of the Calculus of Variations. Gelfand the Euler-Lagrange equations: and Fomin [7] provide a good introduction to this topic. d ∂L ∂L In this section, we will endeavour to keep the presentation rel- − = 0 (5) atively elementary. However, some knowledge of differential and dt ∂q˙i ∂qi integral calculus will be required. Knowledge of basic Newtonian The ODEs derived from the Lagrangian for a classical mechanical mechanics will also be helpful. We will use standard notation from system are called the equations of motion for the system. physics and mathematics for integrals and (partial) derivatives. In The notation in the Euler-Lagrange equation, while standard, is particular, when discussing derivatives with respect to time, we use not exactly clear, especially to a programming language theorist dot notation: x˙ for the first derivative of x with respect to time, and used to a more careful treatment of variables. The two partial x¨ for the second derivative. derivatives ∂L and ∂L take partial derivatives of L with respect to ∂q˙i ∂qi q˙ and q as variables. The derivative with respect to time, d , then 2.1 Lagrangian Mechanics i i dt treats q˙i as a function of time, hence the appearance of the second Lagrangian mechanics is a branch of , along derivatives q¨i in the equations of motion. Sussman and Wisdom with [2, 12]. Analytical mechanics seeks to [20] criticise the standard presentation of Lagrangian mechanics for discover the underlying structure of Newtonian classical mechanics this kind of confusing notation, and define a clearer notation based by reformulating it in different terms. on the Scheme programming language. We elect to stick with the Lagrangian and Action For Lagrangian mechanics, classical me- standard notation as used in physics textbooks just for familiarity. chanical systems are described in terms of Lagrangians L = T − Example 2. Computing the Euler-Lagrange equations for the La- V , where T is the total kinetic energy of the system, and V is the grangian (4), above, yields a pair of ODEs, one each for the x and total potential energy. The Lagrangian is a function of time, the po- y coordinates of the single particle of the system: sitions of all the particles in the system, and their velocities. This is usually written like so, with abuse of notation for q˙ which is a mx¨ = 0 my¨ = −mg variable here, not the derivative of a function: Note that both these ODEs have the form F = mx¨, just as L(t, q, q˙) = T − V in Newton’s second law. Lagrangian mechanics allows, from the principle of stationary action, the derivation of Newton’s second In this expression, q and q˙ are actually vectors containing the law, instead of postulating it as an axiom. components of the position and velocity vectors for each particle. A key feature of Lagrangian mechanics is that the coordinate system 2.2 Noether’s Theorem used to describe the system need not necessarily be the usual cartesian coordinates. For example, one of the components of q As we described in the introduction, Noether’s theorem provides us might be the angle of swing of a pendulum. This use of generalised with deep insights into the properties of the solutions of the Euler- coordinates is one of the advantages of Lagrangian mechanics over Lagrange equations. Formally, Noether’s theorem requires the in- the Newtonian presentation in terms of positions and forces. variance of the action S, rather than invariance of the Lagrangian The Lagrangian for a particular system is used to define the itself. We therefore first define what is meant by invariance of the n action, and then state Noether’s theorem. action of that system. For a particular path q : R → R and endpoints a < b in time, the action of the Lagrangian L over this Invariance of the Action Let path is defined by the following integral: Z b Z b S[q; a; b] = L(t, q(t), q˙(t))dt S[q; a; b] = L(t, q(t), q˙(t))dt a a Below, we will use the principle of stationary action to select the be the action of some physical system described by the Lagrangian n L. Assume a differentiable invertible function Φ: R → R that physically realisable paths from all the possible paths q : R → R . n n transforms time in some way and a function Ψ: R → R that Example 1. We have already seen an example of a Lagrangian, transforms the vector of generalised coordinates ~q to another vector in the introduction, describing a system of two particles coupled of generalised coordinates Ψ(~q). by a spring. Another example of a Lagrangian describes a system The action S is invariant between the endpoints a and b, under consisting of a single particle of mass m acted upon by a constant the transformations Φ and Ψ, if it is the case that for all paths q: downward gravitational field with gravitational potential g: Z b Z Φ(b) ∗ ∗ dq 1 2 2 L(t, q(t), q˙(t))dt = L(s, q (s), (s))ds (6) L(t, x, y, x,˙ y˙) = m(x ˙ +y ˙ ) − mgy (4) ds 2 a Φ(a) Again, the left summand represents the kinetic energy of the system where q∗(s) = (Ψ ◦ q ◦ Φ−1)(s) is the path q transformed by Φ in terms of the mass and horizontal and vertical velocities of the and Ψ. By change of variables, the right hand integral in the above particle. The right summand represents the potential energy of the equation is equal to the following integral: system due to the gravitational field’s strength determined by the Z b dq∗ particle’s distance from a fixed baseline at y = 0. L(Φ(t), q∗(Φ(t)), (Φ(t))) · |Φ(˙ t)|dt ds The Principle of Stationary Action The principle of stationary a action states that the physically realisable paths q are the paths For our application to classical mechanics, the endpoints a and b are that yield stationary points (i.e., either minima or maxima) of the arbitrary, so we can rewrite Equation (6) to the following equation action S[q; a; b], for all a and b. Defining what is exactly meant by a between applications of the Lagrangian: stationary point of the action is beyond the scope of this paper, and ∗ ∗ dq can be found in standard references on the calculus of variations, L(t, q(t), q˙(t)) = L(Φ(t), q (Φ(t)), (Φ(t))) · |Φ(˙ t)| (7) ds For most examples, it will be the case that Φ has the form Φ(t) = In both cases, we have discovered that, as a consequence of in- t + t0 and Ψ is an affine transformation Ψ(q) = Gq + x, where variance under translation in time, the total energy of the system is n G is an n×n real matrix and x ∈ R . In this case, Equation (7) conserved (note that the − signs in the original Lagrangians have simplifies further, using the definition of q∗ and the chain rule of turned into +s). In general, invariance under translation in time im- differentiation, to: plies, via Noether’s theorem, conservation of energy. L(t, q(t), q˙(t)) = L(t + t0, Gq(t) + x, Gq˙(t)) (8) This finishes our short introduction to Lagrangian mechanics and Noether’s theorem. We have shown how Noether’s theorem Equation (8) is obviously implied by the following equation, where can be used to derive conservation laws from invariance properties. we treat q and q˙ as universally quantified variables, instead of as We now go on to showing how suitable invariance properties can be functions of time: derived from types, building towards Section 4, where we construct L(t, q, q˙) = L(t + t0, Gq + x, Gq˙) (9) a type system for writing Lagrangians that expresses invariance properties directly in the types. In Section 5, we give examples of Equation (9) is the form of invariance that the free theorems we invariant Lagrangians and their consequent conservation laws. derive from the types of our typed language for classical mechanics will usually take. We define our type system for classical mechanics in Sections 3 and 4 below. 3. Invariance from Types Example 3. In the introduction, we stated that the Lagrangian (1) We derive invariance properties from types by constructing a re- describing the spring-coupled system is invariant under translation lationally parametric model of System Fω, which we then extend in space. Our running example in this section, the Lagrangian (4) with suitable kind-, type-, and term-level constants for writing in- for the single particle under gravity, is not invariant under all spatial variant Lagrangians. Our relationally parametric model is based on translations, due to the reference in the potential energy term to the the reflexive graph model presented by Atkey [3] (based on previ- distance from the fixed baseline at y = 0. It is invariant under ous work on reflexive graph models of relational parametricity by translations along the x-axis, however. Both Lagrangians are also Robinson and Rosolini [19] and Hasegawa [9]). In reflexive graph invariant under translations in time, a special case of Equation (9): models of relational parametricity, the kinds of System Fω are in- 0 terpreted as reflexive graphs, while types are interpreted as mor- L(t, q, q˙) = L(t + t , q, q˙) phisms of reflexive graphs. As demonstrated by Atkey, uniformly This invariance property is simply a consequence of the fact that interpreting kinds as reflexive graphs allows for a straightforward neither Lagrangian explicitly mentions the time t. interpretation of higher kinds like ∗ → ∗. In the present setting, reflexive graphs will allow us to accomodate arbitrary groupoids as Noether’s Theorem Noether’s theorem applies to actions that are kinds (Section 3.3). Groupoid kinds are the key technical tool that continuously invariant. Being continuously invariant means that, we need to derive the kinds of geometric invariance properties we instead of having fixed transformations Φ and Ψ, we have two need for Noether’s theorem. families of transformations, Φ and Ψ, differentiably indexed by a real parameter , such that Φ0 and Ψ0 are the identity function. 3.1 System Fω: Syntax and Relationally Parametric Model Theorem 1 (Noether). If the action We now present the syntax and relationally parametric semantics Z b of System Fω. Since we will incrementally extend the syntax of the S[q; a; b] = L(t, q(t), q˙(t)) dt system throughout this section and the next, we do not state a final a triple of theorems stating that we have correctly interpreted kinds, is invariant under -indexed families of transformations Φ and Ψ, types and terms. Rather, we state the necessary properties of the then the equation interpretation as three separate properties (Properties 1, 2, and 3, n n ! ! below), and maintain them as we extend the system. d X ∂L X ∂L We deviate from the standard presentation of System Fω in ψi + L − q˙i φ = 0 (10) dt ∂q˙i ∂q˙i that we distinguish between large and small kinds, and only allow i=1 i=1 quantification over small kinds. In particular, the kind of types ∗ holds for all paths q satisfying the Euler-Lagrange equations (5), is not small. The reason for this choice is that we desire a simple where ψ = ∂Ψ and φ = ∂Φ . ∂ =0 ∂ =0 set-theoretic model. However, requiring that we have a set of sets closed under large products, in order to interpret quantification over Proof. E.g., Gelfand and Fomin [7], Section 20. the kind ∗, would force us to use an intuitionistic metatheory [17]. The basic results of the Calculus of Variations that we are relying Example 4. In Example 3, we noted that the Lagrangians (1) and on, such as the derivation of the Euler-Lagrange equations, use (4) are both invariant under arbitrary translations in time. In terms excluded middle in their proofs, so an intuitionistic metatheory of continuous invariance, their corresponding actions are invariant is not an option. We therefore just assume that we have a set of under the families Φ(t) = t +  and Ψ(q) = q. Plugging these “small” sets, which includes the real numbers and is closed under definitions into Equation (10), we learn that, for these systems: subsets, products, function spaces and set comprehensions. 2 ! d X ∂L Syntax The kinds of System Fω include at least the base kind L − q˙i = 0 dt ∂q˙i i=1 of types, ∗, and function and product kinds, as generated by the following grammar: For the spring-coupled particles Lagrangian, (1), we obtain: κ ::= ∗ | κ → κ | κ × κ | · · · d  1 1  1 2 1 2 m(x ˙ 2 +x ˙ 2) + k(x − x )2 = 0 dt 2 1 2 2 1 2 We will extend the collection of kinds in Section 3.2 with type-level natural numbers, and in Section 3.3 with groupoid kinds. and for the single particle under gravity Lagrangian (4), we obtain: We use the judgement κ small to denote when the kind κ will d  1  be interpreted by a small reflexive graph (to be defined below). The m(x ˙ 2 +y ˙2) + mgy = 0 dt 2 kind ∗ is not small, but the construction of function and product α : κ ∈ Θ Θ, α : κ1 ` A : κ2 x : A ∈ Γ Θ | Γ ` e : A Θ ` A ≡ B : ∗

Θ ` α : κ Θ ` λα : κ1.A : κ1 → κ2 Θ | Γ ` x : A Θ | Γ ` e : B

Θ ` F : κ1 → κ2 Θ ` A : κ1 Θ | Γ ` e1 : A1 Θ | Γ ` e2 : A2 Θ | Γ ` e : A1 × A2 Θ ` FA : κ2 i ∈ {1, 2} Θ | Γ ` (e1, e2): A1 × A2 Θ | Γ ` πie : Ai

Θ ` A : κ1 Θ ` B : κ2 Θ ` A : κ1 × κ2 i ∈ {1, 2} Θ | Γ, x : A ` e : B Θ ` hA, Bi : κ1 × κ2 Θ ` πi A : κi Θ | Γ ` λx : A. e : A → B Θ ` A : ∗ Θ ` B : ∗ Θ ` A : ∗ Θ ` B : ∗ Θ | Γ ` e1 : A → B Θ | Γ ` e2 : A Θ ` A × B : ∗ Θ ` A → B : ∗ Θ | Γ ` e1e2 : B

Θ, α : κ ` A : ∗ κ small Θ, α : κ | Γ ` e : A α 6∈ fv(Γ) Θ ` ∀α:κ. A : ∗ Θ | Γ ` Λα:κ. e : ∀α:κ.A

Figure 1. Types and their Kinds Θ | Γ ` e : ∀α:κ.A Θ ` B : κ Θ | Γ ` e [B]: A{B/α}

Figure 3. Terms and their Types Θ, α : κ1 ` A : κ2 Θ ` B : κ1 β Θ ` (λα : κ1.A) B ≡ A{B/α} : κ2 A small reflexive graph is a reflexive graph (O,R, id) where Θ ` A : κ1 → κ2 η O is a small set of objects. We use small reflexive graphs as the Θ ` (λα : κ1.A α) ≡ A : κ1 → κ2 semantic interpretation of small kinds. The interpretation of kinds as reflexive graphs, and small kinds Θ ` A1 : κ1 Θ ` A2 : κ2 as small reflexive graphs is a key property of our semantics that we β will maintain as we add additional kinds in Sections 3.2 and 3.3, Θ ` πi hA1,A2i ≡ Ai : κi below. We state this as Property 1 of our semantics:

Θ ` A : κ1 × κ2 Property 1. Each kind κ is interpreted as a reflexive graph κ . If η J K Θ ` hπ1A, π2Ai ≡ A : κ1 × κ2 κ small, then κ is a small reflexive graph. J K plus: reflexivity, symmetry, transitivity and congruence An appealing interpretation of reflexive graphs is as “categories without composition”. Following this intuition, we define mor- Figure 2. Type equality phisms of reflexive graphs as “functors”, without the preserva- tion of composition condition. A morphism of reflexive graphs (O1,R1, id1) and (O2,R2, id2) is a pair of mappings f : O1 → 0 0 0 kinds preserves smallness: O2 and r : ∀o, o ∈ O1.R1(o, o ) → R2(f o, f o ) such that iden- tities are preserved: r o o (id1 o) = id2 (f o). We use morphisms κ1 small κ2 small κ1 small κ2 small of reflexive graphs below to interpret well-kinded types. O R id κ1 → κ2 small κ1 × κ2 small We will use the notation − , − and − for the first, second and third projections out of tuples representing reflexive graphs. The additional kinds we introduce below will all be small. Similarly, we use −f and −r for the first and second projections The well-kinded types, type equalities, and well-typed terms of out of tuples representing reflexive graph morphisms. ω System F are shown in Figures 1, 2 and 3, respectively. Kinding We now define the interpretations of the basic kinds of System α : κ , ..., α : κ Θ contexts 1 1 n n are denoted by and typing Fω we defined above, making sure that we maintain Property 1. At x : A , ..., x : A Γ contexts 1 1 n n are denoted by . Note that typing base kind, the collection of objects is simply the (large) set of all Γ ∗ contexts only contain well-kinded types of kind . Terms also small sets; edges between A and B are binary relations on A and βη have an equational theory, with -laws for functions, products, B (i.e., subsets of A × B); and the distinguished identity edge is and universal quantification, which we omit. Our presentation of exactly the equality relation: System Fω is entirely standard (see, e.g., Pierce [16]), except for the restriction to small kinds in the formation of universal kinds ∗ = (Set, Rel, ≡) ∀α:κ.A. As is the case for kinds, we will extend the types, type J K The reflexive graph ∗ is not small, due to the collection of all equalities, and terms in Sections 3.2, 3.3 and 4, below. small sets Set not formingJ K a small set. Reflexive Graphs and the Interpretation of Kinds We will in- For higher kinds κ1 → κ2, the collection of objects consists of reflexive graph morphisms from the interpretation of κ1 to the in- terpret every kind κ as a reflexive graph, which we now define. A 0 0 reflexive graph is a triple (O,R, id), where O is a large set of ob- terpretation of κ2; the edges between morphisms (f, r) and (f , r ) jects, R : O × O → Set assigns a small set of directed ‘edges’ are edge transformers; and the distinguished identity relation for to each pair of objects, and id : Πo ∈ O.R(o, o) assigns a dis- (f, r) is just r: tinguished ‘identity’ edge from every object to itself. We think of κ1 → κ2 = the edges of a reflexive graph as abstract “relations” between the J ({(f, r) |K(f, r): κ1 → κ2 }, 0 0 0 R 0 R 0 0 objects. Indeed, in the interpretation of the kind of types, ∗, below, ((f, r), (f , r )) 7→J ΠK o, oJ. κK1 (o, o ) → κ2 (f o, f o ), the edges will be exactly relations. (f, r) 7→ r ) J K J K By the assumption that our collection of small sets is closed under also be assigned interpretations as reflexive graph morphisms, and the formation of function spaces and set comprehension, if κ1 we will ensure that Property 2 is maintained. and κ2 are small reflexive graphs, then so is κ1 → κ2 . J K ProductJ K kinds are interpreted by taking theJ productK of their Interpretation of Terms We omit the straightforward and rela- interpretations as reflexive graphs: tively uninteresting interpretation of well-typed terms Θ | Γ ` e : A, and just state that there is a well-defined function interpreting κ1 × κ2 = each well-typed term, with the property that it takes related en- O O J ( κ1 ×K κ2 , vironments to related results; this is the fundamental theorem of J K J 0 K 0 R 0 R 0 ((o1, o2), (o1, o2)) 7→ κ1 (o1, o1) × κ2 (o2, o2), logical relations for System Fω: id J K id J K (o1, o2) 7→ ( κ1 (o1), κ2 (o2))) For all well-typed terms Θ | Γ ` e : A there is a J K J K Property 3. The collection of objects of the interpretation of a product kind is function e ∈ (∀θ ∈ Θ O. Γ f θ → A f θ), such that, for all the product of the underlying collections of objects of the two parts, θ, θ0 ∈ JΘKO, ρ ∈ ΘJ RK(θ, θJ0),Kγ ∈ ΓJ fKθ and γ0 ∈ Γ f θ0, if and the relational component is simply the product of the relational (γ, γ0) ∈J KΓ rθθ0ρ thenJ K ( e θγ, e θ0γJ0)K∈ A rθθ0ρ. Moreover,J K components. This naturally leads to the identity component being this interpretationJ K is soundJ forK theJβηK equationalJ K theory of terms. defined as the tuple of the identity components of the two parts. Again, by assumption that our collection of small sets is closed 3.2 Discrete Kinds under products, if κ1 and κ2 are small reflexive graphs, then In Section 3.1, we only had a single base kind: the kind ∗ of proper J K J K so is κ1 × κ2 . Product kinds generalise to the interpretation of types, with a specific interpretation as the reflexive graph of sets J K kinding contexts Θ = α1 : κ1, ..., αn : κn, which are interpreted and relations. We now describe two families of base kinds with as the product of the reflexive graph interpretations of κ1, ..., κn: interpretations that are particular sorts of reflexive graph. In this

α1 : κ1, ..., αn : κn = section, we look at discrete kinds; kinds whose reflexive graph O O interpretations are such that the reflexive edges are the only edges J ( κ1 × ... × κnK , 0 R 0 R 0 between objects. In the following section (Section 3.3), we look at (Jθ, θK ) 7→ κ1 J (πK1θ, π1θ ) × ... × κn (πnθ, πnθ ), id id groupoid kinds, where edges are composable and invertible. θ 7→ ( κ1 J (πK1θ), ..., κn (πnθ)))J K J K J K Discrete kinds can be seen as the natural way of lifting types up In Sections 3.2 and 3.3 below, we will extend System Fω with to the kind level. For our purposes, we will only require a kind of additional kinds, and assign them reflexive graph interpretations, natural numbers, which we will use for constructing n-ary vectors making sure that we maintain Property 1. of positions and velocities when we use our type system for writing Lagrangians in Section 4. The kind of natural numbers is small: Interpretation of Types Well-kinded types Θ ` A : κ from Figure 1 are interpreted as reflexive graph morphisms A : Θ → κ ::= · · · | nat nat small κ . We sum this up as a property of our semantics: J K J K J K The interpretation of this kind as a reflexive graph goes as follows: Property 2. Each well-kinded type Θ ` A : κ is interpreted as a reflexive graph morphism A : Θ → κ , such that if nat = (N, (n1, n2) 7→ {∗ | n1 = n2}, n 7→ ∗) J K Θ ` A ≡ B : κ then A = B J. K J K J K The collection of objects of this reflexive graph is simply the set of J K J K The interpretation of the λ-calculus fragment (i.e., variables, natural numbers. The only edges in the graph are unique edges be- λ-abstraction and application, and products) of the language of tween equal numbers. Since we have assumed that our collection of well-kinded types is displayed in Figure 4. The interpretations are small sets contains the natural numbers, we have clearly maintained unsurprising given the reflexive graph interpretation of the kinds Property 1 of our semantics. We add new type-level constructs for the kind Nat of natural κ1 → κ2 and κ1 × κ2 we gave above. Figure 5 shows the interpretations of the basic type construc- numbers, representing zero and successor, and a kind-generic re- tors for function and product types, and of universal quantification. cursion operator: Each of these constructs builds an object of kind ∗, so the object- Θ ` A : nat level interpretation is a small set in Set, and the relation-level in- Θ ` zero : nat Θ ` succ A : nat terpretation is an actual relation. In the cases of the function and product types, the object-level interpretation is just as set-theoretic Θ ` A : nat Θ ` B : κ Θ ` C : κ → κ function and product respectively, and the relation-level interpreta- tion uses the standard logical relations interpretations of these type Θ ` natrecκ ABC : κ constructors. Universal quantification, ∀α:κ. A, is interpreted at These three constructs all have the evident interpretations in terms the object level by taking the dependent product over the objects of of the inductive structure of the natural numbers, and satisfy the the interpretation of κ (this product exists because we have stipu- following β-laws, maintaining Property 2 of our semantics: lated that κ must be small), and then restricting to those elements of the dependent product that preserve relations. This restriction is natrecκ zero BC ≡ B required for this interpretation to preserve identity edges, and so natrecκ (succ A) BC ≡ C (natrecκ ABC) be a reflexive graph morphism. The relation-level interpretation of universal quantification is the standard relational interpretation of 3.3 Groupoid Kinds such types, albeit here generalised to kinds interpreted as arbitrary Discrete kinds are useful for lifting types up to the kind level, but reflexive graphs. they do not really exploit the flexibility of reflexive graphs. More- Well-kinded typing contexts Θ ` Γ are interpreted as reflexive over, they do not provide us with the invariance properties we re- graph morphisms Γ : Θ → ∗ by taking the product of the in- quire to apply Noether’s theorem. By considering kinds whose in- terpretations of theirJ K constituentJ K J types,K similar to the interpretation terpretations are groupoids, we will be able to derive the invariance of the product types A × B. properties that we need. We will extend basic System Fω with additional types and type Recall that a groupoid is a category in which all morphisms equalities in Sections 3.2, 3.3, and 4, below. These new types will have inverses [15]. Functors between categories always preserve f Θ ` αi : κi θ = πiθ r 0 JΘ ` αi : κiK θθ ρ = πiρ J K f O f 0 O R 0 r 0 id Θ ` λα:κ1.A : κ1 → κ2 θ = (λo ∈ κ1 . A (θ, o), λo, o ∈ κ1 , r ∈ κ1 (o, o ). A (θ, o)(θ, o )( Θ θ, r)) r 0 0 O R 0 r 0 0 JΘ ` λα:κ1.A : κ1 → κ2K θθ ρ = λo, o ∈J κK1 J, rK∈ κ1 (o, o ). JA K(θ, o)(θJ, oK)(ρ, r) J K J K J K J K J K J K f f f Θ ` FA : κ2 θ = π1( F θ)( A θ) r 0 r 0 f f 0 r 0 JΘ ` FA : κ2K θθ ρ = F J θθK ρ ( AJ Kθ)( A θ )( A θθ ρ) J K J K J K J K J K f f f Θ ` hA, Bi : κ1 × κ2 θ = ( A θ, B θ) r 0 r 0 r 0 JΘ ` hA, Bi : κ1 × κ2K θθ ρ = (JAK θθJρ, KB θθ ρ) J K J K J K f f Θ ` πiA : κi θ = πi( A θ) r 0 r 0 JΘ ` πiA : κiK θθ ρ = πi(JAK θθ ρ) J K J K Figure 4. Interpretation of type-level λ-calculus as reflexive graph morphisms

Θ ` A → B : ∗ f θ = A f θ → B f θ JΘ ` A → B : ∗Krθθ0ρ = {J(f,K f 0) | ∀J(a,K a0) ∈ A rθθ0ρ. (f a, f 0 a0) ∈ B rθθ0ρ} J K J K J K Θ ` A × B : ∗ f θ = A f θ × B f θ JΘ ` A × B : ∗Krθθ0ρ = {J((Ka, b), (Ja0,K b0)) | (a, a0) ∈ A rθθ0ρ, (b, b0) ∈ B rθθ0ρ} J K J K J K Θ ` ∀α:κ. A : ∗ f θ = {x ∈ (∀o ∈ κ O. A f (θ, o)) | ∀o, o0, r ∈ κ R(o, o0). (x o, x o0) ∈ A r(θ, o)(θ, o0)( Θ idθ, r)} JΘ ` ∀α:κ. A : ∗Krθθ0ρ = {(x, x0) | ∀o,J oK0, r ∈J Kκ R(o, o0). (x o, x0 o0J) ∈K A r(θ, o)(θ0, o0)(ρ, rJ)} K J K J K J K J K Figure 5. Interpretation of basic types as reflexive graph morphisms isomorphisms, so morphisms of groupoids are just functors. Every This structure means that we can add the following type-level con- group is a groupoid with one object, and a morphism from this stants to our system: object to itself for every element of the group: composition is the Θ ` A : Z Θ ` B : Z Θ ` A : Z group operation, inverses are given by the group inverses, and the identity morphism is given by the group unit. Homomorphisms Θ ` 0 : Z Θ ` A + B : Z Θ ` −A : Z between groups are in one-to-one correspondence with functors Each of these constants has a trivial object-level interpretation, due between the corresponding groupoids. An example of a groupoid to the trivial collection of objects in the reflexive graph Z . At the that is not a group is given by the collection of cartesian spaces J K n relation-level, the interpretations are simply given using the group (i.e., R for some n), with all diffeomorphisms (smooth functions structure of the integers. These interpretations satisfy the abelian with smooth inverses) as the morphisms. group axioms, so we add these axioms to the type-level equations. Trivially, every groupoid is a reflexive graph, simply by for- getting the composition and inverses. Likewise, every functor be- Translation and Linear Transformation Groups The group of tween groupoids is a morphism of reflexive graphs, by forgetting integers is not suitable for stating the invariance properties we the preservation of composition. Therefore, given any groupoid, require to apply Noether’s theorem. To do so, we require groups we can add a new kind to System Fω that is interpreted by that of translations and invertible linear transformations. We extend our groupoid, and given any functor f : G1 → G2 between type system with small kinds representing n-dimensional (n ≥ groupoids interpreting kinds G1 and G2, weJ obtainK aJ typeK constant 0) translations, invertible linear transformations, and orthogonal of kind G1 → G2, with interpretation f. transformations: Integers Our first groupoid kind is Z, interpreted by additive κ ::= · · · | T(n) | GL(n) | O(n) T(n), GL(n), O(n) small group of integers. We will use this kind to state the types of the trigonometric functions sin and cos in Section 4.1, which are peri- The interpretations of these kinds follow the same pattern as for the odic with period 2π. The kind Z is small: kind Z above. Each interpretation is a reflexive graph with a triv- ial collection of objects, and relations taken from the appropriate κ ::= · · · | Z Z small group: n ~ The kind Z has the following interpretation as a reflexive graph: T(n) = ({∗}, λ(∗, ∗). R , λ∗. 0) JGL(nK) = ({∗}, λ(∗, ∗). GL(n), λ∗.I) Z = ({∗}, λ(∗, ∗). Z, λ∗. 0) JO(n) K = ({∗}, λ(∗, ∗). O(n), λ∗.I) J K J K Note the difference between this interpretation and the interpreta- Here, we take GL(n) to be exactly the group of invertible real n×n tion of the kind Nat in the previous section. In the interpretation of matricies, and O(n) to be the group of n × n orthogonal matricies Nat, the objects were complex and the relations were trivial. The (recall that an orthogonal matrix is one whose transpose is equal reflexive graph interpreting Z has a trivial collection of objects, but to its inverse). We add the group operations and group axioms for a rich structure at the relation level, given by the group of integers. each of these groupoid kinds in the same way as we did for the kind Z above, except that we use multiplicative notation for the Using the type-level natural numbers and their recursion opera- kinds GL(n) and O(n). Note that, except for GL(0), GL(1), O(0) tor, we can define a type of n-ary products of a cartesian space: and O(1), the groups GL(n) and O(n) are not abelian. 0 We also extend our type system with the following type-level vec = λn:Nat,X:CartSp. natrec n R (λx. X × x) constants. Each of these is interpreted by the correspondingly n-ary products of cartesian spaces will be useful in Section 5 when named homomorphism between the groups involved. For exam- we define Lagrangians describing systems that are generic in the ple, scalen is interpreted by the homomorphism GL(1) → GL(n) number of particles involved. that takes a non-zero real number s to the matrix with ss along the Finally, we include an operator that takes any cartesian space to diagonal and zeros elsewhere. its corresponding proper type: exp : T(1) → GL(1) Θ ` X : CartSp orthon : O(n) → GL(n) Θ ` X : ∗ scalen : GL(1) → GL(n) * + c ∗ − : Z → T(1) with the following straightforward interpretation: f X f θ In the last of these c stands for an arbitrary real constant. Since each X θ = RJ K of these is interpreted by a group homomorphism, we are justified J*X+Krθθ0ρ = {(~x, x~0) | ( X rθθ0ρ)~x = x~0} in adding the group homomorphism laws as axioms to our type- J* +K J K level equational theory, maintaining Property 2. 4. A Type System for Classical Mechanics The Groupoid of Cartesian Spaces Finally in this section, we In the previous section we constructed a way to derive invariance introduce the groupoid kind of cartesian spaces, which we will use properties from types. In Section 2, we saw that Noether’s theorem for the configuration spaces of the classical mechanical systems is a method for deriving conservation laws for classical mechanical we describe in our system. As we noted above, the collection systems from their invariance properties. In this section, we con- of cartesian spaces is a groupoid with a non-trivial collection of struct a type system for constructing invariant Lagrangians. In the objects. We extend our system with a kind of cartesian spaces: following section, Section 5, we present several uses of our type system for defining invariant Lagrangians that describe many dif- κ ::= · · · | CartSp ferent kinds of classical mechanical systems. with the following interpretation: 4.1 Extending System Fω with Smooth Functions CartSp = The Type of Smooth Functions Given a pair of cartesian spaces m n J K n (N, λm, n. {f : R → R | f a diffeomorphism}, λn. idR ) X and Y we provide a way to construct the type of smooth invariant smooth functions from X to Y : We have chosen to represent cartesian spaces of dimension n just as the natural number n. This gives us a canonical representation Θ ` X : CartSp Θ ` Y : CartSp of n-dimensional space for each n, avoiding tricky problems with Θ ` C∞(X,Y ): ∗ isomorphic constructions of the same space. ∞ Cartesian spaces are closed under products: with the following interpretation. Since C (X,Y ) is a proper type, the relational interpretation in this case is a genuine relation. Θ ` X : CartSp Θ ` Y : CartSp We relate functions that are invariant under the diffeomorphism Θ ` X × Y : CartSp interpretations of X and Y in the current relational context ρ: ∞ f X f θ Y f θ with the following interpretation, where the cartesian product of C (X,Y ) θ = {f : RJ K → RJ K | f smooth} J ∞ Kf 0 0 X f θ cartesian spaces adds their dimensions, and composes diffeomor- C (X,Y ) θθ ρ = {(f, f ) | ∀~x ∈ RJ K . phisms “in parallel”: J K ( Y rθθ0ρ)(f ~x) = f 0 ( X rθθ0ρ~x)} J K J K X × Y f θ = X f θ + Y f θ An Example Free Theorem We are now in a position to formally JX × Y Krθθ0ρ = λJ (~x,~yK ). ( JXKrθθ0ρ~x, Y rθθ0ρ~y) show how we can use the relationally parametric model we have J K J K J K defined to derive invariance properties. Consider a closed term with Cartesian spaces are generated by the following construct that the following type: yields the n-dimensional cartesian space (n ≥ 0) that varies with ∞ n the given invertible linear transformation and translation: e : ∀o : O(n).C (R horthon(o), 0i, Rh1, 0i) Θ ` G : GL(n)Θ ` T : T(n) We can deduce, from Property 3 and the relational interpretations of Θ ` nhG, T i : CartSp all the types involved, that the denotation of e satisfies the following R free theorem: 1 As a notational convenience, we write R hG, T i as just RhG, T i n 0 0 ∀O ∈ O(n). ∀~x ∈ R . e (O~x) = e (~x) and R hG, T i as just R . J K J K n The type R hG, T i has the following interpretation as a reflex- This free theorem is exactly the kind of invariance property we re- ive graph morphism: quire to apply Noether’s theorem. Compare the form of the state- ment here, with the mildly more general statement of Equation 9, n f n R hG, T i θ = n back in Section 2. Moreover, e is a smooth function from R to J n Kr 0 r 0 r 0 R hG, T i θθ ρ = λ~x. ( G θθ ρ)~x + T θθ ρ R, just as we require for LagrangianJ K mechanics. J K J K J K n r 0 id Note that R hG, T i θθ ( Θ θ) is the identity diffeomorphism, Combinators for Smooth Functions Of course, such free theo- so this interpretationJ K preservesJ K identity edges, exactly as required rems are pointless without ways to build elements of the smooth in the definition of reflexive graph morphisms. Consequently, the function spaces. We now extend our type system, for the last time, n interpretation of R hG, T i, and also the interpretation for products with a set of term-level constants for constructing smooth func- of cartesian spaces, preserves Property 2. tions, invariant under linear transformations and translations. Our first set of combinators constructs constant and identity bzc = const z smooth functions, and composes smooth functions between carte- ∆ bxc = project (x) sian spaces. We also have combinators for pairing and projection ∆ ∆ for the product of cartesian spaces. blet ~x = e1 in e2c∆ = pair id be1c∆ >>> be2c∆,~x b(e1, ..., en)c∆ = pairn(be1c∆, ..., benc∆) ∞ const : Y → C (X,Y ) be1(e2)c∆ = be2c∆ >>>e1 ∞ id : *C +(X,X) b~cc∆ = const ~c ∞ ∞ ∞ (>>>): C (X,Y ) → C (Y,Z) → C (X,Z) b0c∆ = const 0 ∞ ∞ ∞ pair : C (X,Y ) → C (X,Z) → C (X,Y × Z) be1 + e2c∆ = pair be1c∆ be2c∆ >>> (+) ∞ proj1 : C (X × Y,X) be1 − e2c∆ = pair be1c∆ be2c∆ >>> (−) ∞ proj2 : C (X × Y,Y ) be1e2c∆ = pair be1c∆ be2c∆ >>> (∗) be1 · e2c∆ = pair be1c∆ be2c∆ >>> (·) Next, we include combinators for constant, invariant, vectors, bsin ec∆ = bec∆ >>> sin the zero vector, vector addition, subtraction and scaling. We also bcos ec∆ = bec∆ >>> cos include the Euclidean dot product of vectors. The type dot product bexp ec∆ = bec∆ >>> exp operation is where the orthogonal group kind O(n) is used: the dot be1/e2c∆ = pair be1c∆ be2c∆ >>> (/) product of vectors is invariant under orthogonal transformations. bsqrt ec∆ = bec∆ >>> sqrt n bsum ec = bec >>> sum ~c : R h1, 0i ∆ ∆ n bmap (x. e ) e c = pair id be c >>> (map be c ) 0 : *∀g:GL(n)+. R hg, 0i 1 2 ∆ 2 ∆ 1 ∆,x bcross e1 e2c∆ = pair be1c∆ be2c∆ >>> cross (+) : ∀g:GL(n), t*1, t2:T(n).+ ∞ n n n C (R hg, t1i × R hg, t2i, R hg, t1 + t2i) (−): ∀g:GL(n), t1, t2:T(n). Figure 7. Desugaring of the surface syntax ∞ n n n C (R hg, t1i × R hg, t2i, R hg, t1 − t2i) (∗): ∀g1:GL(1), g2:GL(n). ∞ n n C (Rhg1, 0i × R hg2, 0i, R hscalen(g1)g2, 0i) (·): ∀g:GL(1), o:O(n). 4.2 A Surface Syntax for Smooth Functions ∞ n C (R h(scalen g)(orthon o), 0i× The combinators we have presented allow the construction of n 2 R h(scalen g)(orthon o), 0i, Rh(scalen g) , 0i) smooth invariant functions, but doing so is painful due to the point- free style that they force. In order to be able to actually define Each of these combinators has a straightforward interpretation as readable Lagrangians in our calculus, we define a surface syntax a smooth function, and the proof that the fundamental theorem of for writing smooth functions that is desugared into our extended logical relations is maintained for our system (i.e., that Property 3 version of System Fω. is maintained) follows directly from elementary facts about linear We define a typing judgement of smooth terms Θ | Γ; ∆ ` e : algebra. X by the rules in Figure 6, where Θ and Γ are the kinding and It will be useful for our examples to have the following trigono- typing contexts of our extension of Fω, and ∆ = x1 : X1, ..., xn : metric functions, exponential function and division operation. The Xn is a context of cartesian spaces. That is, for each x : X in ∆, types of each of these functions describes some of their invariance we have Θ ` X : CartSp. properties, such as the periodicity of the sin and cos functions. A The desugaring of smooth function terms into our extension of technical problem with our current system is that we do not account System Fω is via the operation b−c∆, defined in Figure 7. In this for the non-definedness of division at 0, nor do we account for the definition, we make use of two derived combinators for projection square root of negative numbers. However, division and square root and pairing: will be useful in our examples in Section 5, so we take a pragmatic approach, in common with most physics textbooks, and informally projectx:X,∆(x) = proj1 treat them as total functions. projecty:Y,∆(x) = proj2 >>> project∆(x)(x 6= y) ∞ sin : ∀z:Z.C (Rh1, 2π ∗ zi, Rh1, 0i) ∞ cos : ∀z:Z.C (Rh1, 2π ∗ zi, Rh1, 0i) and ∞ exp : ∀t:T(1).C (Rh1, ti, Rhexp t, 0i) pair1(e) = e ∞ −1 (/): ∀g1, g2:GL(1).C (Rhg1, 0i × Rhg2, 0i, Rhg1g2 , 0i) pair (e, ~e) = pair e (pair (~e)) ∞ n+1 n sqrt : ∀g:GL(1).C (Rhg · g, 0i, Rhg, 0i) The following theorem follows easily by induction on the typing Finally, we include three primitive combinators for dealing with derivations of smooth terms, where b∆c = bx1 : X1, ..., xn : n-ary products of vectors. These combinators will be useful when Xnc = X1 × ... × Xn: defining Lagrangians that are generic in the number of particles. ∞ Theorem 2. If Θ|Γ; ∆ ` e : X then Θ|Γ ` bec∆ : C (b∆c,X). ∞ sum : ∀n:Nat, g:GL(1).C (vec n (Rhg, 0i), Rhg, 0i) map : ∀n:Nat.C∞(Z × X,Y ) → C∞(Z × vec n X, vec n Y ) cross : ∀m, n:Nat. 5. Examples of Conservation Laws from Types C∞(vec m X × vec n Y, vec (m ∗ n)(X × Y )) We now present a number of examples of classical mechanical sys- The combinator sum sums the list of real numbers it is given, tems expressible in the type system we constructed in the previ- while cross takes the cartesian product of two vectors of elements ous section, and derive the free theorems and consequent conserved of cartesian spaces, and map maps the given smooth function properties for each one. These examples are all standard examples over a vector. The multiplication of natural numbers m ∗ n is demonstrating the application of Lagrangian mechanics (see, for straightforwardly defined in terms of the natrec recursion operator. example, Landau and Lifschitz [12]). Our contribution here is to We will use cross in Section 5 to define Lagrangians in terms of the express them in a type system that makes clear their invariance interactions between systems of particles. properties. Administrative rules

z : X ∈ Γ x : X ∈ ∆ Θ | Γ; ∆ ` e1 : X1 × ... × Xn Θ | Γ; ∆, x1 : X1, ..., xn : Xn ` e2 : Y * + Θ | Γ; ∆ ` z : X Θ | Γ; ∆ ` x : X Θ | Γ; ∆ ` let x1, ..., xn = e1 in e2 : Y

∞ Θ | Γ ` e1 : C (X,Y ) Θ | Γ; ∆ ` e2 : X Θ | Γ; ∆ ` e1 : X ... Θ | Γ; ∆ ` en : Xn

Θ | Γ; ∆ ` e1(e2): Y Θ | Γ; ∆ ` (e1, ..., en): X1 × ... × Xn

Vector space operations, and dot product n n n ~c ∈ R Θ ` G : GL(n) Θ | Γ; ∆ ` e1 : R hG, T1i Θ | Γ; ∆ ` e2 : R hG, T2i n n n Θ | Γ; ∆ ` ~c : R h1, 0i Θ | Γ; ∆ ` 0 : R hG, 0i Θ | Γ; ∆ ` e1 + e2 : R hG, T1 + T2i

n n n Θ | Γ; ∆ ` e1 : R hG, T1i Θ | Γ; ∆ ` e2 : R hG, T2i Θ | Γ; ∆ ` e1 : RhG1, 0i Θ | Γ; ∆ ` e2 : R hG2, 0i n n Θ | Γ; ∆ ` e1 − e2 : R hG, T1 − T2i Θ | Γ; ∆ ` e1e2 : R hscalen(G1)G2, 0i

n n Θ | Γ; ∆ ` e1 : R hscalen(G)orthon(O), 0i Θ | Γ; ∆ ` e2 : R hscalen(G)orthon(O), 0i 2 Θ | Γ; ∆ ` e1 · e2 : Rhscalen(G) , 0i

Transcendental functions Θ | Γ; ∆ ` e : Rh1,T i Θ | Γ; ∆ ` e : Rh1, 2π ∗ Zi Θ | Γ; ∆ ` e : Rh1, 2π ∗ Zi Θ | Γ; ∆ ` exp e : Rhexp T, 0i Θ | Γ; ∆ ` sin e : Rh1, 0i Θ | Γ; ∆ ` cos e : Rh1, 0i

Division, square root, sum, and cross

Θ | Γ; ∆ ` e1 : RhG1, 0i Θ | Γ; ∆ ` e2 : RhG2, 0i 2 Θ | Γ; ∆ ` e : hG , 0i Θ | Γ; ∆ ` e : vec N ( hG, 0i) e1 −1 R R Θ | Γ; ∆ ` : RhG1G2 , 0i e2 Θ | Γ; ∆ ` sqrt e : RhG, 0i Θ | Γ; ∆ ` sum e : RhG, 0i

Θ | Γ; ∆, x : X ` e1 : Y Θ | Γ; ∆ ` e2 : vec NX Θ | Γ; ∆ ` e1 : vec MX Θ | Γ; ∆ ` e2 : vec NY

Θ | Γ; ∆ ` map (x. e1) e2 : vec NY Θ | Γ; ∆ ` cross e1 e2 : vec (M ∗ N)(X × Y )

Figure 6. A surface syntax for smooth functions

5.1 Single Particles quantifying over translations in space, tx, gives us invariance under Free Particle Our first example is of a simple Lagrangian for a translation in space: single free particle in 3-dimensional space, with no external forces. 3 ˙ ˙ ∀t~x ∈ R . L (t, ~x + t~x, ~x) = L (t, ~x, ~x) We define the Lagrangian using the syntax for smooth terms we J K J K defined in the previous section, in the following kinding, typing Similar to the example of the spring-coupled particles in the intro- and cartesian space contexts: duction, invariance under translations in space yields conservation of linear momentum. Θ = tt : T(1), tx : T(3), o : O(3) Quantification over orthogonal transformations o : O(3) yields Γ = m : Rh1, 0i * + 3 3 the following free theorem, which states that the Lagrangian is in- ∆ = t : Rh1, tti, x : R hortho3(o), txi, x˙ : R hortho3(o), 0i variant under modification of the positions and velocities by arbi- where o represents an arbitrary orthogonal transformation of the trary orthogonal transformations O: space, t represents a translation in time, t represents a 3- t x ∀O ∈ O(3). L (t, O~x,O~x˙) = L (t, ~x, ~x˙) dimensional translation in space, m is the constant mass of the J K J K free particle, t is the current time, x is the current position of the We are particularly interested in the orthogonal transformations particle, and x˙ is the current velocity. derived from rotations, since these will give us the continuous We write the Lagrangian like so, which in the absence of exter- transformations we need to apply Noether’s theorem. For example, nal forces just consists of the kinetic energy term: rotation by an angle  around the x3-axis is given by the following 1 -indexed family of orthogonal matricies: L = m(x ˙ · x˙): Rh1, 0i 2  cos  sin  0  By the fundamental theorem of logical relations for our calculus O =  − sin  cos  0  (Property 3), we can derive the following free theorems, one each 0 0 1 for the three group parameters tt, tx and o. Quantifying over trans- lations in time, tt, gives us invariance under translation in time: This leads to the following -indexed family of transformations, suitable for Noether’s theorem: ˙ ˙ ∀tt ∈ R. L (t + tt, ~x, ~x) = L (t, ~x, ~x)       J K J K x1 x1 x1 cos  + x2 sin  As we saw in Example 4, Noether’s theorem tells us that invariance Ψ x2 = O x2 = −x1 sin  + x2 cos  under translation in time yields conservation of energy. Likewise, x3 x3 x3 Plugging this into Equation 10, and using the definition of L above, 5.3 Pendulum we obtain the following : All our examples above have used rectangular coordinate systems. d Part of the power of the Lagrangian formulation of classical me- (mxx˙ − mxx˙ ) = 0 dt 2 1 chanics is the ability to use appropriate generalised coordinates to describe systems in simple terms. A standard example is that of a This is exactly conservation of around the x - 3 pendulum, where we take the angle θ of swing from the vertical as axis [12]. By considering the families of orthogonal matrices for the coordinate. rotation around other axes, we can also derive conservation laws We use the following kinding, typing and cartesian space con- for angular momentum around arbitrary axes. texts:

Particle in a Potential Field If we now extend the typing con- Θ = tt : T(1), z : Z text of our example with an arbitrary potential energy function V , Γ = m : Rh1, 0i , l : Rh1, 0i * + * + ˙ depending on the position of the particle, we can define the La- ∆ = t : Rh1, tti, θ : Rh1, 2π ∗ zi, θ : Rh1, 0i grangian for a single particle acted upon by a potential field that depends on the position of the particle. The invariance properties where m is the mass at the end of the pendulum (we assume the rod of the whole system will depend upon the invariance properties of of the pendulum to be massless), l is the length of the pendulum, θ the potential field. If we assume the following contexts: is the angle of swing and θ˙ is the current rate of change of the angle of swing. Θ = tt : T(1), o : O(3) Defining the Lagrangian for this system is a matter of simple Γ = m : Rh1, 0i , trigonometry and differential calculus: * + ∞ 3 V : ∀o:O(3).C (R hortho3(o), 0i, Rh1, 0i) 3 3 L = let y = l sin θ in ∆ = t : Rh1, tti, x : R hortho3(o), 0i, x˙ : R hortho3(o), 0i let x˙ = lθ˙ cos θ in where everything is as in the previous example, except for the addi- let y˙ = −lθ˙ sin θ in tion of the polymophic potential energy function V . We incorporate 1 m(x ˙ 2 +y ˙2) − mgy : h1, 0i this into our Lagrangian like so: 2 R 1 From the type of t, we can easily determine that this Lagrangian is L = m(x ˙ · x˙) − V (x): h1, 0i 2 R invariant under translation in time, and hence the total energy is a conserved quantity of this system. We can also derive another free Even though V is left abstract, the types of t, x, x˙ and L tell us that theorem due to the quantification over the group of integers via the this Lagrangian is invariant under translations in time and also un- variable z. However, this does not yield a continuous invariance der all orthogonal transformations. Hence, by Noether’s theorem, of the Lagrangian, and hence no conserved quantity via Noether’s the system this Lagrangian describes has energy and angular mo- theorem. mentum as conserved quantities. 5.4 Oscillators 5.2 The n-Body Problem Coupled Particles Our first example of a Lagrangian in the intro- Using the constructs of our calculus for dealing with n-ary vectors duction was of a pair of particles of equal mass coupled by a spring, of positions and velocities, we can compactly write Lagrangians for which we derived conservation of linear momentum and energy. that describe systems of n particles interacting through Newtonian In terms of our type system, we have the following kinding, typing gravitational attraction. We use the following kinding, typing and and cartesian space contexts, where m is the individual mass of the cartesian space contexts, where the type-level parameter n repre- two particles, and k is the spring constant: sents the number of particles that we are considering. For simplic- ity, we assume that all the bodies involved in the system have equal Θ = tt : T(1), tx : T(1) mass m. Γ = m : Rh1, 0i , k : Rh1, 0i ∆ = t : R*h1, tti, x+1 : Rh*1, txi, x+2 : Rh1, txi, Θ = n : Nat, tt : T(1), tx : T(3), o : O(3) x˙1 : Rh1, 0i, x˙2 : Rh1, 0i Γ = m : Rh1, 0i ∆ = t : R*h1, tti, + In our surface syntax for smooth terms, the Lagrangian is written 3 x : vec n (R hortho3(o), txi), just as it was in the introduction: 3 x˙ : vec n (R hortho3(o), 0i) 1 1 L = m(x ˙ 2 +x ˙ 2) − k2(x − x )2 : h1, 0i The Lagrangian for this system is defined as follows in our surface 2 1 2 2 1 2 R syntax for smooth terms. The kinetic energy component is the The free theorems for this term state the invariance in time and sum of the kinetic of all the particles in the system. The space translation: potential energy components sums up all the gravitational potential energies due to the interactions between each pair of bodies, where ∀tt : R. L (t + tt, x1, x2, x˙1, x˙2) = L (t, x1, x2, x˙1, x˙2) G is the gravitational constant. ∀tx : R.J LK (t, x1 + tx, x2 + tx, x˙1, xJ˙2)K = L (t, x1, x2, x˙1, x˙2) J K J K 1 As we saw in Section 2, these invariance properties allow us to de- L = 2 m(sum (map (x ˙i. x˙i · x˙i))x ˙)− 2 duce conservation of energy and linear momentum for this system. sum (map ((xi, xj ). Gm /|xi − xj |) (cross x x)) : Rh1, 0i We have used the notation |e| as shorthand for sqrt (e · e), i.e., the Damped Oscillator All of the example Lagrangians we have norm of the vector e. looked at so far have had no dependence on time, and so they Again, just by looking at the types of t, x, x˙, and L, we can have all described systems with total energy as a conserved quan- determine via free theorems that this Lagrangian is invariant under tity. A system with a single particle attached to a damped spring translation in time and space, and under all orthogonal transfor- (whose other endpoint is fixed at position 0) provides an example mations. Hence, by Noether’s theorem, this system has energy and of system that does not have energy as a conserved quantity. This linear and angular momentum as conserved quantities. example is taken from Neuenschwander’s book [13]. We use the following kinding, typing and cartesian space contexts: From the type system point of view, it seems clear that greater precision in the types will be required in order to accomodate Θ = t : T(1) t more detailed physical theories. Study of relational parametricity Γ = k : h1, 0i R for dependent types, as done for example by Bernardy et al. [6] and ∆ = t : *h1, t ++t i, x : hexp(−t ), 0i, x˙ : hexp(−t ), 0i R t t R t R t Atkey et al. [4] may be useful here. Also, recent work on homotopy Here, the position and velocity of the single particle scale with the type theory and, in particular ∞-groupoid models of type theory is exponential of the translation in time. This will account for the almost certainly relevant [21]. exponential damping we apply to the system. For simplicity, we For each of the examples in Section 5, we computed the neces- assume that the mass of the particle and the spring constant are sary typing derivations by hand. Type inference for the system was both 1. The Lagrangian for this system looks similar to the coupled have presented here, or some variant of it, is a key item of future pair above, except for an additional damping factor: work. Ideally, the user would enter the description of a Lagrangian,   and the system would be able to tell them the free theorems, and 1 2 1 2 L = x˙ − x exp(t): Rh1, 0i consequent conservation laws that hold. Kennedy [10] and Gundry 2 2 [8] have studied type inference for the related, but simpler, setting From the types of t, x and x˙, we learn that this system is invariant of dimension types. under the families of transformations Φ(t) = t + 2 and Ψ(x) = ex. This leads to the following conservation law for this system, References linking energy and linear momentum: [1] S. Abramsky. High-Level Methods for Quantum Computation and d  1 1 1   Information. Proceedings, LICS, 2004. xx˙ + x˙ 2 + x2 et = 0 dt 2 2 2 [2] V. I. Arnol’d. Mathematical Methods of Classical Mechanics. Springer, 1989. 6. Conclusions [3] R. Atkey. Relational Parametricity for Higher Kinds. Proceedings, CSL, 2012. We have presented a type system for writing invariant Langrangians, with a relationally parametric semantics that allows the derivation [4] R. Atkey, N. Ghani, and P. Johann. A Relationally Parametric Model of Dependent Type Theory. Proceedings, POPL, 2014. of free theorems that can be used with Noether’s theorem to dis- cover conservation laws for classical mechanical systems. Our key [5] R. Atkey, P. Johann, and A. J. Kennedy. Abstraction and Invariance technical contribution has been the observation that relationally for Algebraically Indexed Types. Proceedings, POPL, 2013. parametric models of System Fω admit kinds that are interpreted [6] J.-P. Bernardy, P. Jansson, R. Paterson. Proofs for Free: Parametricity as groupoids, allowing geometric invariance properties to be inte- for Dependent Types. Journal of Functional Programming 22(2), pp. grated directly into the model. 107-152, 2012. [7] I. M. Gelfand and S. V. Fomin, R. A. Silverman (ed.). Calculus of Related Work We are not aware of any other work linking type Variations. Dover Publications, 2000. systems or relational parametricity with invariance properties for [8] A. Gundry. Type Inference for Units of Measure. Technical Report, classical mechanics, or with Noether’s theorem. We have already University of Strathclyde, 2011. mentioned the work of Atkey, Johann and Kennedy [5] that uses [9] R. Hasegawa. Relational Limits in General Polymorphism. Publi- relational parametricity with a specialised type system to derive cations of the Research Institute for Mathematical Sciences 30, pp. geometric invariance properties, albeit in a setting without smooth 535–576, 1994. functions, and without an application to Noether’s theorem. In this [10] A. J. Kennedy. Dimension Types. Proceedings, ESOP, 1994. paper, we have placed Atkey et al.’s work in a more general setting by framing it as an extension of System Fω, which allows us to [11] A. J. Kennedy. Relational Parametricity and Units of Measure. Proceedings, POPL, pp. 442-455, 1997. (a) incorporate type-level computation, as we used to define the n-ary vectors of coordinates; and (b) to reuse previous work on [12] L. D. Landau and E. M. Lifschitz. Mechanics. Pergamon Press. 1967. relationally parametric models of System Fω by Atkey [3]. [13] D. E. Neuenschwander. Emmy Noether’s Wonderful Theorem. The Sussman and Wisdom [20] reformulate Lagrangian and Hamil- John Hopkins University Press, 2011. tonian mechanics in a more programming language style so that it [14] E. Noether, M. Tavel (translator). Invariant Variation Problems. can be implemented within the programming language Scheme. In Transport Theory and Statistical Physics 1(3), pp. 186-207, 1971. doing so, they clear up some of the ambiguous syntax common in Original in Gott. Nachr., 1918:235-257, 1918. the standard presentations of classical mechanics. However, they [15] S. Mac Lane. Categories for the Working Mathematician, 2nd edition. do not attempt to incorporate invariance properties into types, as Springer, 1998. we have done here. [16] B. Pierce. Types and Programming Languages. MIT Press, 2002. There has been prior work on programming languages inspired [17] A. M. Pitts. Polymorphism is Set Theoretic, Constructively. Proc., by theoretical physics. Quantum programming languages, which Category Theory and Computer Science, pp. 12–39, 1987. hope to exploit the properties of quantum computation in an under- [18] J. C. Reynolds. Types, Abstraction and Parametric Polymorphism. standable way, have been the subject of study for several years now. Information Processing 83, pp. 513-523, 1983. We cite Abramsky [1] as an introduction to this field. [19] E. Robinson and G. Rosolini. Reflexive Graphs and Parametric Future Work We have barely scratched the surface of the appli- Polymorphism. Proc., Logic in Computer Science, pp. 364–371, 1994. cability of Noether’s theorem to theoretical physics. Conservation [20] G. J. Sussman and J. Wisdom. Structure and Interpretation of Classical laws for classical and quantum field theories are derivable from the Mechanics. MIT Press, 2001 much more general theorem that Noether originally proved [14]. [21] The Univalent Foundations Program. Homotopy Type Theory. Institute Neuenschwander’s book [13] describes in an easy to read way how for Advanced Study, 2013. Noether’s theorem applies to field theories, allowing for relativistic [22] P. Wadler. Theorems for Free!. Proceedings, FPCA, pp. 347-359, and quantum systems to be studied, and how it naturally leads to 1989. the notion of Gauge invariance, an important principle in modern theories of particle physics.