my blog lives here now
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
9.4 KiB

  1. ---
  2. title: "This Sentence is False, or: On Natural Language, Typing and Proof"
  3. date: September 9th, 2020
  4. ---
  5. The Liar's paradox is often the first paradox someone dealing with logic, even in an informal setting, encounters. It is _intuitively_ paradoxical: how can a sentence be both true, and false? This contradicts (ahem) the law of non-contradiction, that states that "no proposition is both true and false", or, symbolically, $\neg (A \land \neg A)$. Appealing to symbols like that gives us warm fuzzy feelings, because, _of course, the algebra doesn't lie!_
  6. There's a problem with that the appeal to symbols, though. And it's nothing to do with non-contradiction: It's to do with well-formedness. How do you accurately translate the "this sentence is false" sentence into a logical formula? We can try by giving it a name, say $L$ (for liar), and state that $L$ must represent some logical formula. Note that the equality symbol $=$ here is _not_ a member of the logic we're using to express $L$, it's a symbol of this discourse. It's _meta_​logical.
  7. $$ L = \dots $$
  8. But what should fill in the dots? $L$ is the sentence we're symbolising, so "this sentence" must mean $L$. Saying "X is false" can be notated in a couple of equivalent ways, such as $\neg X$ or $X \to \bot$. We'll go with the latter: it's a surprise tool that will help us later. Now we know how to fill in the dots: It's $L \to \bot$.
  9. <details>
  10. <summary>Truth tables demonstrating the equivalence between $\neg A$ and $A \to \bot$, if you are classically inclined.</summary>
  11. <div class="mathpar">
  12. <table>
  13. <tr>
  14. <th> $A$ </th>
  15. <th> $\neg A$ </th>
  16. </tr>
  17. <tr><td>$\top$</td><td>$\bot$</td></tr>
  18. <tr><td>$\bot$</td><td>$\top$</td></tr>
  19. </table>
  20. <table>
  21. <tr>
  22. <th> $A$ </th>
  23. <th> $A\to\bot$ </th>
  24. </tr>
  25. <tr><td>$\top$</td><td>$\bot$</td></tr>
  26. <tr><td>$\bot$</td><td>$\top$</td></tr>
  27. </table>
  28. </div>
  29. </details>
  30. But wait. If $L = L \to \bot$, then $L = (L \to \bot) \to \bot$, and also $L = ((L \to \bot) \to \bot) \to \bot$, and so... forever. There is no finite, well-formed formula of first-order logic that represents the sentence "This sentence is false", thus, assigning a truth value to it is meaningless: Saying "This sentence is false" is true is just as valid as saying that it's false, both of those are as valid as saying "$\neg$ is true".
  31. Wait some more, though: we're not done. It's known, by the [Curry-Howard isomorphism], that logical systems correspond to type systems. Therefore, if we can find a type-system that assigns a meaning to our sentence $L$, then there _must_ exist a logical system that can express $L$, and so, we can decide its truth!
  32. Even better, we don't need to analyse the truth of $L$ logically, we can do it type-theoretically: if we can build an inhabitant of $L$, then it is true; If we can build an inhabitant of $\neg L$, then it's false; And otherwise, I'm just not smart enough to do it.
  33. So what is the smallest type system that lets us assign a meaning to $L$?
  34. # A system of equirecursive types: $\lambda_{\text{oh no}}$[^1]
  35. [^1]: The reason for the name will become obvious soon enough.
  36. We do not need a complex type system to express $L$: a simple extension over the basic simply-typed lambda calculus $\lambda_{\to}$ will suffice. No fancy higher-ranked or dependent types here, sorry!
  37. As a refresher, the simply-typed lambda calculus has _only_:
  38. * A set of base types $\mathbb{B}$,
  39. * Function types $\tau \to \sigma$,
  40. * For each base type $b \in \mathbb{B}$, a set of base terms $\mathbb{T}_b$,
  41. * Variables $v$,
  42. * Lambda abstractions $\lambda v. e$, and
  43. * Application $e\ e'$.
  44. <details>
  45. <summary>Type assignment rules for the basic $\lambda_{\to}$ calculus.</summary>
  46. <div class="math-paragraph">
  47. <div>
  48. $$\frac{x : \tau \in \Gamma}{\Gamma \vdash x : \tau}$$
  49. </div>
  50. <div>
  51. $$\frac{b \in \mathbb{B} \quad x \in \mathbb{T}_{b}}{\Gamma \vdash x : b}$$
  52. </div>
  53. <div>
  54. $$\frac{\Gamma, x : \sigma \vdash e : \tau}{\Gamma \vdash \lambda x. e : \sigma \to \tau}$$
  55. </div>
  56. <div>
  57. $$\frac{\Gamma, e : \sigma \to \tau \quad \Gamma \vdash e' : \sigma}{\Gamma \vdash e\ e' : \tau}$$
  58. </div>
  59. </div>
  60. </details>
  61. First of all, we'll need a type to represent the logical proposition $\bot$. This type is empty: It has no type formers. Its elimination rule corresponds to the principle of explosion, and we write it $\mathtt{absurd}$. The inference rule:
  62. <div class="math-paragraph">
  63. $$\frac{\Gamma \vdash e : \bot}{\mathtt{absurd}\ e : A}$$
  64. </div>
  65. We're almost there. What we need now is a type former that serves as a solution for equations of the form $v = ... v ...$. That's right: we're just _inventing_ a solution to this class of equations---maths!
  66. These are the _equirecursive_ types, $\mu a. \tau$. The important part here is _equi_: these types are entirely indistinguishable from their unrollings. Formally, we extend the set of type formers with type variables $a$ and $\mu$-types $\mu a. \tau$, where $\mu a$ acts as a binder for $a$.
  67. Since we invented $\mu$ types as a solution for equations of the form $a = \tau$, we have that $\mu a. \tau = \tau[\mu a.\tau/a]$, where $\tau[\sigma{}/a]$ means "substitute $\sigma{}$ everywhere $a$ occurs in $\tau$". The typing rules express this identity, saying that anywhere a term might have one as a type, the other works too:
  68. <div class="math-paragraph">
  69. <div>
  70. $$\frac{\Gamma \vdash e : \tau[\mu a.\tau / a]}{\Gamma \vdash e : \mu a. \tau}$$
  71. </div>
  72. <div>
  73. $$\frac{\Gamma \vdash e : \mu a.\tau}{\Gamma \vdash e : \tau[\mu a. \tau / a]}$$
  74. </div>
  75. </div>
  76. Adding these rules, along with the one for eliminating $\bot$, to the $\lambda_{\to}$ calculus nets us the system $\lambda_{\text{oh no}}$. With it, one can finally formulate a representation for our $L$-sentence: it's $\mu a. a \to \bot$.
  77. There exists a closed term of this type, namely $\lambda k. k\ k$, which means: The "this sentence is false"-sentence is true. We can check this fact ourselves, or, more likely, use a type checker that supports equirecursive types. For example, OCaml with the `-rectypes` compiler option does.
  78. We'll first define the empty type `void` and the type corresponding to $L$:
  79. <div class="math-paragraph">
  80. ~~~~{.ocaml}
  81. type void = | ;;
  82. type l = ('a -> void) as 'a ;;
  83. ~~~~
  84. </div>
  85. Now we can define our proof of $L$, called `yesl`, and check that it has the expected type:
  86. <div class="math-paragraph">
  87. ~~~~{.ocaml}
  88. let yesl: l = fun k -> k k ;;
  89. ~~~~
  90. </div>
  91. However. This same function is also a proof that... $\neg L$. Check it out:
  92. <div class="math-paragraph">
  93. ~~~~{.ocaml}
  94. let notl (x : l) : void = x x ;;
  95. ~~~~
  96. </div>
  97. # I am Bertrand Russell
  98. Bertrand Russell (anecdotally) once proved, starting from $1 = 0$, that he was the Pope. I am also the Pope, as it turns out, since I have on hand a proof that $L$ and $\neg L$, in violation of non-contradiction; By transitivity, I am Bertrand Russell. <span style="float: right; display: inline-block;"> $\blacksquare$ </span>
  99. Alright, maybe I'm not Russell (drat). But I am, however, a trickster. I tricked you! You thought that this post was going to be about a self-referential sentence, but it was actually about typed programming language design (not very shocking, I know). It's a demonstration of how recursive types (in any form) are logically inconsistent, and of how equirecursive types _are wrong_.
  100. The logical inconsistency, we all deal with, on a daily basis. It comes with Turing completeness, and it annoys me to no end every single time I accidentally do `let x = ... x ...`{.haskell}. I _really_ wish I had a practical, total functional programming language to use for my day-to-day programming, and this non-termination _everywhere_ is a great big blotch on Haskell's claim of purity.
  101. The kind of recursive types you get in Haskell is _fine_. They're not _great_ if you like the propositions-as-types interpretation, since it's trivial to derive a contradiction from them, but they're good enough for programming that implementing a positivity checker to ensure your definitions are strictly inductive isn't generally worth the effort.
  102. Unless your language claims to have "zero runtime errors", in which case, if you implement isorecursive types instead of inductive types, you are _wrong_. See: Elm. God damn it.
  103. <details>
  104. <summary>So much for "no runtime errors"... I guess spinning forever on the client side is acceptable.</summary>
  105. <div class="flex-list">
  106. ```elm
  107. -- Elm
  108. type Void = Void Void
  109. type Omega = Omega (Omega -> Void)
  110. yesl : Omega
  111. yesl = Omega (\(Omega x) -> x (Omega x))
  112. notl : Omega -> Void
  113. notl (Omega x) = x (Omega x)
  114. ```
  115. </div>
  116. </details>
  117. Equirecursive types, however, are a totally different beast. They are _basically_ useless. Sure, you might not have to write a couple of constructors, here and there... at the cost of _dramatically_ increasing the set of incorrect programs that your type system accepts. Suddenly, typos will compile fine, and your program will just explode at runtime (more likely: fail to terminate). Isn't this what type systems are meant to prevent?
  118. Thankfully, very few languages implement equirecursive types. OCaml is the only one I know of, and it's gated behind a compiler flag. However, that's a footgun that should _not_ be there.
  119. **EDIT** (April 14th, 2021) It's been pointed out to me that you can get equirecursive types in OCaml even without passing `-rectypes` to the compiler. I am not an OCaml expert, so I encourage you to see [here](https://gist.github.com/drvink/a0094680aaae2569951ea4601752944d) for more details.
  120. [Curry-Howard isomorphism]: https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence