Most of my readers are probably familiar with procedural programming, object-oriented programming (OOP), and functional programming (FP). The majority of top programming languages on all of the language popularity charts (like TIOBE) support all three to some extent.
Even if a programmer avoided one or more of those three paradigms like the plague, they’re likely at least aware of them and what they’re about. Or they’re applying one of the paradigms while denying that they’re doing so, like Haskell programmers using the IO or State Monads (procedural programming), or C programmers writing structs of function pointers (object-oriented programming), or Java programmers using streams (functional programming).
The same is sadly not true of logic programming. While some programmers are aware of its existence, and might have experienced a little bit of it in university, it’s not even close to the popularity of the other paradigms. I’d go so far as to say that the majority of programmers have no idea what it’s about, and that’s a shame because logic programming is really good at tackling certain kinds of problems.
OOP and FP are easy to explain in terms of procedural programming concepts, and it’s also pretty easy to explain how to implement them. That’s not really the case for logic programming, but when has that ever stopped me?
What better way to learn something than to implement it?
Why Logic Programming?
If you’ve ever lost your marbles trying to model complex relationships between various concepts as objects with bi-directional pointers to each other and derived properties that need to be cached and all that jazz, then that’s a great example of a problem where you should have used logic programming instead (looking at you OMG and your fancy SysML v2 standard).
In logic programming we don’t program with functions as we do in the other paradigms (procedures and methods are also functions). Functions have a set of inputs and a set of outputs, and mutable inputs can be viewed as just another kind of output.
Rather, in logic programming we program with relations. In logic programming they’re also called predicates, but they’re the same thing really (much like procedures and methods are kinds of functions).
The difference between a relation and a function is that a relation doesn’t have a clear distinction between what is an input and what is an output.
... continue reading