<!--
.. title: Porting the "Worst Datalog" to Python
.. slug: porting-the-worst-datalog-to-python
.. date: 2024-10-30 16:10:17 UTC+02:00
.. tags: python
.. category: 
.. link: 
.. description: 
.. type: text
-->

I really enjoyed the two posts by Christophe Grand about Datalog, [Writing the
Worst Datalog Ever in
26loc](https://buttondown.com/tensegritics-curiosities/archive/writing-the-worst-datalog-ever-in-26loc/)
and [Half Dumb Datalog in 30
loc](https://buttondown.com/tensegritics-curiosities/archive/half-dumb-datalog-in-30-loc/)
(even more than the first one), so I ended up porting them to Python.

There's a bunch of differences to the Clojure versions:

- they are roughly twice as long
- use generators and thus have to environment threading somewhat differently
- a custom class for variables (no symbols in Python)
- some dictionary copying is going on because of mutability (and I didn't want
  to think too much about when mutation is happening).

The semi-naive implementation ended up being roughly 50 lines, which is pretty
reasonable I thought.

The code for them both is in this [Gist](https://gist.github.com/cfbolz/e93e561c5fedf5fba6b2069f250bcd8c):

<script src="https://gist.github.com/cfbolz/e93e561c5fedf5fba6b2069f250bcd8c.js"></script>
