# Wiki Discussion: Prolog in the mind set of SQL

**URL:** <https://swi-prolog.discourse.group/t/wiki-discussion-prolog-in-the-mind-set-of-sql/2661>\
**Category:** Wiki Discussion\
**Created:** [July 20, 2020, 10:39am UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-in-the-mind-set-of-sql/2661 "2020-07-20T10:39:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiki](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@Wiki](https://swi-prolog.discourse.group/u/Wiki)\
**Post date:** [July 20, 2020, 10:39am UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-in-the-mind-set-of-sql/2661/1 "2020-07-20T10:39:03Z")

</div>

This is a topic to discuss the Wiki

[Prolog in the mind set of SQL](https://swi-prolog.discourse.group/t/prolog-in-the-mind-set-of-sql/2660)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 20, 2020, 11:17am UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-in-the-mind-set-of-sql/2661/2 "2020-07-20T11:17:51Z")

</div>

The Wiki `Prolog in the mind set of SQL` is for those who know [SQL](https://en.wikipedia.org/wiki/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)](https://www.swi-prolog.org/pldoc/man?section=assoc): Association lists ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_assoc.pl))  
[library(dicts)](https://www.swi-prolog.org/pldoc/man?section=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](https://www.swi-prolog.org/pldoc/man?section=dicts))  
[dicts](https://www.swi-prolog.org/pldoc/man?section=bidicts) - structures with named arguments  
[library(lists)](https://www.swi-prolog.org/pldoc/man?section=lists): List Manipulation  
[library(nb\_set)](https://www.swi-prolog.org/pldoc/man?section=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)](https://www.swi-prolog.org/pldoc/man?section=ordsets): Ordered set manipulation ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_ordsets.pl))  
[library(tables)](https://www.swi-prolog.org/pldoc/man?section=tables): XSB interface to tables - ([test cases](https://github.com/SWI-Prolog/swipl-devel/tree/master/src/Tests/xsb)) (NB These are not the SQL tables you are looking for)  
[hashtable.pl](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/hashtable.pl) – Hash tables ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_hash_table.pl))  
[rbtrees.pl](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/rbtrees.pl) – Red black trees ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_rbtrees.pl))  
[trie](https://www.swi-prolog.org/pldoc/man?section=trie) - Tries (also called _digital tree_ , _radix tree_ or _prefix tree_ maintain a mapping between a variant of a term (see [=@=/2](https://www.swi-prolog.org/pldoc/man?predicate=%3D@%3D/2)) and a value.  
facts - [Database](https://www.swi-prolog.org/pldoc/man?section=db)

Data manipulation  
[library(aggregate)](https://www.swi-prolog.org/pldoc/man?section=aggregate): Aggregation operators on backtrackable predicates ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_aggregate.pl))  
[library(apply)](https://www.swi-prolog.org/pldoc/man?section=apply): Apply predicates on a list  
[library(yall)](https://www.swi-prolog.org/pldoc/man?section=yall): Lambda expressions - This is used to define higher-order predicates such as the [maplist/2](https://www.swi-prolog.org/pldoc/man?predicate=maplist/2)-5 family or [foldl/4](https://www.swi-prolog.org/pldoc/man?predicate=foldl/4)-7. ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_yall.pl))  
[solution\_sequences.pl](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/solution_sequences.pl) – Modify solution sequences ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_solution_sequences.pl))  
[Finding all Solutions to a Goal](https://www.swi-prolog.org/pldoc/man?section=allsolutions) - Commonly used to convert Prolog facts to Prolog lists. ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/test.pl))

Other  
[library(gensym)](https://www.swi-prolog.org/pldoc/man?section=gensym): Generate unique identifiers  
[library(persistency)](https://www.swi-prolog.org/pldoc/man?section=persistency): Provide persistent dynamic predicates  
[library(thread)](https://www.swi-prolog.org/pldoc/man?section=thread): High level thread primitives ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_thread.pl))  
[SWI-Prolog Unicode library](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/utf8proc.html%27)) - This library is a wrapper around the [utf8proc](http://www.public-software-group.org/utf8proc) library, providing information about Unicode code-points and performing operations (mappings) on Unicode atoms.  
[Tabled execution](https://www.swi-prolog.org/pldoc/man?section=tabling) (SLG resolution)  
[Transactions](https://www.swi-prolog.org/pldoc/man?section=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)](https://www.swi-prolog.org/pldoc/man?section=csv): Process CSV (Comma-Separated Values) data ([test cases](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/Tests/library/test_csv.pl))  
[library(iostream)](https://www.swi-prolog.org/pldoc/man?section=iostream): Utilities to deal with streams  
[SWI-Prolog ODBC Interface](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/odbc.html%27)) ([test cases](https://swi-prolog.discourse.group/t/swi-prolog-connecting-to-postgresql-via-odbc/2404))  
[SWI-Prolog binding to libarchive](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/archive.html%27)) - The library [libarchive](https://github.com/libarchive/libarchive) provides a portable way to access archive files as well as encoded (typically compressed) data.  
[Managing external tables for SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/table.html%27)) - External tables are files using a textual representation of records separated into fields.  
[SWI-Prolog SGML/XML parser](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/sgml.html%27))  
[Google’s Protocol Buffers Library](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/protobufs.html%27))  
[SWI-Prolog RDF parser](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/rdf2pl.html%27))  
SWI-Prolog [Redis client](https://github.com/SWI-Prolog/packages-redis) - open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

**Packs** - NB Use at your own risk

Other  
[library(cql/cql)](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/cql.html%27)): CQL (Constraint Query Language) - A high level interface to SQL databases  
[db\_facts](https://www.swi-prolog.org/pack/list?p=db_facts) - a term based interface to the ODBC and SQLite libraries.  
[docstore](https://www.swi-prolog.org/pack/list?p=docstore) - Document-oriented database for Prolog  
[fld](https://www.swi-prolog.org/pack/list?p=fld) - Easy assess to term args when loading from ODBC or CSV.  
[fluents](https://www.swi-prolog.org/pack/list?p=fluents) - Access all solutions of a goal without backtracking  
[hook\_hybrid](https://www.swi-prolog.org/pack/list?p=hook_hybrid) - Hook assert retract call of _specific_ predicates  
[list\_util](https://www.swi-prolog.org/pack/list?p=list_util) - Predicates for working with lists  
[odf\_sheet](https://www.swi-prolog.org/pack/list?p=odf_sheet) - Load and analyze ODF spreadsheets (.ods)  
[print\_table](https://www.swi-prolog.org/pack/list?p=print_table) - Formats and prints a table on the terminal screen  
[prolog\_rdf](https://www.swi-prolog.org/pack/list?p=prolog_rdf) - Prolog RDF Library  
[prosqlite](https://www.swi-prolog.org/pack/list?p=prosqlite) - An SWI-Prolog interface to SQLite  
[rocksdb](https://www.swi-prolog.org/pack/list?p=rocksdb) - SWI-Prolog interface to RocksDB  
[sparkle](https://www.swi-prolog.org/pack/list?p=sparkle) - Prolog-friendly interface to SPARQL endpoints  
[sparqlprog](https://www.swi-prolog.org/pack/list?p=sparqlprog) - Logic programming with SPARQL  
[sql\_compiler](https://www.swi-prolog.org/pack/list?p=sql_compiler) - Prolog to SQL compiler

External data sources  
[pl\_omdb](https://www.swi-prolog.org/pack/list?p=pl_omdb) - API interface to OMDB (Open Movie Database)  
[pubmed](https://www.swi-prolog.org/pack/list?p=pubmed) - Access pubmed publication records  
[wordnet](https://www.swi-prolog.org/pack/list?p=wordnet) - Access to WordNet database  
[bio\_db](https://www.swi-prolog.org/pack/list?p=bio_db) - The served tables include data from

- [HGNC](https://www.genenames.org/)
- [ENSEMBL](https://useast.ensembl.org/index.html)
- [NCBI](https://www.ncbi.nlm.nih.gov/)
- [UNIPROT](https://www.uniprot.org/)
- [PROSITE](https://prosite.expasy.org/)
- [STRING](https://string-db.org/)
- [GENE ONTOLOGY](http://geneontology.org/)
- [MGI](http://www.informatics.jax.org/) (mouse)

**Other** - Not part of the SWI-Prolog documentation  
[order\_by/2 examples](https://swi-prolog.discourse.group/t/order-by-2-examples/2667)  
[group\_by/4 examples](https://swi-prolog.discourse.group/t/group-by-4-examples/2678)

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.

**Benchmarks**  
[Database micro-benchmark](https://swi-prolog.discourse.group/t/database-micro-benchmark/1231)

References

“Logic and Database Queries” by Moshe Y. Vardi , Ian Barland and Ben McMahan ([ref](https://www.cs.rice.edu/~tlogic/Database/all-lectures.pdf)) (Note: link is now dead but leaving here in case it becomes public again)  
“Foundations of Databases” by Serge Abiteboul, Richard Hull and Victor Vianu ([ref](https://wiki.epfl.ch/provenance2011/documents/foundations%20of%20databases-abiteboul-1995.pdf))

Related topics

[Use setof/3 when goal not exported from module with existential (caret) operator (^)](https://swi-prolog.discourse.group/t/use-setof-3-when-goal-not-exported-from-module-with-existential-caret-operator/3292)  
[How to check for unique “primary keys”](https://swi-prolog.discourse.group/t/how-to-check-for-unique-primary-keys/3407)  
[Unified key-value interface](https://swi-prolog.discourse.group/t/unified-key-value-interface/4718)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 20, 2020, 3:02pm UTC](https://swi-prolog.discourse.group/t/wiki-discussion-prolog-in-the-mind-set-of-sql/2661/3 "2020-07-20T15:02:08Z")

</div>

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.~~
