summaryrefslogtreecommitdiff
path: root/note.tex
blob: 60ad78262cbca6713e96db101346b288a6347674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
% Created 2023-02-02 Thu 17:47
% Intended LaTeX compiler: lualatex
\documentclass[11pt,a4paper]{article}
\usepackage{longtable}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ebproof}
\usepackage{hyperref}
\usepackage[capitalise,noabbrev]{cleveref}
\usepackage{mathtools}
\usepackage{stmaryrd}
\usepackage{unicode-math}
\usepackage{parskip}
\usepackage{newunicodechar}
%% \usepackage[moderate]{savetrees}
\author{Greg Brown}
\date{\today}
\title{Frex\textsuperscript{2}: Normalisation by Evaluation for Second-Order Algebras}
\hypersetup{
 pdfauthor={Greg Brown},
 pdftitle={Frex\textsuperscript{2}: Normalisation by Evaluation for Second-Order Algebras},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={},
 pdfborder={0 0 0},
 pdflang={English}}

\newfontfamily{\fallbackfont}{STIX Two Math}[Scale=MatchLowercase]
\DeclareTextFontCommand\textfallback{\fallbackfont}

\setmathfont{STIX Two Math}

\newunicodechar{β}{\textfallback{β}}
\newunicodechar{ζ}{\textfallback{ζ}}
\newunicodechar{η}{\textfallback{η}}
\newunicodechar{λ}{\textfallback{λ}}
\newunicodechar{↝}{\textfallback{↝}}
\newunicodechar{↦}{\textfallback{↦}}
\newunicodechar{⟨}{\textfallback{⟨}}
\newunicodechar{⟩}{\textfallback{⟩}}

\newcommand*\tarr[2]{\ensuremath{{#1} \rightarrowtail {#2}}}
\newcommand*\ctxext[2]{{#1} + {#2}}
\newcommand*\ctxcons[2]{{#1}, {#2}}
\newcommand*\copair[2]{{#1} + {#2}}
\newcommand*\InFamily[4]{\ensuremath{{#2} \vdash^{#1} {#3} : {#4}}}

\newcommand*\Litt[1]{\texttt{#1}}
\newcommand*\Var[1]{\Litt{x}_{#1}}
\newcommand*\Lapp[2]{\texttt{\ensuremath{#1} \$ \ensuremath{#2}}}
\newcommand*\Labs[2]{\texttt{λ{#1}. \ensuremath{#2}}}
\newcommand*\Lsub[2]{\texttt{\ensuremath{#1}[\ensuremath{#2}]}}

\newcommand*\mvar[2]{\texttt{\ensuremath{#1}⟨\ensuremath{#2}⟩}}
\newcommand*\msub[2]{\texttt{\ensuremath{#1}\{\ensuremath{#2}\}}}

\newcommand*\term[1]{\ensuremath{\mathbb{T}{[{#1}]}}}

\newcommand*\sta[1]{\mathop{\overline{#1}}}
\newcommand*\dyn[1]{\mathop{\underline{#1}}}

\begin{document}

\maketitle

\section{Second-Order Algebra}

Universal second-order algebra is a technique to describe syntax and theories
for algebras with operations that can bind variables. One notable example of a
second-order algebra is the simply-typed lambda calculus (STLC). Second-order
algebras depend on a set \(T\) of types. For instance, the grammar \(T \coloneqq
{N \mid \tarr{T}{T}}\) gives the types for the STLC, where \(N\) describes some
neutral base type (e.g.\ natural numbers).

A \emph{signature} is a pair of a set \(O\) and function \({\vert\cdot\vert} \in {O \to
{{(T^* \times T)}^* \times T}}\). The set \(O\) is the set of operators in the signature.
The function gives the arity and return type of each operator. Each operator can
take a number of arguments, hence the arity is a list of descriptors. Because
arguments can bind variables, each descriptor has a list of bound variable
types, as well as the argument type. Let the notation \((\gamma_i)\sigma_i \vdash o : \tau\)
denote \({\vert o \vert} = {{(\gamma_i , \sigma_i)}_i , \tau}\).

The signature for the STLC has the two operators \(\mathtt{app}_{\alpha,\beta}\) and
\(\mathtt{abs}_{\alpha,\beta}\) for each pair of types \(\alpha\) and \(\beta\). Their arities are
\({\ctxcons{\tarr{\alpha}{\beta}}{\alpha}} \vdash \mathtt{app}_{\alpha,\beta} : \beta\) and \({(\alpha)\beta} \vdash
\mathtt{abs}_{\alpha,\beta} : \tarr{\alpha}{\beta}\).  Application (\(\mathtt{app}\)) takes two
arguments---a function and value---and returns a result. Neither argument to
application binds variables.  Abstraction (\(\mathtt{abs}\)) takes a single
argument, whose type is the output type of the function. This argument has
access to a freshly-bound variable of the input type.

The unpairing operation in STLC with products is also a second-order operation.
The operation \verb|let (x, y) = e in e'| is represented by an operator
\(\mathtt{unpair}_{\alpha,\beta,\tau}\), with \({\ctxcons{\alpha \times \beta}{(\ctxcons{\alpha}{\beta})\tau}} \vdash
\mathtt{unpair}_{\alpha,\beta,\tau} : \tau\). The argument \verb|e| is a pair and has no
additional variables in scope. The second argument \verb|e'| has access to two
new variables, corresponding to the two components of the pair \verb|x| and
\verb|y|\footnotemark.

\footnotetext{Think of a good non-example.}

Define a \emph{sorted family} to be a type-and-context-indexed set. For example,
the family of variables \(\mathcal{I}\,\tau\,\Gamma\) is the set of positions of type \(\tau\) in the
context \(\Gamma\). Write \InFamily{\mathfrak{X}}{\Gamma}{\Litt{t}}{\tau} for \(\Litt{t} \in
{\mathcal{X}\,\tau\,\Gamma}\), for any sorted family \(\mathcal{X}\).

Given a signature \(\Sigma = (O , {\vert\cdot\vert})\), an \emph{algebra} has four constituent
parts:

\begin{itemize}
\item a carrier sorted family \(\mathcal{A}\)
\item an operation \({\llbracket\cdot\rrbracket}_o \in
  {{(\mathcal{A}\,\sigma_i\,(\ctxext{\gamma_i}{\Gamma}))}_i \to {\mathcal{A}\,\tau\,\Gamma}}\) for each operator
  \({(\gamma_i)\sigma_i} \vdash o : \tau\)
\item a mapping \(\mathtt{var} \in {{\mathcal{I}\,\tau\,\Gamma} \to {\mathcal{A}\,\tau\,\Gamma}}\)
\item a mapping \(\mathtt{sub} \in {{\mathcal{A}\,\tau\,\Gamma} \to (\forall \sigma. {{\mathcal{I}\,\sigma\,\Gamma} \to {\mathcal{A}\,\sigma\,\Delta}}) \to
  {\mathcal{A}\,\tau\,\Delta}}\)
\end{itemize}

subject to the following conditions:

\begin{description}
\item[left unit:] \({\mathtt{sub}(\mathtt{var}_i, \sigma)} = \sigma_i\)
\item[right unit:] \({\mathtt{sub}(\Litt{t}, \mathtt{var})} = \Litt{t}\)
\item[associativity:]
  \({\mathtt{sub}(\mathtt{sub}(\Litt{t}, \sigma), \varsigma)} =
    {\mathtt{sub}(\Litt{t}, {i \mapsto {\mathtt{sub}(\sigma_i, \varsigma)}})}\)
\item[naturality:]
  \({\mathtt{sub}({\llbracket \Litt{ts} \rrbracket}_o, \sigma)} =
    {\llbracket{(\mathtt{sub}(\Litt{ts}_i, {\Uparrow\sigma}))}_i\rrbracket}_o\)
\end{description}

where \(\Uparrow\sigma\) uses \(\mathtt{var}\) and \(\mathtt{sub}\) to lift a substitution
\(\sigma\) from \({\mathcal{I}\,\tau\,\Gamma}\to{\mathcal{A}\,\tau\,\Delta}\) to
\({\mathcal{I},\tau\,(\ctxext{\Theta}{\Gamma})}\to{\mathcal{A},\tau\,(\ctxext{\Theta}{\Delta})}\). The dual extension, \({\sigma\Uparrow} \in
  {{\mathcal{I},\tau\,(\ctxext{\Gamma}{\Theta})}\to{\mathcal{A},\tau\,(\ctxext{\Gamma}{\Theta})}}\) will be used later.

The associativity, left and right unit conditions assert that \(\mathcal{A}\) is a
\emph{substitution monoid}. This means that substitution interacts with itself
and variables in an intuitive way. The naturality condition asserts that
substitution passes through operators and is capture-avoiding. Substitution
lifting maps freshly-bound variables to themselves.

For the STLC, an algebra \(\mathcal{A}\) has a carrier \(\mathcal{A}\); a pair of operations
\({\llbracket\cdot\rrbracket}_\mathtt{app} \in {{\mathcal{A}\,(\tarr{\alpha}{\beta})\,\Gamma} \to {\mathcal{A}\,\alpha\,\Gamma} \to
{\mathcal{A}\,\beta\,\Gamma}}\), and \({\llbracket\cdot\rrbracket}_\mathtt{abs} \in
{{\mathcal{A}\,\beta\,(\ctxcons{\alpha}{\Gamma})} \to {\mathcal{A}\,(\tarr{\alpha}{\beta})\,\Gamma}}\); and the variable and
substitution maps.

One example algebra for any signature is the unit algebra \(\mathbf{1}\), where
\(\mathbf{1}\,\tau\,\Gamma = {\{\ast\}}\). Each operation \({\llbracket\cdot\rrbracket}_o\),
and the maps \(\mathtt{var}\) and \(\mathtt{sub}\), return the unique value of
the appropriate type. This trivially satisfies the conditions.

Another example is the algebra of sets and functions for the STLC.\@ First, take
any set \(V\) to represent values of type \(N\). A type \(T\) can then be
interpreted as a set of values \(\mathcal{V}[T]\):
\begin{align*}
{\mathcal{V}[N]} &\mapsto V \\
{\mathcal{V}[\alpha \rightarrowtail \beta]} &\mapsto {{\mathcal{V}[\alpha]} \to {\mathcal{V}[\beta]}}
\end{align*}

We can take the carrier sorted family \({\mathcal{A}\,\tau\,\Gamma} = {(\forall \sigma. {{\mathcal{I}\,\sigma\,\Gamma} \to
{\mathcal{V}[\sigma]}}) \to {\mathcal{V}[\tau]}}\). From here, defining the operations and mappings
is straight forward:
\begin{align*}
{{\llbracket {f , g} \rrbracket}_{\mathtt{app}}(\gamma)} &= {f(\gamma,(g(\gamma)))} \\
{{\llbracket {f} \rrbracket}_{\mathtt{abs}}(\gamma)} &=
  {x \mapsto {f({\mathtt{here} \mapsto x}; {{\mathtt{there}\,i} \mapsto \gamma\,i})}} \\
{\mathtt{var}(i, \gamma)} &= \gamma_i \\
{\mathtt{sub}(f,\sigma,\gamma)} &= {f\,(i \mapsto {\sigma_i(\gamma)})}
\end{align*}

Verifying that this definition satisfies the coherence conditions is left as an
exercise\footnotemark{}.

\footnotetext{This is me being lazy.}

\section{Metavariables}

Here is a typical way of writing the \(\beta\)-reduction relation for STLC:\@

\begin{verbatim}
(λ x. t) $ u ↝ t[x ↦ u]
\end{verbatim}

The variable \verb|t| is implicitly parameterised by the bound variable
\verb|x|, meaning \InFamily{}{\ctxcons{\alpha}{t}}{\Litt{t}}{\beta}. In contrast,
\verb|u| is not parameterised by any terms. We can make the parameters explicit:

\begin{verbatim}
(λ x. t<x>) $ u<> ↝ t<u<>>
\end{verbatim}

\verb|t| and \verb|u| are \emph{metavariables}---free variables with parameters.
We can instantiate metavariables with concrete terms to create new ones. This
property is formalised in the definition of a \emph{syntactic algebra}.

A syntactic algebra for a signature \(\Sigma\) over a sorted family of metavariables
\(\mathfrak{X}\) is:

\begin{itemize}
\item an algebra \(\mathcal{A}\) over the signature
\item a map \(\mathtt{mvar} \in {{\mathfrak{X}\,\tau\,\Pi} \to (\forall \sigma. {{\mathcal{I}\,\sigma\,\Pi} \to {\mathcal{A}\,\sigma\,\Gamma}}) \to
  {\mathcal{A}\,\tau\,\Gamma}}\)
\item and a map \(\mathtt{msub} \in {{\mathcal{A}\,\tau\,\Gamma} \to (\forall {\sigma, \Theta}. {{\mathfrak{X}\,\sigma\,\Theta} \to
  {\mathcal{A}\,\sigma\,(\ctxext{\Theta}{\Delta})}}) \to {\mathcal{A}\,\tau\,(\ctxext{\Gamma}{\Delta})}}\)
\end{itemize}

which satisfy these conditions\footnotemark{}:

\footnotetext{Explain why these conditions exist, and why they are so ugly.}

\begin{enumerate}
\item
  \({\mathtt{sub}(\mathtt{mvar}(\mathfrak{m},\sigma),\varsigma)} =
    {\mathtt{mvar}(\mathfrak{m},{i \mapsto {\mathtt{sub}(\sigma_i,\varsigma)}})}\)
\item
  \({\mathtt{msub}({\llbracket ts \rrbracket}_o,\zeta)} =
    {\llbracket{\mathtt{msub}(ts_i,\zeta)}\rrbracket}_o\)
\item
  \({\mathtt{msub}(\mathtt{var}(i),\zeta)} =
    {\mathtt{var}(\mathtt{inl}(i))}\)
\item
  \({\mathtt{msub}(\mathtt{sub}(t,\sigma),\zeta)} =
    {\mathtt{sub}(\mathtt{msub}(t,\zeta),{\sigma\Uparrow})}\)
\item
  \({\mathtt{sub}(\mathtt{msub}(t,\zeta),{\Uparrow\sigma})} =
    {\mathtt{msub}(t,{\mathfrak{m} \mapsto {\mathtt{sub}(\zeta_{\mathfrak{m}},{\Uparrow\sigma})}})}\)
\item
  \({\mathtt{msub}(\mathtt{mvar}(\mathfrak{m},\sigma),\zeta)} =
    {\mathtt{sub}(\zeta_{\mathfrak{m}},\copair{(i \mapsto {\mathtt{msub}(\sigma_i,\zeta)})}{\mathtt{inr}\circ\mathtt{var}})}\)
\item
  \({\mathtt{msub}(\mathtt{msub}(t,\zeta),\xi)} =
    {\mathtt{msub}(t,{\mathfrak{m} \mapsto {\mathtt{msub}(\zeta_{\mathfrak{m}},\xi)}})}\)
\end{enumerate}

The map \(\mathtt{mvar}\) takes a metavariable \InFamily{\mathfrak{X}}{\Pi}{\mathfrak{m}}{\tau} and
converts it into a term in context \(\Gamma\). This is by giving a value for each of
\(\mathfrak{m}\)'s parameters, achieved via the substitution argument.

The syntactic substitution map \(\mathtt{msub}\) is more complex. First note
that it is linear: the output context is an extension of the input. This means
that metasubstitution can use bound variables, as long as they were bound in a
higher scope. Also observe that the substitution map (the second parameter) is
independant of the input context \(\Gamma\). Because metavariables are always
associated with a substitution, it is sufficient to map only the metavariables
in their original contexts. The context can be ``corrected'' by a substitution.

%% Conditions 1, 2 and 3 require syntactic substitution to pass through normal term
%% operations. \(\zeta\) remains unchanged because it never mentions the original
%% context. The most subtle

The term algebra \term{\mathfrak{X}} for a signature is the free syntactic algebra over
\(\mathfrak{X}\), with for each map \(f \in {{\mathfrak{X}\,\tau\,\Gamma}\to{\mathcal{A}\,\tau\,\Gamma}}\) a unique homomorphism
\(\mathtt{bind}(f) \in {\term{\mathfrak{X}} \to \mathcal{A}}\).

Return to the earlier example of \(\beta\) reduction. We have metavariables \(\mathfrak{X}\)
with two elements: \InFamily{\mathfrak{X}}{\alpha}{\Litt{t}}{\beta} and \InFamily{\mathfrak{X}}{}{\Litt{u}}{\alpha}.
We then have two terms, both of type \(\beta\) in an empty context:
\verb|(λ x. t<x>) $ u<>| and \verb|t<u<>>|.

As a more complex example, we can construct a syntactic substitution map \(\zeta \in
{{\mathfrak{X}\,\sigma\,\Theta} \to {\mathcal{A}\,\sigma\,(\ctxext{\Theta}{\ctxcons{\tarr{\alpha}{\beta}}{\alpha}})}}\)\footnotemark{}:
\begin{gather*}
  \InFamily{\mathcal{A}}{\ctxcons{\alpha}{\ctxcons{\tarr{\alpha}{\beta}}{\alpha}}}{\Lapp{\Var{1}}{\Var{0}}}{\beta} \\
  \InFamily{\mathcal{A}}{\ctxcons{\tarr{\alpha}{\beta}}{\alpha}}{\Var{1}}{\alpha}
\end{gather*}

\footnotetext{Include some examples.}

Metavariables allow a formal definition of equational theories, and then models.
An \emph{equation} is a quintuple \((\mathfrak{X},\Gamma,\tau,\Litt{t},\Litt{u})\) such that
\InFamily{\term{\mathfrak{X}}}{\Gamma}{\Litt{t}}{\tau} and \InFamily{\term{\mathfrak{X}}}{\Gamma}{\Litt{u}}{\tau}. An
\emph{equational theory} is a signature with a finite set of axioms
\InFamily{\term{\mathfrak{X}}}{\Gamma}{\Litt{t} =_a \Litt{u}}{\tau}. This can be extended to an
equivalence relation \InFamily{\term{\mathfrak{X}}}{\Gamma}{\Litt{t} = \Litt{u}}{\tau} by taking
the closure of axioms with respect to metasubstitution.

A \emph{model} of an equational theory is an algebra \(\mathcal{A}\) where all equations
are valid: given an equation \InFamily{\term{\mathfrak{X}}}{\Gamma}{\Litt{t} = \Litt{u}}{\tau},
then for any environment map \(f \in {\mathfrak{X}\,\tau\,\Gamma}\to{\mathcal{A}\,\tau\,\Gamma}\), the equality
\(\mathtt{bind}(f,t) = \mathtt{bind}(f,u)\) holds.

\section{Free Extensions}

%% Computation involving terms of second-order algebras often starts by
%% transforming them into a normal form. Informally, this is a syntactic operation
%% that usually performs some evaluation of the term that preserves the semantics.

%% An algebra (or model) \(\mathcal{A}\) is a \emph{normal form} of term algebra (model)
%% \(\term{\mathfrak{X}}\) if it consists of:

%% \begin{itemise}
%% \item a map \(\mathtt{env} \in {{\mathfrak{X}\,\tau\,\Gamma}\to{\mathcal{A}\,\tau\,\Gamma}}\)
%% \item a homomorphism \(\mathtt{reify} \in \mathcal{A}\to{\term{\mathfrak{X}}}\)
%% \end{itemise}

%% where the composite homomorphism
%% \({{\mathtt{bind}(\mathtt{env})}\circ\mathtt{reify}}\) is the identity. Define
%% \(\mathtt{eval} = {\mathtt{bind}(\mathtt{env})}\).

%% The main structure of a normal form is the \(\mathtt{reify}\) homomorphism,
%% which interprets the algebra as terms. The map \(\mathtt{env}\) provides the
%% intended interpretation of the non-algebraic values of \(\mathcal{A}\), reflected in the
%% identity condition.

%% Note that normal forms make no requirement of \(\mathtt{eval}\) or
%% \(\mathtt{reify}\) to perform any computation. This means that terms are a
%% trivial normal form.

%% At first, you may think that \(\mathcal{A}\) is trivially a normal form of \(\term{\mathcal{A}}\), where
%% \(\mathtt{env}\) is the identity. However, there is in general no homomorphism
%% \(\mathcal{A}\to{\term{\mathcal{A}}}\).

Given an algebra (or model) \(\mathcal{A}\) and metavariables \(\mathfrak{X}\), a \emph{free
extension} of \(\mathcal{A}\) by \(\mathfrak{X}\) is a syntactic algebra (or model) \(\mathcal{F}\) over \(\mathfrak{X}\)
with the following additional structure:

\begin{itemize}
\item a homomorphism \(\mathtt{sta} \in {\mathcal{A}\to\mathcal{F}}\)
\item for every pair of homomorphism \(f \in {\mathcal{A}\to\mathcal{B}}\) and map \(g \in
  {{\mathcal{X}\,\tau\,\Gamma}\to{\mathcal{B}\,\tau\,\Gamma}}\), a unique homomorphism \(\mathtt{interp}(f,g) \in
  {\mathcal{F}\to\mathcal{B}}\)
\end{itemize}

with the following two conditions:

\begin{itemize}
\item \({{\mathtt{interp}(f,g)}\circ\mathtt{sta}} = g\)
\item
  \({{\mathtt{interp}(f,g,{\mathtt{mvar}(\mathfrak{m},\sigma)})}} =
    {\mathtt{sub}(g(\mathfrak{m}),{{\mathtt{interp}(f,g)} \circ \sigma})}\)
\end{itemize}

This is equivalently the algebraic (model) coproduct of \(\mathcal{A}\) and \(\term{\mathfrak{X}}\).
The free extension is an extension in the sense that the algebra \(\mathcal{A}\) is given
an interpretation of metavariables. It is free in the sense that adding the
metavariables minimally restricts the homomorphisms to or from the algebra.

Define the map \(\mathtt{dyn} \in {{\mathfrak{X}\,\tau\,\Gamma}\to{\mathcal{F}\,\tau\,\Gamma}}\) by \(\mathtt{dyn}(\mathfrak{m}) =
\mathtt{msub}(\mathfrak{m},\mathtt{var})\). The names \(\mathtt{sta}\) and \(\mathtt{dyn}\)
come from the perspective of staged compilation: values made with
\(\mathtt{sta}\) are known statically and can be acted on during compilation.
Values defined via \(\mathtt{dyn}\) are only known at runtime and have no
inherent algebraic structure.

Using \(\mathtt{sta}\) and \(\mathtt{dyn}\), there is a homomorphism
\(\mathtt{eval} \in {\term{\mathcal{A} \cup \mathfrak{X}}\to\mathcal{F}}\) given by \(\mathtt{eval} =
{\mathtt{bind}(\mathtt{sta}\cup\mathtt{dyn})}\). Because there is a morphism
\(\mathcal{A}\to{\term{\mathcal{A}}_{/\approx}}\)\footnotemark{}, there is also a morphism
\(\mathtt{reify} \in {\mathcal{F}\to{\term{\mathcal{A} \cup \mathfrak{X}}_{/\approx}}}\) defined using \(\mathtt{interp}\).

\footnotetext{Define what \(\approx\) is.}

%% One universal instance of the free extension is given by \(\mathbb{T}{[\mathcal{A} \cup \mathfrak{X}]}\), quotient
%% the conditions listed earlier. Define homomorphisms \(\mathtt{eval} \in
%% {{\mathbb{T}{[\mathcal{A} \cup \mathfrak{X}]}} \to \mathcal{F}}\) and \(\mathtt{reify} \in {\mathcal{F} \to {\mathbb{T}{[\mathcal{A} \cup \mathfrak{X}]}}}\) given by
%% \(\mathtt{bind}\) and \(\mathtt{interp}\) respectively. \(\mathtt{eval}\)
%% ideally performs static computations, normalising the term in some way, and
%% \(\mathtt{reify}\) exports the normal form as a term.

For brevity, let \(\sta{\Litt{t}}\) represent \(\mathtt{sta}(\Litt{t})\) and
understand \(\dyn{\mathfrak{m}}\) to be \(\mathtt{dyn}(\mathfrak{m})\).

\section{βη-Normal Form as a Free Extension}

In the STLC, βη-normal form is a way of writing terms such that test for βη
equivalence is a simple identity check. I will adapt this normal form to attempt
to produce a free extension of a STLC model \(\mathcal{A}\) by metavariables \(\mathfrak{X}\),
dubbed \(\mathcal{A}_{\beta\eta}[\mathfrak{X}]\).

%% There are a couple of minor conceptual optimisations that we can apply before
%% evaluating terms. Remember that \(\mathtt{sta}\) is a homomorphism, thus
%% \(\Var{i} = \sta{\Var{i}}\) for all variables \(\Var{i}\). By
%% applying this as a substitution on terms, we only have to consider application,
%% abstraction, static values and dynamic metavariables. Further, we write
%% \(\sta{\Litt{t}}\) for \(\mvar{\sta{\Litt{t}}}{\mathtt{var}}\), and treat it
%% as a distinct case.

A term is \emph{dynamic} if there is a subterm that uses a dynamic metavariable.
Otherwise it is static. For example, \(\mvar{\dyn{\mathfrak{m}}}{\zeta}\) and
\(\Lapp{\sta{\Litt{t}}}{(\Labs{x}{\mvar{\dyn{\Litt{n}}}{\Litt{x}}})}\) are
both dynamic, whilst \(\Var{i}\), \(\sta{\Litt{t}}\) and
\(\Labs{x}{\mvar{\sta{\Litt{u}}}{\Litt{x}}}\) are static.

The sorted family \(\mathcal{A}_{\beta\eta}[\mathfrak{X}]\) and a family \(\mathcal{A}_{ne}[\mathfrak{X}]\) are defined
inductively by the following judgements:

\begin{gather*}
\begin{prooftree}
  \hypo{\InFamily{\mathcal{A}_{\beta\eta}[\mathfrak{X}]}{\ctxcons{\Litt{x} : \alpha}{\Gamma}}{\Litt{t}}{\beta}}
  \infer1[1]{\InFamily{\mathcal{A}_{\beta\eta}[\mathfrak{X}]}{\Gamma}{\Labs{x}{\Litt{t}}}{\tarr{\alpha}{\beta}}}
\end{prooftree}
\qquad
\begin{prooftree}
  \hypo{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\Litt{t}}{N}}
  \infer1[2]{\InFamily{\mathcal{A}_{\beta\eta}[\mathfrak{X}]}{\Gamma}{\Litt{t}}{N}}
\end{prooftree}
\\
\begin{prooftree}
  \hypo{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\Litt{t}}{\tarr{\alpha}{\beta}}}
  \hypo{\InFamily{\mathcal{A}_{\beta\eta}[\mathfrak{X}]}{\Gamma}{\Litt{u}}{\alpha}}
  \hypo{\Litt{t} \text{ or } \Litt{u} \text{ uses a dynamic variable}}
  \infer3[3]{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\Lapp{\Litt{t}}{\Litt{u}}}{\beta}}
\end{prooftree}
\\
\begin{prooftree}
  \hypo{\InFamily{\mathfrak{X}}{\Pi}{\mathfrak{m}}{\tau}}
  \hypo{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\sigma_i}{\Pi_i}}
  \infer2[4]{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\mvar{\dyn{\mathfrak{m}}}{\sigma}}{\tau}}
\end{prooftree}
\qquad
\begin{prooftree}
  \hypo{\InFamily{\mathcal{A}}{\Gamma}{\Litt{t}}{\tau}}
  \infer1[5]{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\mvar{\sta{\Litt{t}}}{\mathtt{var}}}{\tau}}
\end{prooftree}
\\
\begin{prooftree}
  \hypo{\InFamily{\mathcal{A}}{\Pi}{\Litt{t}}{\tau}}
  \hypo{\InFamily{\mathcal{A}_{\beta\eta}[\mathfrak{X}]}{\Gamma}{\sigma_i}{\Pi_i}}
  \hypo{\sigma \text{ uses a dynamic variable}}
  \infer3[6]{\InFamily{\mathcal{A}_{ne}[\mathfrak{X}]}{\Gamma}{\mvar{\sta{\Litt{t}}}{\sigma}}{\tau}}
\end{prooftree}
\end{gather*}

In summary, this normal form first requires terms to have no β~reductions
(resulting in a family of neutral terms \(\mathcal{A}_{ne}[\mathfrak{X}]\)). Then terms are
η~expanded so each type constructor has exactly one constructor.

Rules 1~and~2 describe how to convert neutral terms into normal forms: each
function requires an explicit abstraction, and terms of neutral type must be
neutral.

Rule~3 describes which applications are irreducible. The first argument to the
application must itself be irreducible. If the only requirement was for it to be
in normal form, then it could be an abstraction term. This is β-reducible, so
goes against the principal of performing all possible reductions. Further at
least one argument must use a dynamic variable. If both arguments to application
were static, then they are both values in \(\mathcal{A}\) so the application can be
reduced by using application from \(\mathcal{A}\). Thus, evaluation is stuck only if at
least one of the two argument has a dynamic variable.

Rule~4 states that dynamic variables are always stuck. There is no information
to allow for further evaluation, so they are always neutral.

Rules 5~and~6 describe which substitutions into static values are neutral.
Rule~5 says that the identity substitution is stuck. Static values always appear
with a substitution in terms. Because applying the identity substitution doesn't
change the static value, nor does it influence any later substitutions, it is
the maximally-reduced substitution where all components are static. The
hypothesis of rule~6 requires at least one substitution parameter to be dynamic.
Vecause substitutions are performed in parallel, this blocks the full
substitution.

Notice that neither normal forms or neutral terms used bound variables. Any
variable \(\Var{i}\) is equivalent to \(\mvar{\sta{\Var{i}}}{\mathtt{var}}\).
Expanding bound variables into static values can be seen as a reduction, so
should be evaluated according to the principal of performing all possible
reductions.

Using a logical relations argument, it is possible to define a mapping
\(\mathbb{T}[\mathcal{A} \cup \mathfrak{X}] \to \mathcal{A}_{\beta\eta}[\mathfrak{X}]\)\footnotemark{}.

\footnotetext{How, exactly?}

\section{The Equality Problem}

Unfortunately, the normal form just presented using equality is not a model of
STLC.\@ In fact, it is not even an algebra. The problem comes from substitution,
and can be demonstrated by the following two equal terms:
\begin{align*}
\mvar{\sta{\Lapp{\Var{i}}{\Litt{t}}}}{\dyn{\sigma}}
  &= \Lsub{(\Lapp{\sta{\Var{i}}}{\sta{\Litt{t}}})}{\dyn{\sigma}} \\
  &= \Lsub{(\Lapp{\Var{i}}{\sta{\Litt{t}}})}{\dyn{\sigma}} \\
  &= \Lapp{\Lsub{\Var{i}}{\dyn{\sigma}}}{\Lsub{\sta{\Litt{t}}}{\dyn{\sigma}}} \\
  &= \Lapp{\dyn{\sigma_i}}{\mvar{\sta{\Litt{t}}}{\dyn{\sigma}}}
\end{align*}

Assuming the terms have type \(N\), then they are in normal form. The top two
lines normalise to the first term, and the bottom two lines normalise to the
final term. However, because the subterm \(\sta{\Lapp{\Var{i}}{\Litt{t}}}\) is
opaque for an arbitrary algebra \(\mathcal{A}\), these two normal forms for the same term
are distinct.

For this normal form to be an algebra, it must be taken quotient \(\approx\), the same
relation the term algebra is subject to.

\section{Removing the Quotient: Effective Free Extensions}

In practical systems, using a quotient for equality can be undesirable.
Especially when the quotient is undecidable. An \emph{effective} free extension
uses identity of values for equality---both easy to compute and decidable.

I suspect that there is no free extension for STLC that is effective for all
algebras, due to the equality problem. Instead, I will discuss classes of
algebras where the βη-normal form is effective.

The simplest case is when \(\mathcal{A} = \term{\emptyset}\): the initial algebra. In this case,
\(\term{\term{\emptyset} \cup \mathfrak{X}}_{/\approx} \cong \term{\mathfrak{X}}\), and \(\term{\emptyset}_{\beta\eta}[\mathfrak{X}]\) is the
classical βη-normal form extended with metavariables. By design, βη-normal form
identifies terms that are equivalent, thus this is an effective free extension.
This argument can be generalised to \(\mathcal{A} = \term{\mathfrak{Y}}\), for any fixed set of
metavariables \(\mathfrak{Y}\), as \(\term{\term{\mathfrak{Y}} \cup \mathfrak{X}}_{/\approx} \cong \term{\mathfrak{Y} \cup \mathfrak{X}}\).

Now consider an algebra \(\mathcal{A}\) such that there is an isomorphism \(\mathcal{A}\cong\term{\mathfrak{Y}}\).
This is true for all ``normal forms'' of \term{\mathfrak{Y}}, such as weak-head normal
form, βη-normal form, and CPS form. \(\mathcal{A}_{\beta\eta}[\mathfrak{X}]\) is an effective free
extension, because it is isomorphic to \(\term{\mathfrak{Y}}_{\beta\eta}[\mathfrak{X}]\), which is effective.

One thing missing from the previous form is terms that compute on values of the
neutral type. For example, if an algebra \(\mathcal{A}\) treats values of type \(N\) as
naturals, with an addition operator, then \(\mathcal{A}\not\to\term{\mathbb{N}\cup\{+\}}\), because
\(\sta{5} = \sta{\mvar{add}{2, 3}} = \mvar{\sta{add}}{\sta{2}, \sta{3}}\), but
the outer terms are not equal in \term{\mathbb{N}\cup\{+\}}.

Everthing past here is a rough sketch.

Define \(\mathcal{A}\) to be a (sub|super)-model\footnotemark{} of the STLC when there is:

\footnotetext{I don't know which is better. Maybe it should be something
  completely different?}

\begin{itemize}
\item a sorted family \(\mathfrak{Y}\)
\item a set of axioms \(\mathbf{Ax} \in \InFamily{\term{\mathfrak{X}\cup\mathfrak{Y}}}{\emptyset}{t =_{\mathcal{A}} u}{\tau}\)
\item an isomorphism \(\mathcal{A} \cong \term{\mathfrak{Y}}_{/\mathbf{Ax}}\)
\end{itemize}

where

\begin{itemize}
\item abstraction does not occur in  \(\mathbf{Ax}\)
\item all equalities in \(\mathcal{A}\) are generated by β- and η- equalities and \(\mathbf{Ax}\)
\end{itemize}

\appendix
\section{Commentary about \LaTeX}

The character \(\mathfrak{Y}\) is written \verb|\mathfrak{Y}|, yet it looks much more like an ``N'' to
me.

\end{document}

% LocalWords:  unpair/pair