A collection of Prolog libraries that have proven useful in various projects. These libraries are intended to extend the functionality that is already available in the SWI-Prolog standard libraries.
Install SWI-Prolog.
Install this library:
swipl -g 'pack_install(prolog_library_collection)' -t halt
Once installed, modules from this library are loaded as follows:
:- [library(atom_ext)].
This module extends the standard library archive
:
-
archive_extension(?Extension:atom)
Succeeds if
Extension
is a default file name extension for an archive filter or format, as declared in library [[media_type]]. -
archive_media_type(?MediaType:media)
Succeeds if
MediaType
is the Media Type of an archive filter or format. -
archive_open(+In:stream, -Archive:blob)
Opens an archive over all supported and sensible archive filters and formats. This specifically excludes format
mtree
, which is a plain text format that is almost never used yet leads to many false positives in practice.
This module extends the standard library assoc
:
-
merge_assoc(+New:assoc, +Old:assoc, -Merge:assoc)
Merges two assocs into a new one. If the same key exists in
New
andOld
, the format replaces the latter inMerge
. These semantics are inspired by those of the standard library predicatemerge_options/3
in libraryoption
. -
transpose_assoc(+Assoc:assoc, -Transposed:assoc)
Turns an assoc of (key,value) pairs into one with(value,key) pairs.
This module provides additional support for working with atoms:
-
atom_capitalize(+Original:atom, ?Capitalized:atom)
Succeeds if
Capitalized
is a copt ofOrginal
where the first character is in upper case (if applicable). -
atom_ellipsis(+Original:atom, ?MaxLength:between(2,inf), ?Ellipsed:atom)
Succeeds if
Ellipsed
is likeOriginal
, but has ellipsis applied in order to haveMaxLength
. -
atom_postfix(+Original:atom, ?PostFix:atom)
-
atom_postfix(+Original:atom, ?Length:nonneg, ?PostFix:atom)
Succeeds if
Postfix
is a postfix ofOriginal
consisting ofLength
characters. -
atom_prefix(+Original:atom, ?PostFix:atom)
-
atom_prefix(+Original:atom, ?Length:nonneg, ?PostFix:atom)
Succeeds if
Prefix
is a prefix ofOriginal
consisting ofLength
characters. -
atom_strip(+Original:atom, ?Stripped:atom)
-
atom_strip(+Original:atom, +Strip:list(char), ?Stripped:atom)
Succeeds if
Stripped
is a copy ofOriginal
where leading and trailing characters inStrip
have been removed. -
atom_terminator(+Original:atom, +Terminator:atom, ?Terminated:atom)
Succeeds if
Terminated
is a copy ofOriginal
which is ensured to end with theTerminator
character. -
atom_truncate(+Original:atom, +MaxLenhgt:noneng, ?Truncated:atom)
Like
atom_prefix/3
, but theTruncated
atom is theOriginal
atom in caseMaxLength
exceeds theOriginal
atom length.
meta-predicates
This module extends support for working with character-denoting numeric codes:
put_codes(+Codes:list(code))
put_codes(+Out:stream, +Codes:list(code))
This module introduces a generic way for dealing with external configuration files:
cli_arguments(-Args:list(opt)) is det.
conf_json(-Conf:dict) is det.
Streamed processing of CSV files.
Definite Clause Grammars
In directory /dcg
you will find a collection of Definite Clause
Grammar (DCG) modules.
Advanced Bauckus-Naur Form (ABNF)
While DCGs are nice, they can be a bit verbose for expressing common repetition patterns. To make DCGs that include repetitions less verbose, this module implements variable repetition as defined in [[https://tools.ietf.org/html/rfc5234][RFC 5234: Augmented BNF for Syntax Specifications: ABNF]].
Suppose we want to parse sentences, which are non-empty sequences of words:
sentence1([H|T]) -->
word(H),
sentece2(T).
sentence2([H|T]) -->
word(H),
sentence2(T)
sentence2([]) --> "".
When this module is loaded, the same can be written as follows:
sentence(L) -->
+(word, L).
Variable repetition is a metasyntactic construct which states that at
least M
and at most N
occurrences of :Dcg_0
must be processed:
'm*n'(?M:nonneg, ?N:nonneg, :Dcg_0)//
Specific repetition is a metasyntactic construct which states that
exactly N
occurrences of Dcg_0
must be processed:
'#'(?N:nonneg, :Dcg_0)//
Specific repetition is a special case of [[variable repetition]],
because #(N, Dcg_0)
is the same as 'm*n'(N, N, Dcg_0)
.
Kleene star is a metasyntactic construct which states that zero or
more occurrences of Dcg_0
must be processed:
*(?N:nonneg, :Dcg_0)//
Kleene star is a special case of [[variable repetition]], because
*(N, Dcg_0)
is the same as 'm*n'(_, _, Dcg_0)
.
Kleene sum is a metasyntactic construct which states that one or more
occurrences of Dcg_0
must be processed:
+(?N:nonneg, :Dcg_0)//
Kleene sum is a special case of [[variable repetition]], because +(N, Dcg_0)
is the same as 'm*n'(1, _, Dcg_0)
.
Optional sequence is a metasyntactic construct which states that
Dcg_0
should either be processed once or not at all:
?(:Dcg_0)//
Optional sequence is a special case of [[variable repetition]],
because ?(Dcg_0)
is the same as 'm*n'(0, 1, Dcg_0)
.
DCG | Meaning | Name |
---|---|---|
#(?N, :Dcg_0)// |
Process Dcg_0 exactly N times. |
specific repetition |
*(:Dcg_0)// |
Process Dcg_0 0 or more times. |
Kleene star |
'*n'(?N, :Dcg_0)// |
Process Dcg_0 at most N times. |
|
+(:Dcg_0)// |
Process Dcg_0 1 or more times. |
Kleene sum |
?(:Dcg_0)// |
Process Dcg_0 0 or 1 times. |
optional sequence |
'm*'(?M, :Dcg_0)// |
Process Dcg_0 at least M times. |
|
'm*n'(?M, ?N, :Dcg_0)// |
Process Dcg_0 at least M and at most N times. |
variable repetition |
It contains the following modules:
Type | Definition |
---|---|
media |
A compound term of the form media(Super:atom/Sub:atom,Parameters:list(opt)) |
opt |
A unary compound term whose predicate letter is an option name and whose argument is a corresponding option value. |
Dictionaries.
Difference lists.
Handling files and directories.
This module provides extended JSON support on top of the standard
library http/json
:
-
json_load(+File:atom, -Structure:dict) is det.
-
json_save(+File:atom, +Structure:dict) is det.
Running external processes, streaming to/from external processes.
Support for recoding, unpacking, sorting, and hasing streams.
Constructing/decomposing URIs.
This module allows Prolog goals to be called on a stream that encodes an XML DOM:
call_on_xml(+In:stream, +Names:list(atom), :Goal_1) is det.
The following predicates allow the encoding of an XML file or stream to be determined:
xml_encoding(+In:stream, -Encoding:atom) is semidet.
xml_file_encoding(+File:atom, -Encoding:atom) is semidet.
Support for XML Schema 1.1 Part 2: Datatypes.
-
xsd_date_time/3
for translating between XSD date/time representations and date/time representations as supported by [[https://github.com/wouterbeek/Prolog-Library-Collection][Prolog-Library-Collection]]. -
xsd_date_time_type/1
for checking/enumerating the XSD date/time datatype IRIs. -
xsd_encode_string//0
a DCG rule for encoding strings of characters according to the restrictions of the XSD string datatype. -
xsd_numeric_type/1
enumerates XSD numeric datatype IRIs. -
xsd_strict_subtype/2
andxsd:subtype/2
allow the hierarchy of XSD datatype IRIs to be queried.