Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Exploring the world of declarative programming

#1
Exploring the world of declarative programming

Introduction


Most of us use imperative programming languages like C, Python, or Java at home. But the universe of programming languages is endless and there are languages where no imperative command has gone before. That which may sound impossible at the first glance is feasible with Prolog and other so called declarative languages. This article will demonstrate how to split a programming task between Python and Prolog.

In this article I do not want to teach Prolog. There are resources available for that. We will demonstrate how simple it is to solve a puzzle solely by describing the solution. After that it is up to the reader how far this idea will take them.

To proceed, you should have a basic understanding of Python. Installation of Prolog and the Python-Prolog bridge is accomplished using this command:

dnf install pl python3-pyswip

Our exploration uses SWI-Prolog, an actively developed Prolog which has the Fedora package name “pl”. The Python/SWI-Prolog bridge is pyswip.

If you are a bold adventurer you are welcome to follow me exploring the world of declarative programming.

Puzzle


The example problem for our exploration will be a puzzle similar to what you may have seen before.

How many triangles are there?

Getting started


Get started by opening a fresh text file with your favorite text editor. Copy all three text blocks in the sections below (Input, Process and Output) together into one file.

Input


This section sets up access to the Prolog interface and defines data for the problem. This is a simple case so it is fastest to write the data lines by hand. In larger problems you may get your input data from a file or from a database.

#!/usr/bin/python from pyswip import Prolog prolog = Prolog() prolog.assertz("line([a, e, k])") prolog.assertz("line([a, d, f, j])") prolog.assertz("line([a, c, g, i])") prolog.assertz("line([a, b, h])") prolog.assertz("line([b, c, d, e])") prolog.assertz("line([e, f, g, h])") prolog.assertz("line([h, i, j, k])")
  • The first line is the UNIX way to tell that this text file is a Python program.
    Don’t forget to make your file executable by using chmod +x yourfile.py .
  • The second line imports a Python module which is doing the Python/Prolog bridge.
  • The third line makes a Prolog instance available inside Python.
  • Next lines are puzzle related. They describe the picture you see above.
    Single small letters stand for concrete points.
    [a,e,k] is the Prolog way to describe a list of three points.
    line() declares that it is true that the list inside parentheses is a line .

The idea is to let Python do the work and to feed Prolog.

“Process”


This section title is quoted because nothing is actually processed here. This is simply the description (declaration) of the solution.

There is no single variable which gets a new value. Technically the processing is done in the section titled Output below where you find the command prolog.query().

prolog.assertz(""" triangle(A, B, C) :-  line(L1),  line(L2),  line(L3),  L1 \= L2,  member(A, L1),  member(B, L1),  member(A, L2),  member(C, L2),  member(B, L3),  member(C, L3),  A @< B, B @< C""") 

First of all: All capital letters and strings starting with a capital letter are Prolog variables!

The statements here are the description of what a triangle is and you can read this like:

  • If all lines after “:-“ are true, than triangle(A, B, C) is a triangle
  • There must exist three lines (L1 to L3).
  • Two lines must be different. “\=” means not equal in Prolog. We do not want to count a triangle where all three points are on the same line! So we check if at least two different lines are used.
  • member() is a Prolog predicate which is true if the first argument is inside the second argument which must be a list. In sum these six lines express that the three points must be pairwise on different lines.
  • The last two lines are only true if the three points are in alphabetical order. (“@<” compares terms in Prolog.) This is necessary, otherwise [a, h, k] and [a, k, h] would count as two triangles. Also, the case where a triangle contains the same point two or even three times is excluded by these final two lines.

As you can see, it is often not that obvious what defines a triangle. But for a computed approach you must be rather strict and rigorous.

Output


After the hard work in the process chapter the rest is easy. Just have Python ask Prolog to search for triangles and count them all.

total = 0 for result in prolog.query("triangle(A, B, C)"):  print(result)  total += 1 print("There are", total, "triangles.")

Run the program using this command in the directory containing yourfile.py :

./yourfile.py

The output shows the listing of each triangle found and the final count.

 {'A': 'a', 'B': 'e', 'C': 'f'} {'A': 'a', 'B': 'e', 'C': 'g'} {'A': 'a', 'B': 'e', 'C': 'h'} {'A': 'a', 'B': 'd', 'C': 'e'} {'A': 'a', 'B': 'j', 'C': 'k'} {'A': 'a', 'B': 'f', 'C': 'g'} {'A': 'a', 'B': 'f', 'C': 'h'} {'A': 'a', 'B': 'c', 'C': 'e'} {'A': 'a', 'B': 'i', 'C': 'k'} {'A': 'a', 'B': 'c', 'C': 'd'} {'A': 'a', 'B': 'i', 'C': 'j'} {'A': 'a', 'B': 'g', 'C': 'h'} {'A': 'a', 'B': 'b', 'C': 'e'} {'A': 'a', 'B': 'h', 'C': 'k'} {'A': 'a', 'B': 'b', 'C': 'd'} {'A': 'a', 'B': 'h', 'C': 'j'} {'A': 'a', 'B': 'b', 'C': 'c'} {'A': 'a', 'B': 'h', 'C': 'i'} {'A': 'd', 'B': 'e', 'C': 'f'} {'A': 'c', 'B': 'e', 'C': 'g'} {'A': 'b', 'B': 'e', 'C': 'h'} {'A': 'e', 'B': 'h', 'C': 'k'} {'A': 'f', 'B': 'h', 'C': 'j'} {'A': 'g', 'B': 'h', 'C': 'i'} There are 24 triangles.

There are certainly more elegant ways to display this output but the point is:
Python should do the output handling for Prolog.

If you are a star programmer you can make the output look like this:

***************************
* There are 24 triangles. *
***************************

Conclusion


Splitting a programming task between Python and Prolog makes it easy to keep the Prolog part pure and monotonic, which is good for logic reasoning. It is also easy to make the input and output handling with Python.

Be aware that Prolog is a bit more complicated and can do much more than what I explained here. You can find a really good and modern introduction here: The Power of Prolog.



https://www.sickgaming.net/blog/2021/04/...ogramming/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Contribute to Fedora Kernel 5.12 Test Week xSicKxBot 0 14 05-07-2021, 04:58 PM
Last Post: xSicKxBot
  Fedora - Introducing the Fedora i3 Spin xSicKxBot 0 18 05-05-2021, 07:51 PM
Last Post: xSicKxBot
  Fedora - Fedora Linux 34 is officially here! xSicKxBot 0 16 05-01-2021, 06:04 PM
Last Post: xSicKxBot
  Fedora - What’s new in Fedora Workstation 34 xSicKxBot 0 30 04-29-2021, 02:41 PM
Last Post: xSicKxBot
  Fedora - How to rebase to Fedora 34 on Silverblue xSicKxBot 0 28 04-28-2021, 03:17 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora 34 CoreOS Test Day xSicKxBot 0 30 04-23-2021, 06:03 PM
Last Post: xSicKxBot
  Fedora - Something bugging you in Fedora Linux? Let’s get it fixed! xSicKxBot 0 36 04-19-2021, 12:26 PM
Last Post: xSicKxBot
  Fedora - Fedora Workstation 34 feature focus: Btrfs transparent compression xSicKxBot 0 41 04-15-2021, 12:23 PM
Last Post: xSicKxBot
  Fedora - Contribute at Fedora Linux 34 Upgrade, Audio, and Virtualization test days xSicKxBot 0 42 04-06-2021, 10:27 AM
Last Post: xSicKxBot
  Fedora - Fedora Council statement on Richard Stallman rejoining FSF Board xSicKxBot 0 41 04-03-2021, 11:11 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
(Indie Deal) God Eater 3, DRAGON BALL, T...
Last Post: xSicKxBot
Today 10:20 AM
» Replies: 0
» Views: 6
Microsoft - Making remote and hybrid mee...
Last Post: xSicKxBot
Today 05:33 AM
» Replies: 0
» Views: 7
News - JAVA EDITION: SNAPSHOT 21W19A
Last Post: xSicKxBot
Today 05:31 AM
» Replies: 0
» Views: 7
News - Koei Tecmo’s Guzzler Joins Hamste...
Last Post: xSicKxBot
Today 05:30 AM
» Replies: 0
» Views: 8
News - Sega Looking To Release Brand New...
Last Post: xSicKxBot
Today 05:30 AM
» Replies: 0
» Views: 7
Xbox Wire - Xbox Spotlight: Mental Healt...
Last Post: xSicKxBot
Today 05:30 AM
» Replies: 0
» Views: 7
News - Live services push EA to $6.19 bi...
Last Post: xSicKxBot
Today 05:29 AM
» Replies: 0
» Views: 48
News - Blog: Improving the ‘About This G...
Last Post: xSicKxBot
Today 05:29 AM
» Replies: 0
» Views: 44
[Tut] How to Check the Pandas Version in...
Last Post: xSicKxBot
Yesterday 08:38 PM
» Replies: 0
» Views: 8
(Indie Deal) NBA 2K21, MY HERO ONE'S JUS...
Last Post: xSicKxBot
Yesterday 08:37 PM
» Replies: 0
» Views: 6

[-]
Twitter

Copyright © SickGaming.net 2012-2020