6 ms·
Java Generics Are Turing Complete
- ngrilly 10y agoThe title sounds like a bad news to my ears.
- lmm 10y agoOn one level yes. On the other hand it should hopefully reduce the FUD around alternatives with better type systems (Scala), if the type system is turing-complete in any case.
- calvins 10y ago> it should hopefully reduce the FUD around alternatives with better type systems (Scala), if the type system is turing-complete in any case. Beware of the Turing tar-pit in which everything is possible but nothing of interest is easy. Alan Perlis https://en.wikipedia.org/wiki/Turing_tarpit https://en.wikipedia.org/wiki/Turing_tarpit
- pron 10y agoThe problems with richer type systems (they're not "better", just more expressive, just as Dart's unsound, covariant type system is not better or or worse, just different) has nothing to do with them being Turing complete or not. And the "FUD" is nothing more than the very real reality that many teams find those language far too clever for their taste. I like Java, but others may not; you like Scala, but others may not. That's perfectly fine and normal, and doesn't make it FUD. As long as no one has good empirical evidence supporting their claims of their preferred language's superiority, these discussions are all about personal preference anyway. Being Turing complete or not is an intrinsic quality of the type system which has not been shown to have any effect whatsoever on the effectiveness of a language. Similarly, being more or less expressive, being sound or not, supporting subtyping or not, are intrinsic qualities that have not been shown to have either a positive or a negative effect. Those are all true properties -- like Coke cans being red -- but they cannot be assigned good/bad judgment other than in people's personal preferences -- until, that is, someone actually starts collecting data.
- lmm 10y agoWe don't yet have a lot of solid empirical evidence, sure - mostly because we don't have a good measure of productivity. But the fact that we can't measure it well doesn't mean it doesn't exist. There is a fact of the matter to be found, it's more than just personal preference. > And the "FUD" is nothing more than the very real reality that many teams find those language far too clever for their taste. I've heard people literally say "we shouldn't use Scala because its type system is Turing complete" (in an organization that was already using Java). I call that FUD.
- pron 10y ago> But the fact that we can't measure it well doesn't mean it doesn't exist. There is a fact of the matter to be found, it's more than just personal preference. I agree, but we still don't know which side has the advantage and how big the effect is. I believe that if we somehow control for all other factors, the effect of linguistic features is very small, but that "simpler" languages like Java/Kotlin would have the (small) advantage over "complex" ones like Scala. I can support this from personal experience (and also for theoretical reasons), but I can't prove that. You likely have a different experience (and other theoretical justifications), but can't prove your conjecture, either. > I call that FUD. I call that silly. It's just as silly as using intrinsic features as a positive. We shouldn't use Scala for a hundred reasons, none of them having to do anything with the formal language acceptable by its type system. ;)
- Avshalom 10y agoEh, turing complete is a really low bar to clear. In practice it just means the compiler can't be proved to halt on valid code. Avoiding turing completeness requires either very very careful design OR (trivially) a hard limit on size/iterations the compiler is allowed to make (a sufficiently small limit, obviously we call many things turing complete colloquially that have very low limits anyway like 8-bit cpus with ~kb of memory)
- TeeWEE 10y agoTuring complete is by now means a 'low bar'.. Turing complete means that a language, system can compute anything. Anything that can be computed, can be computed with java generics. Thats what it says. Its quite a important term.
- taeric 10y agoI believe the intent is that it is not a very informative bar to cross. Consider that Conway's Game of Life is Turing complete. Doesn't really increase the usage of it for solving problems. Basically, the expressiveness of a language is unrelated to it being Turing complete. And is much more relevant for most conversations.
- Avshalom 10y agoBasically all you need for turing completeness is the ability to jump/branch/ignore-some-bits on some criteria see SUBLEQ or cyclical tag systems or XY combinators. it's actually very hard to avoid in a system that is supposed to do some sort of computation.
- mikeash 10y agoIt's a low bar in that it's easy to achieve. So easy that it's often done by accident, as is the case here and in many other cases (C++ templates are another example). It often takes more work to define a useful language not to be Turing complete.
- naasking 10y ago> In practice it just means the compiler can't be proved to halt on valid code. Which is a terrible outcome, and isn't a problem for nearly any other language save C++.
- kodfodrasz 10y agoToo bad they are still pretty much useless in many cases because of type erasure, and many type constraints cannot be expressed (eg. constructor constraints)... Still way better than no generics at all, but if you are used to better implementations these random limitations can be really painful..
- cel1ne 10y agoKotlin, which is 100% java-interoperable has much nicer generics (declaration-site variance and type projections instead of wildcards). https://kotlinlang.org/docs/reference/generics.html https://kotlinlang.org/docs/reference/generics.html
- rsmckinney 10y agoTake a look at Gosu. It provides: - Reification - Declaration-site generics - Default covariance when variance not specified - Variance inference! In practice default covariance is usually the most suitable; it's the pragmatic approach. But you can make a generic type super sound, if that's what you need. And with variance inference your generic type can safely inherit from generic Java types. http://gosu-lang.github.io/2015/11/22/threading-the-needle.html http://gosu-lang.github.io/2015/11/22/threading-the-needle.h...
- pekk 10y agoIs there a reason to take a look at Gosu instead of Kotlin?
- rsmckinney 10y agoYou mean aside from it providing better and easier generics support? I personally like Kotlin, but I think Gosu has some advantages that should not be overlooked e.g,. Generics support.
- oweiler 10y ago
- vesinisa 10y agoWhat does this mean in practice? That I can compute an arbitrary algorithm with Java generics? How?
- rntz 10y agoWhat it means in practice is that any Java typechecker is either (a) incomplete - it will disallow some programs that should typecheck; (b) unsound - it will allow some programs that shouldn't typecheck; or (c) non-total - it will fail to terminate on some programs. (Or some combination of these three.)
- gpderetta 10y agoNo, that's in theory. In practice not much: any practical sufficiently complex type system quickly gets extended to become Turing complete; from time to time the compiler might get yell at you when you hit the maximum allowed type recursion limit, but usually everything just work. I believe that type checking in unextended Hindley-Milner type systems is exponential, so, even if the algo is guaranteed to terminate in theory it might not do so before the heat death of the universe. Again, in practice outside of test cases demonstrating this 'feature' it doesn't matter.
- valarauca1 10y ago>I believe that type checking in unextended Hindley-Milner type systems is exponential Yes it recursively solves for function typing parameters though the call graph. This isn't guaranteed to exit. If the language you are using annotates function argument's with types[1] then this behavior is avoided and Hindly-Milner becomes effectively linear. This is why type-inference is starting to become a common feature in most languages. [1] Nearly every language does this, but inline clojure are sometimes type inferred. Depends on language/language family.
- jameshart 10y ago"the compiler might get yell at you when you hit the maximum allowed type recursion limit" is an example of the compiler failing to compile something which is valid in the type system.
- btilly 10y agoIs there a link to the paper? Preferably one that you don't have to be at a university to read?
- cwmma 10y agohttp://arxiv.org/pdf/1605.05274v1.pdf http://arxiv.org/pdf/1605.05274v1.pdf in the upper right corner of the page
- magnumkarter 10y agoJust click PDF in the right hand corner.
- amelius 10y agoNow the waiting is for the first compiler that compiles to Java Generics.
- cleeus 10y agobtw, here is the URL for a Java-Generics-Program from the paper: https://gist.github.com/rgrig/b4cdaed3ed9a70dbdb6f158f14b57263 https://gist.github.com/rgrig/b4cdaed3ed9a70dbdb6f158f14b572... comment from the source: // Encodes a Turing machine that counts in binary, and then halts. // Gives stack overflow by default. // If you increase the stack size (javac -J-Xss100M Main.java), // then it type-checks, but it takes a lot of time.
- brudgers 10y agoSo is XSLT: http://www.unidex.com/turing/utm.htm http://www.unidex.com/turing/utm.htm
- rubenolivares 10y agowhine whine whine. go make a language of your own then, that way it's going to be perfect and have no flaws. oh wait, you wouldn't even know where to start.
- hepta 10y agoYou don't need to be a good chef to know a meal is awful. A lot of people use the language (me included) and make it work, doesn't mean it's without fault.
- dang 10y agoWe've banned this user for repeatedly violating the HN guidelines. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html We detached this subthread from https://news.ycombinator.com/item?id=11777678 https://news.ycombinator.com/item?id=11777678 and marked it off-topic.
- deleted 10y ago[deleted]
- Faint 10y agoDING! This is what always happens. If you want to get useful manipulations done to code you'll eventually creep up to a Turing complete system. Now you have a type system where you could calculate any program transformation, albeit in really really convoluted, slow and ugly way. It would have been SO much better to give the language proper macros to begin with. I mean, a library that's actually meant for doing AST manipulations, and a way to tell the compiler "apply this transformation to that code before doing anything else with it", instead of another compiler-only interpreted language inside the original language. People would not have to learn 2 different syntaxes, and compilation would probably be faster (well I'm thinking more of heavily templating dependant C++ libraries here).
- curried_haskell 10y agoYo dawg, do you need a programming language inside your programming language?
- exDM69 10y agoYes. Otherwise you'll end up with an ugly Python or Perl script that generates code, coupled with ugly build system hacks to make it work. This will happen sooner or later.
- DonHopkins 10y agoThe Java culture fetishizes code generation. It's like somebody decreed "We want to take over the world, and one measure of that is lines of code. So there should be a hell of a lot more Java code: start writing Java code that generates more Java code!"
- acjohnson55 10y agoGo culture, too, for that matter.
- LnxPrgr3 10y agoHow else are you going to do stuff like this? https://github.com/phresnel/metatrace https://github.com/phresnel/metatrace I'm just sad that Java's accidental compute system managed to be uglier (and apparently far less useful) than C++'s. The language could use some accidental features to counter the lack of intentional ones.
- chvid 10y agoIn practice in means if you write stuff like this: interface Z {} interface N<x> {} interface L<x> {} interface Qlr<x> {} interface Qrl<x> {} interface E<x> extends Qlr<N<?super Qr<?super E<?super E<?super x>>>>>, Qrl<N<?super Ql<?super E<?super E<?super x>>>>> {} interface Ql<x> extends L<N<?super Ql<?super L<?super N<?super x>>>>>, E<Qlr<?super N<?super x>>> {} interface Qr<x> extends L<N<?super Qr<?super L<?super N<?super x>>>>>, E<Qrl<?super N<?super x>>> {} class Main { L<?super N<?super L<?super N<?super L<?super N<?super E<?super E<?super Z>>>>>>>> doit( Qr<? super E<? super E<? super Z>>> v ){ return v; } } You can make your compiler: 1. Bark out an odd error. (Loop for a while and then stop due to an internal stack overflow.) 2. Compile something that gives a runtime type error.
- cromwellian 10y agoAn GWT engineer and former Scala guy, Lex Spoon, proved a similar theorem several years ago when we were upgrading generics for GWT-RPC. The GWT RPC code generator has to reason about return types of methods and which classes could possibly be deserialized say if you return a List of Foo. Lex showed how to construct the peano arithmetic in Java generics and how to formulate problems such that solving the question of what types could be returned from An Rpc call with List would amount to deciding these small theorems or problems and therefore the GWT SerializationTypeOracleBuilder class would have to solve the halting theorem. That's probably a wrong paraphrasing and maybe lex will see this and chime in but I think the original discussion was lost forever ironically when Wave, which was built with GWT, was shut down.