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.
| λProlog | logic | notes |
|---|---|---|
true | type [MN p. 35] | |
G1, G2 | infix, left assoc.; joins goals [MN p. 35,47–48] | |
D1 & D2 | infix, right assoc.; joins clauses [MN p. 35,47–48] | |
G1 ; G2 | infix, left assoc. [MN p. 35] | |
D => G | infix, right assoc.; implication in goals [MN p. 35,79] | |
A :- G | infix, non-assoc.; “is implied by”, used in clauses [MN p. 35,47–48] | |
pi x\ B, pi (x:T)\ B | , | applied to [MN p. 36–37,99] |
sigma x\ B | applied to [MN p. 36–37,99] | |
x\ t, (x:T)\ t | , | body extends as far right as possible [MN p. 97] |
t1 t2 | application, left assoc., binds tightest [MN p. 97] | |
X, L1 (free, uppercase) | at clause top; in a query | implicit quantification [MN p. 43,46–47] |
_ | a fresh variable per occurrence | anonymous, over the clause [MN p. 46–47] |
X = Y | built-in equality, solved by unification [MN p. 106,287] | |
false (user constant) | of minimal logic | no ex falso for it; is [MN p. 89–90] |
A -> B (in types) | right assoc. [MN p. 12–13] | |
o | the type of propositions [MN p. 34–35] | |
kind c type -> type. | a type constructor of arity 1 | kind declaration [MN p. 11–12] |
type c T. | constant declaration [MN p. 47] |
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).
| language | grammar | source |
|---|---|---|
| fohc | [MN p. 43] | |
| fohh | [MN p. 75] | |
| hohc | as fohc, with in goals and heads | [MN p. 122–123] |
| hohh | as fohh, with in goals and heads | [MN p. 124] |
| 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]). is the set of (possibly open) λ-normal terms over that contain neither nor ; is the set of those that do not contain .
Definition A.2 (rigid and flexible atoms [MN p. 119]). An atom is flexible if the head of its λ-normal form is a variable, and rigid if is a non-logical constant.
Definition A.3 (polarity; hereditary Harrop [MN p. 76]). is positive in ; a positive (negative) occurrence in stays positive (negative) in , , , , , , , and becomes negative (positive) in . 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 , a negative or a term-level , and essentially existential otherwise; in a clause the classification is dual (negative , positive or are essentially universal).
Definition A.5 (Lλ [MN p. 205–206,220]). A goal or clause is in if all quantification in is over non-predicate types and every subterm of with essentially existential has distinct essentially universal variables bound within the scope of .
A.3Uniform proofs#
A sequent says that is provable from program over signature ; says that is proved by backchaining on the clause [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).
Right rules (search semantics of the connectives) [MN p. 39–40]. In the book they are , , , , , ; is .
Rules A.7 (Backchaining (left) rules).
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 replace the guessed by a new logic variable with , and initial becomes provided and 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 and to be α-convertible; (iii) the term of instan and is a λ-term with and (hohc) or for the current (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 for , conjunctions split, and :
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 with unbound can be (1) suspended until rigid, and if only flexible goals remain each head is bound to (complete for intuitionistic higher-order logic); (2) bound eagerly to ; 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.
| language | has an O-proof iff it is provable in | source |
|---|---|---|
| fohc | classical logic, iff intuitionistic logic | [MN p. 45,62] |
| fohh | minimal logic, equivalently intuitionistic logic (classical logic is not sound for this operational semantics) | [MN p. 89,91] |
| hohc | classical, equivalently intuitionistic, Simple Theory of Types without extensionality, infinity and choice | [MN p. 124,148] |
| hohh | higher-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 has the form or : 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 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).
Typing rules [MN p. 98]. : is an instance of ; () not declared in or ; () and differ only in bound-variable names.
Definition A.11 (order of a type [MN p. 13]). if is non-functional; .
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]). is free for in if no free occurrence of in is in the scope of an abstraction (or quantifier) binding a variable free in . replaces the free occurrences of in by , after α-renaming so that is free for (capture-avoiding substitution) [MN p. 101].
| rule | redex contractum | proviso |
|---|---|---|
| α-rewriting | free for in , not free in | |
| β-contraction | none (capture-avoiding ) | |
| η-contraction | not free in | |
| [MN p. 206] | ; up to α: | (second form) not free in |
Definition A.13 (closures [MN p. 100–101]). α-conversion: reflexive, symmetric, transitive closure of α-rewriting. β-reduction: reflexive, transitive closure of α-conversion β-contraction; β-conversion: its symmetric, transitive closure. η-reduction / η-conversion: likewise from η-contraction. λ-conversion (): 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 (), with binder , head (a constant or variable), arguments (β-normal) and body . It is in βη-long normal form if moreover each 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 : () (), which is βη-long.
Definition A.17 (rigid and flexible terms [MN p. 111–112,214]). A β-normal term in a unification problem is rigid if is universally quantified in the prefix (a constant) or a member of its binder , and flexible if 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 [MN p. 105–108,212].
Definition A.18 (unification problem [MN p. 105,211]). A formula with and λ-terms of the same type; the quantifiers form the prefix, the conjunction the matrix.
Definition A.19 (unifier, solution [MN p. 109]). For , a substitution for is legitimate if no is free in its range; a unifier is a legitimate with for all ; a solution is a unifier whose range has free variables only among . 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 , has the four unifiers , , , , none an instance of another, so there is no most general unifier; in the solution cannot mention , and is the only one.
Proposition A.21 (raising [MN p. 107]). With , and : is provable iff is provable.
As a rewrite on prefixes, for a block of universals:
Repeated raising turns any prefix into or even form [MN p. 108], and it preserves [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 is to the left of with universals in between, replace by and move next to .
Skolemization [MN p. 108]. An assumption becomes with a new constant; dually a goal becomes . Skolemization moves an inwards and introduces a constant of raised type; raising moves a 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 , with the signature; (2) is defined as for , so terms may be taken β-normal; (3) η-expansion makes the two sides of each equation have binders of the same length; (4) by the binders are moved into the inner block:
Here and below a rule is read downwards as one transformation step, is multiset union, 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).
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 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).
Huet’s transformation rules [MN p. 214–216], with , , primitive. imitate needs in the scope of ; project needs the target of to be ; so at most 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 -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 ( primitive) to , with one fixed new existential 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 , project () gives , a clash; imitate () and simp give , and each equation again has an imitation () and a projection (), both succeeding. The four leaves compose to the four unifiers of Example A.20. In , imitation reproduces the same problem for a new , so the tree has an infinite path, alongside the unifiers , , , … [MN p. 218].
A.9Higher-order pattern unification#
In 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 () is a pattern if are distinct universal variables quantified to the right of (within its scope); denotes some listing of the variables common to and .
Theorem A.29 ([MN p. 208,221–224]). On problems the rules of Rules A.30 without delay terminate (thanks to the occurs check, [MN p. 222–223]) and return or a most general unifier: 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).
Pattern unification on problems [MN p. 212,214,221–223]; delay is the dynamic- extension [MN p. 225–226]. means a new existential in the block; the bindings (and ) are composed into the answer substitution. Rule names are ours.
Example A.31 (elim, occurs, scope [MN p. 223–224]). In , rr-dec leaves ; elim prunes (since ) and binds , a most general unifier. With right side instead, occurs fails; with , scope fails, because is not among the arguments of and is not inside a flexible subterm.
Example A.32 (elim with pruning [MN p. 224–225]). In , raising over gives ; elim prunes and binds .
Implementations of the same rules. Nipkow’s functional version works on de Bruijn terms with a 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).
The abstract interpreter on decorated goals 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, p. 17–18. In (6) and (7) the clause instance is a new tagged instance relative to of a clause of , and is applied to all remaining goals. A new tagged instance relative to gives tag 1 to untagged constants and a new variable of tag to each of the clause; the original goal starts at 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 of tag may be bound to only if (a) does not occur in (occurs check) and (b) no constant in has a tag greater than . Before the binding is made, every variable in with a tag greater than has its tag lowered to , so that its own later instances respect the restriction on .
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 , the gives , and violates (b).
With the program , the goal becomes , at ; backchaining creates , unifying with binds and lowers to tag 1, so the subgoal fails against the hypothesis ; without lowering, would succeed and smuggle into 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, 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 count the times an is immediately followed by a in the prefix up to and including the quantifier of ; then a universal is in the scope of an existential iff , which makes the pattern test of Definition A.28 local. Raising of is restricted to . 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 the tag test (b) applies to universals in rigid positions of not among (scope in Rules A.30); inside the arguments of a flexible such universals are pruned instead; and lowering a of higher level binds to with at the level of , 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#
- Syntax: Table A.1; grammars of fohc, fohh, hohc, hohh, and : Table A.2 and Definition A.5.
- Proof search: right rules Rules A.6, backchaining Rules A.7, completeness Table A.3.
- Terms: typing Rules A.10, conversion Table A.4, normal forms Definition A.14 and Theorem A.15.
- Prefixes: unifier vs solution Definition A.19, prefix order Example A.20, raising Proposition A.21, problem normal form Section A.6.
- Unification: first-order Rules A.22, Huet Rules A.24 and Example A.27, patterns Rules A.30 and Example A.31.
- Scope by numbers: Rules A.33 and Definition A.34; lowering is what makes the second half of Example A.36 fail.