This is a technical appendix on soundly running Lean tactics within a `Box`

. It also provides some of the more technical background omitted from Section 2.4, such as the inference rules for the development calculus of Lean.
By the end of this appendix, I will have described an 'escape-hatch' to use Lean tactics within a `Box`

proof, meaning that we don't have to forgo using any prexisting tactic libraries by using the `Box`

framework.
The proofs here are perhaps better considered as sketches that could one day be formalised.
Ultimately, these meta-level proofs are not critical to the thesis because the resulting object-level proofs produced through these moves will also be checked by the Lean kernel.

# A.1. Typing of expressions containing metavariables

In this section I will provide a formal set of judgements describing how the development calculus of Lean works.
I will do this by extending the presentation given by Carneiro [Car19[Car19]Carneiro, Mario*Lean's Type Theory* (2019)] to also include typing over expressions containing metavariables.
These definitions are used in the proofs soundness properties for `Box`

in Chapter 3 and in later sections of this appendix.
This section is intended to be 'complete' in the sense that it should be readable as a standalone document to understand how metavariables work in a theorem prover.
I used the knowledge written in this section to implement an interfacehttps://github.com/leanprover-community/lean/pull/69 in the Lean 3 metaprogramming framework [EUR+17[EUR+17]Ebner, Gabriel; Ullrich, Sebastian; *et al.**A metaprogramming framework for formal verification* (2017)Proceedings of the ACM on Programming Languages] for fine-grained control over the metavariable context.

The work in this section is not an original contribution, because de Moura *et al* (the designers of Lean 3) had to produce this theory to create the software in the first place.
There are also many accounts of the theories of development calculi similar to this [SH17[SH17]Sterling, Jonathan; Harper, Robert*Algebraic Foundations of Proof Refinement* (2017)arXiv preprint arXiv:1703.05215, McB00[McB00]McBride, Conor*Dependently typed functional programs and their proofs* (2000), Spi11[Spi11]Spiwack, Arnaud*Verified computing in homological algebra, a journey exploring the power and limits of dependent type theory* (2011)].
The information in this section is gleaned from [EUR+17] and [MAKR15[MAKR15]de Moura, Leonardo; Avigad, Jeremy; *et al.**Elaboration in Dependent Type Theory* (2015)CoRR], the sourcecode of the Lean 3 theorem proverhttps://github.com/leanprover/lean and through many informal chats with the community on the leanprover Zulip serverhttps://leanprover.zulipchat.com.

(A.1) recaps some definitions that were given in Section 2.4.

Let `π : MvarContext`

and `Ξ : Context`

.
`Binder`

s are sugared as `(π₯ βΆ Ξ±)`

Note the use of a smaller colon `βΆ`

for typing judgements vs `:`

for meta-level type assignments. for `β¨π₯, Ξ±β© : Binder`

,
`MvarDecls`

as `(Ξ β’ ?π₯ βΆ Ξ±)`

for unassigned `β¨π₯, Ξ±, Ξ, noneβ© : MvarDecl`

and `(Ξ β’ ?π₯ βΆ Ξ± β π‘)`

for assigned `β¨π₯, Ξ±, Ξ, some π‘β© : MvarDecl`

(`Ξ β’`

may be omitted if not relevant).
I will use the convention that metavariable names always begin with question marks.
Given `π₯ : Name`

, write `π₯ β Ξ`

when `π₯`

appears in `Ξ`

.

To simplify analysis, I will assume that all contexts do not include variable clashes.
That is to say, there are no two binders with the same name in `Ξ`

or `π`

.
Since there are infinitely many variable names to choose from, these kinds of variable clashes can be avoided through renaming.

Each of the definitions in (A.1) are assignable (see Section 2.4.2) as defined in (2.25).
Also define the following for `π : R`

where `R`

is assignable.

`fv(π)`

are the free variables in`π`

.`mv(π)`

are the metavariables in`π`

.`umv(π, π)`

are the unassigned metavariables in`π`

(according to context`π`

).`amv(π, π)`

are the assigned metavariables in`π`

.

Recall (see Section 2.4.1) that a substitution `Ο`

is a dictionary `Sub := Name β Expr`

sending variable names to expressionsA good general account of working with substitutions can be found in [BN98].[BN98]Baader, Franz; Nipkow, Tobias*Term rewriting and all that* (1998).
Given `Ο : Sub`

and an `π : R`

where `R`

is assignable, `Ο π`

replaces each variable in `π`

with the corresponding expression in `Ο`

With the usual caveats for variable clashes as noted in Section 2.4.1.
The notation for substitutions that I use is `β¦π₯ β¦ π‘β¦`

where `π₯ : Name`

is the variable to be substituted and `π‘ : Expr`

is the replacement expressionThe reader may enjoy this list of substitution notations collected by Steele [Ste17]. (link to slides)[Ste17]Steele Jr., Guy L.*It's Time for a New Old Language* (2017).
I will extend this definition to include metavariable substitutions.

A metavariable context `π`

can be viewed as a substitution mapping `?π`

to `π‘`

for each assigned declaration `β¨?π, Ξ±, Ξ, some π‘β© β π`

.
That is, `π`

acts by replacing each instance of an assigned metavariable with its assignment.
Given an assignable `π`

, write `π π`

to be the ** π-instantiation** of

`π`

, created by performing this substitution to `π`

.
`π`

is **when all of the metavariables present in**

`π`

-instantiated`π`

are unassigned with respect to `π`

.This substitution operation on `π`

helps motivate the constraints that make a metavariable context *well-formed*.
In particular, we should expect:

There should be no 'substitution loops':

`π π‘`

should not contain any`π`

-assigned metavariables. For example, the metavariable declaration`β¨?π, Ξ±, Ξ, some ?πβ©`

assigning`?π`

to itself would cause a loop. More perniciously, the assignment could cause an infinitely growing term as in`β¦?π β¦ ?π + ?πβ¦`

. The no-loop property depends on the entire`π`

, as we may have a multi-declaration dependency cycle such as`β¦?π β¦ f(?π), ?π β¦ g(?π)β¦`

.Performing

`π`

on an expression`π‘ : Expr`

(or other assignable object) should preserve the well-formedness of`π‘`

in a context`Ξ`

. To illustrate with some examples:Performing

`β¦?π β¦ "hello"β¦`

to`?π + 4`

would produce the nonsense expression`"hello" + 4`

, so assignments must have the same type as their metavariables.Performing

`β¦?π β¦ π₯ + 2β¦`

to`?π + (Ξ» π₯, π₯ - ?π) 5`

will produce`(π₯ + 2) + (Ξ» π₯, π₯ - (π₯ + 2)) 5`

. But this is badly formed because the variable`π₯`

escapes the scope of its lambda binder. Hence there needs to be a way of making sure that a metavariable assignment can't depend on variables that would cause these malformed expressions. This is why the`MvarDecl`

definition includes a context`Ξ`

for each declaration.

These constraints will be made formal in Section A.1.3.

## A.1.1. Assignment of a metavariable

Given a metavariable context `π`

with an unassigned metavariable `?π`

and a candidate expression `π‘ : Expr`

, we need a way of updating `π`

so that `?π`

is assigned to `π‘`

.
Call this function `assign : MvarContext β Name β Expr β Option MvarContext`

.

The procedure of `assign π ?π π‘`

is as followsThe implementation in core Lean can be found here.:

Find the corresponding declaration

`β¨?π, Ξ±, Ξ, noneβ© β π`

. If it doesn't exist in`π`

or it is already assigned, fail by returning`none`

.Assert that there are no metavariable loops; that is,

`?π β mv(π π‘)`

. (`π π‘`

is the`π`

-instantiation of`π‘`

.)Assert that typings and contexts are correct with

`π;Ξ β’ π‘ βΆ Ξ±`

(to be defined in Section A.1.3).Delete

`?π`

from`π`

.Update

`π`

to be`β¦?π β¦ π π‘β¦ π`

. That is, each occurrence of`?π`

in`π`

is replaced with the`π`

-instantiation of`π‘`

. Now`?π β mv(π)`

.Insert

`β¨?π, Ξ± Ξ, some π‘β©`

into`π`

.

Using this procedure ensures that each time a metavariable `?π`

is assigned, we have `?π β mv(assign π ?π π‘)`

, which means that checking for loops only requires checking `?π β mv(π π‘)`

.

## A.1.2. Dependency ordering

In this section, I will define what it means for a declaration to depend on another one and define an extension of a metavariable context.

Given an assignable `π`

, say that `π`

**depends on** `?π`

when `?π β mv(π)`

.
Given a pair of declarations `πβ = β¨?πβ, Ξ±β, Ξβ, πββ©`

and `πβ = β¨?πβ, Ξ±β, Ξβ, πββ©`

in `π`

, write `πβ βΈ πβ`

when `πβ`

depends on `?πβ`

.
That is `πβ βΈ πβ`

when `Ξ±β`

or `Ξβ`

or `πβ`

depend on `?πβ`

.

Given the list of declarations `π : MvarContext`

as vertices, `βΈ`

forms a directed graph.
Assuming that this graph is acyclic (which will be enforced by the definition of a valid context given later), there exists a topological orderinghttps://en.wikipedia.org/wiki/Topological_sorting of the declarations.
That is, there is an ordering of the declarations in `π`

such that each declaration only depends on earlier declarations.

Note that performing the assignment operation introduced in Section A.1.1 causes the dependency ordering to change: for example supposing `πβ βΈ πβ`

in `π`

and then if `πβ`

is assigned with a term `π‘`

not containing a metavariable, then `πβ`

will no longer depend on `?πβ`

and so `πβ βΈΜ· πβ`

. An assignment may also cause a declaration to depend on metavariables that it did not previously depend on. As such, when an assignment is performed it may be necessary to reorder the declarations in order to recover the topological ordering.

Given `π : MvarContext`

, we will often be adding additional declarations and assignments to `π`

to make a new `π + Ξ : MvarContext`

.
Let's define `Ξ : MvarContextExtension`

as in (A.2).

In order for a `declare ?π Ξ± Ξ`

to be valid for `π`

, require that `π;Ξ β’ Ξ± : Type`

and that `?π β π`

.
Then we have that performing a valid declaration preserves the acylicity of `π`

with respect to `βΈ`

.
An assignment extension also preserves acyclicity; since step 2 of the procedure in Section A.1.1 explicitly forbids dependency loops.

Hence, given a sequence of extensions to `π`

, the `βΈ`

relation is still acyclic and hence there exists a topological ordering of the declarations in `π`

for `βΈ`

.

## A.1.3. Judgements and inference rules for metavariables

Now let's define the following judgements (in the same sense as in Section 2.1.3):

- when
`π;Ξ β’ π βΆ Ξ±`

`π`

has type`Ξ±`

under`π`

and`Ξ`

. - when
`π;Ξ β’ π β‘ π‘`

`π : Expr`

and`π‘ : Expr`

are definitionally equal (see [Car19 Β§2.2]). - when the metavariable context
`ok π`

`π`

is well-formed. - when the given local context
`π β’ ok Ξ`

`Ξ`

is well-formed under`π`

.

The inference rules for these are given in (A.3).

I'll reproduce the list of (non-inductive) typing axioms here for completeness, but please see Carneiro's thesis [Car19] for a more comprehensive version, including a full set of inference rules for let binders, reductions, definitional equality and inductive constructions among others.

[Hur95]Hurkens, Antonius JC*A simplification of Girard's paradox* (1995)International Conference on Typed Lambda Calculi and ApplicationsI will now extend the above analysis to include an account of the metavariable development calculus that Lean uses to represent partially constructed proofs.

## A.1.4. Properties of the Lean development calculus

Finish this section.

In this subsection I note some regularity lemmas for the extended development calculus similarly to Carneiro [Car19 Β§3.2]. The first thing to note is that a judgement `π;Ξ β’ J`

is invariant under a reordering of the declarations of `Ξ`

or `π`

that preserves the dependency ordering.

Using (A.3) and some additional rules for definitional equality (`β‘`

, not printed here) and inductive datatypes, Carneiro proves various properties of the type system, of which the following are relevant for the analysis here:

`(Ξ β’ e : Ξ±) β β’ Ξ ok`

. If the context is not well formed, then we can't make any typing judgements.`Ξ β’ e : Ξ± β fv(e) β Ξ β§ fv(Ξ±) β Ξ`

. If a term is well typed in`Ξ`

then all of the free variables are present.

These can be extended to include metavariables and a metavaraiable context `π`

. do this.

`(π;Ξ β’ π‘ βΆ Ξ±) β (β’ π ok)`

`(π;Ξ β’ π‘ βΆ Ξ±) β (π β’ Ξ ok)`

`(π;Ξ β’ π‘ βΆ Ξ±) β _`

`π;Ξ β’ π‘ : Ξ± β Οπ;Ξ β’ π‘ : Ξ±`

Given `π;Ξ β’ π‘ βΆ Ξ±`

(which I will define in Section A.1.3), we should have that performing the `π`

substitution should not change the validity of the results.
Two things to show: `π;Ξ β’ π‘ βΆ Ξ± β (π + Ξ);Ξ β’ π‘ βΆ Ξ±`

.
And `[..πβ, (?πβπ£), ..πβ];Ξ β’ π‘ βΆ Ξ± β [..πβ, ..β¦?π β¦ π£β¦πβ];β¦?π β¦ π£β¦Ξ β’ β¦?π β¦ π£β¦π‘ βΆ β¦?π β¦ π£β¦Ξ±`

.
Do this in two steps, show that judgements still apply if you instantiate, and then show that you can remove an unmentioned assignment.
And that reordering the `π`

's to another `βΈ`

-totalizer is fine.
That is, `π;Ξ β’ π π‘ βΆ Ξ±`

and `π;Ξ β’ π‘ βΆ π Ξ±`

and `π;(π Ξ) β’ π‘ : Ξ±`

.

## A.1.5. Metavariable dependencies

Say that `?n`

depends on `?m`

in `π`

when any `π`

-instantiated expression in `?n`

's declaration depends on `?m`

.
Is it possible to create an `π`

such that there is a dependency cycle among the metavariables?
For example, can we declare a recursive pair of metavariables `?n : {i : β | i β€ ?m}`

and `?m : {i : β | i β€ ?n}`

?
In the above theory, it is not possible to declare a pair of metavariables with this property because a metavariable context is only `ok`

when there is an explicit ordering on the metavariables such that each does not depend on the last.
This is a consequence of the `π-wideningβ`

and `π-wideningβ`

axioms.
In Lean's actual implementation, it is *possible* to do this through the `tactic.unsafe.type_context`

monad using an unsafe assignment, in this case an infinite-descending expression will attempt to form which will not typecheck.

# A.2. Zippers on `Box`

es

In order to run Lean tactics at various points in the `Box`

structure defined in Chapter 3, we need to navigate to a certain point in a `Box`

, and build up a metavariable context `π`

containing all of the metavariables from the `π―`

-binders and a local context `Ξ`

comprising the variables given in the `β`

-binders above the given point in the `Box`

.
The way to implement this plan is to define a context-aware **zipper** [Hue97[Hue97]Huet, GΓ©rard*Functional Pearl: The Zipper* (1997)Journal of functional programming] on a `Box`

.

Let's first create a coordinate system for `Box`

. Coordinates for functors and inductive datatypes were introduced in Section 2.3.2.

Where here `πβ`

is the coordinate for the `bβ`

argument of the `π`

constructor.
That is, `get πβ (π πβ π₯ πβ) = some πβ`

. Similarly for the other coordinates.
A list of `Coord`

instances can be interpreted as an **address** to a certain point in an expression (see Section 2.3.3).

Next, define a datastructure called a **path** (using the same constructor names as `Box`

es):

We can visualise the zipper as in Figure A.9: a `Box`

is annotated with an additional blob of paint β¬€ at some point in the tree.
The ancestors of β¬€ are in the zipper's `path`

, and everything below is the `cursor`

. Applying `up`

to this zipper will move the β¬€ up to the next node in the `Box`

tree (or return `none`

otherwise). Similarly, `down π π§`

(A.6) will inspect the cursor of `π§`

and move the β¬€ down on its `π`

th recursive argument.
The use of a zipper datastructure is used over an address for β¬€ because the zipper system allows us to modify the cursor in place and then 'unzip' the zipper to perform an operation on a sub-box.

Motivated by Figure A.9, it may be readily verified that `down π (up π§) = some π§`

provided `π§`

's path is not empty and if `π`

is the coordinate corresponding to `π§`

's rightmost path entry.
With similar conditions: `up (down π π§) = some π§`

.

## A.2.1. Contexts of zippers

Define the context of a zipper `π§.ctx`

as:

That is, `π§.ctx`

returns the list of the variables that are bound in the path. For the example `π§`

in Figure A.9, `π§.ctx = [hβ]`

.

Similarly, define the metavariable context `π§.mctx`

of a zipper as

So `π§.mctx`

is a metavariable context containing all of the targets defined above the cursor of `π§`

.

Now, given a zipper `π§`

, write `π§ β’ π‘ βΆ Ξ±`

to mean `π§.mctx;π§.ctx β’ π‘ βΆ Ξ±`

. Similarly for `π : Path`

, `π β’ π‘ βΆ Ξ±`

.

## A.2.2. Zipping and unzipping is sound.

The first thing to show is that given a sound move `π : MvarContext β Context β Box β Option Box`

parametrised by the contexts `π`

and `Ξ`

, then given `π : Box`

and a valid address `π : List Coord`

, we get another sound move `π@π`

defined in (A.12).

Suppose that `β’ πβ βΆ Ξ±`

, then by induction on the typing laws for `Box`

given in Section 3.4.2, we can show `π β’ πβ βΆ ΞΆ`

for some `ΞΆ : Expr`

.
Since `π`

is sound and assuming `π(πβ)`

doesn't fail, we also have `π β’ πβ βΆ ΞΆ`

.
Then finally the typing laws Section 3.4.2 can be applied in reverse to recover `β’ (unzip β¨π, π§β©) βΆ Ξ±`

.

Here we are working towards being able to soundly run a tactic in the context provided by a `Box`

zipper `π§`

by finding ways to manipulate zippers that preserve the inference rules given in (A.3) and (A.4).

## A.2.3. Sound `Path`

updates

We will also need to perform some modifications to the paths of `Zipper`

s.
A path modification `Ο : π β¦ π'`

is *sound* on a zipper `π§ = β¨π, πβ©`

if `π;Ξ β’ unzip β¨π, πβ© βΆ Ξ²`

and `π;Ξ β’ unzip β¨π', πβ© βΆ Ξ²`

.
Hence to show soundness, one simply needs to show that corresponding move `unzip β Ο`

is sound.

We have the following sound path-based moves:

# A.3. Running tactics in `Box`

es

Now that we have the inference rules for metavariables and a definition of a zipper over a `Box`

, we can define how to run a tactic within a `Box`

.

## A.3.1. Hoisting `πͺ`

boxes

Before defining how to make a tactic act on a `Box`

zipper we need to define an additional operation, called `πͺ`

-hoisting.
This is where an `πͺ`

-box is lifted above its parent box.

Proposition: `πͺ`

-hoisting is a sound move. Proof: by compatibility (3.15) it suffices to show that the results are equal, but then this is a corollary of the WLOG proof from the compatibility lemma (3.16).

The above definition (A.16) extends to `πͺ`

-hoisting path entries.

The motivation behind the hoisting is that `πͺ`

boxes are a form of backtracking state in a similar spirit to a logic monad [KSFS05[KSFS05]Kiselyov, Oleg; Shan, Chung-chieh; *et al.**Backtracking, interleaving, and terminating monad transformers: (functional pearl)* (2005)ACM SIGPLAN Notices]. However by including the branching `πͺ`

box on the tree, it is possible to structurally share any context that is shared among the backtracking branches. Hoisting an `πͺ`

box has the effect of causing the backtracking branches to structurally share less of the context with each other. In the most extreme case, we can repeatedly apply `πͺ`

-hoisting to move all of the branches to the top of the box structure, at which point we arrive at something isomorphic to what would arise as a result of using the logic monad form of backtracking.

## A.3.2. Updating the path to reflect a change in context

Given `π§ : Zipper`

, define `π§.goal`

to be first metavariable declared on `π§.path`

.
Hence given a `π§ : Zipper`

with the cursor having shape `π―`

, we can extract a metavariable context `π = mctx π§`

and a special goal metavariable `π§.goal`

, from these, create `π‘π : TacticState`

See Section 2.4.4..
We can now run a tactic `π‘ : Tactic`

on `π‘π `

. If successful, this will return a new tactic state `π‘π '`

.
`π‘π '`

will have an extension of `π`

See (A.2)., call this `π + Ξ`

where `Ξ`

is a list of declarations and assignments successively applied to `π`

. The task here is to create a new `π§': Zipper`

which includes the new targets and assignments in `Ξ`

.

The crux of the task here is to use `Ξ`

to construct a new `π' : Path`

such that unzipping `β¨π', (M + Ξ) π§.cursorβ© : Zipper`

will result in a sound move. In general, this will mean placing new metavariable declarations from `Ξ`

as new `π―`

entries in the path and deleting `π―`

entries which are assigned in `Ξ`

. Additionally, it may be necessary to hoist `πͺ`

boxes and re-order existing `π―`

entries.
This has to be done carefully or else we can introduce ill-formed boxes.

For example, take the zipper shown in (A.19) with metavariable context `π`

containing one metavariable `?π : β`

.
There is a tactic `apply List.length`

that will act on the goal `?π`

by declaring a pair of new metavariables `?Ξ± : sort`

and `?π : List ?Ξ±`

and assigning `?π`

with `List.length ?π`

.
After perfroming this tactic, the new metavariable context `π + Ξ`

is (A.20).

But now if our `π§.path`

is `[π― (?π : β)]`

, unzipping `β¨[π― (?π : β)], (π + Ξ) π§.cursorβ©`

(A.21) will not result in a valid `π : Box`

(i.e. one that we can derive a judgement `β’ π : Ξ²`

) because the `result`

will depend on metavariables `?Ξ±`

and `?π`

which do not have corresponding `π―`

-binders.

We need to make sure that the new metavariables `?Ξ±`

and `?π`

are abstracted and added to the `Path`

as shown in (A.22), so when we unzip we end up with a well-formed `Box`

.

We can find additional complications if the tactic declares new metavariables in a context `E`

other than the current local context `Ξ`

.
This may happen as a result of calling `intros`

or performing an induction step. In these cases, some additional work must be done to ensure that the newly declared metavariable is placed at the correct point in the `Box`

such that the context produced by the path above it is the same as `E`

.
This will be tackled in the next section through the definition of a function called `update`

.

## A.3.3. Definition: `update`

The procedure for correctly adjusting the path to produce valid boxes as exemplified by (A.22) is as follows.
Define a function `update : MvarContextExtension β Path β Option Path`

.
Here, `MvarContextExtension`

(see (A.2)) is either a metavariable assignment or a metavariable declaration for `π`

.

`update Ξ π`

is defined such that `(update Ξ π).mctx = extend Ξ π.mctx`

where `extend`

is defined in (A.2).
To do this, `update`

will either insert a new `π―`

path entry or delete a `π―`

according to whether `Ξ`

is a declaration or an assignment.
Then it will reorder the `π―`

declarations such that it respects the dependency ordering `βΈ`

.

In general, this is not always possible since there there may be a pair of declarations `πβ πβ : MvarDecl`

such that `πβ βΈ πβ`

but `πβ.context β€ πβ.context`

, and so there is not necessarily an ordering of the declarations which is topological on both `β€`

and `βΈ`

.
This case can be handled in theory through redeclaring metavariables and using delayed abstractions as I discuss in Section A.4, however I will not analyse or implement this case because it arises rarely in practice: most of the newly declared metavariables will not be in a different context or in a subcontext of `π.ctx`

. Furthermore most of the tactics which do cause complex declarations to appear such as `intro`

have an equivalent `Box`

move.

Some remarks on (A.23):

`reorder π`

performs a reordering on the`π―`

binders in`π`

to respect`βΈ`

and contexts. As noted earlier, in certain circumstances this may not be possible, in which case fail.To account for

`πͺ`

-boxes: before performing the above`reorder`

ing,*lift*Analogously to (A.16). all`πͺ`

items in`π`

so that the resulting`..πβ`

does not contain any`πͺ`

-binders.In the case that

`Ξ = declare ?π Ξ± Ξ`

and`Ξ β€ π.ctx`

, I am making the assumption that`Ξ±`

doesn't depend on any metavariables whose context is outside`Ξ`

. This can occur if the offending metavariable is wrapped in a delayed abstraction. I will discuss this caveat in Section A.4.The case where

`Ξ > π.ctx`

works by reassigning the newly declared metavariable`?π`

with it's skolemised version and wrapping the declaration in a series of`β`

boxes. A circumstance where this can occur is if the`intros`

tactic was used. I will not analyse the soundness of this case in further detail because it is expected that in these cases the`intro`

*move*on`Box`

es (3.24) should be used instead.

An example of performing the `assign`

case of (A.23) is given in (A.24):

Suppose that a tactic assigned `?π‘β`

with `?π‘β + 4`

. Then without any reordering the box would look like (A.25):

However this is not a valid box because the `π―`

-binder for `?π‘β`

depends on a variable that is not in scope.
Fortunately as discussed in Section A.1.5 a total dependency ordering of metavariables in the same context always exists, and so in `update`

we can use the 'target-swap' (A.15) path-reordering move to rearrange the targets to obey this. This is performed in the `reorder`

step in `update`

(A.23).

The way that `update`

is defined means that

`(update (declare ?π Ξ± Ξ) π§).mctx = extend (declare ?π Ξ± Ξ) π§.mctx`

for a valid declaration with`Ξ β€ π.ctx`

(up to reordering of the declarations in`π§.mctx`

). As mentioned above, the other case`Ξ > π.ctx`

is not considered here. Let`β¨π', π'β© = update (declare ?π Ξ± Ξ) β¨π, πβ©`

and`π β’ π βΆ Ξ±`

. Then`π' β’ π βΆ Ξ±`

too because`π'.ctx = π.ctx`

and`π'.mctx = extend Ξ π.mctx`

. And we saw in Section A.1.4 that adding a declaration to a metavariable context preserves type judgements.Similarly, we have

`(update (assign ?π π£) π§).mctx = extend (assign ?π π£) π§.mctx`

for a valid assignment. Now let`β¨π', π'β© = update (assign ?π π£) β¨π, πβ©`

and`π β’ π βΆ Ξ±`

. Then similarly`π' β’ β¦?π β¦ π£β¦ π βΆ β¦?π β¦ π£β¦ Ξ±`

. Spell out why this means that`β’ unzip π§' βΆ typeof π§`

?

## A.3.4. Applying a tactic to a zipper

We can now put together the components
Now let `zz : Zipper β Zipper`

be some function that navigates to a certain point in a `Box`

and let `tac`

be a tactic, we can define the 'escape hatch' tactic procedure, depicted below in Figure A.26.

# A.4. A variant of `π`

supporting delayed abstraction

A delayed abstraction `π[π₯]`

is a special type of expressionLean 3 calls it a delayed abstraction macro. constructed with a local variable `π₯`

and an expression `π`

which may depend on `π₯`

.
A delayed abstraction represents an abstraction of `π₯`

on `π`

An abstraction is when free variables are replaced with bound variables..
This is used when `π`

depends on a metavariable `?π`

whose declaration context contains `π₯`

. In this case, performing the abstraction immediately would be premature because the metavariable `?π`

might need to depend on `π₯`

.

There are a few possible variants and extensions of the design of `π`

boxes which I considered.
The main limitation of `π`

boxes as detailed above is that the structure of `bβ`

is inscrutable when zipped on `bβ`

.
So in the above example we could not infer the structure of the function through reasoning on `bβ`

.
In order to model this, it is necessary to invoke delayed abstractions.

To get this to work, when zipping to `bβ`

, one needs to first zip to the final `π r`

in `bβ`

, and then instantiate `bβ`

with a *delayed abstraction* of `r`

.
Then, when performing `up πβ`

, if a metavariable under a delayed abstraction has been assigned you need to unzip back through the entirety of `bβ`

and add any new targets to the path.
So a variant `PathItem.πβ`

would change to `Path β Expr β List Name β Expr`

.

# Bibliography for this chapter

- [BN98]Baader, Franz; Nipkow, Tobias
*Term rewriting and all that*(1998)**publisher**Cambridge University Press**isbn**978-0-521-45520-6 - [Car19]Carneiro, Mario
*Lean's Type Theory*(2019)view online - [EUR+17]Ebner, Gabriel; Ullrich, Sebastian; Roesch, Jared; Avigad, Jeremy; de Moura, Leonardo
*A metaprogramming framework for formal verification*(2017)Proceedings of the ACM on Programming Languages**volume**1**number**ICFP**pages**1--29**publisher**ACM New York, NY, USAview online - [Hue97]Huet, GΓ©rard
*Functional Pearl: The Zipper*(1997)Journal of functional programming**volume**7**number**5**pages**549--554**publisher**Cambridge University Pressview online - [Hur95]Hurkens, Antonius JC
*A simplification of Girard's paradox*(1995)International Conference on Typed Lambda Calculi and Applications**pages**266--278**organization**Springer - [KSFS05]Kiselyov, Oleg; Shan, Chung-chieh; Friedman, Daniel P; Sabry, Amr
*Backtracking, interleaving, and terminating monad transformers: (functional pearl)*(2005)ACM SIGPLAN Notices**volume**40**number**9**pages**192--203**publisher**ACM New York, NY, USAview online - [MAKR15]de Moura, Leonardo; Avigad, Jeremy; Kong, Soonho; Roux, Cody
*Elaboration in Dependent Type Theory*(2015)CoRR**volume**abs/1505.04324view online - [McB00]McBride, Conor
*Dependently typed functional programs and their proofs*(2000)view online - [SH17]Sterling, Jonathan; Harper, Robert
*Algebraic Foundations of Proof Refinement*(2017)arXiv preprint arXiv:1703.05215view online - [Spi11]Spiwack, Arnaud
*Verified computing in homological algebra, a journey exploring the power and limits of dependent type theory*(2011)view online - [Ste17]Steele Jr., Guy L.
*It's Time for a New Old Language*(2017)view onlineInvited talk at Clojure/Conj 2017. Slides: http://groups.csail.mit.edu/mac/users/gjs/6.945/readings/Steele-MIT-April-2017.pdf