LMU ☀️ CMSI 3802
LANGUAGES AND AUTOMATA II
HOMEWORK #5 PARTIAL ANSWERS
  1. Here’s a JavaScript program that corresponds to the unit tests you were given.

    regex_exercises.js
    const regexes = {
      canadianPostalCode: /^[A-CEGHJ-NPR-TV-Z]\d[A-CEGHJ-NPR-TV-Z] \d[A-CEGHJ-NPR-TV-Z]\d$/,
      visa: /^4\d{12}(\d{3})?$/,
      masterCard:
        /^(5[1-5]\d{14}|222[1-9]\d{12}|22[3-9]\d{13}|2[3-6]\d{14}|27[01]\d{14}|2720\d{12})$/,
      notThreeEndingInOO: /^(?![\p{L}]oo$)\p{L}*$/iu,
      divisibleBy16: /^(0{1,3}|[01]*0000)$/,
      eightThroughThirtyTwo: /^([89]|[12]\d|3[0-2])$/,
      notPythonPycharmPyc: /^(?!pyc$|python$|pycharm$)\p{L}*$/u,
      restrictedFloats: /^\d+(\.\d*)?(e[+-]?\d{1,3})?$/i,
      palindromes2358:
        /^(?:([abc])\1|([abc])[abc]\2|([abc])([abc])[abc]\4\3|([abc])([abc])([abc])([abc])\8\7\6\5)$/,
      pythonStringLiterals:
        /^([ruf]|fr|rf)?('([^'\n\\]|\\.)*'|"([^"\n\\]|\\.)*"|'''('(?!'')|[^'\n\\]|\\.)*'''|"""("(?!"")|[^"\n\\]|\\.)*""")$/i,
    }
    
    export function matches(name, string) {
      return regexes[name].test(string)
    }
    
  2. To show that the language $EQ$ of all pairs of machines $M_1$ and $M_2$ such that $L(M_1) = L(M_2)$ is undecidable, we can reduce the halting problem to it.

    Assume $EQ$ is decidable. Then there exists an always-halting TM that inputs two descriptions and outputs YES if the machines recognize exactly the same language and NO otherwise.

    Now let’s define a function, $T$ that when given a machine description $\langle M\rangle$ and a string $w$, produces a machine that erases its input and replaces it with $w$, then simply runs $M$ (with input $w$). Thus $L(T(\langle M\rangle, w))$ is $\{w\}$ if $M$ halts on $w$ and is $\varnothing$ otherwise.

    Now, there exists a very simple machine $REJECT\_ALL$ (you should be able to show it, it is so simple) that rejects (says NO to) all of its inputs. Note that $L(REJECT\_ALL) = \varnothing$.

    Now here’s the good stuff. We’re going to make a machine that inputs some $\langle M\rangle$ and $w$, then we are going to run $EQ$ on $T(\langle M\rangle, w)$ and $REJECT\_ALL$. Then, if this run says YES, our new machine will say NO, and vice versa. WE’VE JUST DECIDED THE HALTING PROBLEM, PEOPLE. But the halting problem is undecidable, so our assume that $EQ$ was decidable must have been incorrect. Therefore $EQ$ is undecidable.

    A picture might help:

    reduceHaltToEq.png

    Remember, in this diagram $L(M') = \varnothing$ iff $M$ does not halt on $w$, so the dashed box would indeed be a halting problem decider if $EQ$ were a decider.