TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The limits of type theory: computation vs. interaction

81 pointsby danghicaalmost 10 years ago

13 comments

paganelalmost 10 years ago
I&#x27;m in a no way a computer-science expert, just a mere programmer, but by the looks of it this discussion all boils down to the (quite big, I&#x27;d say) impedance mismatch between real life (&quot;computer programs&quot; in this article) and us, as humans, trying to understand said life and applying rules to it (by in this case applying &quot;type theory&quot;).<p>For example, looking at this:<p>&gt;&gt;&gt; 0 if 1 else &#x27;a&#x27;<p>made me remember one of me older thoughts on this subject, more exactly if the word 2&#x2F;two from a sentence like this: &quot;there are two apples on the table&quot; should be treated as an Integer or a String. Some would say that we should only treat it as an Integer if we intend to do computations on it (for example adding those 2 apples to another 3 apples), to which I&#x27;d ask how come the word&#x27;s essence (its &quot;transformation&quot; from a String to an Integer) changes depending on the type of action we intend to apply on it? (so to speak, English is not my primary language).<p>Anyway, things in this domain are damn complicated, and have been so for the last 2500 years at least, starting with Socrates (or was it Plato, in fact?) who was asking about the essence&#x2F;idea of a table (i.e about its type), i.e. is a table still a table if it only has 3 legs remaining? what about two?, and continuing with Aristotle&#x27;s categories and closer to our days with Frege and Cantor (somehow the table problem can be connected to the latter&#x27;s continuum hypothesis, but I digress). So one of my points is that this is not only a computer science problem.
评论 #9679634 未加载
评论 #9682516 未加载
评论 #9678602 未加载
dschiptsovalmost 10 years ago
I wouldn&#x27;t say that heterogeneous ifs are silly for languages with explicitly type-tagged data (Lisps) where a value has a type, not a variable (actually, there are no variables - there are <i>bindings</i> - symbols acting as pointers to a type-tagged values). Moreover, I would say that homogenous conds and cases (pattern matching) with a &quot;default&quot; branch are way more silly.<p>Also I am very surprised by such statement from &#x27;the author of a several papers with the word type in the title&#x27;.<p>I also would argue that the design of CPUs such that any sequence of bits could be loaded in any in any register and interpreted depending of the current context, instead of having typed registers is not a shortcoming but a fundamental feature. It makes code small and elegant. There is whole TAOCP to see how.
评论 #9679387 未加载
stevanalmost 10 years ago
I&#x27;d disagree with the two statements:<p>&gt; People who think they understand something about the theory of programming languages, including me, tend to agree that what Python does is wrong.<p>&gt; In fact you can program heterogeneous lists in dependently typed languages, but it’s unreasonably complicated.<p>Here&#x27;s some Agda code that shows that both heterogeneous if statements and lists make sense and are easy to work with in type theory.<p><pre><code> data Bool : Set where true false : Bool if : ∀ {ℓ} {P : Bool → Set ℓ} (b : Bool) → P true → P false → P b if true t f = t if false t f = f postulate Char : Set {-# BUILTIN CHAR Char #-} data ℕ : Set where zero : ℕ suc : ℕ → ℕ {-# BUILTIN NATURAL ℕ #-} ex₁ : ℕ ex₁ = if {P = λ b → if b ℕ Char} true 0 &#x27;a&#x27; infixr 5 _∷_ data List {a} (A : Set a) : Set a where [] : List A _∷_ : A → List A → List A data HList : List Set → Set where [] : HList [] _∷_ : ∀ {A As} → A → HList As → HList (A ∷ As) ex₂ : HList (ℕ ∷ Char ∷ (ℕ → ℕ) ∷ []) ex₂ = 1 ∷ &#x27;a&#x27; ∷ suc ∷ []</code></pre>
评论 #9678950 未加载
评论 #9678908 未加载
评论 #9679618 未加载
mafribealmost 10 years ago
Maybe the author plans to write about it in later parts of the article, but it&#x27;s misleading to assume that (1) there is no work on types for interacting processes and (2) that types always have to be based on some underlying ideas form functional programming.<p>Much recent research in programming languages is about types for interacting processes. The most well-known, but by no means only example are the session types pioneered by K. Honda. The key idea is that each process has a bunch of interaction points, and the interaction at each interaction point is constrained by a type. Such types typically<p>- What direction does data flow? E.g. channel x is used for input while channel y does only output.<p>- What kind of data is exchanged on the channel? E.g. x is used to exchange a boolean, while y exchanges pairs, the first component of which is a double, and the second component is a channel name which is used for inputing a string.<p>- How often is a channel used? E.g. x is linear (used exactly once), y is affine (used at most once) while z can be used an arbitrary number of times.<p>This setup has been investigated from many angles, and one of the most beautiful results in this space is that normal types known from lambda-calculus (e.g. function space) can be recovered <i>precisely</i> as special cases of such interaction types, using Milner&#x27;s well-known encoding of functional computation as interaction.
评论 #9681203 未加载
评论 #9684632 未加载
danblickalmost 10 years ago
The article isn&#x27;t loading for me right now, but I thought I&#x27;d mention co-inductive types and how they relate to &quot;interaction&quot;.<p>In Coq, co-inductive types are used to model infinite objects like (never-ending) streams or (non-terminating) program executions.<p><pre><code> Inductive List (A: Set): Set := | Nil: List A | Cons : A -&gt; List A -&gt; List A. CoInductive Stream (A: Set): Set := | SCons: A -&gt; Stream A -&gt; Stream A. Fixpoint len {A: Set} (x: List A): nat := match x with | Nil =&gt; 0 | Cons _ y =&gt; 1 + len y end. CoFixpoint ones : Stream nat := SCons nat 1 ones. </code></pre> If you want to talk about how systems interact with the world, you can use bisimilarity relations to prove &quot;these two systems interact with the world in the same way&quot;.<p>You can also use co-inductive types to embed the temporal logic of actions (TLA) in Coq; TLA is the language Leslie Lamport works with for &quot;Specifying Systems&quot;.<p>* <a href="http:&#x2F;&#x2F;www.labri.fr&#x2F;perso&#x2F;casteran&#x2F;RecTutorial.pdf" rel="nofollow">http:&#x2F;&#x2F;www.labri.fr&#x2F;perso&#x2F;casteran&#x2F;RecTutorial.pdf</a><p>* <a href="http:&#x2F;&#x2F;www.amazon.com&#x2F;Specifying-Systems-Language-Hardware-Engineers&#x2F;dp&#x2F;032114306X" rel="nofollow">http:&#x2F;&#x2F;www.amazon.com&#x2F;Specifying-Systems-Language-Hardware-E...</a><p>* <a href="http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bisimulation" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bisimulation</a>
belovedeaglealmost 10 years ago
&gt; Each monad indicates one particular kind of interaction, such as using memory or perform input-output.<p>NO NO NO. Stop. It was cute when people got this wrong in 2010; now it&#x27;s just ignorant to pretend to know enough about Haskell to tell us something about it and still get this so wrong. There is no inherent connection between monads and statefulness; none whatsoever. Monads just happened to be a useful abstraction to make opaque for the purposes of IO.
评论 #9680403 未加载
edtechdevalmost 10 years ago
[0; &#x27;a&#x27;; fun x -&gt; x + 1];;<p>The common type is object, or perhaps more specifically, non-nil object (object!), if 0 is not treated as nil (value type).<p>If you try to interact with an object (add it to a number, call a method, etc.), your language has to decide what to do: fail because that method is not part of the type object, or force people to cast to the proper sub-type, or allow a kind of dependent typing&#x2F;casting (if type(it)=int: it+1 else if type=char: it.uppercase), or (like boo, python, ruby, etc.) allow &quot;duck typing&quot;: allow any calls&#x2F;methods on the object, and check it at run-time instead of compile-time
评论 #9680282 未加载
评论 #9679044 未加载
muraikialmost 10 years ago
&quot;To me this is one methodological weakness of type theory, the commitment of having types for all terms in the language. Why is that? Types are designed to facilitate composition, but the natural unit for program composition is not the term but the function, or even the module. It makes sense to assign types to modules — it is methodologically consistent. But how we calculate these types could conceivably be more flexible, allowing a global perspective within the unit of composition. Types, as properties at the interface could be calculated using the entire arsenal of (decidable!) logics and static analyses.&quot;<p>I admit to not being that familiar with type theory (I&#x27;ve done a little Scala but most of my FP experience is in Clojure) but when I read this my gut reaction was: &quot;Isn&#x27;t that kind of what Go is doing?&quot;<p>Meaning that in Go, the focus of compositionality isn&#x27;t on basic types and the functions that act on them (see Go&#x27;s lack of generics) but rather the focus is on getting different modules to agree on &quot;what should be done to some stuff&quot; using interfaces. This is putting interaction first but also getting compositionality in a &quot;generic&quot; way given that interfaces in Go are implemented implicitly.
评论 #9683279 未加载
nabla9almost 10 years ago
&gt; The result is to provide memory elements as black box library components, defined and tested in a different, lower-level, language. But at the interface the two levels of abstraction are reconciled and composition can occur.<p>I feel the same way. Uniqueness typing can work as the clue between component and upper lever abstraction.
pronalmost 10 years ago
The question of computation vs. interaction is an fascinating one, but I think there&#x27;s another question (perhaps related) to the applicability of &quot;rich&quot; types to practical programming, and that is regarding the role of types, and how far they <i>should</i> be stretched. I think there is little question that &quot;simple&quot; type systems (like in C&#x2F;C++&#x2F;Java&#x2F;C#) aid in developing large systems. But much of the fascination with type theory (as the article states) has to do with the Curry-Howard isomorphism, and the ability of programs to carry proofs to some of their properties. This is believed to be a powerful tool in software verification. The question then becomes to what extent should this tool be used? What properties <i>can</i> be proven? What properties programmers are willing to try and prove? How much harder is it to prove a property than write a possibly-incorrect algorithm without proof?<p>The benefit of proofs in code would only be worthwhile if it was relatively easy for common programmers to prove the absence of bugs that would have been more costly to find and fix using other methods, and that that ease would not come at the cost of other properties (such as performance). So far I have a feeling that this is not being delivered. Your run-of-the-mill clever use of types usually prevents very &quot;cheap&quot; bugs, and hard bugs are even harder to prevent with types.<p>I&#x27;ll give a concrete and recent real-world example. A few months ago, a bug was discovered in Python&#x27;s and Java&#x27;s sequential sorting algorithm[1], Timsort. It was introduced to Python in 2002, and its adoption by Java in 2011 has probably made it the most used sorting algorithm in the world. Yet, it contained a fatal bug that could cause it to crash by overflowing an array. There are a few interesting observations one could make about the bug and its discovery with relation to type systems.<p>1. The bug was discovered by a verification program that does not rely on type-based proofs in the code. The output from the verification system quickly showed where the bug actually was. AFAIK, the verification program required no assistance with proving the correctness of the algorithm, only that the variants the algorithm seeks to maintain be stated.<p>2. Proving the invariants required by the (rather complicated) algorithm with types would have required dependent types, and would have probably been a lot harder than using the verification program used. It is also unclear (to me) whether the problem and the fix would have been so immediately apparent.<p>3. The interaction of &quot;type-proofs&quot; with program semantics (namely, the requirement of referential transparency) would have made the type-proven algorithm much slower, and this particular algorithm was chosen just for its speed.<p>4. The corrected version ended up not being used because it could have adversely affected performance, and it was noticed that the &quot;incorrect&quot; algorithm with a slightly larger internal array would still always produce correct results for the input sizes currently supported in Java.<p>This example of a purely computational real-world bug shows some more limitations of the type-system approach to verification, I think.<p>Going back to effects and interactions, pure-FP approaches have a hard time dealing with timing properties. I already gave the same example on another discussion today, but the Esterel language (and languages influenced by it) has been used successfully to write 100% verifiable programs for safety-critical domains in the industry for a long time now. It is an imperative language (though it&#x27;s not Turing complete) with side effects and a lot of interactions, yet it has a verifier that uses Pnueli&#x27;s temporal logic to guarantee behavior, without requiring the programmer to supply proofs.<p>To summarize, type systems have shown their usefulness in reducing the cost of software development, but they need to be considered as a component in a larger verification space, and they need to strike a balance between usefulness and ease of use.<p>[1]: <a href="http:&#x2F;&#x2F;www.envisage-project.eu&#x2F;proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.envisage-project.eu&#x2F;proving-android-java-and-pyth...</a><p>[2]: <a href="http:&#x2F;&#x2F;www.envisage-project.eu&#x2F;key-deductive-verification-of-software&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.envisage-project.eu&#x2F;key-deductive-verification-of...</a>
评论 #9679399 未加载
评论 #9679477 未加载
murbard2almost 10 years ago
One can also create a largely generic type, for example a json like object and define polymorphic operators on these objects which handle casting as needed.<p>The program will need to keep track of the types at runtime, but that&#x27;s also true of Python, so what?
jimmyhmilleralmost 10 years ago
The site appears to be down. Anyone have a mirror?
Dewie3almost 10 years ago
I wonder what someone like Conor McBride would have to say about this article.
评论 #9679053 未加载