Show HN: Using SQL's Turing completeness to build Tetris

nffaria | 338 points

It seems at first to be a toy or silly intellectual exercise, but after reading the whole thing it really feels like an example of how constraints can lead to creative solutions. Can't log to stdout in the recursive CTE's loop? Maybe `RAISE NOTICE` will work. Can't take user input from the query itself? What if we stored the input in a table locally and read from that instead with `dblink`?

It's just a lot of fun, kudos for hacking this together, this is the sort of thing that makes me love software so much.

jihadjihad | 4 months ago

Cool project! I remember I had coded a Reinforcement Learning (RL) assignment long ago back in college with just SQL (I was familiar with Oracle back then, so that's what I had used). The course instructor was amused, more so when he saw how loops were implemented: I had a "loop" table with a sequence of N numbers in a column, and used to join with it to "loop" N times!

abhgh | 4 months ago

This is great but even more impressive than the code is all the documentation and explanation of how it works. Well done!

foreigner | 4 months ago

This is hilarious and amazing. But moreso than most such cool hack projects, it has a great writeup. The author really did a great job walking through how it worked. Love it.

fishtoaster | 4 months ago

I think a general purpose programming language that was just SQL with some provisions for user input and rendering would be really cool. Having to model all your state relationally and implement all your logic declaratively ultimately leads to some very nice code.

gfody | 4 months ago

This is awesome. I did linear regression in T-SQL once and it's a fun way to figure out what you can do with the language (eg - if you're unfamiliar with CTEs or cursors).

I'll definitely be checking this out later. Thanks for the post!

otteromkram | 4 months ago

Missed opportunity to call it "TetriSQL".

chaps | 4 months ago

This is a cool project. Just wondering... why did you build it?

jamiehewitt | 4 months ago

This is really really cool. Very cool work and welcome to HN!

gigatexal | 4 months ago

Nice, I remember my boss telling me not to do that when I was an intern for routing services, always wanted to see a working example. Well done.

tisdadd | 4 months ago

Wow this is amazing. Makes me realize how elementary my SQL skills are.

prng2021 | 4 months ago

[dead]

lagadu | 4 months ago

[dead]

firer | 4 months ago

[dead]

ericyd | 4 months ago

The article you linked, "tetris-sql," appears to be using database extensions to overcome SQL's limitations. While the core SQL language itself is not Turing complete, these extensions, such as Common Table Expressions (CTEs) and procedural languages, can provide the necessary constructs for iterative execution and conditional logic.

By leveraging these extensions, the author has effectively created a Turing complete environment within the database system, allowing them to implement Tetris. However, it's important to note that the implementation would be more convoluted and less efficient compared to a traditional programming language designed for game development.

Essentially, the article is demonstrating how to create a Turing complete environment using SQL and its extensions, but it's not a direct representation of SQL's inherent capabilities.

johnwatson11218 | 4 months ago