Appendix A

Reference card: rules and algorithms

This appendix lists the formal rules used in the document, each with the page where its source states it; Chapter 2, Chapter 3, Chapter 4, Chapter 5 and Chapter 7 explain them. Page references [MN p. n] use the printed page numbers ofDale Miller and Gopalan Nadathur. Programming with Higher-Order Logic. Cambridge University Press, Cambridge, 2012; papers are cited with their own page numbers.

A.1λProlog concrete syntax and its logical reading

Table A.1 maps every λProlog construct used in the document to the logical formula it denotes.

λProloglogicnotes
true⊤\toptype 𝗈\mathsf{o} [MN p. 35]
G1, G2G1∧G2G_1\wedge G_2infix, left assoc.; joins goals [MN p. 35,47–48]
D1 & D2D1∧D2D_1\wedge D_2infix, right assoc.; joins clauses [MN p. 35,47–48]
G1 ; G2G1∨G2G_1\vee G_2infix, left assoc. [MN p. 35]
D => GD⊃GD\supset Ginfix, right assoc.; implication in goals [MN p. 35,79]
A :- GG⊃AG\supset Ainfix, non-assoc.; “is implied by”, used in clauses [MN p. 35,47–48]
pi x\ B, pi (x:T)\ B∀x B\forall x\,B, ∀τx B\forall_\tau x\,B∀:(A→𝗈)→𝗈\forall:(A\to\mathsf{o})\to\mathsf{o} applied to λx. B\lambda x.\, B [MN p. 36–37,99]
sigma x\ B∃x B\exists x\,B∃:(A→𝗈)→𝗈\exists:(A\to\mathsf{o})\to\mathsf{o} applied to λx. B\lambda x.\, B [MN p. 36–37,99]
x\ t, (x:T)\ tλx. t\lambda x.\, t, λ(x:τ) t\lambda(x{:}\tau)\,tbody extends as far right as possible [MN p. 97]
t1 t2(t1 t2)(t_1\,t_2)application, left assoc., binds tightest [MN p. 97]
X, L1 (free, uppercase)∀\forall at clause top; ∃\exists in a queryimplicit quantification [MN p. 43,46–47]
_a fresh variable per occurrenceanonymous, ∀\forall over the clause [MN p. 46–47]
X = YX=YX = Ybuilt-in equality, solved by unification [MN p. 106,287]
false (user constant)⊥\bot of minimal logicno ex falso for it; ¬B\neg B is B⊃⊥B\supset\bot [MN p. 89–90]
A -> B (in types)τ→σ\tau\to\sigmaright assoc. [MN p. 12–13]
o𝗈\mathsf{o}the type of propositions [MN p. 34–35]
kind c type -> type.a type constructor of arity 1kind declaration [MN p. 11–12]
type c T.c:τ∈Σc:\tau\in\Sigmaconstant declaration [MN p. 47]
Table A.1: λProlog syntax and logic.

Binding strength, tightest first: application >> => >> & >> , >> ; >> :- [MN p. 35]; the scope of x\ extends as far right as possible [MN p. 37,97].

Pitfall (precedence). D => G1, G2 parses as (D => G1), G2, so a hypothesis meant for a whole conjunction needs parentheses [MN p. 35].

A.2Goal and clause grammars

The four languages share one top-level shape and differ in which connectives goals may contain and in what an atom may be (Table A.2).

Table A.2: Goal formulas GG and program clauses DD. AA: atom; ArA_r: rigid atom.
languagegrammarsource
fohcG::=⊤∣A∣G∧G∣G∨G∣∃τx GG ::= \top \mid A \mid G\wedge G \mid G\vee G \mid \exists_\tau x\,G[MN p. 43]
D::=A∣G⊃D∣D∧D∣∀τx DD ::= A \mid G\supset D \mid D\wedge D \mid \forall_\tau x\,D
fohhG::=⊤∣A∣G∧G∣G∨G∣∃τx G∣D⊃G∣∀τx GG ::= \top \mid A \mid G\wedge G \mid G\vee G \mid \exists_\tau x\,G \mid D\supset G \mid \forall_\tau x\,G[MN p. 75]
D::=A∣G⊃D∣D∧D∣∀τx DD ::= A \mid G\supset D \mid D\wedge D \mid \forall_\tau x\,D
hohcas fohc, with A∈ℋ1ΣA\in\mathcal H_1^\Sigma in goals and heads Ar∈ℋ1ΣA_r\in\mathcal H_1^\Sigma[MN p. 122–123]
hohhas fohh, with A∈ℋ2ΣA\in\mathcal H_2^\Sigma in goals and heads Ar∈ℋ2ΣA_r\in\mathcal H_2^\Sigma[MN p. 124]
hohh+\textit{hohh}^+hohh, plus atomic heads whose head is a variable bound by an essentially universal quantifier that has the clause in its scope[MN p. 125]

Definition A.1 (Herbrand universes [MN p. 122]). ℋ1Σ\mathcal H_1^\Sigma is the set of (possibly open) λ-normal terms over Σ\Sigma that contain neither ∀\forall nor ⊃\supset; ℋ2Σ\mathcal H_2^\Sigma is the set of those that do not contain ⊃\supset.

Definition A.2 (rigid and flexible atoms [MN p. 119]). An atom is flexible if the head hh of its λ-normal form (h t1…tn)(h\,t_1\dots t_n) is a variable, and rigid if hh is a non-logical constant.

Definition A.3 (polarity; hereditary Harrop [MN p. 76]). BB is positive in BB; a positive (negative) occurrence in BB stays positive (negative) in B∧B′B\wedge B', B′∧BB'\wedge B, B∨B′B\vee B', B′∨BB'\vee B, B′⊃BB'\supset B, ∀x B\forall x\,B, ∃x B\exists x\,B, and becomes negative (positive) in B⊃B′B\supset B'. A hereditary Harrop formula is one in which no positive subformula occurrence is a disjunction or an existential quantification.

Definition A.4 (essentially universal / existential [MN p. 205]). In a goal, a bound-variable occurrence is essentially universal if it is bound by a positive ∀\forall, a negative ∃\exists or a term-level λ\lambda, and essentially existential otherwise; in a clause the classification is dual (negative ∀\forall, positive ∃\exists or λ\lambda are essentially universal).

Definition A.5 (Lλ [MN p. 205–206,220]). A goal or clause BB is in LλL_\lambda if all quantification in BB is over non-predicate types and every subterm (x t1…tn)(x\,t_1\dots t_n) of BB with xx essentially existential has t1,…,tnt_1,\dots,t_n distinct essentially universal variables bound within the scope of xx.

A.3Uniform proofs

A sequent Σ;𝒫⟶G\Sigma;\mathcal{P}\longrightarrow G says that GG is provable from program 𝒫\mathcal{P} over signature Σ\Sigma; Σ;𝒫→  D  A\Sigma;\mathcal{P}\xrightarrow{\;D\;}A says that AA is proved by backchaining on the clause DD [MN p. 39,45]. Read bottom-up, the right rules (goal reduction) and the left rules (backchaining) are the two phases of a uniform, i.e. goal-directed, proof; a logic in which such proofs are complete is an abstract logic programming language [MN p. 73].

Rules A.6 (Right rules).

Σ;𝒫⟶⊤  trueΣ;𝒫⟶B1Σ;𝒫⟶B2Σ;𝒫⟶B1∧B2  andΣ;𝒫⟶BiΣ;𝒫⟶B1∨B2  or (i=1,2)\dfrac{}{\Sigma;\mathcal{P}\longrightarrow\top}\;\text{true} \qquad \dfrac{\Sigma;\mathcal{P}\longrightarrow B_1 \qquad \Sigma;\mathcal{P}\longrightarrow B_2}{\Sigma;\mathcal{P}\longrightarrow B_1\wedge B_2}\;\text{and} \qquad \dfrac{\Sigma;\mathcal{P}\longrightarrow B_i}{\Sigma;\mathcal{P}\longrightarrow B_1\vee B_2}\;\text{or}\ (i=1,2)Σ;𝒫⟶B[t/x]Σ;∅⊩t:τΣ;𝒫⟶∃τx B  instanΣ;𝒫,B1⟶B2Σ;𝒫⟶B1⊃B2  augment\dfrac{\Sigma;\mathcal{P}\longrightarrow B[t/x] \qquad \Sigma;\emptyset\Vdash t:\tau}{\Sigma;\mathcal{P}\longrightarrow \exists_\tau x\,B}\;\text{instan} \qquad \dfrac{\Sigma;\mathcal{P},B_1\longrightarrow B_2}{\Sigma;\mathcal{P}\longrightarrow B_1\supset B_2}\;\text{augment}c:τ,Σ;𝒫⟶B[c/x]Σ;𝒫⟶∀τx B  generic (c∉Σ)\dfrac{c{:}\tau,\Sigma;\mathcal{P}\longrightarrow B[c/x]}{\Sigma;\mathcal{P}\longrightarrow \forall_\tau x\,B}\;\text{generic}\ (c\notin\Sigma)

Right rules (search semantics of the connectives) [MN p. 39–40]. In the book they are ⊤R\top R, ∧R\wedge R, ∨R\vee R, ∃R\exists R, ⊃R\supset R, ∀R\forall R; 𝒫,B\mathcal{P},B is 𝒫∪{B}\mathcal{P}\cup\{B\}.

Rules A.7 (Backchaining (left) rules).

Σ;𝒫→  D  AΣ;𝒫⟶A  decide (D∈𝒫)Σ;𝒫→  A  A  initialΣ;𝒫→  D  AΣ;𝒫⟶GΣ;𝒫→  G⊃D  A  ⊃L\dfrac{\Sigma;\mathcal{P}\xrightarrow{\;D\;}A}{\Sigma;\mathcal{P}\longrightarrow A}\;\text{decide}\ (D\in\mathcal{P}) \qquad \dfrac{}{\Sigma;\mathcal{P}\xrightarrow{\;A\;}A}\;\text{initial} \qquad \dfrac{\Sigma;\mathcal{P}\xrightarrow{\;D\;}A \qquad \Sigma;\mathcal{P}\longrightarrow G}{\Sigma;\mathcal{P}\xrightarrow{\;G\supset D\;}A}\;{\supset}LΣ;𝒫→  Di  AΣ;𝒫→  D1∧D2  A  ∧L (i=1,2)Σ;𝒫→  D[t/x]  AΣ;∅⊩t:τΣ;𝒫→  ∀τx D  A  ∀L\dfrac{\Sigma;\mathcal{P}\xrightarrow{\;D_i\;}A}{\Sigma;\mathcal{P}\xrightarrow{\;D_1\wedge D_2\;}A}\;{\wedge}L\ (i=1,2) \qquad \dfrac{\Sigma;\mathcal{P}\xrightarrow{\;D[t/x]\;}A \qquad \Sigma;\emptyset\Vdash t:\tau}{\Sigma;\mathcal{P}\xrightarrow{\;\forall_\tau x\,D\;}A}\;{\forall}LΣ;𝒫⟶A1θ⋯Σ;𝒫⟶AnθΣ;𝒫→  ∀x¯ (A1∧⋯∧An⊃A0)  A  backchain\dfrac{\Sigma;\mathcal{P}\longrightarrow A_1\theta \qquad \cdots \qquad \Sigma;\mathcal{P}\longrightarrow A_n\theta}{\Sigma;\mathcal{P}\xrightarrow{\;\forall\bar x\,(A_1\wedge\dots\wedge A_n\supset A_0)\;}A}\;\text{backchain}(θ maps each xi to a Σ-term of type τi, and A=A0θ)\text{($\theta$ maps each $x_i$ to a $\Sigma$-term of type $\tau_i$, and $A=A_0\theta$)}

Backchaining (left) rules [MN p. 45]; backchain is the compiled form for Prolog-shaped clauses [MN p. 45].

Definition A.8 (O-proof [MN p. 45,77]). A derivation built from the typing rules of terms and the rules of Rules A.6 and Rules A.7.

Logic-variable versions [MN p. 61]. instan and ∀L\forall L replace the guessed tt by a new logic variable XX with Σ;∅⊩X:τ\Sigma;\emptyset\Vdash X:\tau, and initial becomes Σ;𝒫→  A′  A\Sigma;\mathcal{P}\xrightarrow{\;A'\;}A provided AA and A′A' unify, the unifier being applied to the whole derivation built so far.

Higher-order versions [MN p. 123–124]. (i) Formulas are taken in λ-normal form when matched against a rule; (ii) initial requires A′A' and AA to be α-convertible; (iii) the term tt of instan and ∀L\forall L is a λ-term with Σ;∅⊩t:τ\Sigma;\emptyset\Vdash t:\tau and t∈ℋ1Σt\in\mathcal H_1^\Sigma (hohc) or t∈ℋ2Σt\in\mathcal H_2^\Sigma for the current Σ\Sigma (hohh).

Backchaining by elaborationDale Miller. A logic programming language with lambda-abstraction, function variables, and simple unification. Journal of Logic and Computation, 1(4):497–536, 1991, p. 9. With ⟨∅,D⟩∈∣Γ∣Σ\langle\emptyset,D\rangle\in|\Gamma|_\Sigma for D∈ΓD\in\Gamma, conjunctions split, ⟨Δ,G⊃D⟩↦⟨Δ∪{G},D⟩\langle\Delta,G\supset D\rangle\mapsto\langle\Delta\cup\{G\},D\rangle and ⟨Δ,∀x D⟩↦⟨Δ,λnorm(D[t/x])⟩\langle\Delta,\forall x\,D\rangle\mapsto\langle\Delta,\lambda\mathrm{norm}(D[t/x])\rangle:

{Σ;Γ⟶G}G∈ΔΣ;Γ⟶A  bc (A atomic, ⟨Δ,A⟩∈∣Γ∣Σ)\dfrac{\{\Sigma;\Gamma\longrightarrow G\}_{G\in\Delta}}{\Sigma;\Gamma\longrightarrow A}\;\text{bc}\ (A\text{ atomic},\ \langle\Delta,A\rangle\in|\Gamma|_\Sigma)

Search conventions of λProlog [MN p. 60,78]. Conjunctive goals left to right; the left disjunct of a goal and the left conjunct of a clause first; clauses tried in program order (the program is a list); augment adds its clauses at the front; signature and program grow and shrink in a stack discipline.

Flexible goals [MN p. 134]. A goal (P t1…tn)(P\,t_1\dots t_n) with PP unbound can be (1) suspended until rigid, and if only flexible goals remain each head is bound to λx1…λxn ⊤\lambda x_1\dots\lambda x_n\,\top (complete for intuitionistic higher-order logic); (2) bound eagerly to λx1…λxn ⊤\lambda x_1\dots\lambda x_n\,\top; or (3) reported as an error; (2) and (3) are incomplete, yet experience favours (3), since a flexible goal is usually a programming mistake such as a capitalised predicate name.

Table A.3: What O-proofs are complete for.
languageΣ;𝒫⟶G\Sigma;\mathcal{P}\longrightarrow G has an O-proof iff it is provable insource
fohcclassical logic, iff intuitionistic logic[MN p. 45,62]
fohhminimal logic, equivalently intuitionistic logic (classical logic is not sound for this operational semantics)[MN p. 89,91]
hohcclassical, equivalently intuitionistic, Simple Theory of Types without extensionality, infinity and choice[MN p. 124,148]
hohhhigher-order intuitionistic logic[MN p. 124]

Proposition A.9 (flatness of fohc [MN p. 46]). Every sequent in an O-proof of an fohc sequent Σ;𝒫⟶G\Sigma;\mathcal{P}\longrightarrow G has the form Σ;𝒫⟶G′\Sigma;\mathcal{P}\longrightarrow G' or Σ;𝒫→  D  A\Sigma;\mathcal{P}\xrightarrow{\;D\;}A: signature and program never change.

Kanren angle (which rules a miniKanren already has). (aux kanren micro) has goals for true (✓°), and (and°), or (or°) and logic-variable instan (fresh°); a relation defined as a Scheme procedure plays the role of decide/backchain, and its unifier is the first-order one of Rules A.22, occurs check included. Two differences matter: or° interleaves its branches instead of following the depth-first order above, and augment and generic have no counterpart, so (aux kanren micro) covers fohc but not fohh; see Chapter 11.

A.4Typed λ-terms

The typing judgement Σ;Γ⊩t:τ\Sigma;\Gamma\Vdash t:\tau assigns types to λ-terms; quantifiers need no rule of their own because they are constants applied to abstractions [MN p. 98–99].

Rules A.10 (Typing rules).

c:σ∈ΣΣ;Γ⊩c:τ  (τ◃σ)x:τ∈ΓΣ;Γ⊩x:τΣ;Γ⊩g:τ1→τ2Σ;Γ⊩t:τ1Σ;Γ⊩(g t):τ2\dfrac{c:\sigma\in\Sigma}{\Sigma;\Gamma\Vdash c:\tau}\;(\tau\triangleleft\sigma) \qquad \dfrac{x:\tau\in\Gamma}{\Sigma;\Gamma\Vdash x:\tau} \qquad \dfrac{\Sigma;\Gamma\Vdash g:\tau_1\to\tau_2 \qquad \Sigma;\Gamma\Vdash t:\tau_1}{\Sigma;\Gamma\Vdash (g\,t):\tau_2}Σ;Γ,x:τ⊩t:σΣ;Γ⊩λ(x:τ) t:τ→σ  (†)Σ;Γ⊩B:τΣ;Γ⊩C:τ  (‡)\dfrac{\Sigma;\Gamma,x:\tau\Vdash t:\sigma}{\Sigma;\Gamma\Vdash \lambda(x{:}\tau)\,t:\tau\to\sigma}\;(\dagger) \qquad \dfrac{\Sigma;\Gamma\Vdash B:\tau}{\Sigma;\Gamma\Vdash C:\tau}\;(\ddagger)

Typing rules [MN p. 98]. τ◃σ\tau\triangleleft\sigma: τ\tau is an instance of σ\sigma; (†\dagger) xx not declared in Σ\Sigma or Γ\Gamma; (‡\ddagger) BB and CC differ only in bound-variable names.

Definition A.11 (order of a type [MN p. 13]). ord(τ)=0\mathrm{ord}(\tau)=0 if τ\tau is non-functional; ord(τ1→τ2)=max⁡(ord(τ1)+1,ord(τ2))\mathrm{ord}(\tau_1\to\tau_2)=\max(\mathrm{ord}(\tau_1)+1,\mathrm{ord}(\tau_2)).

A.5λ-conversion and normal forms

λ-conversion is the equality of the logic, and every typed term has a unique normal form, so equality is decidable [MN p. 101–102].

Definition A.12 (free for; substitution [MN p. 85,100]). tt is free for xx in ss if no free occurrence of xx in ss is in the scope of an abstraction (or quantifier) binding a variable free in tt. s[t/x]s[t/x] replaces the free occurrences of xx in ss by tt, after α-renaming ss so that tt is free for xx (capture-avoiding substitution) [MN p. 101].

Table A.4: Rewrite rules on subterms [MN p. 100–101].
ruleredex ⇒\Rightarrow contractumproviso
α-rewritingλx. s⇒λy. s[y/x]\lambda x.\, s \Rightarrow \lambda y.\, s[y/x]yy free for xx in ss, yy not free in ss
β-contraction(λx. s) t⇒s[t/x](\lambda x.\, s)\,t \Rightarrow s[t/x]none (capture-avoiding s[t/x]s[t/x])
η-contractionλx. (s x)⇒s\lambda x.\, (s\,x) \Rightarrow sxx not free in ss
β0\beta_0 [MN p. 206](λx. s) x⇒s(\lambda x.\, s)\,x \Rightarrow s; up to α: (λy. s) x⇒s[x/y](\lambda y.\, s)\,x\Rightarrow s[x/y](second form) xx not free in λy. s\lambda y.\, s

Definition A.13 (closures [MN p. 100–101]). α-conversion: reflexive, symmetric, transitive closure of α-rewriting. β-reduction: reflexive, transitive closure of α-conversion ∪\cup β-contraction; β-conversion: its symmetric, transitive closure. η-reduction / η-conversion: likewise from η-contraction. λ-conversion (=βη=_{\beta\eta}): transitive closure of α-, β- and η-conversion; it is a congruence and is the equality of higher-order logic.

Definition A.14 (normal forms [MN p. 101–102,111]). A term is β-normal if it has no β-redex, λ-normal (βη-normal) if it is β-normal with no η-redex. Every β-normal term has the shape λx1…λxn (h t1…tp)\lambda x_1\dots\lambda x_n\,(h\,t_1\dots t_p) (n,p≥0n,p\ge0), with binder x1…xnx_1\dots x_n, head hh (a constant or variable), arguments tit_i (β-normal) and body (h t1…tp)(h\,t_1\dots t_p). It is in βη-long normal form if moreover each tit_i is βη-long and the body has non-functional (for simple types: primitive) type.

Theorem A.15 (normalisation [MN p. 102]). Every typed λ-term has a λ-normal form, unique up to α-conversion; it is computed by β-contracting until β-normal and then η-contracting. Two terms of the same type are λ-convertible iff their λ-normal forms are α-equivalent. Every β-normal form can be η-expanded to an equal βη-long form [MN p. 111].

Example A.16 (η-long expansion [MN p. 111]). With f:(i→i)→if:(i\to i)\to i: f  ⇝  λw. f wf \;\rightsquigarrow\; \lambda w.\, f\,w (w:i→iw:i\to i)   ⇝  λw. f (λu. w u)\;\rightsquigarrow\; \lambda w.\, f\,(\lambda u.\, w\,u) (u:iu:i), which is βη-long.

Definition A.17 (rigid and flexible terms [MN p. 111–112,214]). A β-normal term λx¯ (h t¯)\lambda\bar x\,(h\,\bar t) in a unification problem is rigid if hh is universally quantified in the prefix (a constant) or a member of its binder x¯\bar x, and flexible if hh is existentially quantified; equations are rigid-rigid, flexible-rigid, rigid-flexible or flexible-flexible [MN p. 113].

A.6Unification problems, raising, Skolemization

Higher-order unification problems carry a mixed quantifier prefix, which raising normalises to ∀∃∀\forall\exists\forall [MN p. 105–108,212].

Definition A.18 (unification problem [MN p. 105,211]). A formula Q1x1…Qnxn [t1=s1∧⋯∧tm=sm]Q_1x_1\dots Q_nx_n\,[t_1=s_1\wedge\dots\wedge t_m=s_m] with Qi∈{∀,∃}Q_i\in\{\forall,\exists\} and tj,sjt_j,s_j λ-terms of the same type; the quantifiers form the prefix, the conjunction the matrix.

Definition A.19 (unifier, solution [MN p. 109]). For ∀u1…∀up ∃x1…∃xn ∀w1…∀wq [t1=s1∧⋯∧tm=sm]\forall u_1\dots\forall u_p\,\exists x_1\dots\exists x_n\,\forall w_1\dots\forall w_q\,[t_1=s_1\wedge\dots\wedge t_m=s_m], a substitution θ\theta for x1,…,xnx_1,\dots,x_n is legitimate if no wjw_j is free in its range; a unifier is a legitimate θ\theta with θ(tj)=βηθ(sj)\theta(t_j)=_{\beta\eta}\theta(s_j) for all jj; a solution is a unifier whose range has free variables only among u1,…,upu_1,\dots,u_p. Only a solution guarantees provability, since types are not assumed inhabited [MN p. 109–110].

Example A.20 (the prefix order matters [MN p. 110,213]). With g:i→i→ig:i\to i\to i, ∀a ∃F [(F a)=(g a a)]\forall a\,\exists F\,[(F\,a)=(g\,a\,a)] has the four unifiers F:=λx. g a aF:=\lambda x.\, g\,a\,a, λx. g x a\lambda x.\, g\,x\,a, λx. g a x\lambda x.\, g\,a\,x, λx. g x x\lambda x.\, g\,x\,x, none an instance of another, so there is no most general unifier; in ∃F ∀a [(F a)=(g a a)]\exists F\,\forall a\,[(F\,a)=(g\,a\,a)] the solution FF cannot mention aa, and λa. g a a\lambda a.\, g\,a\,a is the only one.

Proposition A.21 (raising [MN p. 107]). With x:τx:\tau, y:σy:\sigma and h:σ→τh:\sigma\to\tau: ∀y ∃x G\forall y\,\exists x\,G is provable iff ∃h ∀y G[(h y)/x]\exists h\,\forall y\,G[(h\,y)/x] is provable.

As a rewrite on prefixes, for a block of universals:

∀y1:σ1…∀yk:σk  ∃x:τ  G  ⇝  ∃h:σ1→⋯→σk→τ  ∀y1…∀yk  G[(h y1…yk)/x].\forall y_1{:}\sigma_1\dots\forall y_k{:}\sigma_k\;\exists x{:}\tau\;G \;\rightsquigarrow\; \exists h{:}\sigma_1\to\dots\to\sigma_k\to\tau\;\forall y_1\dots\forall y_k\;G[(h\,y_1\dots y_k)/x].

Repeated raising turns any prefix into ∀∃∀\forall\exists\forall or even ∃∀\exists\forall form [MN p. 108], and it preserves LλL_\lambda [MN p. 208]. Miller’s transition formDale Miller. A logic programming language with lambda-abstraction, function variables, and simple unification. Journal of Logic and Computation, 1(4):497–536, 1991, p. 14–16: if ∃v\exists v is to the left of ∃u\exists u with universals w¯\bar w in between, replace uu by (u′ w¯)(u'\,\bar w) and move ∃u′\exists u' next to ∃v\exists v.

Skolemization [MN p. 108]. An assumption ∀x ∃y D\forall x\,\exists y\,D becomes ∀x D[(f x)/y]\forall x\,D[(f\,x)/y] with f:σ→τf:\sigma\to\tau a new constant; dually a goal ∃x ∀y G\exists x\,\forall y\,G becomes ∀f ∃x G[(f x)/y]\forall f\,\exists x\,G[(f\,x)/y]. Skolemization moves an ∃\exists inwards and introduces a constant of raised type; raising moves a ∀\forall inwards and introduces an existential variable of raised type; relating unifiers of Skolemized problems to the original ones is problematic for higher-order variables, whereas raising is an exact equivalence [MN p. 107–108].

Normal form of problems [MN p. 212]. Before solving, (1) the prefix is raised to ∀a¯ ∃X¯ ∀y¯\forall\bar a\,\exists\bar X\,\forall\bar y, with ∀a¯\forall\bar a the signature; (2) θ(t)\theta(t) is defined as ((λx1…λxn t) t1…tn)((\lambda x_1\dots\lambda x_n\,t)\,t_1\dots t_n) for θ={⟨xi,ti⟩}\theta=\{\langle x_i,t_i\rangle\}, so terms may be taken β-normal; (3) η-expansion makes the two sides of each equation have binders of the same length; (4) by (λx. t=λx. s)≡∀x (t=s)(\lambda x.\, t=\lambda x.\, s)\equiv\forall x\,(t=s) the binders are moved into the inner ∀\forall block:

⟨𝒬; {λx. s=λx. t}⊎E⟩⟨𝒬 ∀x; {s=t}⊎E⟩  abs⟨𝒬; {λx. s=t}⊎E⟩⟨𝒬; {λx. s=λx. (t x)}⊎E⟩  eta (t not an abstraction)\dfrac{\langle\mathcal Q;\ \{\lambda x.\, s=\lambda x.\, t\}\uplus E\rangle}{\langle\mathcal Q\,\forall x;\ \{s=t\}\uplus E\rangle}\;\text{abs} \qquad \dfrac{\langle\mathcal Q;\ \{\lambda x.\, s=t\}\uplus E\rangle}{\langle\mathcal Q;\ \{\lambda x.\, s=\lambda x.\, (t\,x)\}\uplus E\rangle}\;\text{eta}\ (t\text{ not an abstraction})

Here and below a rule is read downwards as one transformation step, ⊎\uplus is multiset union, xx is fresh, and eta also applies with the two sides exchanged.

A.7First-order unification

Term reduction, reorientation and variable elimination with occurs check; higher-order pattern unification has the same two critical parts, simplification and variable elimination [MN p. 224].

Rules A.22 (First-order unification).

E⊎{f t1…tn=f s1…sn}E⊎{t1=s1,…,tn=sn}  decomposeE⊎{f t¯=g s¯}⊥  clash (f≠g or different types)\dfrac{E\uplus\{f\,t_1\dots t_n=f\,s_1\dots s_n\}}{E\uplus\{t_1=s_1,\dots,t_n=s_n\}}\;\text{decompose} \qquad \dfrac{E\uplus\{f\,\bar t=g\,\bar s\}}{\bot}\;\text{clash}\ (f\neq g\text{ or different types})E⊎{t=x}E⊎{x=t}  orient (t not a variable)E⊎{x=x}E  deleteE⊎{x=t}⊥  check (x∈fv(t), t≠x)\dfrac{E\uplus\{t=x\}}{E\uplus\{x=t\}}\;\text{orient}\ (t\text{ not a variable}) \qquad \dfrac{E\uplus\{x=x\}}{E}\;\text{delete} \qquad \dfrac{E\uplus\{x=t\}}{\bot}\;\text{check}\ (x\in\mathrm{fv}(t),\ t\neq x)E⊎{x=t}E[t/x]⊎{x=t}  eliminate (x∉fv(t), x∈fv(E))\dfrac{E\uplus\{x=t\}}{E[t/x]\uplus\{x=t\}}\;\text{eliminate}\ (x\notin\mathrm{fv}(t),\ x\in\mathrm{fv}(E))

Term reduction, reorientation and variable elimination [MN p. 28]; the occurs check is mandatory [MN p. 32].

Theorem A.23 ([MN p. 28–29]). Every sequence of steps (never eliminating twice on the same unchanged equation) terminates in ⊥\bot or in a solved form (each left side a variable occurring nowhere else), which is a most general unifier, unique up to renaming.

A.8Huet’s procedure

On a normalised problem (Section A.6) Huet’s procedure simplifies rigid-rigid equations and guesses the head of a flexible term facing a rigid one [MN p. 214–216].

Rules A.24 (Huet’s transformation rules).

⟨𝒬; {c s1…sn=c t1…tn}⊎E⟩⟨𝒬; {s1=t1,…,sn=tn}⊎E⟩  simp (c universal)⟨𝒬; {c s¯=d t¯}⊎E⟩⊥  clash (c≠d universal)\dfrac{\langle\mathcal Q;\ \{c\,s_1\dots s_n=c\,t_1\dots t_n\}\uplus E\rangle}{\langle\mathcal Q;\ \{s_1=t_1,\dots,s_n=t_n\}\uplus E\rangle}\;\text{simp}\ (c\text{ universal}) \qquad \dfrac{\langle\mathcal Q;\ \{c\,\bar s=d\,\bar t\}\uplus E\rangle}{\bot}\;\text{clash}\ (c\neq d\text{ universal})⟨𝒬; {r=f}⊎E⟩⟨𝒬; {f=r}⊎E⟩  swap (r rigid, f flexible)\dfrac{\langle\mathcal Q;\ \{r=f\}\uplus E\rangle}{\langle\mathcal Q;\ \{f=r\}\uplus E\rangle}\;\text{swap}\ (r\text{ rigid, }f\text{ flexible})⟨𝒬1 ∃F 𝒬2; {F t1…tn=c s1…sm}⊎E⟩⟨𝒬1 ∃H1…∃Hm 𝒬2; θ({F t¯=c s¯}⊎E) ⁣↓β⟩  imitate\dfrac{\langle\mathcal Q_1\,\exists F\,\mathcal Q_2;\ \{F\,t_1\dots t_n=c\,s_1\dots s_m\}\uplus E\rangle}{\langle\mathcal Q_1\,\exists H_1\dots\exists H_m\,\mathcal Q_2;\ \theta(\{F\,\bar t=c\,\bar s\}\uplus E)\!\downarrow_\beta\rangle}\;\text{imitate}θ={⟨F, λx1…λxn (c (H1 x1…xn)⋯(Hm x1…xn))⟩},∀c∈𝒬1,Hi:τ1→⋯→τn→σi\theta=\{\langle F,\ \lambda x_1\dots\lambda x_n\,(c\,(H_1\,x_1\dots x_n)\cdots(H_m\,x_1\dots x_n))\rangle\},\quad \forall c\in\mathcal Q_1,\quad H_i:\tau_1\to\dots\to\tau_n\to\sigma_i⟨𝒬1 ∃F 𝒬2; {F t1…tn=c s1…sm}⊎E⟩⟨𝒬1 ∃H1…∃Hk 𝒬2; θ({F t¯=c s¯}⊎E) ⁣↓β⟩  projecti\dfrac{\langle\mathcal Q_1\,\exists F\,\mathcal Q_2;\ \{F\,t_1\dots t_n=c\,s_1\dots s_m\}\uplus E\rangle}{\langle\mathcal Q_1\,\exists H_1\dots\exists H_k\,\mathcal Q_2;\ \theta(\{F\,\bar t=c\,\bar s\}\uplus E)\!\downarrow_\beta\rangle}\;\text{project}_iθ={⟨F, λx1…λxn (xi (H1 x1…xn)⋯(Hk x1…xn))⟩},τi=φ1→⋯→φk→β,Hj:τ1→⋯→τn→φj\theta=\{\langle F,\ \lambda x_1\dots\lambda x_n\,(x_i\,(H_1\,x_1\dots x_n)\cdots(H_k\,x_1\dots x_n))\rangle\},\quad \tau_i=\varphi_1\to\dots\to\varphi_k\to\beta,\quad H_j:\tau_1\to\dots\to\tau_n\to\varphi_j

Huet’s transformation rules [MN p. 214–216], with F:τ1→⋯→τn→βF:\tau_1\to\dots\to\tau_n\to\beta, c:σ1→⋯→σm→βc:\sigma_1\to\dots\to\sigma_m\to\beta, β\beta primitive. imitate needs FF in the scope of cc; projecti_i needs the target of τi\tau_i to be β\beta; so at most n+1n+1 choices exist per equation [MN p. 216]. The rewriting format is ours, in the style of the transformation-based presentation of Huet’s procedureGérard Huet. A unification algorithm for typed λ\lambda-calculus. Theoretical Computer Science, 1:27–57, 1975 mentioned on [MN p. 227]; side conditions and substitution terms are the book’s.

Definition A.25 (pre-unification; canonical substitution [MN p. 217–218]). Reducing a problem to one with only flexible-flexible equations is pre-unification, and the composed substitution is a pre-unifier. The canonical substitution maps each existential of type τ1→⋯→τn→σ\tau_1\to\dots\to\tau_n\to\sigma (σ\sigma primitive) to λx1…λxn Hσ\lambda x_1\dots\lambda x_n\,H^\sigma, with one fixed new existential HσH^\sigma per primitive type; on a flexible-flexible problem it is a unifier.

Theorem A.26 (matching trees [MN p. 219–220]). In the matching tree (nodes: simplified matrices; arcs: imitation/projection substitutions for one selected flexible-rigid equation; leaves: failure, or success when only flexible-flexible equations remain), if the root problem has a unifier then a success node exists at finite depth, and that unifier is the composed pre-unifier followed by a unifier of the success node, whichever equation is selected at each node. Since unifiability is undecidable, some trees have an infinite path and no success node [MN p. 220]. As a test for unifiability the procedure is nonredundant [MN p. 227]; mgus may not exist, complete sets of unifiers may be infinite, and searching for them must be redundant [MN p. 213,226].

Example A.27 (a matching tree [MN p. 216–219]). For ∀a ∀g ∃F [(F a)=(g a a)]\forall a\,\forall g\,\exists F\,[(F\,a)=(g\,a\,a)], project1_1 (F:=λx. xF:=\lambda x.\, x) gives a=(g a a)a=(g\,a\,a), a clash; imitate (F:=λx. g (H1 x) (H2 x)F:=\lambda x.\, g\,(H_1\,x)\,(H_2\,x)) and simp give {(H1 a)=a, (H2 a)=a}\{(H_1\,a)=a,\ (H_2\,a)=a\}, and each equation again has an imitation (λx. a\lambda x.\, a) and a projection (λx. x\lambda x.\, x), both succeeding. The four leaves compose to the four unifiers of Example A.20. In ∀g ∃F ∀x [(F (g x))=(g (F x))]\forall g\,\exists F\,\forall x\,[(F\,(g\,x))=(g\,(F\,x))], imitation reproduces the same problem for a new HH, so the tree has an infinite path, alongside the unifiers λx. x\lambda x.\, x, λx. g x\lambda x.\, g\,x, λx. g (g x)\lambda x.\, g\,(g\,x), … [MN p. 218].

A.9Higher-order pattern unification

In LλL_\lambda every flexible term is a pattern, at most one substitution is useful for each flexible-rigid equation, and the procedure becomes a deterministic algorithm with most general unifiers [MN p. 221–223].

Definition A.28 (pattern [MN p. 220]). A flexible term (F c1…cn)(F\,c_1\dots c_n) (n≥0n\ge0) is a pattern if c1,…,cnc_1,\dots,c_n are distinct universal variables quantified to the right of FF (within its scope); c¯∩d¯\bar c\cap\bar d denotes some listing of the variables common to c¯\bar c and d¯\bar d.

Theorem A.29 ([MN p. 208,221–224]). On LλL_\lambda problems the rules of Rules A.30 without delay terminate (thanks to the occurs check, [MN p. 222–223]) and return ⊥\bot or a most general unifier: LλL_\lambda unification is decidable and unitaryDale Miller. A logic programming language with lambda-abstraction, function variables, and simple unification. Journal of Logic and Computation, 1(4):497–536, 1991. Types are never consulted to build the unifier [MN p. 224]; the untyped variant fails rigid-rigid equations of different aritiesDale Miller. A logic programming language with lambda-abstraction, function variables, and simple unification. Journal of Logic and Computation, 1(4):497–536, 1991, § 9.3.

Rules A.30 (Pattern unification).

⟨𝒬; {λx. s=λx. t}⊎E⟩⟨𝒬 ∀x; {s=t}⊎E⟩  abs⟨𝒬; {λx. s=t}⊎E⟩⟨𝒬; {λx. s=λx. (t x)}⊎E⟩  eta (t not an abstraction)⟨𝒬; {r=F c¯}⊎E⟩⟨𝒬; {F c¯=r}⊎E⟩  swap\dfrac{\langle\mathcal Q;\ \{\lambda x.\, s=\lambda x.\, t\}\uplus E\rangle}{\langle\mathcal Q\,\forall x;\ \{s=t\}\uplus E\rangle}\;\text{abs} \qquad \dfrac{\langle\mathcal Q;\ \{\lambda x.\, s=t\}\uplus E\rangle}{\langle\mathcal Q;\ \{\lambda x.\, s=\lambda x.\, (t\,x)\}\uplus E\rangle}\;\text{eta}\ (t\text{ not an abstraction}) \qquad \dfrac{\langle\mathcal Q;\ \{r=F\,\bar c\}\uplus E\rangle}{\langle\mathcal Q;\ \{F\,\bar c=r\}\uplus E\rangle}\;\text{swap}⟨𝒬; {h s1…sn=h t1…tn}⊎E⟩⟨𝒬; {s1=t1,…,sn=tn}⊎E⟩  rr-dec (h universal)⟨𝒬; {h s¯=k t¯}⊎E⟩⊥  rr-clash (h≠k)\dfrac{\langle\mathcal Q;\ \{h\,s_1\dots s_n=h\,t_1\dots t_n\}\uplus E\rangle}{\langle\mathcal Q;\ \{s_1=t_1,\dots,s_n=t_n\}\uplus E\rangle}\;\text{rr-dec}\ (h\text{ universal}) \qquad \dfrac{\langle\mathcal Q;\ \{h\,\bar s=k\,\bar t\}\uplus E\rangle}{\bot}\;\text{rr-clash}\ (h\neq k)⟨𝒬; {F c¯=G d¯}⊎E⟩⟨𝒬 ∃H; θ(E)⟩  ff-diff (F≠G)θ={⟨F,λc¯. H (c¯∩d¯)⟩, ⟨G,λd¯. H (c¯∩d¯)⟩}\dfrac{\langle\mathcal Q;\ \{F\,\bar c=G\,\bar d\}\uplus E\rangle}{\langle\mathcal Q\,\exists H;\ \theta(E)\rangle}\;\text{ff-diff}\ (F\neq G) \quad \theta=\{\langle F,\lambda\bar c.\,H\,(\bar c\cap\bar d)\rangle,\ \langle G,\lambda\bar d.\,H\,(\bar c\cap\bar d)\rangle\}⟨𝒬; {F c1…cn=F d1…dn}⊎E⟩⟨𝒬 ∃H; θ(E)⟩  ff-sameθ={⟨F,λc¯. H e¯⟩}, e¯=the ci with ci=di\dfrac{\langle\mathcal Q;\ \{F\,c_1\dots c_n=F\,d_1\dots d_n\}\uplus E\rangle}{\langle\mathcal Q\,\exists H;\ \theta(E)\rangle}\;\text{ff-same} \quad \theta=\{\langle F,\lambda\bar c.\,H\,\bar e\rangle\},\ \bar e=\text{the } c_i \text{ with } c_i=d_i⟨𝒬; {F c¯=t}⊎E⟩⟨𝒬 ∃G¯′; θ(ρ(E))⟩  elimt rigid; ρ={⟨G,λd¯. G′ (d¯∩c¯)⟩∣(G d¯) flexible in t} (pruning);t′=ρ(t); F∉fv(t′); every universal of t′ in the scope of F is in c¯;θ={⟨F,λc¯. t′⟩}\dfrac{\langle\mathcal Q;\ \{F\,\bar c=t\}\uplus E\rangle}{\langle\mathcal Q\,\exists\bar G';\ \theta(\rho(E))\rangle}\;\text{elim} \quad \begin{array}{l} t \text{ rigid};\ \rho=\{\langle G,\lambda\bar d.\,G'\,(\bar d\cap\bar c)\rangle \mid (G\,\bar d) \text{ flexible in } t\} \text{ (pruning)};\\ t'=\rho(t);\ F\notin\mathrm{fv}(t');\ \text{every universal of } t' \text{ in the scope of } F \text{ is in } \bar c;\\ \theta=\{\langle F,\lambda\bar c.\,t'\rangle\} \end{array}⟨𝒬; {F c¯=t}⊎E⟩⊥  occurs(t rigid, F∈fv(ρ(t)))⟨𝒬; {F c¯=t}⊎E⟩⊥  scope(t rigid, ρ(t) has a universal in the scope of F not in c¯)\dfrac{\langle\mathcal Q;\ \{F\,\bar c=t\}\uplus E\rangle}{\bot}\;\text{occurs} \quad (t \text{ rigid},\ F\in\mathrm{fv}(\rho(t))) \qquad \dfrac{\langle\mathcal Q;\ \{F\,\bar c=t\}\uplus E\rangle}{\bot}\;\text{scope} \quad (t \text{ rigid},\ \rho(t) \text{ has a universal in the scope of } F \text{ not in } \bar c)⟨𝒬; {s=t}⊎E⟩⟨𝒬; E⟩  with s=t deferred  delay\dfrac{\langle\mathcal Q;\ \{s=t\}\uplus E\rangle}{\langle\mathcal Q;\ E\rangle\ \text{ with } s=t \text{ deferred}}\;\text{delay}(a flexible side of s=t is not a pattern; the equation is resumed when substitutions change its status)\text{(a flexible side of $s=t$ is not a pattern; the equation is resumed when substitutions change its status)}

Pattern unification on ∀∃∀\forall\exists\forall problems [MN p. 212,214,221–223]; delay is the dynamic-LλL_\lambda extension [MN p. 225–226]. 𝒬 ∃H\mathcal Q\,\exists H means a new existential in the ∃\exists block; the bindings θ\theta (and ρ\rho) are composed into the answer substitution. Rule names are ours.

Example A.31 (elim, occurs, scope [MN p. 223–224]). In ∀f ∀g ∃U ∃V ∀w ∀x ∀y [(f (U x y))=(f (g (V y w)))]\forall f\,\forall g\,\exists U\,\exists V\,\forall w\,\forall x\,\forall y\,[(f\,(U\,x\,y))=(f\,(g\,(V\,y\,w)))], rr-dec leaves (U x y)=(g (V y w))(U\,x\,y)=(g\,(V\,y\,w)); elim prunes V:=λy. λw. V′ yV:=\lambda y.\, \lambda w.\, V'\,y (since w∉{x,y}w\notin\{x,y\}) and binds U:=λx. λy. g (V′ y)U:=\lambda x.\, \lambda y.\, g\,(V'\,y), a most general unifier. With right side (g (U y w))(g\,(U\,y\,w)) instead, occurs fails; with (g w)(g\,w), scope fails, because ww is not among the arguments of UU and is not inside a flexible subterm.

Example A.32 (elim with pruning [MN p. 224–225]). In ∀f ∃U ∀x ∀y ∀z ∃V [(U x)=(f V)]\forall f\,\exists U\,\forall x\,\forall y\,\forall z\,\exists V\,[(U\,x)=(f\,V)], raising VV over x,y,zx,y,z gives (U x)=(f (V′ x y z))(U\,x)=(f\,(V'\,x\,y\,z)); elim prunes V′:=λxλyλz (W x)V':=\lambda x\lambda y\lambda z\,(W\,x) and binds U:=λx (f (W x))U:=\lambda x\,(f\,(W\,x)).

Implementations of the same rules. Nipkow’s functional version works on de Bruijn terms with a ∀∃∀\forall\exists\forall prefixTobias Nipkow. Functional unification of higher-order patterns. In M. Vardi, editor, 8th Symposium on Logic in Computer Science, pages 64–74, New York, 1993. IEEE; Dowek, Hardin, Kirchner and Pfenning recast the rules (Invert, Pruning, Same-variable) in the λσ-calculus of explicit substitutions, where raising is the rule Anti-Exp-λGilles Dowek, Thérèse Hardin, Claude Kirchner, and Frank Pfenning. Unification via explicit substitutions: the case of higher-order patterns. In M. Maher, editor, Joint International Conference and Symposium on Logic Programming (JICSLP), pages 259–273. MIT Press, 1996, p. 8–9; Nadathur and Linnell drop the prefix in favour of level tagsGopalan Nadathur and Natalie Linnell. Practical higher-order pattern unification with on-the-fly raising. In ICLP 2005: 21st International Logic Programming Conference, volume 3668 of LNCS, pages 371–386, Sitges, Spain, 2006. Springer (Section A.10).

A.10Universe levels

Tagging every eigenvariable and logic variable with a number replaces the explicit prefix: the tag records which constants a variable may seeGopalan Nadathur, Bharat Jayaraman, and Keehang Kwon. Scoping constructs in logic programming: implementation problems and their solution. Journal of Logic Programming, 25(2):119–161, 1995, p. 15–18.

Rules A.33 (The abstract interpreter on decorated goals).

⟨G1∧G2,𝒫,I⟩⟨G1,𝒫,I⟩, ⟨G2,𝒫,I⟩  (1)⟨G1∨G2,𝒫,I⟩⟨Gi,𝒫,I⟩  (2) (i=1,2)⟨∃x G,𝒫,I⟩⟨G[w/x],𝒫,I⟩  (3) (w new, tag I)\dfrac{\langle G_1\wedge G_2,\mathcal{P},I\rangle}{\langle G_1,\mathcal{P},I\rangle,\ \langle G_2,\mathcal{P},I\rangle}\;(1) \qquad \dfrac{\langle G_1\vee G_2,\mathcal{P},I\rangle}{\langle G_i,\mathcal{P},I\rangle}\;(2)\ (i=1,2) \qquad \dfrac{\langle \exists x\,G,\mathcal{P},I\rangle}{\langle G[w/x],\mathcal{P},I\rangle}\;(3)\ (w\text{ new, tag }I)⟨(D1∧⋯∧Dn)⊃G,𝒫,I⟩⟨G,𝒫∪{D1,…,Dn},I⟩  (4)⟨∀x G,𝒫,I⟩⟨G[c/x],𝒫,I+1⟩  (5) (c new, tag I+1)\dfrac{\langle (D_1\wedge\dots\wedge D_n)\supset G,\mathcal{P},I\rangle}{\langle G,\mathcal{P}\cup\{D_1,\dots,D_n\},I\rangle}\;(4) \qquad \dfrac{\langle \forall x\,G,\mathcal{P},I\rangle}{\langle G[c/x],\mathcal{P},I+1\rangle}\;(5)\ (c\text{ new, tag }I+1)⟨A,𝒫,I⟩σ(⟨G,𝒫,I⟩)  (6) (instance G⊃A′)⟨A,𝒫,I⟩solved  (7) (instance A′)\dfrac{\langle A,\mathcal{P},I\rangle}{\sigma(\langle G,\mathcal{P},I\rangle)}\;(6)\ (\text{instance }G\supset A') \qquad \dfrac{\langle A,\mathcal{P},I\rangle}{\text{solved}}\;(7)\ (\text{instance }A')

The abstract interpreter on decorated goals ⟨G,𝒫,I⟩\langle G,\mathcal{P},I\rangleGopalan Nadathur, Bharat Jayaraman, and Keehang Kwon. Scoping constructs in logic programming: implementation problems and their solution. Journal of Logic Programming, 25(2):119–161, 1995, p. 17–18. In (6) and (7) the clause instance is a new tagged instance relative to II of a clause of 𝒫\mathcal{P}, and σ=mgu(A,A′)\sigma=\mathrm{mgu}(A,A') is applied to all remaining goals. A new tagged instance relative to II gives tag 1 to untagged constants and a new variable of tag II to each ∀x\forall x of the clause; the original goal starts at I=1I=1 with its constants and free variables tagged 1.

Definition A.34 (binding condition and lowering, p. 16).Gopalan Nadathur, Bharat Jayaraman, and Keehang Kwon. Scoping constructs in logic programming: implementation problems and their solution. Journal of Logic Programming, 25(2):119–161, 1995 A logic variable XX of tag ii may be bound to tt only if (a) XX does not occur in tt (occurs check) and (b) no constant in tt has a tag greater than ii. Before the binding is made, every variable in tt with a tag greater than ii has its tag lowered to ii, so that its own later instances respect the restriction on XX.

Proposition A.35 (Prop. 4.1).Gopalan Nadathur, Bharat Jayaraman, and Keehang Kwon. Scoping constructs in logic programming: implementation problems and their solution. Journal of Logic Programming, 25(2):119–161, 1995 Derivations of the interpreter are sound and complete with respect to intuitionistic provability, and the choice of the next decorated goal to reduce is immaterial.

Example A.36 (scope failure [MN p. 85,106]). ?- sigma x\ pi y\ x = y. fails: the query gives X1X^1, the ∀\forall gives c2c^2, and X1:=c2X^1:=c^2 violates (b). With the program ∀z (q(z)⊃p(d(z)))\forall z\,(q(z)\supset p(d(z))), the goal ∃x ∀y (q(y)⊃p(x))\exists x\,\forall y\,(q(y)\supset p(x)) becomes q(c2)⊃p(X1)q(c^2)\supset p(X^1), at I=2I=2; backchaining creates Z2Z^2, unifying p(d(Z2))p(d(Z^2)) with p(X1)p(X^1) binds X1:=d(Z2)X^1:=d(Z^2) and lowers ZZ to tag 1, so the subgoal q(Z1)q(Z^1) fails against the hypothesis q(c2)q(c^2); without lowering, Z2:=c2Z^2:=c^2 would succeed and smuggle cc into XXGopalan Nadathur, Bharat Jayaraman, and Keehang Kwon. Scoping constructs in logic programming: implementation problems and their solution. Journal of Logic Programming, 25(2):119–161, 1995, p. 16.

Levels for higher-order patternsGopalan Nadathur and Natalie Linnell. Practical higher-order pattern unification with on-the-fly raising. In ICLP 2005: 21st International Logic Programming Conference, volume 3668 of LNCS, pages 371–386, Sitges, Spain, 2006. Springer, p. 8–9. Let l(y)l(y) count the times an ∃\exists is immediately followed by a ∀\forall in the prefix up to and including the quantifier of yy; then a universal uu is in the scope of an existential xx iff l(x)<l(u)l(x)\lt l(u), which makes the pattern test of Definition A.28 local. Raising of gg is restricted to y¯⇑g={u∈y¯∣l(u)≤l(g)}\bar y{\Uparrow}g=\{u\in\bar y\mid l(u)\le l(g)\}. New existentials created by imitation, projection, pruning or flexible-flexible steps are quantified “at the same place” as the variable they replace [MN p. 221–222,225], i.e. they take its level, not a new one.The book describes the same device as lists of universals “over which it can be raised”, initial segments of the prefix, represented by numerical indices [MN p. 225].

Remark A.37 (higher-order binding condition; reconstruction in our notes, not in the book). In elim for X c¯=tX\,\bar c=t the tag test (b) applies to universals in rigid positions of tt not among c¯\bar c (scope in Rules A.30); inside the arguments of a flexible (Y d¯)(Y\,\bar d) such universals are pruned instead; and lowering a YY of higher level binds YY to (Y′ a¯)(Y'\,\bar a) with Y′Y' at the level of XX, which is delayed raising [MN p. 225].

Kanren angle (what levels require). A miniKanren with generic goals must record a level for every logic variable and eigenvariable and must enforce the binding condition, with lowering, inside its unifier; see Chapter 11.

A.11Summary