Programming logic and logic programming


I see many people speaking about programming logic when they refer to programming. Well, there is no formal logic definition for programming logic. What people usually refer when using this term is structured programming, object-oriented programming or procedural programming.

Now let's talk about logic programming, which is something entirely different from programming logic or any of the popular definitions for it. Logic programming was one of the first ways to program and is based on formal logic.

Formal logic also has some popular confusion about it, when speaking about formal logic some people confuse it for Boolean Algebra. Well, let me try to make it clear: Formal logic is something most people "kinda" know but never really thought about it and logic programming is based on that. Everyone must have read sometime these kinds of phrases:


Concrete is grey.
Walls are made of concrete.
Hence,

walls are grey.


Well, here is the news: this is something that can be expressed using formal logic and also logic programming. I will use first-order logic to try to explain it.

One of the ways to define the phrase: "Concrete is grey" using first-order logic is: "is(concrete, grey)". In this statement, "is()" is a function (not the same as a function in OOP or procedural programming); "concrete" and "grey" are atoms. These elements are called terms.

A function usually defines a relation between the inner terms (a term is an atom or a function). In this case, it defines a relation between "concrete" and "grey". Try to think of functions as verbs and atoms as substantives.

Now the next statement "Walls are made of concrete" can be written as "made-of(walls, concrete)". The same as before, " made-of ()" is a function; " walls" and "concrete" are atoms.

Now, these 2 statements are facts. They are ground truths that do not change. Another statement we can make rules of inference. A rule of inference is a statement that is used to deduce (or to infer) logical consequences. In this example, the defined rule would be:

$is(A,B) \wedge made$-$of(C,A) \implies is(C,B)$

This is commonly read as (A is B) and (C made-of A) implies (C is B).
This A, B and C are variables, that means they can have any value when unifying (I'm not going to enter in-depth into unification, just think that capital letters can have any value). So, looking at our facts and at this rule, one possible assignment of variables would be:

A="concrete", B="grey", C="walls"

Assigning these values, our rule would end up as:

$is(concrete,grey) \wedge made$-$of(walls,concrete) \implies is(concrete,grey)$

This assignment is usually done by the compiler in logic programming. So, once you define a rule with variables it will try to match values with all available facts (and chain of rules).

With these 2 facts and this rule, the result is exactly the expressions we defined before:


Concrete is grey. is(concrete, gray)
Walls are made of concrete. made-of(walls, concrete)
Hence,
Walls are grey. (logical consequence of the rule)


In these statements, rules are usually omitted. On our daily lives, these rules are kind of language-dependent and every verb has some hidden meaning or rule about it, but in the end, the logical consequences are made by our brain automatically whenever we hear something.

Comments

Popular posts from this blog

Unity3D/C# - Asynchronous Programming - Coroutines vs await/async

C# - Simple Graph Interfaces and a MeshGraph

Unity/C# - Grids - Simple Grid Segment struct