# Calculating Programs from Galois Connections

Galois connections are ubiquitous in mathematics and computing science. One is often amazed that, once two functions are identified as a Galois connection, a long list of nice and often useful properties follow from one concise, elegant defining equation. But how does one construct a program from a specification given as a Galois connection? This is the topic of a recent work of José Nuno Oliveira and I, and this article is an advertisement.

### Galois Connections as Specifications

In program construction one often encounters program specification of the form “… the smallest such number”, “the longest prefix of the input list satisfying …”, etc. A typical example is whole number division: given a natural number `x` and a positive integer `y`, `x / y` is the largest natural number that, when multiplied by `y`, is at most `x`. For another example, the Haskell function `takeWhile p` returns the longest prefix of the input list such that all elements satisfy predicate `p`.

Such specifications can be seen as consisting of two parts. The easy part specifies a collection of solution candidates: numbers that are at most `x` after multiplication with `y`, or all prefixes of the input list. The hard part, on the other hand, picks one optimal solution, such as the largest, the longest, etc., among the collection.

Our goal is to calculate programs for such specifications. But how best should the specification be given in the first place? Take division for example, one might start from a specification that literally translates our description above into mathematics:
``` ```

``    x / y = ⋁{ z | z * y ≤ x } ``
``` ```

As we know, however, suprema is in general not easy to handle. One could also explicitly name the remainder:
``` ```

``    z = x / y  ≡  (∃ r : 0 ≤ r < y : x = z * y + r)``
``` ```

at the cost of existentially quantifying over the remainder.

A third option looks surprising simpler: given `x` and `y`, the value `x / y` is such that for all `z`,
``` ```

``    z * y ≤ x  ≡  z ≤ x / y(1)``
``` ```

Why is this sufficient as a definition of `x / y`? Firstly, by substituting `x / y` for `z`, the right hand side of `≡` reduces to true, and we obtain on the left hand side `(x / y) * y ≤ x`. This tell that `x / y` is a candidate --- it satisfies the easy part of the specification. Secondly, read the definition from left to right: `z * y ≤ x ⇒ z ≤ x / y`. It says that `x / y` is the largest among all the numbers satisfying the easy part.

Equations of the form are called Galois connections. Given preorders `⊑` and `≤`, Functions `f` and `g` form a Galois connection if for all `x` and `z` we have
``` ```

``    f z ⊑ x  ≡  z ≤ g x(2)``
``` ```

The function `f` is called the lower adjoint and `g` the upper adjoint.

The definition of division above is a Galois connection where `f = (* y)` and `g = (/ y)`. For another example, `takeWhile p` can be specified as an upper adjoint:
``` ```

``    map p? zs ⊑ xs  ≡  zs ⊑ takeWhile p xs(3)``
``` ```

where `⊑` is the prefix ordering: `ys ⊑ xs` if `ys` is a prefix of `xs`, and `map p?` is a partial function: `map p? xs = xs` if `p x` holds for each `x` in `xs`.

We love Galois connections because once two functions are identified as such, a long list of useful properties follows: `f (g x) ⊑ x`, `z ≤ g (f z)`, `f` and `g` are monotonic, and are inverses of each other in the other's range... etc.

These are all very nice. But can one calculate a program from a Galois connection? Given `⊑`, `≤`, and `f`, how does one construct `g`?

### The "Shrink" Operator

José discovered and proposed a relational operator to handle such calculations. To use the operator, we have to turn the Galois connection `(1)` into point-free style. We look at the left hand side of `(1)`: `f z ⊑ x`, and try to write it as a relation between `z` and `x`. Let `f°` denote the relational converse of `f` -- roughly, think of it as the inverse function of `f`, that it, it maps `f z` to `z`, and let `∘` denote relational composition -- function composition extended to relations. Thus `f z ⊑ x` translates to
``` ```

``    f° ∘ (⊑)``
``` ```

It is a relation between `z` and `x`: putting `x` on the left hand side of `f° ∘ (⊑)`, it relates, through `⊑`, to `f z`, which is then mapped to `z` through `f°`.

Then we wish that `f° ∘ (⊑)` can be transformed into a (relational) fold or unfold, which is often the case because the defining components: `⊑`, `≤`, and `f`, are often folds or unfolds. Consider the lower adjoint of `takeWhile p` in `(3)`. Since `⊑`, the relation that takes a list and returns a prefix of the list, can be defined as a fold on lists, `(map p?)° ∘ (⊑)`, by fold fusion, is also a fold. Consider `(1)`, since `≤` and `(* y)` are both folds on natural numbers, `(* y)° ∘ (≤)` can be both a fold and an unfold.

In our paper we showed that a Galois connection `(2)` can be transformed into
``` ```

``    g = (f° ∘ (⊑)) ↾ (≥)``
``` ```

where `↾` is the new operator José introduced. The relation `S ↾ R`, pronounced "`S` shrunk by `R`", is a sub-relation of `S` that yields, for each input, an optimal result under relation `R`. Note that the equation made the easy/hard division explicit: `f° ∘ (⊑)` is the easy part: we want a solution `z` that satisfies `f z ⊑ x`, while `≥` is the criteria we use, in the hard part, to choose an optimal solution.

The `↾` operator is similar to the `min` operator of Bird and de Moor, without having to use sets (which needs a power allegory). It satisfies a number of useful properties. In particular, we have theorems stating when `(↾ R)` promotes into folds and unfolds. For example,
``` ```

``    (fold S) ↾ R ⊇ fold (S ↾ R)``
``` ```

if `R` is transitive and `S` is monotonic on `R`.

With the theorems we can calculate `g`. Given `g`, specified as an upper adjoint in a Galois connection with lower adjoint `f`, we first try to turn `f° ∘ (⊑)` into a fold or an unfold, and then apply the theorems to promote `(↾ (≥))`. For more details, take a look at our paper!

# Programming from Galois connections — principles and applications

Shin-Cheng Mu and José Nuno Oliveira. Technical Report TR-IIS-10-009, Academia Sinica, December 2010.
[PDF]

This report is an extended version of our conference submission Programming from Galois connections.

Problem statements often resort to superlatives such as in eg. “… the smallest such number”, “… the best approximation”, “… the longest such list” which lead to specifications made of two parts: one defining a broad class of solutions (the easy part) and the other requesting the optimal such solution (the hard part).

This report introduces a binary relational combinator which mirrors this linguistic structure and exploits its potential for calculating programs by optimization. This applies in particular to specifications written in the form of Galois connections, in which one of the adjoints delivers the optimal solution at target.

The framework encompasses re-factoring of results previously developed by Bird and de Moor for greedy and dynamic programming, in a way which makes them less technically involved and therefore easier to understand and play with.

# Programming from Galois connections

Shin-Cheng Mu and José Nuno Oliveira. In the 12th International Conference on Relational and Algebraic Methods in Computer Science (RAMiCS #12), LNCS 6663, pages 294-313. May 30 – June 3, 2011.
[PDF]

Problem statements often resort to superlatives such as in eg. “… the smallest such number”, “… the best approximation”, “… the longest such list” which lead to specifications made of two parts: one defining a broad class of solutions (the easy part) and the other requesting the optimal such solution (the hard part).

This paper introduces a binary relational combinator which mirrors this linguistic structure and exploits its potential for calculating programs by optimization. This applies in particular to specifications written in the form of Galois connections, in which one of the adjoints delivers the optimal solution at target.

The framework encompasses re-factoring of results previously developed by Bird and de Moor for greedy and dynamic programming, in a way which makes them less technically involved and therefore easier to understand and play with.

An accompanying technical report is available online.

# An Exercise Utilising Galois Connections

Given two partial orders `(A, ⊑)`, `(B, ≼)`, two functions `f : A → B`, `g : B → A` form a Galois connection between them if for all `a : A`, `b : B` we have

``  f a ≼ b ≣ a ⊑ g b``

We will refer to this defining property as “GC” later. The function `f` is called the lower adjoint and `g` the upper adjoint of the Galois connection. Galois connections are interesting because once two functions are identified as such, they immediately satisfy a rich collection of useful properties:

• letting `a := g b` in GC, we get `f (g b) ≼ b`;
• letting `b := f a`, we get `a ⊑ g (f a)`;
• `f` is monotonic, since:
`````` f a₁ ≼ f a₂
≣   { GC }
a₁ ⊑ g (f a₂)
⇐  {  since a ⊑ g (f a) }
a₁ ⊑ a₂``````
• similarly, `g` is monotonic: `b₁ ≼ b₂ ⇒ f b₁ ⊑ f b₂`,

and many more.

In the recent work of Sharon and me on maximally dense segments we needed quite a number of functions to be monotonic, idempotent, etc. It only occurred to me after submitting the paper: could they be defined as Galois connections? The number of properties we needed in the paper is huge and it would be nice to establish them on fewer basic properties. And it looks prettier.

### Longest Prefix Up to a Certain Sum

One such function is `trim` in the paper, but it is sufficient to consider a simplification: let `sam : [Int] → [Int]` (for “sum atmost”) return the longest prefix of the input list whose sum is no larger than a constant `U`. Denote “`x` is a prefix of `y`” by `x ⊑ y`. We want to show that `sam` satisfies

• monotonicity: `x ⊑ y ⇒ sam x ⊑ sam y`, and
• idempotence: `sam (sam x) = sam x`.

Can they be derived by defining `sam` as a Galois connection?

I learned from José N. Oliveira‘s talk A Look at Program “G”alculation in IFIP WG 2.1 #65 Meeting how (the uncurried version of) `take` can be defined as a Galois connection. It turns out that `sam` is just the same. We consider a slight generalisation `sam' : (Int, [Int]) → [Int]` that takes an upper bound as a parameter. It can be characterised by:

``sum y ≤ b  ∧  y ⊑ x   ≣   y ⊑ sam' (b, x)``

There is in fact a Galois connection hidden already! To see that, define `⟨f, g⟩ a = (f a, g a)` (in the Haskell Hierarchy Library it is defined in Control.Arrow as `&&&`), and denote the product of binary relations by `×`, that is, if `a ≤ b` and `x ⊑ y` then `(a,x)` is related to `(b,y)` by `≤×⊑`. We write a composed relation as an infix operator by surrounding it in square brackets `(a,x) [≤×⊑] (b,y)`.

Using these notations, the defining equation of `sam'` can be rewritten as:

``⟨sum, id⟩ y [≤×⊑] (b,x)   ≣   y ⊑ sam' (b,x)``

Thus `sam'` is the upper adjoint in a Galois connection between `((Int, [Int]), ≤×⊑)` and `([Int], ⊑)`!

Now that `⟨sum, id⟩` and `sam'` form a Galois connection, we have:

• `f (g b) ≼ b` instantiates to `⟨sum, id⟩ (sam' (b,x)) [≤×⊑] (b,x)`, that is, `sum (sam' (b,x)) ≤ b` and `sam' (b,x) ⊑ x`;
• `a ⊑ g (f a)` instantiates to `x ⊑ sam' (sum x, x)`. Together with the previous property we have `x = sam' (sum x, x)`;
• monotonicity of the lower adjoint instantiates to `y₁ ⊑ y₂ ⇒ sum y₁ ≤ sum y₂ ∧ y₁ ⊑ y₂`;
• monotonicity of the upper adjoint instantiates to
``(b₁,x₁) [≤×⊑] (b₂,x₂)   ⇒   sam' (b₁,x₁) ⊑ sam' (b₂,x₂) ``

that is

``b₁ ≤ b₂   ∧  x₁ ⊑ x₂   ⇒   sam' (b₁,x₁) ⊑ sam' (b₂,x₂) ``

a generalisation of the monotonicity we want.

Finally, to show idempotence, we reason

``````   sam' (b₁, x) ⊑ sam' (b₁, sam' (b₂, x))
≣   { GC }
⟨sum, id⟩ (sam' (b₁, x)) [≤×⊑]  (b₁, sam' (b₂, x))
≣   { definitions }
sum (sam' (b₁, x)) ≤ b₁   ∧   sam' (b₁, x) ⊑ sam' (b₂, x)
⇐  { properties above }
b₁ ≤ b₂``````

These are all nice and pretty. There is another function, however, that is much harder to deal with, which I will write about next time.