1/*  Author:        Jan Wielemaker
    2    E-mail:        jan@swi-prolog.org
    3    WWW:           http://www.swi-prolog.org
    4    Copyright (c)  2024, SWI-Prolog Solutions b.v.
    5    All rights reserved.
    6
    7    Redistribution and use in source and binary forms, with or without
    8    modification, are permitted provided that the following conditions
    9    are met:
   10
   11    1. Redistributions of source code must retain the above copyright
   12       notice, this list of conditions and the following disclaimer.
   13
   14    2. Redistributions in binary form must reproduce the above copyright
   15       notice, this list of conditions and the following disclaimer in
   16       the documentation and/or other materials provided with the
   17       distribution.
   18
   19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   20    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   22    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   23    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   24    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   25    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   26    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   27    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   29    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30    POSSIBILITY OF SUCH DAMAGE.
   31*/
   32
   33:- module(iso4217,
   34          [ iso4217/5,        % ?CtryNm, ?CcyNm, ?Ccy, ?CcyNbr, ?CcyMnrUnts
   35            iso4217_version/1 % -Version
   36          ]).   37:- autoload(library(sgml), [load_xml/3]).   38:- autoload(library(terms), [foldsubterms/4]).

Access ISO 4217 currency codes

This module loads `data/table.xml` to create clauses for iso4217/5 on the fly.

See also
- `data/table.xml` was extracted from https://pypi.org/project/iso4217/ */
 iso4217(?CtryNm, ?CcyNm, ?Ccy, ?CcyNbr, ?CcyMnrUnts) is nondet
True when the relation is true for a country. For example:
?- iso4217(CtryNm, CcyNm, 'EUR', CcyNbr, CcyMnrUnts).
CtryNm = 'Ă…LAND ISLANDS',
CcyNm = 'Euro',
CcyNbr = 978,
CcyMnrUnts = 2 ;
CtryNm = 'ANDORRA',
CcyNm = 'Euro',
CcyNbr = 978,
CcyMnrUnts = 2 ;
...

Note that SWI-Prolog JIT indexing makes the table efficient regardless of the query.

Arguments:
CtryNm- Country name
CcyNm- Currency name
Ccy- Currency code ([A-Z]{3})
CcyNbr- Currency number
CcyMnrUnts- Currency minor units
 iso4217_version(-Version) is det
True when Version is the version as in 'YYYY-MM-DD' (an atom). E.g.
?- iso4217_version(X).
X = '2024-06-25'.
   79term_expansion(iso4217('CtryNm', 'CcyNm', 'Ccy', 'CcyNbr', 'CcyMnrUnts'),
   80               Clauses) :-
   81    iso4217_clauses(Clauses).
   82
   83:- det(iso4217_clauses/1).   84iso4217_clauses([iso4217_version(Version)|Clauses]) :-
   85    absolute_file_name('../data/table.xml', File, [access(read)]),
   86    load_xml(File, DOM, [space(remove)]),
   87    DOM = [element('ISO_4217', ['Pblshd'=Version], _)],
   88    foldsubterms(iso4217_clause, DOM, Clauses, []).
   89
   90iso4217_clause(element('CcyNtry', _,
   91                       [ element('CtryNm',[],[CtryNm]),
   92                         element('CcyNm',[],[CcyNm]),
   93                         element('Ccy',[],[Ccy]),
   94                         element('CcyNbr',[],[CcyNbrA]),
   95                         element('CcyMnrUnts',[],[CcyMnrUntsA])
   96                       ]),
   97               [ iso4217(CtryNm, CcyNm, Ccy, CcyNbr, CcyMnrUnts) | List ],
   98               List) :-
   99    atom_number(CcyNbrA, CcyNbr),
  100    atom_number(CcyMnrUntsA, CcyMnrUnts).
  101
  102iso4217('CtryNm', 'CcyNm', 'Ccy', 'CcyNbr', 'CcyMnrUnts')