
































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!

































Contents vii
The exam
The evaluation is solely based on an oral exam , which can involve written exercises. According to the course introduction [01 p.20] the exam typically consists of:
In class (L01, 24:19–25:31) At the first lecture the professor described the exam: some pre- liminary questions taken from the list in the Files tab of the Teams channel (“nearly 150 questions” at that time; the current list has 242), then one or two exercises solved together, “like the ones that I will present to you in the lectures”, and possibly a proof. Registration on the university system is mandatory. A sample exam was shown (the one summarised above).
The sample exam shown in class [01 p.21] mixes exactly these ingredients: “Is f monotone/con- tinuous? What is its least fixpoint?”, “What are the rules of the type system of HOFL?”, “How is iteration achieved in CCS?”, “Find the principal type and the denotational semantics of a HOFL term”, “Prove the Switch Lemma”, a CCS bisimulation exercise.
Exercise family Typical request Chapter Unification, logic programs solve G; write Horn clauses; goal-oriented derivation 1 Induction on IMP prove termination/determinacy (or counterexample) for an extended IMP; rule induction
3, 2
Equivalence prove/disprove c 1 ∼ c 2 operationally or denotationally 2, 5 Orders, fixpoints is it a CPO? monotone? continuous? least fixpoint? 4 Denotational IMP semantics of a new command, fixpoint computation, con- sistency
5
HOFL principal type, operational/denotational semantics of a term
7, 9
CCS LTS of a process, (weak) bisimilarity, HML distinguishing formula
11, 12
Temporal logics LTL/CTL/μ-calculus formulas and their evaluation 13
Course map
Lecture numbers follow the 2025/26 calendar of the course page; recording LNN is lecture NN.
viii Contents
Lect. Topic Slides Rec. Book 1 introduction 01 L01 1 2 from syntax to semantics, SOS, small/big- step, denotational
02 L02 1.
3–4 signatures, unification, logical systems, logic programming
03, 04 L03, L04 2
5–7 well-founded, structural and rule induc- tion; IMP
05a, 05b, 05c L05–L07 3, 4.
8 equivalence; well-founded recursion 06, 07 L08 3.3, 4. 9–11 partial orders, CPOs, Kleene, ICO 08a, 08b, 08c L09–L11 5 11–13 denotational semantics of IMP, consis- tency; exercises
09, 10, 10a L11–L13 6
14–15 Haskell 11a, 11b (L14 missing), L15 — 16–17 HOFL: types, operational semantics 12a, 12b (L16 missing), L17 7 18–21 domain theory, HOFL denotational, con- sistency
13a, 13b, 14, 15 L18–L21 8–
21–22 Erlang, Google Go 16a, 16b L21, L22 — 23–26 CCS, bisimulation, Knaster–Tarski, HML 17, 18a–c, 19 L23–L26 11.1–11. 27–28 weak semantics; CCS at work (CAAL) 20, 21 (L27, L28 missing) 11. 29–30 LTL, CTL∗, CTL, μ-calculus 22a, 22b (L29 missing), L30 12 30–34 back to IMP: calculus of relations, KAT 23a–d L30, L32–L34 —
The questions in MPP-questions.txt about the π-calculus, Markov chains, Larsen–Skou logic and PEPA are not covered by this year’s slides; they are answered briefly from the book (Ch. 13–16) in Chapter 15.
2 Chapter 1. Preliminaries: Semantics, Inference Rules, Unification
In class (L02, 22:25–27:12) The two examples were used to motivate the whole course: reading the syntax alone you cannot tell what the switch prints or what the C function returns (“the behaviour is not specified: any compiler can return the value it likes”). Writing a formal semantics forces you to decide these cases, gives implementers a machine-independent reference and lets testers compare their results against a precise specification.
Definition 1.2 – Three approaches to semantics [02 p.24–33]
Advantages of SOS [02 p.27]: rules are defined by induction on the syntax ( structural ), translate immediately into Horn clauses (so a Prolog prototype interpreter comes for free), rules for different constructs are neatly separated, the mathematics is simple. Advantages of the denotational approach: mathematical elegance, compositionality, but it is difficult to apply to concurrent and interactive systems.
Definition 1.3 – Compositionality principle [02 p.29] The meaning of a composite construct does not depend on the particular form of its con- stituent constructs, but only on their meanings. Formally, the denotational semantics is defined by structural recursion : for each operator f of the syntax there is a semantic function f^ ˆ such that Jf (t 1 ,... , tn)K = fˆ (Jt 1 K,... , JtnK).
In class (L02, 38:01) Compositionality is “the key principle” of the denotational approach: the meaning of a composite construct depends only on the meanings of its components, not on how they are written. This is what makes denotational semantics suited to proving equivalences (replace a part by another with the same meaning). Static analyses, instead, care about the way the program is written.
Syntax [02 p.37–38]: E ::= N | E ⊕ E | E ⊗ E, where N ranges over numerals (syntax: the string 101 ) and n over numbers (mathematical objects: five). We work with abstract syntax trees, so 1 ⊕ 2 ⊗ 3 is ambiguous only as a string; brackets pick a tree. Note that nothing forces ⊕ to be the sum: the same syntax could denote matrices, sets with union/intersection, strings, …The semantics decides.
Small-step semantics. States mix expressions and intermediate results: E ::= n | N | E ⊕ E | E ⊗ E. A step is E 0 → E 1 ; an evaluation is E 0 → E 1 → · · · → Ek → n, written E 0 →∗^ n, and
1.1. From syntax to semantics 3
we expect n 6 →.
N → n
num n 0 ⊕ n 1 → n
sum (n = n 0 + n 1 ) n 0 ⊗ n 1 → n
prod (n = n 0 · n 1 )
E 0 → E′ 0 E 0 ⊕ E 1 → E′ 0 ⊕ E 1
sumL
sumR
and the analogous (prodL), (prodR). With these rules (1 ⊕ 2) ⊗ (3 ⊕ 4) has many computations (one may evaluate the right operand first), all ending in 21 : the relation is non-deterministic and the question is confluence. To impose left-to-right evaluation we replace (sumR) by E 1 → E 1 ′ n 0 ⊕ E 1 → n 0 ⊕ E′ 1
sumR’
(and the same for prodR): now the right operand can move only when the left one is already a number, and the computation is unique.
Big-step semantics. A single judgement E → n represents a whole computation:
N → n
num
E 0 → n 0 E 1 → n 1 E 0 ⊕ E 1 → n
sum (n = n 0 + n 1 )
E 0 → n 0 E 1 → n 1 E 0 ⊗ E 1 → n
prod (n = n 0 · n 1 )
Example 1.4 – A big-step derivation [02 p.53]
sum
sum
(1 ⊕ 2) ⊗ (3 ⊕ 4) → 21
prod
Big-step rules are usually simpler and correspond to an efficient interpreter, but they cannot express non-terminating computations : a derivation exists only for terminating programs.
Denotational semantics. Domain N and interpretation function EJ·K : Exp → N defined by structural recursion: EJN K = n EJE 0 ⊕ E 1 K = EJE 0 K + EJE 1 K EJE 0 ⊗ E 1 K = EJE 0 K · EJE 1 K. E.g. EJ(1 ⊕ 2) ⊗ (3 ⊕ 4)K = (1 + 2) · (3 + 4) = 21. The choice of the domain has immediate consequences: since EJ·K is a (total) function into N, every expression has exactly one meaning, so determinacy and normalisation are obvious. In class (L02, 72:42–77:37) The professor insists on writing the properties as first-order for- mulas before proving them: termination is “for every expression there exists a value”, deter- minacy is “ for all values n, m, if E evaluates to both then n = m”. For determinacy “if the premise is false we don’t care”: an expression with no result satisfies it vacuously; it fails only when two different results (say 5 and 6) can be derived.
These formulas are asked verbatim at the exam (“What is the formula to express … in opera- tional/denotational semantics?”).
1.2. Grammars, signatures and terms 5
is an infinite sequence, intermediate states are visible (useful for concurrency and interleav- ing). Big-step: → relates a program directly to its final result, one derivation per complete computation, simpler rules and efficient interpreters, but non-terminating programs simply have no derivation (divergence is not distinguished from being stuck), and intermediate states are invisible. “How to state consistency?” ∀p, r. p →∗^ r ⇐⇒ p → r ⇐⇒ JpK = r (and for IMP: 〈c, σ〉 → σ′^ ⇐⇒ CJcKσ = σ′).
Exercise 1.1 (02 p.68: expressions with variables)
Extend the syntax to E ::= x | N | E ⊕ E | E ⊗ E and redefine the three semantics.
Solution We need memories σ ∈ M = {σ | σ : X → N}. Machine states become pairs 〈E, σ〉.
Big-step: add 〈x, σ〉 → σ(x)
and put σ in every rule, e.g.
〈E 0 , σ〉 → n 0 〈E 1 , σ〉 → n 1 〈E 0 ⊕ E 1 , σ〉 → n 0 + n 1
Small-step: 〈x, σ〉 → 〈n, σ〉 with n = σ(x) (the memory never changes, since expressions have no side effects). Denotational: EJ·K : Exp → (M → N), EJxKσ = σ(x), EJE 0 ⊕ E 1 Kσ = EJE 0 Kσ + EJE 1 Kσ, etc. All properties are restated with a universal quantification over σ: e.g. E 0 ≡d E 1 iff ∀σ. EJE 0 Kσ = EJE 1 Kσ.
1.2 Grammars, signatures and terms
Definition 1.8 – Grammar A (context-free) grammar is a set of productions A ::= α 1 | · · · | αk where A is a non-terminal (syntactic category) and each αi is a string of terminals and non-terminals; L(A) is the set of strings of terminals derivable from A. In the course grammars are used to describe abstract syntax : each production is an operator building a tree from its sub-trees, so a grammar is just a (many-sorted) signature (see below), and also a set of inference rules (example 1.15).
Exercise 1.2 (01 p.30, 02 p.9) (a) Let A ::= ε | 0 A | 1 B and B ::= 0B | 1 A. What are L(A) and L(B)? (b) Over the alphabet {(, )} give a grammar for balanced parentheses.
Solution (a) A “has seen an even number of 1s”, B an odd number; only A can stop. Hence L(A) = binary strings with an even number of 1s (e.g. A → 0 A → 01 B → 011 A → 011 ), L(B) = binary strings with an odd number of 1s. Proof by simultaneous induction on the length. (b) S ::= ε | (S) | SS.
Definition 1.9 – Unsorted signature, terms [03 p.12–18; book §2.1] A (one-sorted) signature is a pair (Σ, ar ) where Σ is a set of function symbols (operators) and ar : Σ → N gives their arity. Equivalently, an arity-indexed family Σ = {Σn}n∈N with Σn = ar −^1 (n); symbols in Σ 0 are constants. Given an infinite set X of variables, the set of terms TΣ,X is the least set such that
t ::= x | c | f (t 1 ,... , tn) (x ∈ X, c ∈ Σ 0 , f ∈ Σn).
vars ⋃ (t) is defined by structural recursion: vars (x) = {x}, vars (c) = ∅, vars (f (t 1 ,... , tn)) = i vars (ti). A term is^ closed^ (ground) if^ vars (t) =^ ∅;^ TΣ^ =^ TΣ,∅^ ⊆^ TΣ,X^.
6 Chapter 1. Preliminaries: Semantics, Inference Rules, Unification
Running example: Σ 0 = { 0 }, Σ 1 = { succ }, Σ 2 = { plus }. Then succ (0) ∈ TΣ; plus ( succ (x), 0) ∈ TΣ,X \TΣ with vars = {x}; succ ( plus (0), x) and succ ( succ (0), plus (x)) are not terms (wrong arities) [03 p.21].
Definition 1.10 – Many-sorted signature [05b; book §4.1.3] Given a set S of sorts , a many-sorted signature is a family Σ = {Σs 1 ...sn,s} of sets of operators indexed by the sorts of the arguments and of the result: f ∈ Σs 1 ...sn,s means f : s 1 ×· · ·×sn → s. Terms are sorted: if ti has sort si then f (t 1 ,... , tn) has sort s. Example (IMP): sorts { Aexp , Bexp , Com }, with e.g. + ∈ Σ Aexp Aexp , Aexp , ≤∈ Σ Aexp Aexp , Bexp , while ∈ Σ Bexp Com , Com.
Difference : an unsorted signature only fixes the number of arguments, so every combination of terms is a term; a many-sorted one also fixes the sort of each argument and of the result, so only well-sorted combinations are terms (e.g. while 3 do skip is not a term of IMP). A many-sorted signature is like a very simple type system built into the syntax.
Definition 1.11 – Type system A type system is a logical system (set of inference rules) whose judgements are typing asser- tions such as t : τ (“term t has type τ ”) or Γ ` t : τ. It is used to discard ill-formed programs statically (limit errors, allow compiler optimisations, discourage malpractices); different type systems can be defined for the same language. Example: the HOFL type system, Section 7.2.
Definition 1.12 – Substitution, more general than [03 p.22–26] A substitution is ρ : X → TΣ,X , the identity except for finitely many variables, written ρ = [x 1 = t 1 ,... , xn = tn] (xi all different). It lifts to terms: tρ (or ρ(t)) is obtained by simultaneously replacing every occurrence of each xi in t with ti. E.g. with ρ = [x = succ (y), y = 0] and t = plus ( plus (x, y), succ (x)), tρ = plus ( plus ( succ (y), 0), succ ( succ (y))) (simultaneous: the y introduced for x is not replaced). Composition: t(ρ 1 · ρ 2 ) = (tρ 1 )ρ 2. t is more general than t′^ (t ≤mgt t′; t′^ is an instance of t) if ∃ρ. t′^ = tρ. The relation is reflexive and transitive but not antisymmetric: succ (x) and succ (y) are more general than each other (they differ by a renaming). On substitutions: ρ ≤mgt ρ′^ if ∃ρ′′. ∀x. ρ′(x) = ρ′′(ρ(x)).
Examples [03 p.25]: plus (x, succ (y)) ≤mgt plus (0, succ ( succ (z))) (take [x = 0, y = succ (z)]); plus (0, x) and plus (y, 0) are incomparable, but both are more general than plus (0, 0).
1.3 Inference rules and logical systems
Definition 1.13 – Inference rule, axiom, logical system [book Def. 2.1–2.3; 04 p.9– 13]
An inference rule p 1 · · · pn q
(premises pi, one conclusion q, possibly a side condition and a name) says: if the premises are valid then so is the conclusion. Variables in a rule are implicitly universally quantified; a rule instance is obtained by applying a substitution to all of p 1 ,... , pn, q. An axiom is a rule with no premises (a fact). A logical system is a set R of inference rules (with a rule scheme standing for all its instances).
Pitfall A rule instance is valid even if its premises are false:
is an instance
8 Chapter 1. Preliminaries: Semantics, Inference Rules, Unification
Definition 1.17 – Unification problem [03 p.29–33]
Given G = {ℓ 1 =? r 1 ,... , ℓn =? rn} with ℓi, ri ∈ TΣ,X , a solution (unifier) is a substitution ρ with ∀i. ℓiρ = riρ; sols (G) is the set of solutions. The (interesting) unification problem asks for a most general unifier (mgu): ρ ∈ sols (G) such that ∀ρ′^ ∈ sols (G). ρ ≤mgt ρ′. Solutions may not exist, and even if they exist they are not unique: for { succ (x) =? succ ( succ (y))} both [x = succ (y)] (the mgu) and [x = succ (0), y = 0] are solutions.
In class (L03, 25:23 and 76:10) Why unification appears in a semantics course: to apply an inference rule to a goal you must match the rule’s conclusion with the goal, and both may contain variables, so plain matching is not enough. The rules of the algorithm are deliberately small and “slow” so that each step is justified. Once you understand which goals they aim at, by hand you can often reach the most general unifier without writing every formal step, but at the exam be ready to show the steps when asked.
Algorithm 1.18 – Unification algorithm [03 p.34–37] Transform G by solution-preserving steps (G and G′^ are equivalent if sols (G) = sols (G′)) until either failure, or G is in solved form {x 1 =? t 1 ,... , xk =? tk} with the xi all different and {x 1 ,... , xk} ∩
i vars (ti) =^ ∅; then^ [x^1 =^ t^1 ,... , xk^ =^ tk]^ is an mgu. The rules:
delete G ∪ {t =? t} becomes G decompose G ∪ {f (t 1 ,... , tm) =? f (u 1 ,... , um)} becomes G ∪ {t 1 =? u 1 ,... , tm =^? um} swap G ∪ {f (t 1 ,... , tm) =? x} becomes G ∪ {x =? f (t 1 ,... , tm)} eliminate G ∪ {x =? t}, if x ∈ vars (G) \ vars (t) becomes G[x = t] ∪ {x =? t} conflict G ∪ {f (t 1 ,... , tm) =? g(u 1 ,... , uh)} fails if f 6 = g or m 6 = h occur-check G ∪ {x =? f (t 1 ,... , tm)} fails if x ∈ vars (f (t 1 ,... , tm))
(Constants are operators with m = 0: 0 = 0? is deleted, 0 =? succ (x) is a conflict.)
Intuition Decompose and conflict compare the outermost symbols; swap puts variables on the left; eliminate propagates a binding everywhere else in G (keeping the equation itself); occur-check rejects x = f (... x.. .), which would need an infinite term. The algorithm always terminates (a measure: number of unsolved variables, size of terms, number of equations with a non-variable on the left).
In class (L03, 64:20) The occur-check handles equations like x =? f (... x.. .), which are recursive and have no (finite) solution. Real implementations, Prolog in particular, sometimes omit it for efficiency, at the price of soundness on such goals.
In class (L03, 60:32–61:46; L04, 06:56) Why the side condition of eliminate (x ∈ vars (G) vars (t))? “This rule is applicable only if there is at least one occurrence to replace, otherwise it makes no sense”: we would apply it over and over. Once x has been propagated, its equation can be “kept aside”, since x no longer occurs elsewhere. And swap is only for equations with a non-variable on the left and a variable on the right: for x =? z (two variables) just eliminate.
1.5. Logic programming 9
Example 1.19 – Three runs [03 p.38–40]
1.5 Logic programming
“Algorithm = logic + control” (Kowalski): a logic program describes what (Horn clauses); the interpreter (SLD resolution) provides how. Prolog (Colmerauer, Roussel, 1972) is based on this idea. Definition 1.20 – Atomic formula, formula, Horn clause, logic program [book Def. 2.7–2.10; 04 p.23–26] Besides the signature Σ of function symbols, fix a signature Π = {Πn} of predicate symbols. An atomic formula is p(t 1 ,... , tn) with p ∈ Πn, ti ∈ TΣ,X ; a formula is a possibly empty conjunction a 1 ,... , an of atomic formulas. A Horn clause is h :- a 1 ,... , an. (head h atomic, body a formula), analogous to the rule a 1 · · · an h
; with empty body it is a fact. A logic program is a set (list) of Horn clauses. Given a goal g, the question is: which instances of g are theorems?
Algorithm 1.21 – SLD resolution (Selective Linear Definite clause) [04 p.28–35] Repeat until no goal is left:
Pitfall (1) The mgu must be applied to the whole new goal, because atomic goals share variables. (2) Every time a clause is used its variables must be renamed with fresh ones, otherwise unrelated uses clash [04 p.31–32].
Example 1.22 – Sum on unary naturals [04 p.37–43; book Ex. 2.6] Σ 0 = { 0 }, Σ 1 = {s}; sum (x, y, z) means x + y = z: sum(0, y, y). sum(s(x), y, s(z)) :- sum(x, y, z).
Goal sum (s(s(0)), s(s(0)), n) (“2 + 2 =?”): the first clause fails (s(s(0)) 6 = 0); with the variant sum (s(x 1 ), y 1 , s(z 1 )), σ 1 = [x 1 = s(0), y 1 = s(s(0)), n = s(z 1 )], ˆσ 1 = [n = s(z 1 )], new goal sum (s(0), s(s(0)), z 1 ); then σˆ 2 = [z 1 = s(z 2 )], goal sum (0, s(s(0)), z 2 ); the fact gives σ ˆ 3 = [z 2 = s(s(0))] and □. cas: n = s(s(s(s(0)))). The same program runs “backwards”:
1.7. Solved exercises 11
1.7 Solved exercises
Exercise 1.3 (03 p.41)
Solve { plus (x, succ (x)) =? plus (0, y), plus (y, z) =? plus (z, w)}.
Solution Decompose both: {x ? = 0 , succ (x) ? = y, y ? = z, z ? = w}. Eliminate x: {x = 0? , succ (0) =? y, y =? z, z =? w}. Swap: y =? succ (0); eliminate y: {x = 0? , y =? succ (0), succ (0) ? = z, z ? = w}. Swap and eliminate z: {... , z ? = succ (0), succ (0) ? = w}; swap: w =? succ (0). Solved form; mgu [x = 0, y = succ (0), z = succ (0), w = succ (0)].
Exercise 1.4 (PSC 01, Ex. 1; 04 p.47) With Σ 0 = { 0 }, Σ 1 = {s}, extend the program for sum with: (1) prod ∈ Π 3 ; (2) pow ∈ Π 3 ; (3) div ∈ Π 2 , telling whether the first argument divides the second.
Solution prod(0, y, 0). prod(s(x), y, z) :- prod(x, y, w), sum(w, y, z). % (x+1)y = xy+y pow(x, 0, s(0)). pow(x, s(y), z) :- pow(x, y, w), prod(w, x, z). % x^(y+1) = x^yx div(x, y) :- prod(x, k, y). % x|y iff xk=y for some k
(For div one may equally use prod (k, x, y); with prod (x, k, y) the recursion is on the divisor.)
Exercise 1.5 (PSC 01, Ex. 2; 04 p.4–6)
Solve (1) G 1 = { prod (s(x), y, s(z)) ? = prod (y, z, x)}; (2) G 2 = { pow (x, s(y), x) ? = pow (s(y), z, z)}; (3) G 3 = { div (x, s(y)) =? div (z, x), div (y, s(z)) =? div (u, s(u))}.
Solution (1) Decompose: {s(x) =? y, y =? z, s(z) =? x}. Swap: y =? s(x); eliminate y: {y =? s(x), s(x) =? z, s(z) =? x}. Swap: z =? s(x); eliminate z: {y =? s(x), z =? s(x), s(s(x)) =? x}. Swap: x =? s(s(x)): occur-check fails , no unifier. (2) Decompose: {x =? s(y), s(y) =? z, x =? z}. Eliminate x: {x =? s(y), s(y) =? z, s(y) =? z}. Swap both: z =? s(y); eliminate z (the other copy becomes s(y) =? s(y), deleted). Solved: mgu = [x = s(y), z = s(y)], i.e. the unified term is pow (s(y), s(y), s(y)). (3) Decompose: {x ? = z, s(y) ? = x, y ? = u, s(z) ? = s(u)}, and again s(z) ? = s(u) into z ? = u. Eliminate z (by z =? u): {x =? u, s(y) =? x, y =? u, z =? u}. Eliminate x: s(y) =? u. Eliminate y: {x ? = u, s(u) ? = u, y ? = u, z ? = u}. Swap: u ? = s(u): occur-check fails , no unifier.
Exercise 1.6 (PSC 01, Ex. 3; 04 p.46–49) Give goal-oriented derivations for: (1) sum (x, s(0), s(s(0))); (2) prod (s(s(0)), y, s(s(0))); (3) div (z, s(s(0))).
Solution (1) Fact sum (0, y 1 , y 1 ): unify x = 0, y 1 = s(0), y 1 = s(s(0)) ⇒ s(0) =? s(s(0)), con- flict, fail. Clause 2 with variant sum (s(x 1 ), y 1 , s(z 1 )) :- sum (x 1 , y 1 , z 1 ): σ = [x = s(x 1 ), y 1 = s(0), z 1 = s(0)], ˆσ 1 = [x = s(x 1 )], goal sum (x 1 , s(0), s(0)). Fact: [x 1 = 0, y 2 = s(0)], □. cas
12 Chapter 1. Preliminaries: Semantics, Inference Rules, Unification
x = s(0) (1 + 1 = 2). (2) prod (s(s(0)), y, s(s(0))) ↖ prod (s(0), y, w 1 ), sum (w 1 , y, s(s(0))) (clause 2, x 1 = s(0)) ↖ prod (0, y, w 2 ), sum (w 2 , y, w 1 ), sum (w 1 , y, s(s(0))) (clause 2) ↖ sum (0, y, w 1 ), sum (w 1 , y, s(s(0))) (fact, w 2 = 0 ) ↖ sum (y, y, s(s(0))) (fact, w 1 = y). Now the fact would need y = 0 and 0 = s(s(0)): fail. Clause 2: y = s(x 5 ), y 5 = s(x 5 ), z 5 = s(0), goal sum (x 5 , s(x 5 ), s(0)); fact: x 5 = 0 and s(0) = s(0): □. cas y = s(0) ( 2 · 1 = 2). (3) div (z, s(s(0))) ↖ prod (z, k, s(s(0))). Fact prod (0, y, 0): 0 =? s(s(0)), fail. Clause 2: z = s(x 1 ), goal prod (x 1 , k, w 1 ), sum (w 1 , k, s(s(0))). Fact for prod : x 1 = 0, w 1 = 0, goal sum (0, k, s(s(0))), fact: k = s(s(0)), □: first answer z = s(0). Backtracking into clause 2 for prod (x 1 , k, w 1 ): x 1 = s(x 2 ), then x 2 = 0, w 2 = 0, sum (0, k, w 1 ) gives w 1 = k and sum (k, k, s(s(0))) gives k = s(0) as in (2): second answer z = s(s(0)). Further backtracking tries z = 3, 4 ,... and never terminates: the search tree is infinite although all the answers ( 1 and 2 ) have been found.
Exercise 1.7 (MOD-style) Using only sum , compute all answers of? sum (x, y, s(s(0))).
Solution Fact: x = 0, y = s(s(0)). Clause 2: x = s(x 1 ), goal sum (x 1 , y, s(0)): fact gives x 1 = 0 , y = s(0), i.e. x = s(0); clause 2 again gives x 1 = s(x 2 ), sum (x 2 , y, 0): fact x 2 = 0, y = 0, i.e. x = s(s(0)); clause 2 fails (s(z) ? = 0 conflict). Answers: (0, 2), (1, 1), (2, 0), and the search tree is finite.
Summary