Wiki Discussion: Prolog in the mind set of SQL

This is a topic to discuss the Wiki

Prolog in the mind set of SQL

The Wiki Prolog in the mind set of SQL is for those who know SQL and want to understand how to do similar queries in Prolog.

Parts of SWI-Prolog of interest related to SQL

Data Structures
library(assoc): Association lists (test cases)
library(dicts): Dict utilities (Not sure if this one should be here or how to use it, including so it is not orphaned if needed) (Examples with docs)
library(lists): List Manipulation
library(nb_set): Non-backtrackable set (Not sure if this one should be here or how to use it, including so it is not orphaned if needed)
library(ordsets): Ordered set manipulation (test cases)
library(tables): XSB interface to tables - (test cases) (NB These are not the SQL tables you are looking for) – Hash tables (test cases) – Red black trees (test cases)
trie - Tries (also called digital tree , radix tree or prefix tree maintain a mapping between a variant of a term (see =@=/2) and a value.
facts - Database

Data manipulation
library(aggregate): Aggregation operators on backtrackable predicates (test cases)
library(apply): Apply predicates on a list
library(yall): Lambda expressions - This is used to define higher-order predicates such as the maplist/2-5 family or foldl/4-7. (test cases) – Modify solution sequences (test cases)
Finding all Solutions to a Goal - Commonly used to convert Prolog facts to Prolog lists. (test cases)

library(gensym): Generate unique identifiers
library(persistency): Provide persistent dynamic predicates
library(thread): High level thread primitives (test cases)
SWI-Prolog Unicode library - This library is a wrapper around the utf8proc library, providing information about Unicode code-points and performing operations (mappings) on Unicode atoms.
Tabled execution (SLG resolution)
Transactions - Transactions allow running a goal in isolation . The goals running inside the transaction‘see’ the database as it was when the transaction was started together with database changes done by the transaction goal.

Included for importing or exporting data
library(csv): Process CSV (Comma-Separated Values) data (test cases)
library(iostream): Utilities to deal with streams
SWI-Prolog ODBC Interface (test cases)
SWI-Prolog binding to libarchive - The library libarchive provides a portable way to access archive files as well as encoded (typically compressed) data.
Managing external tables for SWI-Prolog - External tables are files using a textual representation of records separated into fields.
SWI-Prolog SGML/XML parser
Google’s Protocol Buffers Library
SWI-Prolog RDF parser
SWI-Prolog Redis client - open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

Packs - NB Use at your own risk

library(cql/cql): CQL (Constraint Query Language) - A high level interface to SQL databases
db_facts - a term based interface to the ODBC and SQLite libraries.
docstore - Document-oriented database for Prolog
fld - Easy assess to term args when loading from ODBC or CSV.
fluents - Access all solutions of a goal without backtracking
hook_hybrid - Hook assert retract call of specific predicates
list_util - Predicates for working with lists
odf_sheet - Load and analyze ODF spreadsheets (.ods)
print_table - Formats and prints a table on the terminal screen
prolog_rdf - Prolog RDF Library
prosqlite - An SWI-Prolog interface to SQLite
rocksdb - SWI-Prolog interface to RocksDB
sparkle - Prolog-friendly interface to SPARQL endpoints
sparqlprog - Logic programming with SPARQL
sql_compiler - Prolog to SQL compiler

External data sources
pl_omdb - API interface to OMDB (Open Movie Database)
pubmed - Access pubmed publication records
wordnet - Access to WordNet database
bio_db - The served tables include data from

Other - Not part of the SWI-Prolog documentation
order_by/2 examples
group_by/4 examples

Note: This is not meant to be a comprehensive list, it is just the entries I found while doing a scan of the SWI-Prolog documentation.

Database micro-benchmark


“Logic and Database Queries” by Moshe Y. Vardi , Ian Barland and Ben McMahan (ref)
“Foundations of Databases” by Serge Abiteboul, Richard Hull and Victor Vianu (ref)

Related topics

Use setof/3 when goal not exported from module with existential (caret) operator (^)
How to check for unique “primary keys”

1 Like

Clearly by looking at the items above missing test cases, IMHO many test cases need to be created so that others have examples of how to use the predicates. I know in initially learning Prolog not having examples was what kept me from using those predicates.

When I note that the test cases should be created, that means we the SWI-Prolog community should contribute to SWI-Prolog by writing the test cases and not expect this of others.

Also in creating the above list it needs to be further segregated out into different data structures such as list, rbtrees, association lists, facts, dicts, nb_set, ordsets, tables, etc. as many predicates for one data structure do not operate on other data structures. However many of the libraries tied to a data structure come with predicates can translate to/from Prolog list so all is not lost.