#  Spinor domain wall and test fermions on an arbitrary domain wall

Vladimir Dzhunushaliev<EMAIL_ADDRESS>Department of Theoretical
and Nuclear Physics, Al-Farabi Kazakh National University, Almaty 050040,
Kazakhstan  Institute of Experimental and Theoretical Physics, Al-Farabi
Kazakh National University, Almaty 050040, Kazakhstan  Academician J. Jeenbaev
Institute of Physics of the NAS of the Kyrgyz Republic,
265a, Chui Street, Bishkek 720071, Kyrgyzstan  Laboratory for Theoretical
Cosmology, International Centre of Gravity and Cosmos, Tomsk State University
of Control Systems and Radioelectronics (TUSUR), Tomsk 634050, Russia
Vladimir Folomeev<EMAIL_ADDRESS>Institute of Experimental and Theoretical
Physics, Al-Farabi Kazakh National University, Almaty 050040, Kazakhstan
Academician J. Jeenbaev Institute of Physics of the NAS of the Kyrgyz
Republic,
265a, Chui Street, Bishkek 720071, Kyrgyzstan  Laboratory for Theoretical
Cosmology, International Centre of Gravity and Cosmos, Tomsk State University
of Control Systems and Radioelectronics (TUSUR), Tomsk 634050, Russia  Dina
Zholdakhmet<EMAIL_ADDRESS>Department of Theoretical and Nuclear
Physics, Al-Farabi Kazakh National University, Almaty 050040, Kazakhstan
Institute of Experimental and Theoretical Physics, Al-Farabi Kazakh National
University, Almaty 050040, Kazakhstan

###### Abstract

We consider a spinor domain wall embedded in a five-dimensional spacetime with
a nondiagonal metric. The corresponding plane symmetric solutions for linear
and nonlinear spinor fields with different parameters are obtained. It is
shown that in the general case the metric functions and spinor fields do not
possess $Z_{2}$ symmetry with respect to the domain wall. We study the angular
momentum density of the domain wall arising because of the presence of the
spinor field creating the wall. The properties of test fermions located on an
arbitrary domain wall are considered. The concepts of the “second spin”
(arising due to the properties of the Lorentz group generators in a five-
dimensional spacetime) and of the “second magnetic field” (representing the
components $F_{i5}$ of the electromagnetic field five-tensor) are introduced.
We find eigenspinors of the “second spin” and show that some of them represent
the Bell states. In the nonrelativistic limit we derive the Pauli equation for
the test fermions on the domain wall which contains an extra term describing
the interaction of a spin-$1/2$ particle with the “second magnetic field”;
this allows the possibility of an experimental verification of the existence
of extra dimensions.

Thick domain wall solutions, Dirac equation, “second spin”, “second magnetic
field”, test fermions, Pauli equation, experimental verification

##  I Introduction

The study of self-consistent solutions to the Einstein-Dirac equations is a
fascinating and very difficult problem. Such solutions are few at present. The
main difficulty is that spinors in the Dirac equation have a spin and hence
there are no spherically symmetric solutions: they must be at least axially
symmetric, which greatly complicates deriving solutions describing a
gravitating spinor field. To simplify matters, one can take two spinor fields
with oppositely directed spins, as is done, for example, in Refs. [1, 2] for
particlelike solutions. In cosmology, classical spinor fields have been
considered in Refs. [3, 4, 5, 6]. In astrophysics, stars supported by spinor
fields have been studied in Refs. [7, 8, 9, 10, 11, 12]. [Note here that Refs.
[10, 12] deal with a single spinor field describing spinning (axially
symmetric) configurations.]

A consideration of a hypothesis, called brane world scenario, according to
which we live on a thin leaf (brane) embedded into some multidimensional space
(bulk) is of special interest. In constructing brane models, one usually
either uses various scalar fields (for a review, see Ref. [13]) or performs
modeling within some extended theories of gravity (see the review [14]).
However, it is of some interest to employ another fundamental fields as a
matter source supporting the brane. In this connection, in the present paper
we continue our previous investigations began in Refs. [15, 16] where thick
brane solutions supported by nonlinear spinor fields have been considered (see
also the recent work [17] where five-dimensional domain wall solutions
supported by a nonlinear spinor field are under investigation). Working within
the Einstein-Dirac theory, in the first part of the current paper, we study
five-dimensional plane symmetric solutions describing domain walls. The main
distinctive feature of the present study is the use of a nondiagonal metric;
this logically follows from the fact that a spinor field must possess a spin
whose presence will in general result in the appearance of a nondiagonal
component in the metric which describes a rotation. We will examine the case
where gravitation interacts both with linear and nonlinear spinor fields. The
nonlinear spinor field is of special interest, since in the absence of gravity
the nonlinear spinor field (unlike the linear one) permits solitonlike
solutions (see, e.g., Refs [18, 19]).

In the second part of the paper, we study the properties and behavior of test
fermions living on any domain wall. It will be shown that the spatial part of
the five-dimensional spin tensor splits into an ordinary spin and a “second
spin” which is represented by the components $\Sigma_{5i}$ of the five-
dimensional spin tensor. In order to study the characteristics of the motion
of the test fermions on the domain wall, we will obtain a nonrelativistic
approximation for the five-dimensional Dirac equation (the Pauli equation)
which will contain extra terms (compared with the Pauli equation in our four-
dimensional spacetime). One of these terms involves the component $A_{5}$ of
the five-potential of an electromagnetic field, and another one contains the
direct interaction between a spin and a “second magnetic field” which is
introduced as a vector $\mathcal{H}_{i}=F_{i5}$, where $F_{AB}$ is the
electromagnetic field five-tensor. It is remarkable that the presence of such
interaction may lead to experimentally measurable consequences; this in turn
may permit one to verify the hypothesis that our world is a domain wall (or a
brane) embedded in some multidimensional space (bulk).

In the present study we regard spinor fields as classical ones, although it is
known that in nature they are described as quantum fields. In this connection,
the question arises as to when one can systematically regard fermionic quantum
fields as classical spinors? This question is considered, in particular, in
Ref. [20] where the conclusion has been drawn that a classical spinor field
may appear either as a result of some effective description of a more
complicated quantum system or when a quantum state of a spinor is in some
sense “close” to a vacuum state where a classical consideration of a massive
Dirac spinor may be a good approximation.

It is worth mentioning that, strictly speaking, the configurations studied
here cannot yet be referred to as branes, since this would demand to
demonstrate that the domain walls under consideration are able to trap zero
modes of various matter fields corresponding to particles or fields in the
Standard Model. In particular, for fermions, this can be done, for example,
analogously to the problem of trapping particles with spin $1/2$ on the domain
wall considered in the pioneering paper [21] where the authors studied the
question of trapping massless fermions on the domain wall supported by a
scalar field. Later on a number of works have been done devoted to the
question of localization of Dirac fermion fields on thick branes (for a
review, see, e.g., Ref. [14]) For our case, this question must be considered
separately.

##  II Field equations

We work within the five-dimensional theory of gravitation with a source of
matter in the form of a nonlinear spinor field $\psi$ with the total action
(hereafter we work in units $8\pi G=c=\hbar=1$)

$S=\int\left(-\frac{R}{2}+\Lambda+\mathcal{L}_{m}\right)\sqrt{g}\;d^{5}x,$

where $R$ is the five-dimensional scalar curvature, $\Lambda$ is the five-
dimensional cosmological constant, $g$ is the determinant of the five-
dimensional metric $g_{AB}$ with $A,B=0,1,2,3,5$ being the world index. For
our purposes, we have taken the plus sign in front of the cosmological
constant $\Lambda$. In turn, the Lagrangian of the nonlinear spinor field is
of the form

$\mathcal{L}_{m}=\frac{\imath}{2}\left(\bar{\psi}\not{\nabla}\psi-\bar{\psi}\overleftarrow{\not{\nabla}}\psi\right)-m\bar{\psi}\psi+V(\bar{\psi},\psi),$

where $m$ is some parameter and the potential $V(\bar{\psi},\psi)$ is
understood to be so chosen that the condition $\bar{\psi}\frac{\partial
V}{\partial\bar{\psi}}=2V$ holds. In what follows, we will use the potential

$V=\frac{\lambda}{2}\left(\bar{\psi}\psi\right)^{2},$

where $\lambda$ is some parameter. The corresponding five-dimensional Einstein
and Dirac equations are

$\displaystyle E_{ab}\equiv R_{ab}-\frac{1}{2}\eta_{ab}R+\eta_{ab}\Lambda-
T_{ab}$ $\displaystyle=$ $\displaystyle 0,$ (1)
$\displaystyle\left(\imath\Gamma^{a}e_{a}^{\phantom{a}A}D_{A}-m+\frac{\partial
V}{\partial\bar{\psi}}\right)\psi$ $\displaystyle=$ $\displaystyle 0,$ (2)

where $a,b=\bar{0},\bar{1},\bar{2},\bar{3},\bar{5}$ are the Lorentz indices;
$A=0,1,2,3,5$ is the world index; $e^{a}_{\phantom{a}A}$ is the 5-bein;
$\Gamma^{a}$ are the five-dimensional Dirac matrices in flat Minkowski space;
$D_{A}\psi=\left(\partial_{A}-\frac{1}{4}\omega_{A}^{\phantom{A}ab}\Gamma_{ab}\right)\psi$
is the covariant derivative of the spinor $\psi$;
$\Gamma_{ab}=\frac{1}{2}\left(\Gamma_{a}\Gamma_{b}-\Gamma_{b}\Gamma_{a}\right)$;
$\not{\nabla}\psi=e^{A}_{a}\gamma^{a}D_{A}\psi$;
$\eta_{ab}=\text{diag}\left(1,-1,-1,-1,-1\right)$ is the five-dimensional
covariant Minkowski metric. According to the textbook [22], the energy-
momentum tensor for the spinor field is taken in the form

$T_{a}^{\;A}=\frac{\imath}{2}\bar{\psi}\left(\Gamma^{A}e_{a}^{\phantom{a}B}+\Gamma_{a}g^{AB}\right)D_{B}\psi-\frac{\imath}{2}D_{B}\bar{\psi}\left(\Gamma^{A}e_{a}^{\phantom{a}B}+\Gamma_{a}g^{AB}\right)\psi-
e_{a}^{\phantom{a}A}\mathcal{L}_{m},$ (3)

where $\Gamma^{A}=e_{a}^{\,\,A}\Gamma^{a}$ are the five-dimensional Dirac
matrices in a curved spacetime;
$g^{AB}=e_{a}^{\phantom{a}A}e_{b}^{\,\,B}\eta^{ab}$ is the five-dimensional
contravariant metric tensor; $\bar{\psi}=\psi^{\dagger}\Gamma^{\bar{0}}$ is
the Dirac conjugate spinor;
$D_{A}\bar{\psi}=\bar{\psi}\left(\overleftarrow{\partial}_{A}+\frac{1}{4}\omega_{A}^{\phantom{A}ab}\Gamma_{ab}\right)$
with $\bar{\psi}\overleftarrow{\partial}_{A}=\partial_{A}\bar{\psi}$. Notice
here that our definition of the energy-momentum tensor (3) has the opposite
sign compared with Ref. [22] in order to be consistent with the definitions
for $R_{ab}$ from Ref. [23].

The five-dimensional Dirac matrices in flat Minkowski space are

$\Gamma^{\bar{0}}=\begin{pmatrix}0&\mathbb{I}_{2\times 2}\\\
\mathbb{I}_{2\times
2}&0\end{pmatrix},\quad\Gamma^{\bar{i}}=\begin{pmatrix}0&-\sigma_{\bar{i}}\\\
\sigma_{\bar{i}}&0\end{pmatrix}\,\text{with}\,\,\bar{i}=1,2,3,\quad\Gamma^{\bar{5}}=\begin{pmatrix}-\imath\mathbb{I}_{2\times
2}&0\\\ 0&\imath\mathbb{I}_{2\times 2}\end{pmatrix},$

where $\mathbb{I}_{2\times 2}$ is the $2\times 2$ unit matrix, and
$\sigma_{\bar{i}}$ are the Pauli matrices

$\sigma_{\bar{1}}=\begin{pmatrix}0&1\\\
1&0\end{pmatrix},\quad\sigma_{\bar{2}}=\begin{pmatrix}0&-\imath\\\
\imath&0\end{pmatrix},\quad\sigma_{\bar{3}}=\begin{pmatrix}1&0\\\
0&-1\end{pmatrix}.$

We seek a wall-like solution of the system (1) and (2). To do this, let us
choose the following orthonormal 5-bein:

$e^{0}_{A}dx^{A}=\chi(r)dt,\quad e^{1}_{A}dx^{A}=\phi(r)dx,\quad
e^{2}_{A}dx^{A}=\phi(r)dy,\quad e^{3}_{A}dx^{A}=\xi(r)dt+\eta(r)dz,\quad
e^{5}_{A}dx^{A}=dr,$ (4)

such that

$ds^{2}\equiv\eta_{ab}\left(e^{a}_{A}dx^{A}\right)\left(e^{b}_{B}dx^{B}\right)=\left(\chi^{2}-\xi^{2}\right)dt^{2}-\phi^{2}\left(dx^{2}+dy^{2}\right)-\eta^{2}dz^{2}-dr^{2}-2\,\xi\eta\,dt\,dz.$
(5)

In turn, for the spinor field, we employ the $\mathfrak{Ansatz}$

$\psi=e^{\imath(\Omega t+Mz)}\begin{pmatrix}A(r)\\\ 0\\\ B(r)\\\
0\end{pmatrix},$ (6)

where $\Omega$ and $M$ are some constants. Substituting this
$\mathfrak{Ansatz}$ and 5-bein from (4) in Eqs. (1) and (2) and taking into
account (3), one can obtain the following set of the Einstein-Dirac equations:

$\displaystyle\frac{\xi^{\prime\prime}}{\xi}$ $\displaystyle=$
$\displaystyle-\frac{5}{12}\frac{\xi^{\prime
2}}{\chi^{2}}-\frac{5}{12}\frac{\xi^{2}}{\chi^{2}}\frac{\eta^{\prime
2}}{\eta^{2}}+\frac{\eta^{\prime 2}}{\eta^{2}}+\frac{1}{3}\frac{\phi^{\prime
2}}{\phi^{2}}+\frac{2}{3}\frac{\eta^{\prime}}{\eta}\frac{\phi^{\prime}}{\phi}-\frac{5}{3}\frac{\eta^{\prime}}{\eta}\frac{\chi^{\prime}}{\chi}+\frac{2}{3}\frac{\phi^{\prime}}{\phi}\frac{\chi^{\prime}}{\chi}+\frac{5}{6}\frac{\xi\eta^{\prime}\xi^{\prime}}{\eta\chi^{2}}-\frac{\eta^{\prime}}{\eta}\frac{\xi^{\prime}}{\xi}-2\frac{\xi^{\prime}}{\xi}\frac{\phi^{\prime}}{\phi}+\frac{\xi^{\prime}}{\xi}\frac{\chi^{\prime}}{\chi}$
(7) $\displaystyle-$
$\displaystyle\frac{1}{2}\frac{AB\left[\xi^{2}\eta^{\prime}-\eta\xi\xi^{\prime}+2\chi\left(\eta\chi^{\prime}-\chi\eta^{\prime}\right)\right]}{\eta\xi\chi}+\frac{1}{3}\Big{\\{}\frac{A^{2}\left[M\left(3\chi^{2}+\chi\xi-\xi^{2}\right)+\eta\left(\xi\left(\Omega-2\lambda\chi
B^{2}\right)-3\Omega\chi\right)\right]}{\eta\xi\chi}$ $\displaystyle+$
$\displaystyle\frac{B^{2}\left[\Omega\eta\left(\xi+3\chi\right)-M\left(\xi^{2}+\xi\chi-3\chi^{2}\right)\right]}{\eta\xi\chi}+\Lambda\Big{\\}},$
$\displaystyle\frac{\chi^{\prime\prime}}{\chi}$ $\displaystyle=$
$\displaystyle\frac{7}{12}\frac{\xi^{\prime
2}}{\chi^{2}}+\frac{7}{12}\frac{\xi^{2}}{\chi^{2}}\frac{\eta^{\prime
2}}{\eta^{2}}+\frac{1}{3}\frac{\phi^{\prime
2}}{\phi^{2}}+\frac{2}{3}\frac{\eta^{\prime}}{\eta}\frac{\phi^{\prime}}{\phi}-\frac{2}{3}\frac{\eta^{\prime}}{\eta}\frac{\chi^{\prime}}{\chi}-\frac{4}{3}\frac{\phi^{\prime}}{\phi}\frac{\chi^{\prime}}{\chi}-\frac{7}{6}\frac{\xi\eta^{\prime}\xi^{\prime}}{\eta\chi^{2}}$
(8) $\displaystyle-$
$\displaystyle\frac{1}{2}\frac{AB\left(\eta\xi^{\prime}-\xi\eta^{\prime}\right)}{\eta\chi}-\frac{1}{3}\left\\{\frac{A^{2}\left[-M\left(2\xi+\chi\right)+2\eta\left(\Omega+\lambda\chi
B^{2}\right)\right]}{\eta\chi}+\frac{B^{2}\left[2\Omega\eta+M\left(\chi-2\xi\right)\right]}{\eta\chi}-\Lambda\right\\},$
$\displaystyle\frac{\eta^{\prime\prime}}{\eta}$ $\displaystyle=$
$\displaystyle-\frac{5}{12}\frac{\xi^{\prime
2}}{\chi^{2}}-\frac{5}{12}\frac{\xi^{2}}{\chi^{2}}\frac{\eta^{\prime
2}}{\eta^{2}}+\frac{1}{3}\frac{\phi^{\prime
2}}{\phi^{2}}-\frac{4}{3}\frac{\eta^{\prime}}{\eta}\frac{\phi^{\prime}}{\phi}-\frac{2}{3}\left(\frac{\eta^{\prime}}{\eta}\frac{\chi^{\prime}}{\chi}-\frac{\phi^{\prime}}{\phi}\frac{\chi^{\prime}}{\chi}\right)+\frac{5}{6}\frac{\xi\eta^{\prime}\xi^{\prime}}{\eta\chi^{2}}$
(9) $\displaystyle-$
$\displaystyle\frac{1}{2}\frac{AB\left(\xi\eta^{\prime}-\eta\xi^{\prime}\right)}{\eta\chi}-\frac{1}{3}\left\\{\frac{A^{2}\left[M\left(\xi+2\chi\right)-\eta\left(\Omega-2\lambda\chi
B^{2}\right)\right]}{\eta\chi}-\frac{B^{2}\left[\Omega\eta+M\left(2\chi-\xi\right)\right]}{\eta\chi}-\Lambda\right\\},$
$\displaystyle\frac{\phi^{\prime\prime}}{\phi}$ $\displaystyle=$
$\displaystyle\frac{1}{12}\frac{\xi^{\prime
2}}{\chi^{2}}+\frac{1}{12}\frac{\xi^{2}}{\chi^{2}}\frac{\eta^{\prime
2}}{\eta^{2}}-\frac{2}{3}\frac{\phi^{\prime
2}}{\phi^{2}}-\frac{1}{3}\left(\frac{\eta^{\prime}}{\eta}\frac{\phi^{\prime}}{\phi}-\frac{\eta^{\prime}}{\eta}\frac{\chi^{\prime}}{\chi}+\frac{\phi^{\prime}}{\phi}\frac{\chi^{\prime}}{\chi}\right)-\frac{1}{6}\frac{\xi\eta^{\prime}\xi^{\prime}}{\eta\chi^{2}}$
(10) $\displaystyle-$
$\displaystyle\frac{1}{3}\left\\{\frac{B^{2}\left[M\left(\xi+\chi\right)-\Omega\eta\right]}{\eta\chi}-\frac{A^{2}\left[M\left(\chi-\xi\right)+\eta\left(\Omega-2\lambda\chi
B^{2}\right)\right]}{\eta\chi}-\Lambda\right\\},$ $\displaystyle A^{\prime}$
$\displaystyle=$
$\displaystyle-A\left[\frac{\left(\xi+2\chi\right)\eta^{\prime}}{4\eta\chi}-\frac{\xi^{\prime}}{4\chi}+\frac{\phi^{\prime}}{\phi}+\frac{\chi^{\prime}}{2\chi}-m\right]-2\lambda
A^{2}B+\Omega\frac{B}{\chi}-M\frac{B\left(\xi+\chi\right)}{\eta\chi},$ (11)
$\displaystyle B^{\prime}$ $\displaystyle=$ $\displaystyle
B\left[\frac{\left(\xi-2\chi\right)\eta^{\prime}}{4\eta\chi}-\frac{\xi^{\prime}}{4\chi}-\frac{\phi^{\prime}}{\phi}-\frac{\chi^{\prime}}{2\chi}-m\right]+2\lambda
AB^{2}-\Omega\frac{A}{\chi}+M\frac{A\left(\xi-\chi\right)}{\eta\chi},$ (12)

where the prime denotes differentiation with respect to the fifth coordinate
$r$. The above gravitational equations represent respectively the following
combinations of the Einstein equations (1):

$-\frac{1}{3}E_{\bar{0}\bar{0}}+\frac{2}{3}E_{\bar{1}\bar{1}}-\frac{2}{3}E_{\bar{3}\bar{3}}+E_{\bar{0}\bar{3}}=0,\,\frac{2}{3}E_{\bar{0}\bar{0}}+\frac{2}{3}E_{\bar{1}\bar{1}}+\frac{1}{3}E_{\bar{3}\bar{3}}=0,\,-\frac{1}{3}E_{\bar{0}\bar{0}}+\frac{2}{3}E_{\bar{1}\bar{1}}-\frac{2}{3}E_{\bar{3}\bar{3}}=0,\,-E_{\bar{0}\bar{0}}-E_{\bar{1}\bar{1}}+E_{\bar{3}\bar{3}}=0.$

This enabled us to write them in the form where each equation contains a
higher derivative only of one metric function. In turn, in addition to these
gravitational equations, we also have the constraint equation
[$(\bar{5}-\bar{5})$-component of the Einstein equations (1)]

$\displaystyle\frac{\xi^{2}\eta^{\prime
2}}{4\eta^{2}\chi^{2}}-\frac{\xi\eta^{\prime}\xi^{\prime}}{2\eta\chi^{2}}+\frac{\xi^{\prime
2}}{4\chi^{2}}+\frac{2\eta^{\prime}\phi^{\prime}}{\eta\phi}+\frac{\phi^{\prime
2}}{\phi^{2}}+\frac{\eta^{\prime}\chi^{\prime}}{\eta\chi}+2\frac{\phi^{\prime}\chi^{\prime}}{\phi\chi}$
$\displaystyle=\Lambda-2mAB+2\lambda
A^{2}B^{2}-\Omega\frac{A^{2}+B^{2}}{\chi}+M\frac{A^{2}\left(\xi-\chi\right)+B^{2}\left(\xi+\chi\right)}{\eta\chi},$
(13)

which contains only first derivatives of the metric functions. It will be used
below in assigning boundary conditions.

##  III Domain wall solutions

The equations (7)-(12) permit a number of solutions given below. To derive
these solutions, we will begin from the fact that in the neighbourhood of the
domain wall $r=0$ the solutions can be represented as a power series in $r$,

$\displaystyle\xi\approx\xi_{0}+\frac{\xi_{2}}{2}r^{2},\quad\chi\approx\chi_{0}+\frac{\chi_{2}}{2}r^{2},\quad\eta\approx\eta_{0}+\frac{\eta_{2}}{2}r^{2},\quad\phi\approx\phi_{0}+\frac{\phi_{2}}{2}r^{2},$
(14) $\displaystyle A\approx A_{0}+A_{1}r,\quad B\approx B_{0}+B_{1}r.$ (15)

These expansions will be used as boundary conditions in solving the equations
(7)-(12).

###  III.1 The case of $\Omega=M=0$

Consider first the simplest case of static solutions with $\Omega=M=0$. For
this case, the Dirac equations (11) and (12) can be integrated analytically in
the form

$AB=\frac{C}{\chi\eta\phi^{2}},$

where $C$ is an integration constant. In this case, it can be shown that
solutions for the metric functions are linearly dependent:

$\xi=\alpha\chi,\quad\eta=\beta\chi,\quad\phi=\gamma\chi,$ (16)

where $\alpha,\beta$, and $\gamma$ are some constants, and $0<\alpha<1$ to
ensure that the signature of the metric (5) remains unchanged. As a result,
the Einstein equations (7)-(10) reduce to one equation

$\frac{\chi^{\prime\prime}}{\chi}+\left(\frac{\chi^{\prime}}{\chi}\right)^{2}+\frac{2\tilde{C}^{2}\lambda}{3\chi^{8}}-\frac{\Lambda}{3}=0,$
(17)

and the constraint equation (II) yields

$6\left(\frac{\chi^{\prime}}{\chi}\right)^{2}+\frac{2\tilde{C}m}{\chi^{4}}-\frac{2\tilde{C}^{2}\lambda}{\chi^{8}}-\Lambda=0,$
(18)

where the new constant $\tilde{C}=C/\left(\beta\gamma^{2}\right)$ is
introduced. Taking into account the boundary conditions (14), one can find the
following particular solution of the equation (17) that also satisfies the
constraint equation (18) (this is achieved by suitably adjusting the
integration constant $\tilde{C}$):

$\chi=\left\\{-\frac{\tilde{C}\,m\left[\sqrt{1-\frac{2\lambda\Lambda}{m^{2}}}\cosh{\left(2\sqrt{\frac{2\Lambda}{3}}r\right)}-1\right]}{\Lambda}\right\\}^{1/4}\quad\text{with}\quad\tilde{C}=\frac{m\chi_{0}^{4}}{2\lambda}\left(1+\sqrt{1-\frac{2\lambda\Lambda}{m^{2}}}\right).$

It is evident that this solution is symmetric with respect to the domain wall
located at $r=0$. (Notice here that, using this solution, one can find the
spinor functions $A$ and $B$ in an analytical form; however, since these
expressions are too cumbersome, we do not show them here.) The corresponding
solutions are exemplified in Fig. 1. It is worth noting here that, in the case
under consideration, the solutions for the spinor functions $A$ and $B$ are
asymmetric, while their product $AB\equiv\tilde{C}/\chi^{4}$ is symmetric with
respect to the domain wall (see the right panel of Fig. 1).

Figure 1: Examples of the solutions with $\Omega=M=0$ and with $\Lambda=1$,
$\lambda=-1$, $m=0.2$. The solutions for the metric functions $\eta$ and
$\phi$ follow from Eq. (16) for an appropriate choice of the arbitrary
parameters $\beta$ and $\gamma$.

Asymptotically (as $r\to\pm\infty$), the spinor functions $A$ and $B$ tend to
zero, and the metric functions exhibit an exponential growth. Accordingly, the
scalar curvature

$\displaystyle R$ $\displaystyle=$
$\displaystyle\frac{8}{3}\Lambda-\frac{16}{3}\lambda
A^{2}B^{2}+\frac{2}{3}\frac{\Omega}{\chi}\left(A^{2}+B^{2}\right)-\frac{2}{3}\frac{M}{\eta\chi}\left[A^{2}\left(\xi-\chi\right)+B^{2}\left(\xi+\chi\right)\right]+\frac{1}{6}\frac{\xi^{2}\eta^{\prime
2}}{\eta^{2}\chi^{2}}+\frac{1}{6}\frac{\xi^{\prime
2}}{\chi^{2}}+\frac{2}{3}\frac{\phi^{\prime 2}}{\phi^{2}}$ $\displaystyle+$
$\displaystyle\left(\frac{4}{3}\frac{\phi^{\prime}}{\phi}-\frac{1}{3}\frac{\xi\xi^{\prime}}{\chi^{2}}\right)\frac{\eta^{\prime}}{\eta}+\left(\frac{4}{3}\frac{\phi^{\prime}}{\phi}+\frac{2}{3}\frac{\eta}{\eta^{2}}\right)\frac{\chi^{\prime}}{\chi}$

behaves asymptotically as $R\to 10/3\Lambda$; for $\Lambda>0$ under
consideration, this corresponds to an asymptotically anti-de Sitter spacetime.

Particularly simple is the case of a linear spinor field (i.e., when
$\lambda=0$). For this case, Eq. (17) has the following particular solution:

$\chi=\chi_{0}\sqrt{\cosh\left(\sqrt{\frac{2\Lambda}{3}}r\right)}.$

Substituting this in the Dirac equations (11) and (12) (which are now
separated for the functions $A$ and $B$) and taking into account the need to
satisfy the constraint equation (II), one has the following solutions for the
spinor fields (asymmetric with respect to the domain wall):

$A=A_{0}e^{mr}\operatorname{sech}{\left(\sqrt{\frac{2\Lambda}{3}}r\right)},\quad
B=\frac{\Lambda}{2A_{0}m}e^{-mr}\operatorname{sech}{\left(\sqrt{\frac{2\Lambda}{3}}r\right)}.$

Asymptotically, on both sides of the domain wall, there is the following
behavior of the spinor fields:

$\text{as}\quad r\to\pm\infty:A\to
2A_{0}\exp{\left[\mp\left(\sqrt{\frac{2\Lambda}{3}}\mp m\right)r\right]},\quad
B\to\frac{\Lambda}{A_{0}m}\exp{\left[\mp\left(\sqrt{\frac{2\Lambda}{3}}\pm
m\right)r\right]}.$

Hence we see that regular solutions for the spinor fields are possible only if

$\sqrt{\frac{2\Lambda}{3}}-m>0.$

###  III.2 The case of nonzero $\Omega$ and $M$

When the free parameters $\Omega$ and $M$ are nonzero, solutions for the
metric functions are not already symmetric with respect to the domain wall. In
this case regular solutions do exist both for simultaneously nonzero $\Omega$
and $M$ and in the case when one of these parameters is zero. Using the
boundary conditions (14) and (15), the numerical solutions to the equations
(7)-(12) are exemplified in Fig. 2.

Figure 2: Examples of the solutions with $\Lambda=1$, $\Omega=0.1$, $m=0.2$,
and $M=0.05$ for the nonlinear spinor field with the nonlinearity parameter
$\lambda=-1$.

In the case of a linear spinor field (i.e., when $\lambda=0$), there are also
regular solutions shown in Fig. 3.

Figure 3: Examples of the solutions with $\Lambda=1$, $\Omega=0.1$, $m=0.5$,
and $M=0.05$ for the linear spinor field.

It is seen from the graphs given in Figs. 2 and 3 that, as in the case of
$\Omega=M=0$ considered above, we deal with an asymptotically anti-de Sitter
spacetime.

###  III.3 The case without spinor field

In the absence of the spinor field (but when $\Lambda$ is present) there are
no regular solutions to Eqs. (7)-(12).

###  III.4  Three-dimensional energy density of the system

In this subsection we consider a question of the energy per unit volume of the
domain wall, which is defined as

$\displaystyle
E_{\text{3D}}=\int_{-\infty}^{\infty}T_{0}^{0}\sqrt{{}^{5}g}dr\equiv\frac{1}{2}\int_{-\infty}^{\infty}\frac{\phi^{2}}{\chi}\Big{\\{}B^{2}\left[M\xi\left(\xi+\chi\right)-\Omega\eta\left(\xi+2\chi\right)\right]$
(19) $\displaystyle\hskip
139.41832pt+A^{2}\left[M\xi\left(-\xi+\chi\right)-\Omega\eta\left(-\xi+2\Omega\chi\right)\right]+4\lambda
A^{2}B^{2}\eta\chi^{2}+AB\eta\left(\xi\chi^{\prime}-\chi\xi^{\prime}\right)\Big{\\}}dr.$

It is evident that for a physically realistic system this quantity must be
finite. Taking into account that asymptotically the metric functions diverge
exponentially (an anti-de Sitter spacetime) and the spinor functions decrease
exponentially, it is necessary to follow the behavior of the expression in the
integrand depending on the values of the free parameters of the system. To do
this, let us write down the corresponding asymptotic (as $r\to\infty$)
expressions for the metric functions and spinor fields:

$\displaystyle\xi\to\xi_{\infty}e^{\sqrt{\Lambda/6}r},\quad\chi\to\chi_{\infty}e^{\sqrt{\Lambda/6}r},\quad\eta\to\eta_{\infty}e^{\sqrt{\Lambda/6}r},\quad\phi\to\phi_{\infty}e^{\sqrt{\Lambda/6}r},$
(20) $\displaystyle A\to
C_{1}e^{-\left(m+\sqrt{3\Lambda/2}\right)r}+C_{2}e^{-\left(\sqrt{2\Lambda/3}-m\right)r},\quad
B\to\tilde{C}_{1}e^{-\left(m+\sqrt{2\Lambda/3}\right)r},$ (21)

where $\xi_{\infty},\chi_{\infty},\eta_{\infty},\phi_{\infty}$ are some
constants, $C_{1}$ and $C_{2}$ are integration constants and

$\tilde{C}_{1}=C_{1}\frac{\left(12m+\sqrt{6\Lambda}\right)\eta_{\infty}\xi_{\infty}}{6\left[\Omega\eta_{\infty}-M\left(\xi_{\infty}+\chi_{\infty}\right)\right]}.$
(22)

It is seen from Eq. (21) that in the general case asymptotically decaying
solution for the function $A$ can exist only when

$0<m<\sqrt{\frac{2}{3}\Lambda}.$ (23)

Next, substituting (20) and (21) in Eq. (19), we have the following asymptotic
(as $r\to\infty$) form for the expression in the integrand:

$\displaystyle\frac{1}{2}\frac{\phi_{\infty}^{2}}{\chi_{\infty}}$
$\displaystyle\Big{\\{}C_{2}^{2}\left[\Omega\eta_{\infty}\left(\xi_{\infty}-2\chi_{\infty}\right)+M\xi_{\infty}\left(\chi_{\infty}-\xi_{\infty}\right)\right]e^{-\left(\sqrt{\Lambda/6}-2m\right)r}$
$\displaystyle-\tilde{C}_{1}^{2}\left[\Omega\eta_{\infty}\left(\xi_{\infty}+2\chi_{\infty}\right)-M\xi_{\infty}\left(\chi_{\infty}+\xi_{\infty}\right)\right]e^{-\left(\sqrt{\Lambda/6}+2m\right)r}\Big{\\}}.$
(24)

(Similar expression can also be obtained for the case of $r\to-\infty$.) Hence
we see that to obtain an asymptotically decaying expression, it is necessary
that

$m<\frac{1}{2}\sqrt{\frac{\Lambda}{6}}.$

Since this condition is stronger than (23), the resulting restriction on the
system parameters is therefore

$0<m<\frac{1}{2}\sqrt{\frac{\Lambda}{6}}.$

When this condition is fulfilled, the expression in the integrand of Eq. (19)
will decrease asymptotically, and the corresponding integral will be finite.

One further remark may be made, to complete this subsection. The formulas
obtained above are invalid for the case considered in Sec. III.1 where
$\Omega=M=0$ [cf. Eqs. (22) and (24)]. However, proceeding in a similar
manner, one can show that the energy of the configuration from Sec. III.1 is
also finite.

###  III.5 Angular momentum density of the domain wall

According to the definition of the angular momentum five-tensor,

$M_{AB}=x_{A}T^{t}_{\phantom{t}B}-x_{B}T^{t}_{\phantom{t}A},$

one can introduce the angular momentum density three-vector

$L_{i}=\frac{1}{2}\epsilon_{ijk}M^{jk},$

as well as the “second angular momentum density three-vector”

$\mathcal{L}_{i}=M_{i5}.$

Using the components of the energy-momentum tensor (A)-(49) given in the
Appendix A, one can find the following expressions for the components of the
angular momentum density $L_{x,y}$ and the “second angular momentum density”
$\mathcal{L}_{z}$:

$\displaystyle L_{x,y}=$ $\displaystyle\begin{pmatrix}-y\\\
x\end{pmatrix}T^{t}_{\phantom{t}z},$ (25) $\displaystyle\mathcal{L}_{z}=$
$\displaystyle-rT^{t}_{\phantom{t}z}.$ (26)

In cylindrical coordinates, Eq. (25) yields

$L_{\varphi}=\rho
T^{t}_{\phantom{t}z}\quad\text{with}\quad\rho=\sqrt{x^{2}+y^{2}}.$

The typical spatial distributions of the component $T^{t}_{\phantom{t}z}$
[which is given by the expression (45)] appearing in Eqs. (25) and (26) are
plotted in Fig. 4. Notice that for the case considered in Sec. III.1 where
$\Omega=M=0$ and the metric functions are linearly dependent, the expression
(45) is identically zero.

The physical reason for the appearance of the angular momenta $L_{\varphi}$
and $\mathcal{L}_{z}$ is the existence of the current density
$j^{A}=\bar{\psi}\gamma^{A}\psi$, which has the following nonvanishing
componets:

$j^{t}=\frac{1}{\chi}\left(A^{2}+B^{2}\right),\quad
j^{z}=\frac{A^{2}}{\eta}\left(1-\frac{\xi}{\chi}\right)-\frac{B^{2}}{\eta}\left(1+\frac{\xi}{\chi}\right).$

Figure 4: The distributions of the component $T^{t}_{\,\,z}$ for the solutions
represented in Fig. 2 (for the nonlinear spinor field; shown by the solid
line) and in Fig. 3 (for the linear spinor field; shown by the dashed line).

##  IV Spin of test fermions on the domain wall

In this section we consider the properties of spin operators of test fermions
confined on the domain wall. In order to distinguish these fermions from the
spinor field $\psi$ supporting the domain wall, we denote them as $\chi$.
(Note that in this section we use the letters $\chi,\eta$, and $\phi$ to
denote the spinor functions. We have used above the same symbols for the
metric functions, but this should not lead to misunderstanding.) The
consideration given below can also be applied to any domain wall, not
necessarily to the domain walls obtained above.

In a four-dimensional spacetime, three-dimensional spin operators can be
represented by one three-vector $\Sigma_{i}$ dual to the $i,j=1,2,3$
components of the generators of the Lorentz algebra,

$\Sigma_{i}=\frac{1}{2}\epsilon_{ijk}\sigma_{jk}=\frac{\imath}{4}\epsilon_{ijk}\left[\gamma_{j},\gamma_{k}\right].$

This construction can be adopted to a five-dimensional spacetime where the
domain wall under consideration is embedded. In the five-dimensional
spacetime, the generators of the Lorentz algebra are

$\Sigma^{ab}=\frac{\imath}{4}\left[\gamma^{a},\gamma^{b}\right]\quad\text{with}\quad
a,b=0,1,2,3,5.$

Generalising the construction of the spin in a four-dimensional spacetime,
spin operators in a five-dimensional spacetime will be the matrices

$\Sigma^{\alpha\beta}=\frac{\imath}{4}\left[\gamma^{\alpha},\gamma^{\beta}\right]\quad\text{with}\quad\alpha,\beta=1,2,3,5.$

Unlike the four-dimensional spacetime, it looks impossible to dualize these
matrices; for this reason, the spin operators form now not a vector, but a
spatial tensor $\Sigma^{\alpha\beta}$. Being antisymmetric, this tensor is
equivalent to two three-vectors

$\Sigma_{i}=\frac{1}{2}\epsilon_{ijk}\Sigma_{jk},\quad\mathcal{S}_{i}=\Sigma_{5i}$

with $i,j,k=1,2,3$. The operators $\Sigma_{i}$ have the same form as the
standard spin operators in a four-dimensional spacetime. In turn, the
operators $\mathcal{S}_{i}$ have the form

$\mathcal{S}_{i}=\frac{1}{2}\begin{pmatrix}0&\sigma_{i}\\\
\sigma_{i}&0\end{pmatrix}.$

The commutation relations for these operators are

$\displaystyle\left[\Sigma_{i},\Sigma_{j}\right]=$
$\displaystyle\imath\epsilon_{ijk}\Sigma_{k},$ (27)
$\displaystyle\left[\mathcal{S}_{i},\mathcal{S}_{j}\right]=$
$\displaystyle\imath\epsilon_{ijk}\Sigma_{k},$ (28)
$\displaystyle\left[\Sigma_{i},\mathcal{S}_{j}\right]=$
$\displaystyle\imath\epsilon_{ijk}\mathcal{S}_{k}.$ (29)

The operators $\Sigma_{i}$ are standard operators of the projection of the
spin on the $x^{i}$-axis. Let us call the operators $\mathcal{S}_{i}$ as the
operators of the projection of the “second spin” on the $x^{i}$-axis.
Consistent with the commutation relations (28) and (29), it is seen that the
projections of the “second spin” on the $x^{i}$ and $x^{j}$ axes (with $i\neq
j$) cannot be measured simultaneously. Also, it is seen from Eq. (29) that
eigenvalues of the operators $\Sigma_{i}$ and $\mathcal{S}_{j}$ can be
measured simultaneously only if $i=j$.

The eigenvalue problem for the operator of the “second spin” is

$\mathcal{S}_{i}\chi_{i,n,\pm}=s_{i,\pm}\chi_{i,n,\pm},$

where $s_{i,\pm}$ are eigenvalues and $\chi_{i,n,\pm}$ are eigenspinors of the
operator of the “second spin”, and the index $i$ describes the components of
the “second spin” along the $x,y,z$ axes, the index $n$ corresponds to the
number of the eigenspinor, and $\pm$ corresponds to the projection of the
“second spin” on the $i$-axis. Solving this problem, we get the following
eigenvalues and eigenspinors:

$\displaystyle s_{x,-}=$
$\displaystyle-\frac{1}{2},\chi^{T}_{x,1,-}=\left\\{-1,0,0,1\right\\},\chi^{T}_{x,2,-}=\left\\{0,-1,1,0\right\\},$
(30) $\displaystyle s_{x,+}=$
$\displaystyle+\frac{1}{2},\chi^{T}_{x,1,+}=\left\\{1,0,0,1\right\\},\chi^{T}_{x,2,+}=\left\\{0,1,1,0\right\\},$
(31) $\displaystyle s_{y,-}=$
$\displaystyle-\frac{1}{2},\chi^{T}_{y,1,-}=\left\\{\imath,0,0,1\right\\},\chi^{T}_{y,2,-}=\left\\{0,-\imath,1,0\right\\},$
(32) $\displaystyle s_{y,+}=$
$\displaystyle+\frac{1}{2},\chi^{T}_{y,1,+}=\left\\{-\imath,0,0,1\right\\},\chi^{T}_{y,2,+}=\left\\{0,\imath,1,0\right\\},$
(33) $\displaystyle s_{z,-}=$
$\displaystyle-\frac{1}{2},\chi^{T}_{z,1,-}=\left\\{0,1,0,1\right\\},\chi^{T}_{z,2,-}=\left\\{-1,0,1,0\right\\},$
(34) $\displaystyle s_{z,+}=$
$\displaystyle+\frac{1}{2},\chi^{T}_{z,1,+}=\left\\{0,-1,0,1\right\\},\chi^{T}_{z,2,+}=\left\\{1,0,1,0\right\\}.$
(35)

Note that the eigenspinors are orthogonal each other (for simplicity, we
omitted the indices $i$ and $\pm$):

$\bar{\chi}_{1}\chi_{2}=0.$

###  IV.1 Eigenspinors of the operator of the “second spin” and the Bell
states

In this subsection we show that some eigenspinors of the operator of the
“second spin” (30)-(33) are the Bell states. To do this, let us introduce
quantum states $\left|0\right\rangle$ and $\left|1\right\rangle$ for some
quantum system that can take only two possible states. For example, this can
be the projection of the spin on the $z$-axis. These quantum states can be
written in the form of the following Weyl spinors:

$\left|0\right\rangle=\begin{pmatrix}0\\\
1\end{pmatrix},\left|1\right\rangle=\begin{pmatrix}1\\\ 0\end{pmatrix}.$

Then, apart from a normalization factor, the eigenspinors (30)-(35) can be
represented as

$\displaystyle\chi_{x,1,\pm}=$
$\displaystyle\left|0\right\rangle\otimes\left|0\right\rangle\pm\left|1\right\rangle\otimes\left|1\right\rangle,$
(36) $\displaystyle\chi_{x,2,\pm}=$
$\displaystyle\left|0\right\rangle\otimes\left|1\right\rangle\pm\left|1\right\rangle\otimes\left|0\right\rangle,$
(37) $\displaystyle\chi_{y,1,\pm}=$
$\displaystyle\left|0\right\rangle\otimes\left|0\right\rangle\mp\imath\left|1\right\rangle\otimes\left|1\right\rangle,$
(38) $\displaystyle\chi_{y,2,\pm}=$
$\displaystyle\left|0\right\rangle\otimes\left|1\right\rangle\pm\imath\left|1\right\rangle\otimes\left|0\right\rangle,$
(39) $\displaystyle\chi_{z,1,\pm}=$
$\displaystyle\mp\left|1\right\rangle\otimes\left|0\right\rangle+\left|0\right\rangle\otimes\left|0\right\rangle=\left(\mp\left|1\right\rangle+\left|0\right\rangle\right)\otimes\left|0\right\rangle,$
(40) $\displaystyle\chi_{z,2,\pm}=$
$\displaystyle\pm\left|1\right\rangle\otimes\left|1\right\rangle+\left|0\right\rangle\otimes\left|1\right\rangle=\left(\pm\left|1\right\rangle+\left|0\right\rangle\right)\otimes\left|1\right\rangle.$
(41)

The quantum states (36)-(39) are called the Bell states or sometimes the EPR
states or EPR pairs (after Bell or Einstein, Podolsky, and Rosen, who first
pointed out the strange properties of such states). Strictly speaking, the
entangled states (36)-(39) differ from the Bell states by some factors on the
right-hand sides of Eqs. (36)-(39). In quantum calculations, the Bell states
describe an entangled pair of qubits. The difference of the quantum states
(36)-(39) from those given by Eqs. (40)-(41) is that the former cannot be
represented as a tensor product of states, while the latter, as one sees from
the right-hand sides of Eqs. (40)-(41), can be represented as tensor products
of some quantum states.

It must be mentioned here that all the quantum states of the spin $\Sigma_{i}$
can be represented as tensor products:

$\displaystyle\chi_{x,\pm}=$
$\displaystyle\left(\left|0\right\rangle+\left|1\right\rangle\right)\otimes\left(\left|0\right\rangle\pm\left|1\right\rangle\right),\quad\chi_{y,\pm}=\left(\left|0\right\rangle+\left|1\right\rangle\right)\otimes\left(\left|0\right\rangle\pm\imath\left|1\right\rangle\right),$
$\displaystyle\chi_{y,+}=$
$\displaystyle\left(\left|0\right\rangle+\left|1\right\rangle\right)\otimes\left|1\right\rangle,\hskip
42.67912pt\chi_{y,-}=\left(\left|0\right\rangle+\left|1\right\rangle\right)\otimes\left|0\right\rangle.$

###  IV.2 Nonrelativistic limit for the test fermions

In order to understand what extra features (compared with the four-dimensional
case) appear for the test fermions living on the domain wall embedded in the
five-dimensional bulk, we consider the nonrelativistic limit of the five-
dimensional Dirac equation, i.e., the Pauli equation in the five-dimensional
bulk spacetime. In such case we should rewrite the Dirac equation for a flat
five-dimensional spacetime and take into account the interaction with a
gravitational field which is now described by the potential energy $m\Phi$,
where $m$ is the mass of a particle and $\Phi$ is the Newtonian gravitational
potential.

In order to obtain the nonrelativistic limit for the five-dimensional Dirac
equation (the Pauli equation), we proceed in the standard way and write the
Dirac equation in the following form (for clarity, in this subsection we
resurrect $\hbar$ and $c$ in the equations):

$\imath\hbar\partial_{t}\begin{pmatrix}\phi\\\
\eta\end{pmatrix}=c\begin{pmatrix}\sigma_{i}\hat{\Pi}_{i}\eta\\\
\sigma_{i}\hat{\Pi}_{i}\phi\end{pmatrix}+\imath
c\hat{\Pi}_{5}\begin{pmatrix}\eta\\\
-\phi\end{pmatrix}+mc^{2}\begin{pmatrix}\phi\\\
-\eta\end{pmatrix}+\left(eA_{0}+m\Phi\right)\begin{pmatrix}\phi\\\
\eta\end{pmatrix},$ (42)

where $\hat{\Pi}_{i}=\hat{p}_{i}-e/cA_{i}$, $\hat{p}_{i}$ being the three-
dimensional momentum operator, $\hat{\Pi}_{5}=\hat{p}_{5}-e/cA_{5}$, and
$A_{B}=\left(A_{0},A_{i},A_{5}\right)$ is the electromagnetic five-potential.
Also, as in the four-dimensional case, the four-component spinor $\chi$ is
again decomposed into two two-component spinors,

$\chi=\begin{pmatrix}\phi\\\ \eta\end{pmatrix}.$

The relativistic energy of a particle described by Eq. (42) contains also its
rest energy $mc^{2}$. In arriving at the nonrelativistic approximation, this
energy must be excluded by introducing a new function $\chi\to\chi
e^{-imc^{2}t/\hbar}$. Next, using the same nonrelativistic approximations as
those employed in the four-dimensional case,

$\left|\imath\hbar\dot{\eta}\right|,\left|eA_{0}\eta\right|,\text{ and
}\left|m\Phi\eta\right|\ll\left|mc^{2}\eta\right|,$

one can obtain from the lower component of Eq. (42) the relation

$\eta=\frac{\sigma_{i}\hat{\Pi}_{i}-\imath\hat{\Pi}_{5}}{2mc}\phi.$

In this way, after substituting of this into the upper part of the Eq. (42),
we finally arrive at the required Pauli equation for the five-dimensional
spacetime:

$\imath\hbar\dot{\phi}=\left[\frac{1}{2m}\left(\hat{\vec{p}}-\frac{e}{c}\vec{A}\right)^{2}+\frac{1}{2m}\left(\hat{p}_{5}-\frac{e}{c}A_{5}\right)^{2}-\frac{e\hbar}{2mc}\vec{\sigma}\left(\vec{H}+\vec{\mathcal{H}}\right)+\left(eA_{0}+m\Phi\right)\right]\phi,$
(43)

where the vector $\vec{H}\equiv H_{i}=(1/2)\epsilon_{ijk}F^{jk}$ is the
magnetic field and the vector $\vec{\mathcal{H}}\equiv\mathcal{H}_{i}=F_{i5}$
is the “second magnetic field”.

The physical meaning of the new terms appearing in Eq. (43) (compared with the
four-dimensional Pauli equation) is as follows. Because of the presence of the
second term in the square brackets, there are quantum corrections to the
motion of a test fermion which is located on the domain wall embedded in the
bulk. These corrections are not associated with the spin of the fermion. In
turn, the presence of the term with $\vec{\mathcal{H}}$ results in quantum
corrections to the motion of the particle which are due to the presence of the
spin. This term describes the potential energy of a particle with a spin in
the “second magnetic field.” Hence, if the gradient of this field differs from
zero, trajectories of particles possessing different projections of a spin on
some direction in the presence of the “second magnetic field” will be
different from one another (the Stern-Gerlach effect).

Thus, if the motion of a test fermion takes place on the domain wall, only the
“second magnetic field” $\vec{\mathcal{H}}$ in the five-dimensional Pauli
equation (43) will lead to the corrections associated with the fact that our
world is embedded as a domain wall in the five-dimensional spacetime. This
correction allows the possibility of an experimental verification of the
existence of extra dimensions.

##  V Conclusion

In the first part of the present paper, we have considered the self-consistent
set of the Einstein-Dirac equations and shown that there exist plane symmetric
solutions for one gravitating classical spinor field. These solutions can be
treated as describing a thick domain wall embedded in a five-dimensional
spacetime.

The second part of the paper is devoted to exploring the properties and
behavior of test fermions localized on any domain wall. In a five-dimensional
spacetime, we cannot introduce the concept of a spin vector since the
procedure of dualization of the spatial part of the spin tensor results in a
tensor, and not in a vector. Nevertheless, the spatial part of the five-
dimensional spin tensor can be represented as some combination of two three-
vectors. The first of these vectors corresponds to an ordinary spin, and we
referred to the second one as the “second spin”. It was shown that eigenvalues
of the operator of the “second spin” are also $\hbar/2$, and some eigenspinors
are the Bell states.

To study the behavior of test fermions, we have obtained a nonrelativistic
approximation for the five-dimensional Dirac equation, which is a natural
generalization of the Pauli equation containing now additional terms that
describe (i) the presence of the component $A_{5}$ of the electromagnetic
potential; and (ii) the interaction of a spin-$1/2$ particle with the
components $F_{i5}$ of the electromagnetic field five-tensor, which we called
the “second magnetic field.” The presence of the term with the component
$A_{5}$ of the electromagnetic potential must lead to the appearance of the
Aharonov-Bohm effect; however, to register this effect, it is necessary to
escape beyond the domain wall, i.e., one has to have a solenoid directed along
the fifth dimension. By contrast, the effects associated with the presence of
the “second magnetic field” can be measured directly, since this “second
magnetic field” interacts with the spin directly.

Summarizing the results obtained,

  * •

We have found regular symmetric and asymmetric thick domain wall solutions
supported by one classical spinor field. In doing so, we have examined both
linear and nonlinear spinor fields by choosing different values of the free
parameters of the system. Note that, as the numerical calculations indicate,
for the regular solutions to exist, the presence of the cosmological constant
is necessary.

  * •

We have studied the properties of these domain wall solutions which depend on
the presence or absence of the nonlinearity and mass term of the spinor field.
It is shown that the nonlinearity of the spinor field brings no qualitative
changes to the behavior of the solutions, although the numerical values of the
fields are quite different.

  * •

We have demonstrated that there are the nonvanishing components $L_{\varphi}$
and $\mathcal{L}_{z}$ of the angular momenta density associated with the
current of the spinor field. The presence of the component $L_{\varphi}$
allows the possibility of an experimental verification of the existence of the
domain wall by using the spin-orbit interaction.

  * •

The behavior and properties of test fermions living on any domain wall have
been investigated:

    * –

It has been shown that the spatial part of the five-dimensional spin tensor
can be represented as a combination of two vectors, the first of which is an
ordinary spin, and the second vector can be called the “second spin.”

    * –

The eigenvalues ($\hbar/2$) and eigenspinors for the “second spin” have been
found. It has been demonstrated that some eigenspinors are the Bell states.

    * –

The nonrelativistic approximation for the five-dimensional Dirac equation (the
Pauli equation) has been derived.

    * –

It has been shown that the Pauli equation obtained contains extra terms
associated with the fact that the domain wall is embedded in a five-
dimensional bulk. One of these terms is related to the appearance of the
component $A_{5}$ in the Pauli equation, and the second one is due to the fact
that the spin interacts with the components $F_{i5}$ of the electromagnetic
field five-tensor (one can call it the “second magnetic field”).

    * –

It has been demonstrated that the interaction of the “second magnetic field”
with the spin results in observational effects which enable one to verify
experimentally the hypothesis of the existence of extra dimensions.

## Acknowledgments

This research has been funded by the Science Committee of the Ministry of
Science and Higher Education of the Republic of Kazakhstan (Grant No.
AP14869140, “The study of QCD effects in non-QCD theories”). We are also
grateful to V. Ivashchuk for fruitful discussions.

##  Appendix A The components of the energy-momentum tensor

For the metric (5) and $\mathfrak{Ansatz}$ (6), the mixed energy-momentum
tensor (3) yields the following nonzero components:

$\displaystyle T^{t}_{\phantom{t}t}=$ $\displaystyle 2\lambda A^{2}B^{2}$
$\displaystyle+\frac{A^{2}}{2\chi}\left[\Omega\left(\frac{\xi}{\chi}-2\right)+M\left(\frac{\xi}{\eta}-\frac{\xi^{2}}{\eta\chi}\right)\right]+\frac{AB}{2}\left(\frac{\xi\chi^{\prime}}{\chi^{2}}-\frac{\xi^{\prime}}{\chi}\right)+\frac{B^{2}}{2\chi}\left[M\left(\frac{\xi}{\eta}+\frac{\xi^{2}}{\eta\chi}\right)-\Omega\left(\frac{\xi}{\chi}+2\right)\right],$
(44) $\displaystyle T^{t}_{\phantom{t}z}=$
$\displaystyle\frac{A^{2}}{2\chi}\left[\Omega\frac{\eta}{\chi}-M\left(1+\frac{\xi}{\chi}\right)\right]+AB\frac{\eta}{2\chi}\left(\frac{\chi^{\prime}}{\chi}-\frac{\eta^{\prime}}{\eta}\right)-\frac{B^{2}}{2\chi}\left[\Omega\frac{\eta}{\chi}+M\left(1-\frac{\xi}{\chi}\right)\right],$
(45) $\displaystyle T^{x}_{\phantom{x}x}=$ $\displaystyle
T^{y}_{\phantom{y}y}=2\lambda A^{2}B^{2},$ (46) $\displaystyle
T^{z}_{\phantom{z}t}=$ $\displaystyle
A^{2}\frac{(\xi-\chi)^{2}}{2\eta^{2}\chi}\left[M\left(1+\frac{\xi}{\chi}\right)-\Omega\frac{\eta}{\chi}\right]+AB\frac{\xi}{2\eta\chi}\left[\eta^{\prime}\left(\frac{\chi^{2}}{\eta\xi}-\frac{\xi}{\eta}\right)-\chi^{\prime}\left(\frac{\xi}{\chi}+\frac{\chi}{\xi}\right)+2\xi^{\prime}\right]$
$\displaystyle+$ $\displaystyle
B^{2}\frac{(\xi+\chi)^{2}}{2\eta^{2}\chi}\left[M\left(1-\frac{\xi}{\chi}\right)+\Omega\frac{\eta}{\chi}\right],$
(47) $\displaystyle T^{z}_{\phantom{z}z}=$ $\displaystyle 2\lambda A^{2}B^{2}$
$\displaystyle+\frac{A^{2}}{2\eta}\left[M\left(\frac{\xi^{2}}{\chi^{2}}+\frac{\xi}{\chi}-2\right)-\Omega\frac{\eta\xi}{\chi^{2}}\right]+AB\frac{\xi}{2\chi}\left(\frac{\xi^{\prime}}{\xi}-\frac{\chi^{\prime}}{\chi}\right)+\frac{B^{2}}{2\eta}\left[M\left(-\frac{\xi^{2}}{\chi^{2}}+\frac{\xi}{\chi}+2\right)+\Omega\frac{\eta\xi}{\chi^{2}}\right],$
(48) $\displaystyle T^{r}_{\phantom{r}r}=$ $\displaystyle 2\lambda
A^{2}B^{2}+BA^{\prime}-AB^{\prime}+AB\frac{\xi}{2\chi}\left(\frac{\eta^{\prime}}{\eta}-\frac{\xi^{\prime}}{\xi}\right).$
(49)

## References

  * [1] F. Finster, J. Smoller, and S. T. Yau, “Particle-like solutions of the Einstein-Dirac equations,” Phys. Rev. D 59, 104020 (1999).
  * [2] F. Finster, J. Smoller, and S. T. Yau, “Particle-like solutions of the Einstein-Dirac-Maxwell equations,” Phys. Lett. A 259, 431 (1999).
  * [3] B. Saha and T. Boyadjiev, “Bianchi type I cosmology with scalar and spinor fields,” Phys. Rev. D 69, 124010 (2004).
  * [4] B. Saha, “Spinor field and accelerated regimes in cosmology,” Grav. Cosmol. 12, 215 (2006).
  * [5] B. Saha, “Nonlinear spinor field in Bianchi type-I cosmology: Inflation, isotropization, and late time acceleration,” Phys. Rev. D 74, 124030 (2006).
  * [6] B. Saha, “Spinor field with induced nonlinearity in Bianchi VI cosmology: Exact and numerical solutions,” Grav. Cosmol. 16, 160 (2010).
  * [7] C. A. R. Herdeiro, A. M. Pombo, and E. Radu, “Asymptotically flat scalar, Dirac and Proca stars: discrete vs. continuous families of solutions,” Phys. Lett. B 773, 654 (2017).
  * [8] V. Dzhunushaliev and V. Folomeev, Dirac stars supported by nonlinear spinor fields, Phys. Rev. D 99, 084030 (2019).
  * [9] V. Dzhunushaliev and V. Folomeev, Dirac star in the presence of Maxwell and Proca fields, Phys. Rev. D 99, 104066 (2019).
  * [10] C. Herdeiro, I. Perapechka, E. Radu, and Y. Shnir, “Asymptotically flat spinning scalar, Dirac and Proca stars,” Phys. Lett. B 797, 134845 (2019).
  * [11] V. Dzhunushaliev and V. Folomeev, Dirac star with SU(2) Yang-Mills and Proca fields, Phys. Rev. D 101, 024023 (2020).
  * [12] C. Herdeiro, I. Perapechka, E. Radu, and Y. Shnir, “Spinning gauged boson and Dirac stars: A comparative study,” Phys. Lett. B 824, 136811 (2022).
  * [13] V. Dzhunushaliev, V. Folomeev, and M. Minamitsuji, “Thick brane solutions,” Rept. Prog. Phys. 73, 066901 (2010).
  * [14] Y. X. Liu, “Introduction to Extra Dimensions and Thick Braneworlds,” [arXiv:1707.08541 [hep-th]].
  * [15] V. Dzhunushaliev and V. Folomeev, “Spinor brane,” Gen. Rel. Grav. 43, 1253 (2011).
  * [16] V. Dzhunushaliev and V. Folomeev, “Thick brane solutions supported by two spinor fields,” Gen. Rel. Grav. 44, 253 (2012).
  * [17] Z. Q. Cui and Y. X. Liu, “Spinor walls in five-dimensional warped spacetime,” Eur. Phys. J. C 83, 275 (2023).
  * [18] R. Finkelstein, R. LeLevier, and M. Ruderman, “Nonlinear Spinor Fields,” Phys. Rev. 83, 326 (1951).
  * [19] R. Finkelstein, C. Fronsdal, and P. Kaus, “Nonlinear Spinor Field,” Phys. Rev. 103, 1571 (1956).
  * [20] C. Armendariz-Picon and P. B. Greene, “Spinors, inflation, and nonsingular cyclic cosmologies,” Gen. Rel. Grav. 35, 1637 (2003).
  * [21] V. A. Rubakov and M. E. Shaposhnikov, “Do We Live Inside a Domain Wall?,” Phys. Lett. B 125, 136 (1983).
  * [22] T. Ortin, Gravity and Strings (Cambridge, UK; New York: Cambridge University Press, 2004).
  * [23] N. J. Popławski, “Spacetime and fields”, arXiv:0911.0334.


() insertion without Effects, Lights or Magic
Oleg Kiselyov
Tohoku University, Japan
Jeremy Yallop
University of Cambridge, UK

Let insertion in program generation is producing code with
definitions (let-statements). Although definitions precede uses in
generated code, during code generation `uses' come first: we might not
even know a definition is needed until we encounter a reoccurring
expression.  Definitions are thus generated `in hindsight', which
explains why this process is difficult to understand and implement –
even more so for parameterized, recursive and mutually recursive

We have earlier presented an interface for let(rec)
insertion – i.e. for generating (mutually recursive) definitions.
We demonstrated its expressiveness and applications, but not its
implementation, which relied on effects and compiler magic.

We now show how one can understand let insertion, and hence implement
it in plain OCaml.  We give the first denotational semantics of
let(rec)-insertion, which does not rely on any effects at all.  The
formalization has guided the implementation of let(rec) insertion in the
current version of MetaOCaml.

§ INTRODUCTION

Code generation, whether using quasiquotes or code combinators, is
compositional: nested function calls in the generating program lead to
nested expressions in the generated code, and code for larger
expressions is built by incorporating code for sub-expressions
unchanged. Here is an example, with code combinators (used later in the
paper). Suppose |t1| denotes the code “”; |+
are the combinators to generate addition (resp.,
let- and lambda-expressions). Then

fnCx. (clet t1 (fun y -> x +

generates the code

fun x7 -> (let y8=1+2 in (x7 + y8))

(The parentheses show the corresponding nested scopes.)

There is, however, often a need for a sub-expression to generate a
let-statement that should scope over a larger (parent)
expression [2] – e.g. to avoid
recomputations. Continuing the example, we would like to replace
|clet| above with a combinator |glet| such that

fnCx. (glet t1 (fun y -> x +

would now generate a more optimal

let y8=1+2 in fun x7 -> (x7 + y8)

As this example shows, let-insertion is non-compositional: it
scrambles the nesting of generated binding forms, opening the
possibility of generating code with unbound or mistakenly bound
variables [5]. It is not a mere possibility:
generating code with unbound variables does occur in practice, and is
difficult to debug, as reported in [12].  The
non-compositionality becomes glaring when generating recursive (and
especially mutually recursive) definitions [15].

Our aim is to understand the meaning of the let-inserting code
generators, such as |glet| and its more general
forms |genlet| or |genletrec|.  We have two goals:
designing a type system to statically prevent the generation of ill-scoped
code; and reasoning about programs that generate let(rec)
statements (not just about the code that they generate).

We report work-in-progress towards these goals: a denotational
semantics that for the first time describes what |genlet| and
|genletrec| mean by themselves, and in a compositional way.
That is, the apparent intrinsic non-compositionality of let-insertion
described above turns out to be a mere appearance.

The key idea is virtual let-bindings: whereas
|clet| introduces an ordinary let-binding whose location is fixed,
|glet| generates the code for a fresh variable accompanied by a
virtual binding of that variable. Virtual bindings do not
have (yet) a fixed location: they are attached to the expression that
uses their bound variables and `float up' when their attached
expression is incorporated into a bigger expression. Eventually, when
they reach a point that a metaprogrammer has marked with a dedicated
primitive, virtual bindings are converted to real let-bindings.

Our denotational semantics is executable: it serves as a small
standalone metaprogramming system that implements the
previously-proposed interface [15]
for generating mutually recursive
definitions; it is sufficiently complete to express the example
programs used to introduce that interface.

Compositionality let us build the system in pure OCaml using no

Furthermore, our semantics has already led to improvements and
simplifications to the earlier interface.

When saying that our system performs let-insertion without any
effects, we need to clarify what an effect is. After all, even
lambda-abstractions [3] or variable substitution
<cit.> may be regarded as effects. Previously,
let-insertion required control effects (realized as delimited control
or continuation-passing transformation)
[1, 9]
or, at the very least, state (realized as mutable state or state-passing)
[13]. The present paper demonstrates that none of these
are needed. As a consequence, if in a generator expression such as
|e1 +
summand generators  |e1| and |e2| may be evaluated
independently or even concurrently, they can be so evaluated even if
either or both perform let-insertion. That let-insertion
does not have to impose an order on evaluation is new and surprising.

The next section introduces denotational semantics for code
generation, which
<ref> extends to support the ordinary let insertion,
and then mutually recursive-let–insertion. <ref>
describes the realization in the current version of MetaOCaml.
Related work is briefly described in <ref>.

The complete code of the executable semantics along with many examples
is available online.[<http://okmij.org/ftp/meta-programming/genletrec>] MetaOCaml
(version N111) incorporating let(rec) insertion is available from

§ SEMANTICS OF CODE GENERATION

As the |Base| calculus
we take the standard call-by-value simply-typed lambda calculus
with constants, ordinary let-expressions and (potentially mutually)
recursive letrec-expressions: think of the most basic,
side-effect–free subset of OCaml.  Figure <ref> presents its
syntax.  There, |c0|, |c1|, |c2| and |c3| stand for constants of the
corresponding arity. Applications of constants to fewer arguments than
their arity are not considered expressions. The calculus includes
integer and boolean literals (as zero-arity constants), the successor
operation |succ| as an arity-1 constant, and  arithmetic and comparison
operations on integers, of obvious types, as arity-2 constants:
see Fig. <ref>, left column.
The type system with
judgements |'G vee e:t| is entirely standard and elided for brevity.
in the full version, add the type system figure. Then state the
theorem that the type system is sound because eval clauses are all
total. Draw the attention why E[x] rho = rho(x) is total:
because, in full, E[Gamma |- x:t] rho, rho is in T[Gamma], hence
x, which must be mentioned in Gamma, is in the domain of rho.
This is trivial for now: but when we add let-insertion we see how
this assumption, x is always in the domain of rho, is violated.
Perhaps state it as a preposition: x is in the domain of rho.
Incidentally, try to give type to mkvar. It becomes clear how to
design a type system to prevent scope extrusion, from the type of
mkvar that makes it total.
Perhaps the judgement should be instead of Gamma |- e:t,
$e^\Gamma: t$. See Pientka's et al. paper in POPL 2022.

Variables     |x,y,z,u,f,n,r|…

t ::= int | bool | t -> t

e ::= x | c0 | c1 e | c2 e e | c3 e e e
| fnx. e | e e
| if e then e else e
| let x=e in e
| let rec x=e and x=e ... in e

v ::= c0 | fnx. e

Syntax of the base calculus;  are constants
of arity $i$: see Fig. <ref>, left column.

0,1,2,3,...  : int
true, false: bool
succ: int -> int
+: int -> int -> int
=: int -> int -> bool

cint: int -> int code
cbool: bool -> bool code
cif: bool code -> $t$ code -> $t$ code -> $t$ code
fnC: ($t_1$ code -> $t_2$ code) -> ($t_1\to t_2$) code
@: ($t_1\to t_2$) code -> $t_1$ code -> $t_2$ code
clet: $t_1$ code -> ($t_1$ code -> $t_2$ code) -> $t_2$ code

Constants of Base (left column) and its extension Codec
(right column) with their types and arities: the arity of a constant
is the number of arrows in its type.  Although the constants may
have function types, they are not expressions, unless used with the
right number of arguments.  The metavariable $t$ stands for any
type. For arity-2 constants such as   and , we
use infix notation. We write expressions with the constant
$\underline\lambda$, viz.,
$\underline\lambda\mathsf{(\lam{x}e)}$, as
We silently add other arithmetic and comparison constants and
code combinators, similar to  and .

A different way of presenting the calculus and its type system is in
the form of an OCaml signature (Appendix <ref>), which helps make
the semantics executable.

Here are some sample expressions:

t1   := 1 + 2
sq   := fnx. x * x
gib5 := fnx.fny.
let rec loop n =
if n=0 then x else if n=1 then y else
loop (n-1) + loop (n-2)
in loop 5

The notation |name := exp| is not part of the calculus; it is used to
attach a name to an expression for easy reference. The function
|gib5| computes the 5th element of the Fibonacci sequence whose first
two elements are given as arguments.

The |Base| calculus both represents the code that we generate and
serves as the core of the generating code. For generation, we extend
|Base| with an additional family of types |t code| whose values
represent generated |Base| expressions of type |t|. We also
add the means of producing these code values: constants (a.k.a. code-generating
combinators) in the right column of Fig.<ref>.
Below are some expressions in this
extension of |Base|, called |Codec|; each expression serves as a generator of
the corresponding earlier |Base| expression:

ct1   := cint 1 +
csq   := fnCx. x *
cgib5 := fnCx.fnCy.
let rec loop n =
if n=0 then x else if n=1 then y else
loop (n-1) +
in loop 5

Here |cint| generates the code of an
integer literal; |+
combines the code of summands to produce the code of an addition expression;
|fnCx.body| generates the code of a function given a generator
for its body, the variable |x| within the expression |body|
representing the bound variable in the (to be) generated function.

In what sense |csq| and |cgib5| respectively represent |sq| and |gib5| will
be clear after we describe the semantics of the calculus.

§.§ Semantics of

We consider two denotational semantics of |Base|, to be indexed by the
subscripts |R| (for `run') or |S| (for `show');
|X| (or omitted subscript) stands for either.

First, notated by the subscript |R|, is the
standard Scott-Strachey semantics for a typed Church-style calculus,
with one small wrinkle. Its semantic domains and the interpretation
- of its types
are standard:

\[
\TR{int} = \mathbb{Z}_\bot
\qquad
\TR{bool} = \{\mathtt{tt},\mathtt{ff},\bot\}
\qquad
\TR{t_1\to t_2} = \TR{t_1} \to \TR{t_2}
\]

If $A$ is a set and $B$ is a domain, $A\to B$ is a continuous map from
$A$ to $B$, which is also a domain. We also introduce
$\Nom$ as a countably infinite set of names and $\Loc$ as a set of finite
sequences of small (i.e. bounded) numbers, for which we adopt the OCaml list
notation. Such a sequence can also be considered a name. Therefore,
we take $\Loc\subset\Nom$ and treat $\Loc$ as names, distinct from the names
appearing in source |Base| terms.

The semantic function $\mathcal{E}_X\big[\Gamma\vdash
\mathsf{e}:\mathsf{t}\big] \in \mathcal{D}_X[\mathsf{t}]$
gives meaning to (the type
derivation of) a potentially open expression |e|, where
$\mathcal{D}_X[\mathsf{t}]$ along with auxiliary domains is defined as follows.
(When writing semantic functions, we shall show only the expression rather than
its entire type derivation, and often
elide $\Gamma$ and the type annotations to avoid clutter.)
Actually, I should have annotated D with Gamma, and wrote
$D_X^\Gamma[t] = T[\Gamma]\to \Loc \to T[t]$ where $T[\Gamma]$ is a
record, which can be regarded as a finite map, but the names $\Nom$
should be in dom(Gamma)
But here we are sloppy and introduce partiality.

𝒟_X[𝗍] = Env_X →→t

Env_X = ↣t

where $A\rightarrowtail B$ is a finite map from $A$ to $B$. If $\rho$
is such a map, $\rho[k{\to} v]$ is its extension, $\rho\mid_{\not=k}$
is its restriction (removing the association for $k$) and
$\mathrm{dom}\,\rho$ is its domain; $\varnothing$ is the empty map.
We write $\mathsf{modify}\,\rho\,k\,u$ the modification of the element
at key $k$ by an update function $u$, that is: $\rho[k{\to}u\,\rho(k)]$.

The semantic function
$\mathcal{M}_X\BR{e:t} \in \TX{t}$ gives meaning to
programs (i.e. to type derivations of closed expressions):

\[
\mathcal{M}_X\BR{e} = \mathcal{E}_X\BR{e}\ \varnothing\  \mathsf{[]}
\]

The semantic rules are almost entirely standard:
the extra $\Loc$ argument, written as |'l|, is the wrinkle.
Here is the rule for abstraction:

ER[fnx.e]! 'r 'l = $\lam{x}\mathcal{E}_R\BR{e}$ 'r[x->$x$] (1::'l)

For now, |'l| is not actually used, and might as well be
absent. It will help soon, in <ref>.
It will also help to re-write the semantic
rules in the form shown in Fig. <ref>, with the `composition'
and variable reference rules common to all semantics, and the
semantic-specific strict |mk| functions.
For instance, $\MK{lam}_R$ takes the variable name and the
denotation of a (generally open) expression and constructs the
denotation of a lambda-abstraction. The re-written rules make it very
clear that the denotation of, say, an abstraction is constructed from the
denotation of the abstraction body and the name of the abstracted
variable. needs the rules for constants like e1+e2, and for if
and let rec

(a) composition rules
i    = int_X 𝗂

e_1 + e_2    = add_X e_1 e_2

x    = var 𝗑

xe    = lam_X 𝗑 e

e_1 e_2    = app_X e_1 e_2

let x=e_1 in e_2    = let_X 𝗑 e_1 e_2

(b) semantics of variable reference (generic)
var 𝗑    = ρℓρ(𝗑)

(c) $\mathsf{mk}$ functions for the |R| semantics
int_R i    = ρℓi

add_R d_1 d_2    =
ρℓ (d_1 ρ  (1::ℓ)) + (d_2 ρ (2::ℓ))

lam_R 𝗏 d    =
ρℓx d ρ[𝗏→ x]  (1::ℓ)

app_R d_1 d_2    =
ρℓ (d_1 ρ  (1::ℓ)) (d_2 ρ (2::ℓ))

let_R 𝗏 d_1 d_2    =
ρℓ(xd_2 ρ[𝗏→x] (2::ℓ))
(d_1 ρ (1::ℓ))

(d) $\mathsf{mk}$ functions for the |S| semantics
int_S i    = ρℓ i

add_S d_1 d_2    =
ρℓ (d_1 ρ  (1::ℓ)) (d_2 ρ (2::ℓ))

lam_S 𝗏 d    =
ρℓ 𝗏 (d ρ[𝗏→ 𝗏]  (1::ℓ))

app_S d_1 d_2    =
ρℓ (d_1 ρ  (1::ℓ)) (d_2 ρ (2::ℓ))

let_S 𝗏 d_1 d_2    =
ρℓ 𝗏

(d_1 ρ (1::ℓ))               (d_2 ρ[𝗏→𝗏] (2::ℓ))

Semantics of Base

The semantics of |Base| just given could rightly be called
`extensional'. We also have an `intensional' |Base| semantics,
notated by the superscript |S|, which maps
an expression to its symbolic form (a string, for example). Here
$\TS{t}$ is always a string and the functions
$\MK{lam}_S$ etc. build strings (see Fig.<ref>(d)).
|S| is a
trivially compositional, bona fide denotational semantics, and even
mentioned as such by [10]. Usually it is quite
useless – but not here.

§.§ Semantics of

The semantics $\mathcal{M}^X_Y[-]$ and $\EXY{-}$ of |Codec|
is an extension of a semantics of |Base|. There are now two indices: the
subscript |Y| notates the semantics (|R| or |S|) of the generator,
whereas the superscript |X| labels the semantics used for the
generated code. (For a two-level language, |Y| being |R| is the most
useful variant; we often leave this implicit and drop |Y|.)
The semantic domain of |t code| is (for now; it will be extended when
we come to let- and let-rec-insertion):

\[
\TUX{t\ code} = \mathcal{D}_X[\mathsf{t}]
\]

A |t code| value represents a potentially open (think of generating
function bodies) |Base| expression |e| of type |t|. Its meaning
is, therefore, $\mathcal{E}_X[\mathsf{e}]$: the meaning the |Base|
semantics |X| gives to it.
If we annotated DX with Gamma, as we should, then the question
arises: which Gamma to use in the above equation? The answer is to
annotate t code with Gamma: contextual modal type theory. But we can
omit then, and prove that one can do things safely at the expense of
a dynamic check (at code generation time!) and emitting good error messages.

Since |Codec| is an extension of |Base|, its semantics $\EXY{-}$ is an
extension of $\mathcal{E}_Y\BR{-}$ with the rules for
the meaning of constant expressions of type |t code|, such as the
following rules for generation of an integer literal, abstraction, application
and let-expression. Generating addition, comparison, if-expression
is similar. As far as variable references are concerned,
$\EXY{x} = \mathcal{E}_Y\BR{x} = \MK{var}\ \mathsf{x}$.

\begin{align*}
\EXY{\underline{int}\ i}\rho\ell &= \MK{int}_X\ \mathsf{i}
\\
\EXY{\quant{\underline\lambda}{x}e}\rho\ell &=
\MK{lam}_X\ \ell\
(\EXY{e}\, \rho[\mathsf{x}\to \MK{var}\ \ell]\ (1::\ell))
\\
\EXY{e_1\ \underline{@}\ e_2}\rho\ell &=
\MK{app}_X\ (\EXY{e_1}\,\rho\,(1::l))\ (\EXY{e_2}\,\rho\,(2::l))
\\
\EXY{clet\ e_1\ e_2}\rho\ell &=
\MK{let}_X\ \ell\ (\EXY{e_1}\,\rho\,(1::l))\
((\EXY{e_2}\,\rho\,(2::l))\ (\MK{var}\ \ell))
\end{align*}

When we generate an abstraction or a let-expression, the current |'l|
acts as the fresh name for the (to be) bound variable. Recall that the
function $\MK{lam}$ (Fig.<ref>) takes a variable name
and the denotation for the abstraction body and gives the denotation
for the abstraction. Thus the role of |'l|, the only non-standard
aspect of the |Base| semantics, is to serve as a deterministic name
generator: the fresh name to be used in an expression is the path from
the root of the Böhm tree.

If we use the |R| semantics for the generated code (that is, choose
|X| to be |R|) we see that
|MRR[ct1]| is exactly |MR[t1]!| (which is
the integer 3), |MRR[csq]!| and |MR[sq]!| both mean the
squaring function, and |MRR[cgib5]!| and |MR[gib5]!| both
mean the function that takes two arguments |x| and |y| and returns the sum of
5 copies of |y| and 3 copies of |x|.

If we use the |S| semantics, $\mathcal{M}^S_R[\mathsf{ct1}]$ and
$\mathcal{M}_S[\mathsf{t1}]$ still
coincide (both mean the string |1+2|). $\mathcal{M}^S_R[\mathsf{csq}]$
and $\mathcal{M}_S[\mathsf{sq}]$ are generally different but
lambdaexpression strings. Whereas
$\mathcal{M}_S[\mathsf{gib5}]$ is the string of the
|gib5| code (potentially $\alpha$-converted),
$\mathcal{M}^S_R[\mathsf{cgib5}]$ is the string

fnx.fny. (((y + x) + y) + (y + x)) + ((y + x) + y)

It is an `optimized' version of |gib5|, in the sense that the loop is
unrolled; however, it contains several instances of code
duplication. Avoiding this code duplication is where let-insertion
comes in.

§ LET-INSERTION

To support let-insertion, we add to |Codec| two more forms:
|let locus l in e| and |genlet l e_m e|. The former, like
the ordinary let, binds the so-called locus variable |l| in |e|.
In the expression |genlet l e_m e|, |l| is a locus variable (previously bound
by |let locus|), |e_m| is a so-called memo key (for now, an |int|
expression) and |e| is a |t code| expression (|t| is a |Base| type).
Roughly, |e| generates the right-hand-side of the let-binding, |l|
tells where to insert it, and the memo key instructs which |e| are to
be shared. We describe the |genlet| arguments in more detail after the
example, the slightly adjusted |cgib5|:

clgib5 := fnCx.fnCy. let locus l in
let rec loop n =
if n=0 then x else if n=1 then y else
genlet l (n-1) (loop (n-1)) +
in loop 5

To a first approximation, one may think of |genlet l e_m e| as
generating |let z=c in z| where |z| is fresh and |c| is the code produced by
the expression |e|. Such `let-expansion' is useless, however. It becomes
more useful when the binding |let z=c in| is actually placed somewhere
`higher' in the overall generated code. The form |let locus l| marks
that `higher' place where the bindings produced by |genlet| are to be
placed. Since let-insertion is very common, different parts of the
generator may do their own let-insertions at different places; the
locus variable |l| is to connect |genlet| with its corresponding
Thus intuitively, |genlet l e_m e| will insert the
|let z=c in| at the place marked by  and return the code
of the bound variable |z| (which is distinct from any other
variables in the code).

Placing let-bindings `higher'
in the code is useful because they may be shared. The memo key defines
the equivalence classes: expressions with the same
memo key are to be shared. Therefore, if |genlet l e_m e| finds that there
is already a let-binding produced by an earlier |genlet| with
the same |l| and the memo key, |genlet l e_m e| returns the code of the earlier
bound variable.

Using the semantics of these operations, explained below, we can
see that whereas
|MRR[clgib5]!| remains the same as |MR[gib5]!|,
|MSR[clgib5]!| is the string

fnx.fny. let z = y in let u = x in
let v = z + u in let w = v + z in
let x6 = w + v in x6 + w

which is indeed an optimized version of |gib5|, without either loops
or duplication.

§.§ Semantics of let-insertion

The key point was that the binding introduced by |genlet l e_m e| is
not yet placed; its placement will be decided only later, upon
seeing the corresponding |let locus|. For now, the |genlet|'s binding
is `floating' – we say `virtual'. To accommodate virtual bindings
we extend the semantics domain of |t code|: it is now a tuple, whose
first component is the earlier semantic domain of code values (<ref>),
and whose second component is the virtual bindings. Formally, the semantic domain is:
t code =
𝒟_X[𝗍] ×(↣𝒱^X)

𝒱^X =    (𝒦×𝒦) Set×(𝒦↣ℬ^X)

ℬ^X =    ×𝒟_X[𝗍]

Virtual bindings are indexed by the locus where they will be actually
inserted. Furthermore, virtual bindings with the same locus $l$ and the
same memo key $k$ belong to the same equivalence class. We take
the locus to be an element of $\Loc$ and introduce the set
$\mathcal{K}$ of memo keys. All in all,
virtual bindings is a finite map $\Loc\rightarrowtail\mathcal{V}^X$,
where $\mathcal{V}^X$ describes virtual bindings with the same locus.
If $\nu$ is such a map, we take $\nu(l)=\varnothing$ if $l\not\in
\mathrm{dom}(\nu)$.
Further thought, considering
|genlet l 2 (cint 3 +
virtual bindings have to be (partially) ordered. (We shall see an
example soon.) Thus the elements of
$\mathcal{V}^X$ are tuples $\langle R,b \rangle$ where $R$ is a preorder on
$\mathcal{K}$ and $b$ is a finite map
$\mathcal{K}\rightarrowtail \mathcal{B}^X$. Here, $\mathcal{B}^X$
describes one equivalence class of virtual bindings: a tuple
$\langle n,d,\overline n\rangle$ where $n$ is the name to bind,
$d$ is the ($\EX{-}$ denotation of the) expression to which $n$
will be bound to, and
$\overline n$ is the set of names: names equivalent to $n$.

The earlier semantic rules for $\EXY{-}$ dealing with code generation
have to be amended to account for the extended semantic domain:
$\EXY{\underline{int}\ i}$ produces the empty virtual binding,
and the other rules propagate the virtual bindings of their
subexpressions, merging as needed:

\begin{align*}
\EXY{\underline{int}\ i}\rho\ell &=
\langle \MK{int}_X\ \mathsf{i},\ \varnothing\rangle
\\
\EXY{\quant{\underline\lambda}{x}e}\rho\ell &=
\langle \MK{lam}_X\ \ell\ d,\ \nu\rangle\qquad \textrm{where}\\
& \langle d,\nu \rangle =
\EXY{e}\, \rho[\mathsf{x}\to \langle \MK{var}\ \ell,\varnothing\rangle]\
\\
\EXY{e_1\ \underline{@}\ e_2}\rho\ell &=
\langle \MK{app}_X\ d_1\ d_2,\ \mathsf{merge}\ \nu_1\ \nu_2\rangle
\qquad \textrm{where}\\
& \langle d_1,\nu_1 \rangle = \EXY{e_1}\,\rho\,(1::l)\\
& \langle d_2,\nu_2 \rangle = \EXY{e_2}\,\rho\,(2::l)
\end{align*}

The operation $\mathsf{merge}$ for virtual
bindings is described later.

As we said earlier, |genlet l e_m e| generates a fresh name to which
the code produced by |e| will eventually be bound; that fresh name is
accompanied by the new virtual binding of that name to the result of
|e|. Here, |e| is a |t code| expression: the generator of the
expression to bind. It itself may be accompanied by virtual
bindings. The new binding added by |genlet l e_m e| may in general
depend upon those bindings, and hence has to be added as `greater' in
the preorder $R$. (In contrast, |e_m| is an |int| rather than
an |int code| expression, and so its denotation $\EXY{e_m}\,\rho\,(1::\ell)$,
written as $k$ below, is (when |Y| is |R|) just an integer: in
general, an element of $\mathcal{K}$.)

\begin{align*}
\EXY{genlet\ l\ e_m\ e}\rho\ell &= \langle\:\MK{var}\,\ell,\
\mathsf{modify}\,\nu\,\rho(\mathsf{l})\,
(\mathsf{addb}\,k\, \langle\ell,d_b\rangle)\:\rangle\qquad \textrm{where}\\
k & = \EXY{e_m}\,\rho\,(1::\ell)\\
\langle d_b,\nu\rangle & = \EXY{e}\,\rho\,(2::\ell)
\end{align*}

(If |e_m| or |e| diverge, so does |genlet l e_m e|.)

The semantic function $\mathsf{addb}\, k\, \langle n,d\rangle\, v$
adds a new virtual binding of $n$ with $d$ to the equivalence class
$k$ of the virtual bindings $v\in \mathcal{V}^X$ with the same locus.
Recall that the virtual bindings $v$ is a pair, of preorder $R$ and
the set of equivalence classes $b$, indexed by the memo key. There are
two cases to consider: if $b$ already includes the equivalence class
for $k$, we add $n$ to that class (and disregard the right-hand-side
$d$ since we already have an equivalent one). Otherwise, we add to $b$
the new equivalence class for $k$ containing just the binding of $n$
to $d$, and update the preorder $R$ so that $k$ becomes the `latest'.

𝖺𝖽𝖽𝖻: 𝒦→(×𝒟_X[t])→𝒱^X→𝒱^X

𝖺𝖽𝖽𝖻 k ⟨n,d⟩ ⟨R,b⟩=

⟨R, b[k→⟨n',d',n'∪{n}⟩]⟩

b(k) = ⟨n',d',n'⟩

⟨R ∪{(k,k)}
∪{⟨k',k⟩|k'∈dom  R}, b[k→⟨n,d,∅⟩]⟩

k∉dom  b

The form |let locus l in e| converts the virtual bindings
for the locus |l|
produced by |e| into real let-bindings. To a
first approximation, the conversion can be understood as turning the
sequence of virtual bindings into nested let-expressions. We should
mind the dependency among the bindings and nest the
let-expressions in the `right' order.

\begin{align*}
\EXY{let\ locus\ l\ in\ e}\rho\ell &=
\langle \mathsf{bind} (\mathsf{ordered}\,\nu(\ell))\,d,\
\nu\mid_{\not=\ell}\rangle
\qquad \textrm{where}\\
&\langle d,\nu\rangle = \EXY{e}\,\rho[\mathsf{l}\to\ell]\,(1::\ell)
\end{align*}

The semantic function $\mathsf{ordered}: \mathcal{V}^X\to
\mathcal{B}^X Seq$ converts $\langle R,b\rangle$ to a sequence of bindings
$\mathcal{B}^X$ in an order consistent with $R$. The semantic
function |bind| converts a sequence of bindings to nested

[⟨n_1,d_1,n_1⟩,⟨n_2,d_2,n_2⟩,…]  d

𝗆𝗄𝗅𝖾𝗍_X  n_1 d_1 (𝗌𝗎𝖻𝗌𝗍  n_1
(𝗆𝗄𝗅𝖾𝗍_X  n_2 d_2 (𝗌𝗎𝖻𝗌𝗍  n_2  n_2

Recall, the semantic function |mklet| (see Fig.<ref>)
builds the denotation of
|let x=e in e'| from the variable name |x|,
$\mathcal{E}_X[\mathsf e]$ and
$\mathcal{E}_X[\mathsf{e'}]$. In a virtual binding $\langle n,d,\overline n\rangle$, the set
$\overline n$ contains the variables other than $n$ in the same
equivalence class with it. They are all substituted with $n$:
𝗌𝗎𝖻𝗌𝗍  n n d =
ρℓ d ρ[(n'→ n |n'∈n)] ℓ

The semantics function $\mathsf{merge}\, \nu_1\,\nu_2$ mentioned earlier
merges the virtual bindings $\nu_1$ and $\nu_2$,
by using $\mathsf{addb}$ to add one-by-one the virtual bindings of
$\nu_2$ to $\nu_1$, in an order consistent with the $R$ preorder of

Later prove that our denotation really respects the dependency with
respect variables and the produces let-statements in the correct
order, never leading to scope extrusion (provided locus itself is
positioned correctly.)

An example featuring code duplication and nesting should show how
everything fits together. As a warm-up, a generator without |genlet|:

let locus l in
let x = (cint 6 +
((x +

has the meaning in the |MSR[-]!| semantics as a string

(((6 + 7) + 20) * ((6 + 7) + 30)) / 100

with evidently duplicate code. We can eliminate the duplication by putting
let-expressions into the generated code, using |genlet|:
let locus l in
let x = genlet l 1
(cint 6 +
(genlet l 2 (x +
genlet l 3 (x +

The three |genlet| expressions are each in their own equivalence class
and hence pass distinct memo keys as their second arguments.
(Such usage can be automated – in fact, it has been, in MetaOCaml.)
The code is typeset so that each notable expression is on its own line
for easy reference. Furthermore, we write $\ell_i$ for the Böhm
tree location (an element of $\Loc$) corresponding to line $i$.

The denotation is computed as follows. Let $\rho_0$ be the initial environment
and $\rho=\rho_0[\mathsf{l}\to \ell_1]$.
We also assume $\MK{mul}$ and
$\MK{div}$ functions similar to $\MK{add}$.

(int 6 + int 7)ρℓ_3 =
⟨d_3, ∅⟩    where
d_3 = add_X (int_X 6) (int_X 7)

genlet l 1
(int 6 + int 7)ρℓ_2 =
⟨var_X ℓ_2, {ℓ_1→v_2}⟩  where

v_2 = 𝖺𝖽𝖽𝖻 1 ⟨ℓ_2,d_3⟩ ∅=

This was the denotation of the expression
the variable  is bound to. The variable is then used twice,
on lines 4 and 6. In the following we take $\rho_1$ to be $\rho$
extended with the binding for .
genlet l 2 (x + int 20)ρ_1ℓ_4 =
⟨var_X ℓ_4, {ℓ_1→v_4}⟩  where

d_4 = add_X (var_X ℓ_2) (int_X 20)

v_4 = 𝖺𝖽𝖽𝖻 2 ⟨ℓ_4,d_4⟩ v_2 =

genlet l 3 (x + int 30)ρ_1ℓ_6 =
⟨var_X ℓ_6, {ℓ_1→v_6}⟩  where

d_6 = add_X (var_X ℓ_2) (int_X 30)

v_6 = 𝖺𝖽𝖽𝖻 3 ⟨ℓ_6,d_6⟩ v_2 =

When computing the denotation for the product expression,
we have to merge the virtual bindings $v_4$ and $v_6$
… * …ρ_1ℓ_5 =
⟨mul_X (var_X ℓ_4) (var_X ℓ_6),
{ℓ_1→v_5}⟩  where

v_5 =

The merged virtual bindings propagate to the denotation of
the division expression, and converted to real let-bindings by
let-locus. The whole expression thus has the denotation
let locus l in …ρ_oℓ_1 =⟨

let_X ℓ_2 (add_X (int_X 6) (int_X 7)) (

let_X ℓ_4 (add_X (var_X ℓ_2) (int_X 20)) (

let_X ℓ_6 (add_X (var_X ℓ_2) (int_X 30)) (

div_X(mul_X (var_X ℓ_4) (var_X ℓ_6))(int_X 100)))), ∅⟩

The interface for |genlet| described above differs from our previous
proposal [15] in that here we combine memoization and
let-insertion. Although both memoization and let-insertion are usually
implemented in terms of effects, we have used no effects at all.

If |let locus| in |clgib5| is positioned above |fnCy....|, so called
scope-extrusion occurs, resulting in the generated code
with have unbound variables (as we can verify in our semantics).
It is the subject of ongoing work to develop a type system to
statically prevent such problems.

§.§ Generating (mutually) recursive definitions

It turns out that |genletrec| for generating (mutually) recursive
definitions presented by [15] is a minor variant of the
above |genlet|, with almost the same semantics. The syntax is the
same: |genletrec l e_m e| for requesting the binding (and obtaining
the name of the to-be-bound variable) and |let rec locus l in e| for
actually generating let-rec statements at that locus.

Here is the example: specializing the Ackermann function challenge by
Neil Jones. The |ack2| below is the two-argument Ackermann function
|ack| partially applied to two:

ack2 :=
let rec ack = fnm.fnn.
if m=0 then n+1 else
if n=0 then ack (m-1) 1 else
ack (m-1) (ack m (n-1))
in ack 2

Below is the generator of |ack2|, matching |ack2| in form.

cack2 :=
let rec locus l in
let rec ack = fnm.fnC n.
if m=0 then n+
cif (n =
(genletrec l (m-1) (ack (m-1)) @ cint 1)
(genletrec l (m-1) (ack (m-1))  @  (genletrec l m  (ack m) @ (n-
in genletrec l 2 (ack 2)

Here |genletrec| is needed not just to obtain optimal code; without
|genletrec| we get no code at all: divergence. Using the semantics
below, |MSR[cack2]!| gives the following code

let rec x = fnu. if u = 0 then y 1 else y (x (u - 1))
and y = fnv. if v = 0 then z 1 else z (y (v - 1))
and z = fnw. w + 1 in x

Semantically, |genletrec| is also close to |genlet|: both return the
name of a to-be-bound variable, accompanied by a virtual binding.
Whereas for |genlet l e_m m| the binding associates the name to the
expression produced by |e| (the generated code), for
|genletrec l e_m e| the virtual binding associates the name to |e| itself
(one may say, unevaluated generator expression). Thus |genletrec| is
even lazier, introducing even more virtual bindings. Formally,
we extend $\mathcal{B}^X$ to
ℬ'^X =
×(𝒟_X[𝗍] + t code)

where $A+B$ is to be read as a disjoint union (with tags
$\mathtt{inl}$ and $\mathtt{inr}$) with a separately added $\bot$. The
left summand $\mathcal{D}_X[\mathsf{t}]$ is inherited from
$\mathcal{B}^X$: the meaning of the code for the right-hand-side of
the binding. The right summand $\TUX{t\ code}$ is (the approximation of)
the code for the right-hand-side. The $\TUX{t\ code}$ definition
hence becomes recursive and has to be understood as the solution to
the domain equation.  Typically, genletrec occurs within a
normal let rec. The right-hand-side may include recursive calls. So,
the right-hand-side is an approximation, in the information order,
of that recursive invocation. letrec will find the fixpoint, the
limit of the chain of approximations. In the full paper, we should
greatly expand on this. Then

\begin{align*}
\EXY{genletrec\ l\ e_m\ e}\rho\ell &=
\langle \MK{var}\,\ell,\
\varnothing[l{\to} \mathsf{addb}\,
k\, \langle\ell,\mathtt{inr}\ \EXY{e}\rho\,(2::\ell)\rangle\,
\varnothing]\rangle
\qquad\textrm{where}\\
l = \rho(\mathsf{l})\\
k = \EXY{e_m}\,\rho\,(1::\ell)
\end{align*}

Virtual bindings now contain yet to be evaluated generator expressions
for the right-hand-side of bindings. To generate the
let-rec-statement, we have to evaluate them – which may create more
virtual bindings, which have to be merged and again evaluated (the
evaluation may also diverge). This complex process of evaluation is
called canonicalization, and performed by the semantic function
$\mathsf{canon}\,\nu\,l$ which takes virtual bindings and the locus
$l$ and returns updated bindings $\nu'$ such that $\nu'(l)$ are all
canonical: each $\mathcal{B}'^X$ is actually $\mathcal{B}^X$. The
function is the least fixpoint of the following recursive equation.
𝖼𝖺𝗇𝗈𝗇 ν  l =

ν if $\nu(l)$ are all canonical

𝖼𝖺𝗇𝗈𝗇  (𝗆𝖾𝗋𝗀𝖾 ν[l→⟨R,b'⟩] ν”)  l  where

⟨R,b⟩= ν(l)

⟨n,𝚒𝚗𝚛 d',n⟩= b(k),   k ∈dom(b)

⟨d,ν”⟩= d'

b' = b[k→⟨n,𝚒𝚗𝚕 d,n⟩]

That is, among the bindings $\nu(l)$ with the same locus $l$ we pick
an equivalence class with a non-canonical virtual binding
$\langle n,\mathtt{inr}\ d',\overline n\rangle$. If such a class does
not exist, we are done. Let $k$ be the memo key of that class. If
$d'$ is $\bot$, so is the whole $\mathsf{canon}\,\nu\, l$. If not,
it is a pair, containing (the meaning of) the code $d$ for the
right-hand-side of the binding, plus its accompanying virtual bindings
$\nu''$. We update the class $k$ so it now contains the canonical
binding $\langle n,\mathtt{inl}\ d,\overline n\rangle$ and merge the result
with $\nu''$. It may happen that $\nu''$ contains a non-canonical
binding for the same locus $l$ and the same memo key $k$. In fact,
this happens in the
|cack2| example above: evaluating |ack 2| produces virtual
bindings that contain |ack 2| again. The operation $\mathsf{merge}$
folds such bindings, with the already
canonical $\mathtt{inl}\ d$ used as the right-hand-side
for the $\nu''(l)(k)$ binding – thus canonicalization may eventually
We need to elaborate the cack2 example, showing how fixpoint is
found, and how generated names at recursive invocations come out

The form |let rec locus l in e| is semantically almost the same as
|let locus l in e|, differing only in the extra step of
canonicalization of the virtual bindings produced by |e|. After
canonicalization, the virtual bindings are converted into real
bindings in the same way as for |let locus l in e|
(only we produce one letrec-expression rather than a nest of
let-expressions, and therefore do not bother with $\mathsf{ordered}$.)
later on, expand

§ METAOCAML IMPLEMENTATION

Formal semantics is not the end, but the means; it is developed to be
used. One application, the subject of future work, is reasoning about
generating programs and making sure the generated code is not only
well-formed and well-typed but also intended. Another application, the
subject of the present paper, is to clarify edge cases, and attempt to
minimize them once exposed. The formal development has already proven
useful: it has improved our previous design for generating mutually
recursive definitions [15], which led to the
straightforward implementation in the current (N111) version of
MetaOCaml [2]. We now briefly
describe the implemented interface.

The let-insertion interface in MetaOCaml is as follows:

type locus
val locus_global : locus
val genlet : ?name:string -> ?locus:locus -> 'a code -> 'a code
val with_locus : (locus -> 'w code) -> 'w code

type locus_rec
val mkgenlet : ?name:string -> locus_rec -> ('k->'k->bool) ->
(('k -> ('a->'b) code) -> ('k -> ('a->'b) code))
val with_locus_rec : (locus_rec -> 'w code) -> 'w code

Here |genlet| is a version of |genlet l e_m e| described earlier, for
the case of all memo keys being distinct. The first optional argument
of the MetaOCaml |genlet| is the hint for the variable name to
generate (useful if one wishes to see variable names in the generated
code other than |t_34| and the like.) Locus is the second argument: if
omitted, it defaults to |locus_global|, which is the implicit locus at
the very beginning of the program. The current interface (and also the
implementation) thus subsumes the locus-less |genlet| of the previous
MetaOCaml version.

The type |locus_rec| and the operations |mkgenlet| and
|with_locus_rec| deal with potentially mutually recursive
memoizing let-insertion. In the earlier |cack2| we have seen that the
memoization key occurs also in the expression to bind, as an argument
to some function. Such a pattern seems common, and |mkgenlet|
interface is built around it. Furthermore, memo keys are no longer integers;
therefore, the user has to supply the key comparison function
|'k->'k->bool|. Again, it is better to see an example – the same
Ackermann function specialization example, but written this time in
MetaOCaml (the example is part of the MetaOCaml test suite).

let sack m =
with_locus_rec @@ fun l ->
let g = mkgenlet l (=) in
let rec loop m =
if m = 0 then .<fun n -> n + 1>. else
.<fun n -> if n = 0 then . (g loop (m-1)) 1
else . (g loop (m-1)) (. (g loop m) (n-1))>.
in g loop m

The interface is designed so that we could blindly put |g|
before every recursive call, and obtained the desired generator.
The generated code is identical to that for |cack2| shown earlier.

The major difference of MetaOCaml let-insertion is that it never
produces ill-scoped code (that is, code with a scope extrusion).  In
MetaOCaml, let(rec) is actually inserted either at the place of the
explicit |let locus|, or at the binding that dominates all free
variables of the bound expression, whichever has the narrowest scope.

§ RELATED WORK

It was recognized early
on [1, 9]
that one can use control effects (either direct or realized via CPS)
to answer the compositionality challenge of the ordinary, well-nested
let-insertion. [4] give a comprehensive formal
treatment.  Unfortunately, neither the standard CPS nor the
well-understood |shift| operator are of any help with let-insertion
that does not follow the stack discipline and crosses
already-generated bindings. This problem was discussed in
[5], which proposed a very complicated
transformation for hygienic let-insertion across bindings, whose
correctness was only conjectured. The theory of code movement across
already-generated bindings was later developed in
[6], using operational semantics; it
did not include let-insertion however.

Semantics for multi-stage languages have from the very earliest
works [14] generally been given operationally;
our denotational presentation is unusual in this respect.  In contrast,
earlier work on two-level languages used a similar style to ours:
[11] give a denotational semantics in which the
meta-language semantics is parameterized by the semantics of the base
language.  (However, that work did not investigate let-insertion.)

Generating (mutually) recursive bindings has not previously been
formally considered at all, to our knowledge.

§ CONCLUSIONS

We have developed an executable
denotational semantics for let(rec) insertion.  The next step is to
develop a type system that prevents scope extrusion. Our semantics,
for the first time, lets us reason about the code with the generated
let-statements, and we plan to demonstrate this facility on standard
interesting examples
(e.g. from [4, 15, 6]).

Another item for future work is incorporating various strategies
for code motion into our framework, including:
* what we have now, with programmer-supplied locations for each piece of code
* MetaOCaml's bubble-up approach
* LMS's sea-of-nodes approach, with a global value-dependency
analysis that performs both code motion and dead code elimination.

For example, I think the sea-of-nodes approach could be thought of as
dealing exclusively with floating_code and, rather than using
genlet_locus, searching for an optimal set of locations that satisfies
the free-variable dependencies.

§ ACKNOWLEDGMENTS

We are grateful to the anonymous reviewers for many very helpful
suggestions, which greatly improved the presentation.
We thank Yukiyoshi Kameyama for hospitality.
This work was partially supported by JSPS KAKENHI Grant Number

[1]
Anders Bondorf.
Improving binding times without explicit CPS-conversion.
In Proceedings of the 1992 ACM Conference on LISP and Functional
Programming, LFP '92, pages 1–10, New York, NY, USA, 1992. ACM.

[2]
Oleg Kiselyov.
The design and implementation of BER MetaOCaml.
In Michael Codish and Eijiro Sumii, editors, Functional and
Logic Programming, volume 8475 of Lecture Notes in Computer Science,
pages 86–102. Springer International Publishing, 2014.

[3]
Oleg Kiselyov.
Higher-order programming is an effect, 2017.
HOPE Workshop at ICFP 2017.

[4]
Yukiyoshi Kameyama, Oleg Kiselyov, and Chung-chieh Shan.
Shifting the stage: Staging with delimited control.
Journal of Functional Programming, 21(6):617–662, November

[5]
Yukiyoshi Kameyama, Oleg Kiselyov, and Chung-chieh Shan.
Combinators for impure yet hygienic code generation.
Science of Computer Programming, 112 (part 2):120–144,
November 2015.

[6]
Oleg Kiselyov, Yukiyoshi Kameyama, and Yuto Sudo.
Refined environment classifiers - type- and scope-safe code
generation with mutable cells.
In Atsushi Igarashi, editor, Programming Languages and Systems -
14th Asian Symposium, APLAS 2016, Hanoi, Vietnam, November 21-23, 2016,
Proceedings, volume 10017 of Lecture Notes in Computer Science, pages
271–291, 2016.

[7]
Oleg Kiselyov, Shin-Cheng Mu, and Amr Sabry.
Not by equations alone: Reasoning with extensible effects.
Journal of Functional Programming, 31:e2, 2021.

[8]
Oleg Kiselyov and K. C. Sivaramakrishnan.
Eff directly in OCaml.
In Kenichi Asai and Mark R. Shinwell, editors, Proceedings ML
Family Workshop / OCaml Users and Developers workshops, ML/OCAML 2016,
Nara, Japan, September 22-23, 2016, volume 285 of EPTCS, pages
23–58, 2016.

[9]
Julia L. Lawall and Olivier Danvy.
Continuation-based partial evaluation.
In Proceedings of the 1994 ACM Conference on LISP and Functional
Programming, LFP '94, pages 227–238, New York, 1994. ACM.

[10]
Peter D. Mosses.
Denotational semantics.
In J. van Leewen, editor, Handbook of Theoretical Computer
Science, volume B: Formal Models and Semantics, chapter 11, pages 577–631.
The MIT Press, New York, NY, 1990.

[11]
Flemming Nielson and Hanne Riis Nielson.
Two-Level Functional Languages.
Cambridge University Press, Cambridge, 1992.

[12]
Georg Ofenbeck, Tiark Rompf, and Markus Püschel.
RandIR: differential testing for embedded compilers.
In Proceedings of the 7th ACM SIGPLAN Symposium on Scala,
SCALA@SPLASH 2016, pages 21–30. ACM, October 30 - November 4 2016.

[13]
Eijiro Sumii and Naoki Kobayashi.
A hybrid approach to online and offline partial evaluation.
Higher-Order and Symbolic Computation, 14(2–3):101–142,
September 2001.

[14]
Walid Taha, Zine-El-Abidine Benaissa, and Tim Sheard.
Multi-stage programming: Axiomatization and type safety.
In Kim Guldstrand Larsen, Sven Skyum, and Glynn Winskel, editors,
Automata, Languages and Programming, 25th International Colloquium,
ICALP'98, Aalborg, Denmark, July 13-17, 1998, Proceedings, volume 1443 of
Lecture Notes in Computer Science, pages 918–929. Springer, 1998.

[15]
Jeremy Yallop and Oleg Kiselyov.
Generating mutually recursive definitions.
In Proceedings of the 2019 ACM SIGPLAN Workshop on Partial
Evaluation and Program Manipulation, PEPM 2019, pages 75–81, New York, NY,
USA, 2019. ACM.

§ BASE AND CODEC, EXECUTABLE

The base calculus can be represented as an OCaml signature. Calculus
expressions of type |'a| are represented as OCaml values of
type |'a repr|. The mutually recursive |mletrec| takes a collection
of clauses indexed by |idx|; the first argument to |mletrec| indicates the
number of clauses. We gave |mletrec| a general type, with polymorphic |'a|.
In all practical cases, |'a| should be a function type and
| ('a->'b) repr| should start with a lambda.
The form |letrec| is an instance of the general |mletrec|.

The |R| and |S| semantics of the calculus are then implementations
of the signature. Using OCaml to express the denotational semantics
was argued in <cit.>.

type 'a repr

val lam  : ('a repr -> 'b repr) -> ('a->'b) repr
val let_ : 'a repr -> ('a repr ->'b repr) -> 'b repr
val (/)  : ('a->'b) repr -> ('a repr -> 'b repr) (* application *)
val if_  : bool repr -> 'a repr -> 'a repr -> 'a repr

type idx = int
val mletrec : idx ->
((idx -> 'a repr) -> (idx -> 'a repr)) ->
((idx -> 'a repr) -> 'w repr) -> 'w repr

val letrec : (('a->'b) repr -> 'a repr -> 'b repr) ->
(('a->'b) repr -> 'w repr) -> 'w repr

val int  : int -> int repr
val bool : bool -> bool repr

val succ   : int repr -> int repr
val ( +  ) : int repr -> int repr -> int repr
val ( -  ) : int repr -> int repr -> int repr
val ( *  ) : int repr -> int repr -> int repr
val ( =. ) : int repr -> int repr -> bool repr

Base calculus represented in OCaml: its syntax as OCaml signature

type 'a cd  (* the type of code values *)

val clam  : ('a cd repr -> 'b cd repr) -> ('a->'b) cd repr
val clet : 'a cd repr -> ('a cd repr -> 'b cd repr) -> 'b cd repr

val cint : int  -> int cd repr
val cbool : bool -> bool cd repr

val csucc : int cd repr -> int cd repr

val ( +
val ( -
val ( *
val (=

val (/
val cif_   : bool cd repr -> 'a cd repr -> 'a cd repr -> 'a cd repr

type memo_key = int repr
type locus_t
val genlet_locus : (locus_t -> 'w cd repr) -> 'w cd repr
val genlet : locus_t -> memo_key -> 'a cd repr -> 'a cd repr

val genletrec_locus : (locus_t -> 'w cd repr) -> 'w cd repr
val genletrec : locus_t -> memo_key -> ('a->'b) cd repr -> ('a->'b) cd repr

Codec calculus represented in OCaml: its syntax as OCaml
signature. The calculus includes the whole Base calculus; only the
extension is shown.

Figure <ref> presents the |Codec| calculus in the form
of an OCaml signature. To be precise, the figure shows only the
extension of |Base|, with the type of code values and combinators to
produce them. A code value is annotated only with the type of the
expression it generates, with no further classifiers (at present).

The combinator |clam| builds a lambda-expression given the open
expression for the function body. Let-insertion combinators are
the simple, local
let-insertion |clet| demonstrated in <ref>;
|genlet| for non-recursive definitions, described in
<ref> and |genletrec| for mutually recursive definitions,
<ref>. The locus argument of the latter two is
a variable bound by
the corresponding |genlet_locus|, not an expression; therefore its
type is just |locus_t| rather than |locus_t repr|.