Course Test · University: upper level
Theory of computation: RatMath answers
The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.
Agrees 18 Can't yet 2
Libraries used here: core, discrete, logic. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.
- 1.Deterministic automataAgreesDesign a DFA over accepting words with an even number of 1s. Does it accept 1011? 1001?Independent answerTwo states, even (start, accepting) and odd, swapped by each 1. 1011 has three 1s: rejected. 1001 has two: accepted.RatMath code
const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"], delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } }); [M.accepts("1011"), M.accepts("1001")]Output[false, true]
- 2.Minimal DFAAgreesHow many states does the minimal DFA for binary words ending in 01 have?Independent answerIt must remember whether the word ends in nothing useful, in 0, or in 01: three states, which are pairwise distinguishable (by the suffixes 01, 1, and ).RatMath code
logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA("(0|1)*01"))).states.lengthOutput3
- 3.Subset constructionAgreesConvert to a DFA and minimise it. How many states? Does it accept and ?Independent answerThe minimal DFA tracks the longest suffix that is a prefix of : : 4 states. ends in : yes; : no.RatMath code
const D = logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA("(a|b)*abb"))); [D.states.length, D.accepts("aababb"), D.accepts("abab")]Output[4, true, false]
- 4.Equivalent regular expressionsAgreesAre and equivalent?Independent answerYes: both denote all words over (every letter is an block).RatMath code
logic.equivalentAutomata(logic.regexToNFA("(a|b)*"), logic.regexToNFA("(a*b*)*")).equivalentOutputtrue
- 5.Product constructionAgreesBuild an automaton for words over with an even number of s and an odd number of s. Test and .Independent answerPair the two 2-state parity automata: 4 states, accepting (even, odd). : 2 s, 1 : yes. : no.RatMath code
const par = (letter, acc) => logic.automaton({ states: ["e", "o"], alphabet: "ab", start: "e", accept: [acc], delta: { e: { a: letter === "a" ? "o" : "e", b: letter === "b" ? "o" : "e" }, o: { a: letter === "a" ? "e" : "o", b: letter === "b" ? "e" : "o" } } }); const P = logic.product(par("a", "e"), par("b", "o")); [P.states.length, P.accepts("aab"), P.accepts("ab")]Output[4, true, false]
- 6.From DFA to regular expressionAgreesGive a regular expression for binary words with an even number of 1s.Independent answer, or equivalently .RatMath code
const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"], delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } }); const r = String(logic.toRegex(M)); [r, logic.equivalentAutomata(logic.regexToNFA(r), logic.regexToNFA("0*(10*10*)*")).equivalent]Output["0*|0*1((0|10*1)*)10*", true]
- 7.Context-free grammarsAgreesGive a grammar for . Does it generate ? ? List its words of length at most 6.Independent answer. : yes; : no. Words: .RatMath code
const g = logic.grammar("S -> a S b | ε"); [g.accepts("aabb"), g.accepts("aab"), g.words(6).words]Output[true, false, ["", "ab", "aabb", "aaabbb"]]
- 8.Chomsky normal formAgreesConvert to Chomsky normal form.Independent answerNew start ; remove : ; terminals , ; split: , , and .RatMath code
String(logic.grammar("S -> a S b | ε").toCNF())OutputS → T0 X0 | T0 T1 X0 → S T1 S0 → ε | T0 X0 | T0 T1 T0 → a T1 → b
The rules print one per line (line breaks shown here as spaces). - 9.Pushdown automataAgreesRun a PDA for (push on , pop on ) on and .Independent answer: push, push, pop, pop, the stack is back to its bottom: accept. : one is left: reject.RatMath code
const P = logic.pda({ start: "q0", accept: ["q2"], moves: [ ["q0", "a", "ε", "q0", "A"], ["q0", "ε", "ε", "q1", "ε"], ["q1", "b", "A", "q1", "ε"], ["q1", "ε", "Z", "q2", "Z"]] }); [P.accepts("aabb"), P.accepts("aab")]Output[true, false]
- 10.Pumping lemmaCan't yetProve that is not regular.Independent answerIf it were, with pumping length , write with , . Then , and is not in the language. Contradiction.RatMath code
// No proof tool: a finite check only. The minimal DFA for words a^n b^n with n ≤ N needs more states as N grows. [2, 4, 6].map((N) => logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA(Array.from({ length: N + 1 }, (_, n) => "a".repeat(n) + "b".repeat(n) || "ε").join("|")))).states.length)Output[6, 10, 14]
Independent answer, in RatMath's notationnot regular (a proof)
Proofs about all words are out of reach; the code only shows the minimal DFA for the finite pieces growing with , which is evidence, not the pumping argument. - 11.Turing machinesAgreesRun a Turing machine that adds one to a binary number on the input 1011.Independent answerMove to the right end, turn trailing 1s into 0s moving left, then turn the first 0 into 1: .RatMath code
const r = logic.binaryIncrement().run("1011"); [r.status, r.tape]Output["accepted", "1100"]
- 12.Primitive recursionAgreesUsing only primitive recursive building blocks, compute , and .Independent answer, , .RatMath code
const { add, power, factorial } = logic.recursive.examples; [add.at(3, 4), power.at(2, 10), factorial.at(7)]Output[7n, 1024n, 5040n]
- 13.Composition of recursive functionsAgreesDefine by composing addition with projections, and evaluate .Independent answer, .RatMath code
const { compose, proj, examples } = logic.recursive; compose(examples.add, [proj(1, 1), proj(1, 1)]).at(21)Output42n
- 14.MinimisationAgreesUse the operator to compute the integer square root of 30: the least with .Independent answer: .RatMath code
const { mu } = logic.recursive; // mu(f) finds the least y with f(y, x) = 0 (the searched argument comes first); here f = 0 exactly when (y + 1)^2 > x const f = { arity: 2, name: "gt", at: (y, x) => ((y + 1n) * (y + 1n) > x ? 0n : 1n) }; mu(f).at(30)Output5n
The test function is written in JavaScript rather than built from the primitive recursive pieces. - 15.The halting problemCan't yetIs there an algorithm that decides, for every program and input, whether the program halts?Independent answerNo (Turing). If decided halting, the program that runs on its own code and does the opposite would halt exactly when it does not.RatMath code
// A run with a budget can only report "running" for a machine that has not stopped yet. const loop = logic.turing({ start: "s", accept: "y", reject: "n", delta: { "s,_": ["s", "_", "R"] } }); loop.run("", { budget: 1000 }).statusOutputrunning
Independent answer, in RatMath's notationundecidable (a proof)
No computation can stand in for the diagonal argument;runwith a budget reportsrunning, as the documentation says it must. - 16.CircuitsAgreesA full adder adds bits and a carry . What are the sum and carry for ?Independent answer: sum 1, carry 1.RatMath code
logic.adders.full().evaluate({ a: 1, b: 1, cin: 1 })Output{ sum: 1, cout: 1 } - 17.Ripple-carry adderAgreesHow many gates and what depth does a ripple-carry adder for 4-bit numbers have, built from full adders of 5 gates and depth 3?Independent answerFour full adders: about 20 gates; the carry passes through each stage, so the depth grows linearly (roughly for the carry chain).RatMath code
const c = logic.adders.ripple(4); [c.size, c.depth]Output[17, 7]
The exact counts depend on how the half and full adders are built; the check accepts the textbook range. - 18.Master theoremAgreesSolve and asymptotically.Independent answermatches : . beats : .RatMath code
[discrete.masterTheorem({ a: 2, b: 2, d: 1 }).bound, discrete.masterTheorem({ a: 8, b: 2, d: 2 }).bound]Output["Θ(n log n)", "Θ(n^3)"]
- 19.SAT as a decision problemAgreesIs satisfiable? Give a satisfying assignment.Independent answerTry : then (clause 2), so (clause 3); clause 1 holds by , clause 4 by . Satisfiable with .RatMath code
const r = logic.satisfiable("(x1 | x2) & (~x1 | x3) & (~x2 | ~x3) & (x1 | ~x3)"); [r.status, r.model ?? r.assignment]Output["satisfiable", { x1: true, x2: false, x3: true }] - 20.Counting accepted wordsAgreesHow many binary words of length 4 have an even number of 1s?Independent answerHalf of the 16: .RatMath code
const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"], delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } }); M.words(4).filter((w) => w.length === 4).lengthOutput8