Processing math: 100%

Wednesday, September 12, 2018

Reading list for getting started on Gradual Typing

Which papers would I recommend for getting started on understanding the research on gradual typing? That's a hard question because there are a lot of papers to choose from and, as research papers, their primary goal was not to give a good introduction, but instead to describe some scientific contribution. I really ought to write a proper introduction, but in the mean time, here's my choice of a few papers to get started.
  1. Refined Criteria for Gradual Typing
    This paper does a decent job of surveying research related to gradual typing and situating it with respect to other areas of research in programming languages and type systems. The paper includes a modern and, what I would deem canonical, specification of the Gradually Typed Lambda Calculus (GTLC). Finally, the paper gives formal criteria for what it means for a language to be gradually typed, including the gradual guarantee.
  2. Blame and Coercion: Together Again for the First Time (alternative location)
    The runtime semantics of a gradually typed language is typically given in two parts: 1) a translation to a cast calculus and 2) an operational semantics for the cast calculus. Nowadays,  I recommend using coercions to express casts because they help to constrain the design space in a good way, they are easily extended to handle blame tracking, and they can be compressed to ensure space efficiency (time too!). This paper defines an easy-to-understand coercion calculus λC and a space-efficient calculus λS, proves that they are equivalent to the standard cast calculus λB, and also reviews the blame safety theorem.
  3. Abstracting Gradual Typing (alternative location)
    This paper presents a general framework based on abstract interpretation for understanding gradual typing and for extending gradual typing to handle languages that make use of other predicates on types, such as subtyping. The framework provides guidance for how to define the consistency relation and for how to derive an operational semantics.
After reading the above papers, there's plenty more to enjoy! See the bibliography maintained by Sam Tobin-Hochstadt.

Thursday, August 09, 2018

Intersection Types, Sub-formula Property, and the Functional Character of the Lambda Calculus

Intersection Types, Sub-formula Property, and the Functional Character of the Lambda Calculus

Last December I proved that my graph model of the lambda calculus, once suitable restricted, is deterministic. That is, I defined a notion of consistency between values, written v1v2, and showed that any two outputs of the same program are consistent.
Theorem (Determinism)
If vE[[e]]ρ, vE[[e]]ρ, and ρρ, then vv.
Recall that values are integers or finite relations; consistency for integers is equality and consistency for relations means mapping consistent inputs to consistent outputs. I then restricted values to be well formed, meaning that they must be consistent with themselves (and similarly for their parts).

Having proved the Determinism Theorem, I thought it would be straightforward to prove the following related theorem about the join of two values.
Theorem (Join)
If vE[[e]]ρ, vE[[e]]ρ, ρ is well formed, ρ is well formed, and ρρ,
then vvE[[e]](ρρ).
I am particularly interested in this theorem because β-equality can be obtained as a corollary. E[[(λx.e)e]]ρ=E[[[x:=e]e]]ρ This would enable the modeling of the call-by-name λ-calculus and it would also enable the use of β-equality in a call-by-value setting when e is terminating (instead of restricting e to be a syntactic value).

Recall that we have defined a partial order on values, and that, in most partial orders, there is a close connection between notions of consistency and least upper bounds (joins). One typically has that vv iff vv exists. So my thinking was that it should be easy to adapt my proof of the Determinism Theorem to prove the Join Theorem, and I set out hoping to finish in a couple weeks. Hah! Here we are 8 months later and the proof is complete; it was a long journey that ended up depending on a result that was published just this summer, concerning intersection types, the sub-formula property, and cut elimination by Olivier Laurent. In this blog post I’ll try to recount the journey and describe the proof, hopefully remembering the challenges and motivations. Here is a tar ball of the mechanization in Isabelle and in pdf form.

Many of the challenges revolved around the definitions of , consistency, and . Given that I already had definitions for and consistency, the obvious thing to try was to define such that it would be the least upper bound of . So I arrived at this partial function: nn=nf1f2=f1f2 Now suppose we prove the Join Theorem by induction on e and consider the case for application: e=(e1e2). From vE[[e1e2]] and vE[[e1e2]] we have

  • fE[[e1]]ρ, v2E[[e2]]ρ, v3v4f, v3v2, and vv4 for some f,v2,v3,v4.

  • fE[[e2]]ρ, v2E[[e2]]ρ, v3v4f, v3v2, and vv4 for some f,v2,v3,v4.

By the induction hypothesis we have ffE[[e1]] and v2v2E[[e2]]. We need to show that v3v4ffv3v2v2vvv4 But here we have a problem. Given our definition of in terms of set union, there won’t necessarily be a single entry in ff that combines the information from both v3v4 and v3v4. After all, ff contains all the entries of f and all the entries of f, but the set union operation does not mix together information from entries in f and f to form new entries.

Intersection Types to the Rescue

At this point I started thinking that my definitions of , consistency, and were too simple, and that I needed to incorporate ideas from the literature on filter models and intersection types. As I’ve written about previously, my graph model corresponds to a particular intersection type system, and perhaps a different intersection type system would do the job. Recall that the correspondence goes as follows: values correspond to types, corresponds to subtyping <: (in reverse), and corresponds to intersection . The various intersection type systems primarily differ in their definitions of subtyping. Given the above proof attempt, I figured that I would need the usual co/contra-variant rule for function types and also the following rule for distributing intersections over function types. (AB)(AC)<:A(BC) This distributivity rule enables the “mixing” of information from two different entries.

So I defined types as follows: A,B,C,D::=nABAB and defined subtyping according to the BCD intersection type system (Lambda Calculus with Types, Barendregt et al. 2013). A<:AA<:BB<:CA<:CAB<:AAB<:BC<:AC<:BC<:ABC<:AB<:DAB<:CD(AB)(AC)<:A(BC) I then adapted the definition of consistency to work over types. (Because this definition uses negation, it is easier to define consistency as a recursive function in Isabelle instead of as an inductively defined relation.) nn=(n=n)n(CD)=falsen(CD)=nC and nD(AB)n=false(AB)(CD)=(AC and BD) or AC(AB)(CD)=(AB)C and (AB)D(AB)n=An and Bn(AB)(CD)=AC and AD and BC and BD

Turning back to the Join Theorem, I restated it in terms of the intersection type system and rebranded it the Meet Theorem. Instead of using the letter ρ for environments, we shall switch to Γ because they now contain types instead of values.
Theorem (Meet)
If Γe:A, Γe:B, and ΓΓ, then ΓΓe:AB.
By restating the theorem in terms of intersection types, we have essentially arrived at the rule for intersection introduction. In other words, if we can prove this theorem we will have shown that the intersection introduction rule is admissible in our system.

While the switch to intersection types and subtyping enabled this top-level proof to go through, I got stuck on one of the lemmas that it requires, which is an adaptation of Proposition 3 of the prior blog post.
Lemma (Consistency and Subtyping)

  1. If AB, A<:C, and B<:D, then CD.

  2. If AB, C<:A, D<:B, then CD.

In particular, I got stuck in the cases where the subtyping A<:C or B<:D was derived using the transitivity rule.

Subtyping and the Sub-formula Property

For a long time I’ve disliked definitions of subtyping in which transitivity is given as a rule instead of proved as a theorem. There are several reasons for this: a subtyping algorithm can’t directly implement a transitivity rule (or any rule that is not syntax directed), reasoning by induction or cases (inversion) is more difficult, and it is redundant. Furthermore, the presence of the transitivity rule means that subtyping does not satisfy the sub-formula property. This term sub-formula property comes from logic, and means that a derivation (proof) of a formula only mentions propositions that are a part of the formulate to be proved. The transitivity rule breaks this property because the type B comes out of nowhere, it is not part of A or C, the types in the conclusion of the rule.

So I removed the transitivity rule and tried to prove transitivity. For most type systems, proving the transitivity of subtyping is straightforward. But I soon realized that the addition of the distributivity rule makes it significantly more difficult. After trying and failing to prove transitivity for some time, I resorted to reading the literature. Unfortunately, it turns out that none of the published intersection type systems satisfied the sub-formula property and vast majority of them included the transitivity rule. However, there was one paper that offered some hope. In a 2012 article in Fundamenta Informaticae titled Intersection Types with Subtyping by Means of Cut Elimination, Olivier Laurent defined subtyping without transitivity and instead proved it, but his system still did not satisfy the sub-formula property because of an additional rule for function types. Nevertheless, Olivier indicated that he was interested in finding a version of the system that did, writing

“it would be much nicer and much more natural to go through a sub-formula property”

A lot of progress can happen in six years, so I sent an email to Olivier. He replied,

“Indeed! I now have two different sequent-calculus systems which are equivalent to BCD subtyping and satisfy the sub-formula property. I am currently writting a paper on this but it is not ready yet.”

and he attached the paper draft and the Coq mechanization. What great timing! Furthermore, Olivier would be presenting the paper, titled Intersection Subtyping with Constructors, at the Workshop on Intersection Types and Related System in Oxford on July 8, part of the Federated Logic Conference (FLOC). I was planning to attend FLOC anyways, for the DOMAINS workshop to celebrate Dana Scott’s 85th birthday.

Olivier’s systems makes two important changes compared to prior work: he combines the distributivity rule and the usual arrow rule into a single elegant rule, and to enable this, he generalizes the form of subtyping from A<:B to A1,,AnB, which should be interpreted as meaning A1An<:B. Having a sequence of formulas (types) on the left is characteristic of proof systems in logic, including both natural deduction systems and sequence calculi. (Sequent calculi, in addition, typically have a sequence on the right that means the disjunction of the formulas.) Here is one of Olivier’s systems, adapted to my setting, which I’ll describe below. Let Γ range over sequences of types. Γ1,Γ2AΓ1,n,Γ2AΓ1,Γ2AΓ1,BC,Γ2AΓAΓBΓABΓ1,B,C,Γ2AΓ1,BC,Γ2AnnAC1,,CnD1,,DnBC1D1,,CnDnAB The first two rules are weakening rules for singleton integers and function types. There is no weakening rule for intersections. The third and fourth rules are introduction and elimination rules for intersection. The fifth rule is reflexivity for integers, and the last is the combined rule for function types.

The combined rule for function types says that the intersection of a sequence of function types i=1n(CiDi) is a subtype of AB if A<:i{1n}Ciandi{1n}Di<:B Interestingly, the inversion principle for this rule is the β-sound property described in Chapter 14 of Lambda Calculus with Types by Barendregt et al., and is the key to proving β-equality. In Olivier’s system, β-soundness falls out immediately, instead of by a somewhat involved proof.

The regular subtyping rule for function types is simply an instance of the combined rule in which the sequence on the left contains just one function type.

The next step for me was to enter Olivier’s definitions into Isabelle and prove transitivity via cut elimination. That is, I needed to prove the following generalized statement via a sequence of lemmas laid out by Olivier in his draft.
Theorem (Cut Elimination)
If Γ2B and Γ1,B,Γ3C, then Γ1,Γ2,Γ3C.
The transitivity rule is the instance of cut elimination where Γ2=A and both Γ1 and Γ3 are empty.

Unfortunately, I couldn’t resist making changes to Olivier’s subtyping system as I entered it into Isabelle, which cost me considerable time. Some of Olivier’s lemmas show that the collection of types on the left, that is, the As in A1,,AnB, behave like a set instead of a sequence. I figured that if the left-hand-side was represented as a set, then I would be able to bypass several lemmas and obtain a shorter proof. I got stuck in proving Lemma Le which states that Γ1,AB,Γ2C implies Γ1,A,B,Γ2C. Olivier’s subtyping rules are carefully designed to minimize the amount of overlap between the rules, and switching to a set representation increases the amount of overlap, making the proof of this lemma more difficult (perhaps impossible?).

So after struggling with the set representation for some time, I went back to sequences and was able to complete the proof of cut elimination, with a little help from Olivier at FLOC. I proved the required lemmas in the following order.
Lemma (Weakening)
If Γ1,Γ2A, then Γ1,B,Γ2A.
(Proved by induction on A.)
Lemma (Axiom)
AA
(Proved by induction on A.)
Lemma (Permutation)
If Γ1A and Γ2 is a permutation of Γ1, then Γ2A.
(Proved by induction on the derivation of Γ1A, using many lemmas about permutations.)
Lemma (Le)
If Γ1,AB,Γ2C, then Γ1,A,B,Γ2C.
(Proved by induction on the derivation of Γ1,AB,Γ2C.)
Lemma (Collapse Duplicates)
If Γ1,A,A,Γ2C, then Γ1,A,Γ2C.
(This is proved by well-founded induction on the lexicographical ordering of the pair (n,k) where n is the size of A and k is the depth of the derivation of Γ1,A,A,Γ2C. Proof assistants such as Isabelle and Coq do not directly provide the depth of a derivation, but the depth can be manually encoded as an extra argument of the relation, as in Γ1,A,A,Γ2kC.)
The Cut Elimination Theorem is then proved by well-founded induction on the triple (n,k1,k2) where n is the size of B, k1 is the depth of the derivation of Γ2B, and k2 is the depth of the derivation of Γ1,B,Γ3C.

We define subtyping as follows. A<:B=AB

The BCD subtyping rules and other derived rules follow from the above lemmas.
Proposition (Properties of Subtyping)

  1. A<:A.

  2. If A<:B and B<:C, then A<:C.

  3. If C<:A and B<:D, then AB<:CD.

  4. If A1<:B, then A1A2<:B.

  5. If A2<:B, then A1A2<:B.

  6. If B<:A1 and B<:A2, then B<:A1A2.

  7. If A<:C and B<:D, then AB<:CD.

  8. (AB)(AC)<:A(BC).

  9. (AC)(BD)<:(AB)(CD)

Consistency and Subtyping, Resolved

Recall that my switch to intersection types was motivated by my failure to prove the Consistency and Subtyping Lemma. We now return to the proof of that Lemma. We start with a handful of lemmas that are needed for that proof.
Lemma (Consistency is Symmetric and Reflexive)

  1. If AB, then BA.

  2. If wf(A), then AA.

It will often be convenient to decompose a type into its set of atoms, defined as follows. atoms(n)={n}atoms(AB)={AB}atoms(AB)=atoms(A)atoms(B)

The consistency of two types is determined by the consistency of its atoms.
Lemma (Atomic Consistency)

  1. If AB, Catoms(A), and Datoms(B), then CD.

  2. If (for any Catoms(A) and Datoms(B), CD), then AB.

  3. If AB, then CD for some Catoms(A) and Datoms(B).

  4. If CD, Catoms(A), and Datoms(B), then AB.

There are also several properties of subtyping and the atoms of a type.
Lemma (Atomic Subtyping)

  1. If A<:B and Catoms(B), then A<:C.

  2. If A<:n, then natoms(A).

  3. n<:A if and only if atoms(A){n}.

  4. If C<:AB, then DEatoms(C) for some D,E.

  5. If ΓA and every atom in Γ is a function type, then every atom of A is a function type.

And we have the following important inversion lemma for function types. We use the following abbreviations: dom(Γ)={AB.ABΓ}cod(Γ)={BA.ABΓ}

Lemma (Subtyping Inversion for Function Types)
If C<:AB, then there is a sequence of function types Γ such that

  1. each element of Γ is an atom of C,

  2. For every DEΓ, we have A<:D, and

  3. cod(Γ)<:B.

Note that item 2 above implies that A<:dom(Γ).

Lemma (Consistency and Subtyping)

  1. If AB, A<:C, and B<:D, then CD.

  2. If AB, C<:A, D<:B, then CD.

(1) The proof is by strong induction on the sum of the depths of A, B, C, and D. We define the depth of a type as follows. depth(n)=0depth(AB)=1+max(depth(A),depth(B))depth(AB)=max(depth(A),depth(B)) To show that CD it suffices to show that all of their atoms are consistent. Suppose Catoms(C) and Datoms(D). So we need to show that CD. We proceed by cases on C.

  • Case C=n1:
    We have A<:C and therefore n1atoms(A). Then because AB, we have atoms(B){n1}. We have B<:D, so we also have atoms(D){n1}. Therefore CD.

  • Case C=C1C2:
    We have A<:C1C2, so by inversion we have some sequence of function types Γ1 such that every element of Γ1 is an atom of A, C1<:dom(Γ1), and cod(Γ1)<:C2.

    We also know that D is a function type, say D=D1D2. (This is because we have A<:C, so we know that A1A2atoms(A) for some A1,A2. Then because AB, we know that all the atoms in B are function types. Then because B<:D and Datoms(D), we have that D is a function type.) So by inversion on B<:D1D2, we have some sequence of function types Γ2 such that every element of Γ2 is an atom of B, D1<:dom(Γ2), and cod(Γ2)<:D2.

    It’s the case that either C1D1 or C1D1.

    • Sub-case C1D1.
      It suffices to show that C2D2. By the induction hypothesis, we have dom(Γ1)dom(Γ2).

      As an intermediate step, we shall prove that cod(Γ1)cod(Γ2), which we shall do by showing that all their atoms are consistent. Suppose Aatoms(cod(Γ1)) and Batoms(cod(Γ2)). There is some A1A2Γ1 where Aatoms(A2). Similarly, there is B1B2Γ2 where Batoms(B2). Also, we have A1A2atoms(A) and B1B2atoms(B). Then because AB, we have A1A2B1B2. Furthermore, we have A1B1 because dom(Γ1)dom(Γ2), so it must be the case that A2B2. Then because Aatoms(A2) and Batoms(B2), we have AB. Thus concludes this intermediate step.

      By another use of the induction hypothesis, we have C2D2, and this case is finished.

    • Sub-case C1D1.
      Then we immediately have C1C2D1D2.

  • Case C=C1C2:
    We already know that C is an atom, so we have a contradiction and this case is vacously true.

The next two lemmas follow from the Consistency and Subtyping Lemma and help prepare to prove the case for application in the Join Theorem.
Lemma (Application Consistency)
If A1A2, B1B2, A1<:B1C1, A2<:B2C2, and all these types are well formed, then C1C2.
(This lemma is proved directly, without induction.)
Lemma (Application Intersection)
If A1<:B1C1, A2<:B2C2, A1A2, B1B2, and C1C2, then (A1A2)<:(B1B2)(C1C2).
(This lemma is proved directly, without induction.)

Updating the Denotational Semantics

Armed with the Consistency and Subtyping Lemma, I turned back to the proof of the Join Theorem, but first I needed to update my denotational semantics to use intersection types instead of values. For this we’ll need the definition of well formed types that we alluded to earlier.

wf(n)wf(A)wf(B)wf(AB)ABwf(A)wf(B)wf(AB)

Here are some examples and non-examples of well-formed types. wf(4)wf(33)¬wf(34)wf((01)(23))¬wf((01)(02)) It is sometimes helpful to think of well-formed types in terms of the equivalence classes determined by subtype equivalence: AB=A<:B and B<:A For example, we have 3(33), so they are in the same equivalence class and 3 would be the representative.

We also introduce the following notation for all the well-formed types that are super-types of a given type. A={BA<:B and wf(B)}

We shall represent variables with de Bruijn indices, so an environment Γ is a sequence of types. The denotational semantics of the CBV λ-calculus is defined as follows. E[[n]]Γ=nE[[x]]Γ=ifx<|Γ|thenΓ[k]elseE[[λe]]Γ={Awf(A) and F[[A]]eΓ}E[[e1e2]]Γ={C|A,B.AE[[e1]]Γ,BE[[e2]]Γ,A<:BC, and wf(C)}E[[f(e1,e2)]]Γ={C|A,B,n1,n2.AE[[e1]]Γ,BE[[e2]]Γ,A<:n1,B<:n2,[[f]](n1,n2)<:C,wf(C)}E[[ife1thene2elsee3]]Γ={B|A,n.AE[[e1]]Γ,A<:n,n=0BE[[e3]]Γ,n0BE[[e2]]Γ}F[[n]]eΓ=falseF[[AB]]eΓ=F[[A]]eΓ and F[[B]]eΓF[[AB]]eΓ=BE[[e]](A,Γ)

It is easy to show that swapping in a “super” environment does not change the semantics.

Lemma (Weakening)

  1. If F[[A]]eΓ1, Γ1<:Γ2 and (B,Γ1,Γ2.BE[[e]]Γ1,Γ2<:Γ1BE[[e]]Γ2), then F[[A]]eΓ2.

  2. If AE[[e]]Γ1 and Γ2<:Γ1, then AE[[e]]Γ2.

(Part 1 is proved by induction on A. Part 2 is proved by induction on e and uses part 1.)

The Home Stretch

Now for the main event, the proof of the Meet Theorem!
Theorem (Meet)
If A1E[[e]]Γ1, A2E[[e]]Γ2, both Γ1 and Γ2 are well formed, and Γ1Γ2,
then A1A2E[[e]](Γ1Γ2) and wf(A1A2).
Proof We proceed by induction on e.

  • Case e=k (k is a de Bruijn index for a variable):
    We have Γ1[k]<:A1 and Γ2[k]<:A2, so Γ1[k]Γ2[k]<:A1A2. Also, because Γ1Γ2 we have Γ1[k]Γ2[k] and therefore A1A2, by the Consistency and Subtyping Lemma. So we have wf(A1A2) and this case is finished.

  • Case e=n:
    We have n<:A1 and n<:A2, so n<:A1A2. Also, we have A1A2 by the Consistency and Subtyping Lemma. So we have wf(A1A2) and this case is finished.

  • Case e=λe:
    We need to show that wf(A1A2) and F[[A1A2]]e(Γ1Γ2). For the later, it suffices to show that A1A2, which we shall do by showing that their atoms are consistent. Suppose A1atoms(A1) and A2atoms(A2). Because F[[A1]]eΓ1 we have A1=A11A12 and A12E[[e]](A11,Γ1). Similarly, from F[[A2]]eΓ2 we have A2=A21A22 and A22E[[e]](A21,Γ2). We proceed by cases on whether A11A21.

    • Sub-case A11A21:
      By the induction hypothesis, we have wf(A12A22) from which we have A12A22 and therefore A11A12A21A22.

    • Sub-case A11A21:
      It immediately follows that A11A12A21A22.

    It remains to show F[[A1A2]]e(Γ1Γ2). This follows from two uses of the Weakening Lemma to obtain F[[A1]]e(Γ1Γ2) and F[[A2]]e(Γ1Γ2).

  • Case e=(e1e2):
    We have B1E[[e1]]Γ1C1E[[e2]]Γ1B1<:C1A1wf(A1) and B2E[[e1]]Γ2C2E[[e2]]Γ2B2<:C2A2wf(A2) By the induction hypothesis, we have B1B2E[[e1]](Γ1Γ2)wf(B1B2) and C1C2E[[e2]](Γ1Γ2)wf(C1C2) We obtain A1A2 by the Application Consistency Lemma, and then by the Application Intersection Lemma we have B1B2<:(C1C2)(A1A2) So we have A1A2E[[e]](Γ1Γ2).

    Also, from A1A2, wf(A1), and wf(A2), we conclude that wf(A1A2).

  • Case e=f(e1,e2):
    (This case is not very interesting. See the Isabelle proof for the details.)

  • Case e=ife1thene2elsee3:
    (This case is not very interesting. See the Isabelle proof for the details.)

I thought that the following Subsumption Theorem would be needed to prove the Meet Theorem, but it turned out not to be necessary, which is especially nice because the proof of the Subsumption Theorem turned out to depend on the Meet Theorem!
Theorem (Subsumption)
If AE[[e]]Γ, A<:B, and both B and Γ are well-formed, then BE[[e]]Γ.
The proof is by induction on e and all but the case e=λe are straightforward. For that case, we use the following lemmas.
Lemma (Distributivity for F)
If F[[(AB)(CD)]]eΓ, AC, and everything is well formed, then F[[(AC)(BD)]]eΓ.
(The proof is direct, using the Meet Theorem and the Weakening Lemma.)
Lemma (F and Intersections)
Suppose Γ1 is a non-empty sequence of well-formed and consistent function types. If F[[Γ1]]eΓ2, then F[[dom(Γ1)cod(Γ1)]]eΓ2.
(The proof is by induction on Γ1 and uses the previous lemma.)

Conclusion

This result can be viewed a couple ways. As discussed at the beginning of this post, establishing the Meet Theorem means that the this call-by-value denotational semantics respects β-equality for any terminating argument expression. This is useful in proving the correctness of a function inlining optimizer. Also, it would be straightforward to define a call-by-name (or need) version of the semantics that respects β-equality unconditionally.

Secondly, from the viewpoint of intersection type systems, this result shows that, once we require types to be well formed (i.e. self consistent), we no longer need the intersection introduction rule because it is a consequence of having the subtyping rule for distributing intersections through function types.

Tuesday, April 24, 2018

What do real numbers have in common with lambdas? and what does continuity have to do with it?

Continuous functions over the real numbers

As a high school student and undergraduate I learned in Calculus that
  1. real numbers involve infinity in precision, e.g. some have no finite decimal representation, and
  2. a continuous function forms an unbroken line, a necessary condition to be differentiable.
For an example, the decimal representation of 2 goes on forever: 1.41421 Later on, in a course on Real Analysis, I learned that one way to define the real numbers is to declare them to be Cauchy sequences, that is, infinite sequences of rational numbers that get closer and closer together. So, for example, 2 is declared to be the sequence 1,32,1712,577408, described by the following recursive formulas.
A0=1An+1=An2+1An(1)
Depending on how close an approximation to 2 you need, you can go further out in this sequence. (Alternatively, one can represent 2 by its sequence of continued fractions.)
For an example of a continuous function, Figure 1 depicts x3x24x. On the other hand, Figures 2 and 3 depict functions that are not continuous. The function 1/abs(x2)1/4 in Figure 2 is not continuous because it goes to infinity as it approaches 2. The function (x+1)sign(x) in Figure 3 is not continuous because it jumps from 1 to 1 at 0.

Figure 1. The function x3x24x is continuous.
Figure 2. The function 1/abs(x2)1/4 is not continuous at 2.
Figure 3. The function (x+1)sign(x) is not continuous at 0.

You may recall the ϵ-δ definition of continuity, stated below and depicted in Figure 4.
A function f is continuous at a point x if for any ϵ>0 there exists a δ>0 such that for any x in the interval (xδ,x+δ), f(x) is in (f(x)ϵ,f(x)+ϵ).
In other words, when a function is continuous, if you want to determine its result with an accuracy of ϵ, you need to measure the input with an accuracy of δ.

Figure 4. The ϵ-δ definition of continuity.
One connection between the infinite nature of real numbers and continuity that only recently sunk-in is that continuous functions are the ones that can be reasonably approximated by applying them to approximate, finitely-represented inputs. For example, suppose you wish to compute f(2) for some continuous function f. You can accomplish this by applying f to each rational number in the Cauchy sequence for 2 until two subsequent results are closer than your desired accuracy. On the other hand, consider trying to approximate the function from Figure 2 by applying it to rational numbers in the Cauchy sequence for 2. No matter how far down the sequence you go, you’ll still get a result that is wrong by an infinite margin!

The λ-calculus and continuous functions

In graduate school I studied programming languages and learned that
  1. the λ-calculus is a little language for creating and applying functions, and
  2. Dana S. Scott’s semantics of the λ-calculus interprets λ’s as continuous functions.
For example, the λ expression λx.x+1 creates an anonymous function that maps its input x, say a natural number, to the next greatest one. The graph of this function is {01,12,23,} which is infinite. So we have our first similarity between the real numbers and λ’s, both involve infinity.
A key characteristic of the λ-calculus is that functions can take functions as input. Thus, the semantics of the λ-calculus is also concerned with functions over infinite entities (just like functions over the real numbers). For example, here is a λ expression that takes a function f and produces a function that applies f twice in succession to its input x. λf.λx.f(f(x)) The graph of this function is especially difficult to write down. Not only does it have an infinite domain and range, but each element in the domain and range is an infinite entity. {{01,12,23,}{02,13,24,},{00,12,24,}{00,14,28,},}
Denotational semantics for the λ-calculus interpret λ’s as continuous functions, so just based on the terminology there should be another similarity with real numbers! However, these continuous functions are over special sets called domains, not real numbers, and the definition of continuity in this setting bears little resemblance to the ϵ-δ definition. For example, in Dana S. Scott’s classic paper Data Types as Lattices, the domain is the powerset of the natural numbers, P(N). This domain can be used to represent a function's graph by encoding (create a bijection) between pairs and natural numbers, and between sets and naturals. The following are the easier-to-specify directions of the two bijections, the mapping from pairs to naturals and the mapping from naturals to sets of naturals.
n,m=2n(2m+1)1set(0)=set(1+k)={m}set(n)if n,m=k
Scott defines the continuous functions on P(N) as those functions h that satisfy
h(f)={h(g)gfinf}(2)
In other words, the value of a continuous function h on some function fP(N) must be the same as the union of applying h to all the finite subgraphs of f. One immediately wonders, why are the λ-definable functions continuous in this sense? Consider some λ expression h that takes as input a function f.
But f is a function; an infinite object. What does it mean to “compute” with an “infinite” argument? In this case it means most simply that h(f) is determined by asking of f finitely many questions: f(m0),f(m1),...,f(mk1). —Dana S. Scott, A type-theoretical alternative to ISWIM, CUCH, OWHY, 1969.
Put another way, if h terminates and returns a result, then it will only have had a chance to call f finitely many times. So it suffices to apply h instead to a finite subset of the graph of f. However, we do not know up-front which subset of f to use, but it certainly suffices to try all of them!

Relating the two kinds of continuity

But what does equation (2) have to do with continuous functions over the real numbers? What does it have to do with the ϵ-δ definition? This question has been in the back of my mind for some time, but only recently have I had the opportunity to learn the answer.
To understand how these two kinds of continuity are related, it helps to focus on the way that infinite entities can be approximated with finite ones in the two settings. We can approximate a real number with a rational interval. For example, refering back to the Cauchy sequence for 2, equation (1), we have 2(1712,32) Of course an approximation does not uniquely identify the thing it approximates. So there are other real numbers in this interval, such as 2.1. 2.1(1712,32)
Likewise we can approximate the infinite graph of a function with a finite part of its graph. For example, let G be the a graph with just one input-output entry. G={12} Then we consider G to be an approximation of any function that agrees with G (maps 1 to 2), which is to say its graph is a superset of G. So the set of all functions that are approximated by G can be expressed with a set comprehension as follows: {fGf}. In particular, the function +1 that adds one to its input is approximated by G. {01,12,23,}{fGf} But also the function ×2 that doubles its input is approximated by G. {00,12,24,}{fGf} Of course, a better approximation such as G={12,23} is able to tell these two functions apart.
The interval (17/12,3/2) and the set {fGf} are both examples of neighborhoods (aka. base elements) in a topological space. The field of Topology was created to study the essence of continuous functions, capturing the similarities and abstracting away the differences regarding how such functions work in different settings. A topological space is just some set X together with a collection B of neighborhoods, called a base, that must satisfy a few conditions that we won’t get into. We’ve already seen two topological spaces.
  1. The real numbers form a topological space where each neighborhood consists of all the real numbers in a rational interval.
  2. The powerset P(N) forms a topological space where each neighborhood consists of all the functions approximated by a finite graph.
The ϵ-δ definition of continuity generalizes to topological spaces: instead of talking about intervals, it talks generically about neighborhoods. In the following, the interval (f(x)ϵ,f(x)+ϵ) is replaced by neighborhood E and the interval (xδ,x+δ) is replaced by neighborhood D.
A function f is continuous at a point x if for any neighborhood E that contains f(x), there exists a neighborhood D that contains x such that for any y in D, f(y) is in E.
Now let us instantiate this topological definition of continuity into P(N).
A function f over P(N) is continuous at X if for any finite set E such that Ef(X), there exists a finite set D with DX such that for any Y, DY implies Ef(Y).
Hmm, this still doesn’t match up with the definition of continuity in equation (2) but perhaps they are equivalent. Let us take the above as the definition and try to prove equation (2).
First we show that h(f){h(g)gfinf} Let x be an arbitrary element of h(f). To show that x is in the right-hand side we need to identify some finite g such that gf and xh(g), that is, {x}h(g). But this is just what continuity gives us, taking h as f, f as X, {x} as E, g as D, and also g as Y. Second we need show that {h(g)gfinf}h(f) This time let x be an element of {h(g)gfinf}. So we known there is some finite set g such that xh(g) and gf. Of course {x} is a finite set and {x}h(g), so we can apply the definition of continuity to obtain a finite set E such that Eg and for all Y, EY implies {x}h(Y). From Eg and gf we transitively have Ef. So instantiating Y with f we have {x}h(f) and therefore xh(f).
We have shown that the topologically-derived definition of continuity for P(N) implies the definition used in the semantics of the λ-calculus, i.e., equation (2). It is also straightforward to prove the other direction, taking equation (2) as given and proving that the topologically-derived definition holds. Thus, continuity for functions over real numbers really is similar to continuity for λ functions, they are both instances of continuous functions in a topological space.

Continuous functions over partial orders

In the context of Denotational Semantics, domains are often viewed as partial orders where the ordering gf means that g approximates f, or f is more informative than g. The domain P(N) with set containment forms a partial order. Refering back to the examples in the first section, with G={12} and G={12,23}, we have GG, G+1, and G×2. In a partial order, the join xy of x and y is the least element that is greater than both x and y. For the partial order on P(N), join corresponds to set union.
In the context of partial orders, continuity is defined with respect to infinite sequences of ever-better approximations: f0f1f2 A function h is continuous if applying it to the join of the sequence is the same as applying it to each element of the sequence and then taking the join.
h(nNfn)=nNh(fn)(3)
But this equation is not so different from the equation (2) that expresses continuity on P(N). For any function f (with infinite domain) we can find an sequence (fn)n=0 of ever-better but still finite approximations of f such that f=nNfn Then both equation (2) and (3) tell us that h(f) is equal to the union of applying h to each fn.

Further Reading

The following is the list of resources that I found helpful in trying to understand the relationship between real numbers, λ’s, and the role of continuity.
  • Data Types as Lattices by Dana S. Scott.
  • A type-theoretical alternative to ISWIM, CUCH, OWHY by Dana S. Scott.
  • The Formal Semantics of Programming Languages by Glynn Winskel.
  • Topology via Logic by Steven Vickers.
  • Topology (2nd Edition) by James R. Munkres.
  • Introduction to Lattices and Order by B.A. Davey and H.A. Priestley.
  • The Wikipedia articles on