Since semester is almost over and my head is about to explode, today's song will be "Hold on" by "The Break", covered by the awsome cover band "Walk off the Earth", hope you enjoy, but mostly, if you're having a bad time, to remember you that you just have to hold on to what you got, and sooner or later, things are going to turn around great.
The main difference I see in logic programming compared to traditional programming paradigms is that you don't really program the solution to a specific problem, instead you program the logic rules and relationships of a certain problem and just as it if were magic, you get the result.
So, what's the magic behind logic programming? Well, in logic programming, once you set up the logic rules, the program tries to match using some sort of query and any sucessful match is considered a solution.
Let me explain this more clear:
(run* [q] (membero q [1 2 3]))
The code above (taken from "Seven more languages in seven weeks" does the following:
First, run* means that it will try to find all solutions, [q] is a given item, the third argument is a list which contains the function membero, which tries to find a relationship between the two proceding arguments, then we have q again, which is an item, and [1, 2, 3] which is a collection of values.
Translated to english that statement would mean something like: "Give me all items which match [1,2,3] so the result would be [1,2,3]
However, if we switched order in the argumens of membero we would get something like
(run* [q] (membero [1,2,3] q))
Which would return an infinite sequence of solutions, if we restrict the search, we could specify to end the search using run number where number is the upper bound limit of our solutions list, we could do something like:
(run 3 [q] (membero [1,2,3] q))
That would return the first three valid solutions of the relationship, which would be
[1,2,3 . _0] where . means something like "I don't care what _0 is, because anything entered would be a valid solution.
As we can clearly see in the last example, the logic behind logic programming is a lot like relational programming, where the expressions can be evaluated from both sides to find a relationship.
In logic programming, we search for a relationship between data and our logic rules to find a solution.
-Is logic programming really that flexible?
-Yes, that is why it bends minds.
The second solution would be [_0 , 1, 2, 3, . _1] where _0 would be "I still don't care" and _1 would be "I don't care either".
And so on, so basically the algorithm would return ANY possible list that has a consecutive 1,2,3 as values (emphasis on ANY) .
So, to this point we've seen that logic programming is flexible, powerful and reliable.
What's the trade-off? (As I've said in many previous blog entries, everything in software is a trade-off).
The main trade-off is efficiency, because since you never tell the computer which steps to make, logic programming mainly involves making every possible permutation of the problem's values to try a find a solution.
However, in exchange you get small, very, very powerful code. (Just imagine how many lines and how complicated compared to the previous code shown would it be to program that algorithm on some horrible, non-logical and non-smart language like Lua?
As a Lua programmer, my mind just melted the first time I saw logic programming (really).
It adds a new tool to the programmers mindset, and the advantages of logic programming language we read in Chapter 6 of "Seven more languages in seven weeks", Minikanren is that it binds with a lot of other programming languages, so you can have the best of both worlds.
That's all for now my fellow reader(s).
See you next week, got to pick up what's left of my brain from the floor.
Wololo.