2 title: "Logic Programming Is Also Pretty Easy?"
8 summary: "I've been hearing about MiniKanren and MicroKanren for a while, and I finally just decided to go and read the paper to see if I could understand it. Here's my understanding of it."
9 prev: posts/2020/syntatic-unification
12 Last time we implemented unification, with a few paths forward in mind.
13 One of those was logic programming.
15 In a logic program, you write equations that describe what things are true, and then the logic program engine searches for a solution to those equations.
16 For certain types of problems, this can be a great way to implement things.
17 For instance, the Rust compiler is currently trying to [reimplement some of their type system this way][chalk] in order to make it simpler and support more features.
18 There are some full-featured logic programming languages like [Prolog][] that have a long history.
19 When someone talks about logic programming, they mention Prolog most of the time.
20 There are other smaller languages like [Datalog][] which is designed for queries and analysis, but it's still a large language, much more than we can implement.
21 We want something smaller.
23 [chalk]: https://smallcultfollowing.com/babysteps/blog/2017/01/26/lowering-rust-traits-to-logic/
24 [prolog]: https://en.wikipedia.org/wiki/Prolog
25 [datalog]: https://en.wikipedia.org/wiki/Datalog
27 Luckily, we've got some options.
28 The [Kanrens][] are a family of small logic languages that are embedded in a host language, letting you write normal code and getting all the features of the host language included for free.
29 They were originally made for Scheme and ["The Reasoned Schemer"][reasoned-schemer], with the miniKanren language.
30 Later, microKanren was created with the goal "how simple can we make this?"
31 [The microKanren paper][] is very readable.
32 But if you don't want to go read a paper, I'm gonna explain it here.
34 [kanrens]: http://minikanren.org/
35 [reasoned schemer]: https://mitpress.mit.edu/books/reasoned-schemer-second-edition
36 [the microkanren paper]: http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf
38 ### What's In A Logic Program?
48 How we'll represent goals in Python
56 ### Writing Intersting Logic Programs