Interestingly, a :- use_module(ModuleB) is not needed in ModuleA if ModuleB is loaded via the main init.pl file by issuing the :- use_module(ModuleB) directive there.
Run
?- list_undefined.
to find out which predicates are undefined.
| Did you know ... | Search Documentation: |
| Predicate use_module/1 |
use_module(+Files)
The imported predicates act as weak symbols in the module
into which they are imported. This implies that a local definition of a
predicate overrides (clobbers) the imported definition. If the flag
warn_override_implicit_import
is true (default), a warning is printed. Below is an
example of a module that uses library(lists), but redefines flatten/2,
giving it a totally different meaning:
:- module(shapes, []). :- use_module(library(lists)). flatten(cube, square). flatten(ball, circle).
Loading the above file prints the following message:
Warning: /home/janw/Bugs/Import/t.pl:5:
Local definition of shapes:flatten/2
overrides weak import from lists
This warning can be avoided by (1) using use_module/2
to only import the predicates from the lists library that
are actually used in the‘shapes’module, (2) using the except([flatten/2])
option of use_module/2,
(3) use
:- abolish(flatten/2).
before the local definition or (4) setting
warn_override_implicit_import
to false. Globally disabling this warning is only
recommended if overriding imported predicates is common as a result of
design choices or the program is ported from a system that silently
overrides imported predicates.
Note that it is always an error to import two modules with use_module/1 that export the same predicate. Such conflicts must be resolved with use_module/2 as described above.
The SWI-Prolog HTTP server doesn't not server static files by default.
You explicitly have to add a handler telling it to do so. The
declarations below tell the server to serve files from the current
directory under / on the server.
:- use_module(library(http/http_server)).
:- use_module(library(http/http_files)).
:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).
Now, start the server and visit http://localhost:8080/.
?- http_server([port(localhost:8080)]). % Started server at http://localhost:8080/ true.
localhost: only binds the loopback network,
avoiding to expose your website to the outside world.With the help of library(csv) and library(json), we read a CSV file and use the column names in the first row to emit JSON.
For this example, we use the file weather.csv:
city,temp_lo,temp_hi,prcp,date San Francisco,46,50,0.25,1994-11-27 San Francisco,43,57,0,1994-11-29 Hayward,37,54,,1994-11-29
The script csv_to_json.pl reads the CSV from standard input or from
the file provided as the first argument of the script. It converts
the contents to a list of dicts, then writes this as JSON to standard
output:
:- initialization(main, main).
:- use_module(library(csv)).
:- use_module(library(json)).
main :-
( current_prolog_flag(argv, [CSV_file|_])
-> csv_read_file(CSV_file, CSV, [])
; csv_read_stream(current_input, CSV, [])
),
CSV = [Colnames|Rows],
Colnames =.. [row|Names],
maplist(row_dict(Names), Rows, Dicts),
json_write_dict(current_output, Dicts, [null('')]).
row_dict(Names, Row, Dict) :-
Row =.. [row|Fields],
pairs_keys_values(Data, Names, Fields),
dict_create(Dict, _, Data).
Note: versions before 9.3.33 must use library(http/json) instead of library(json).
The null('') option to json_write_dict/3 is necessary to convert the
empty "prcp" field in the last row to a missing value represented as
null in the JSON output.
This is how we can use it to convert weather.csv to JSON:
$ swipl csv_to_json.pl weather.csv
[
{
"city":"San Francisco",
"date":"1994-11-27",
"prcp":0.25,
"temp_hi":50,
"temp_lo":46
},
{
"city":"San Francisco",
"date":"1994-11-29",
"prcp":0,
"temp_hi":57,
"temp_lo":43
},
{
"city":"Hayward",
"date":"1994-11-29",
"prcp":null,
"temp_hi":54,
"temp_lo":37
}
]
Interestingly, a :- use_module(ModuleB) is not needed in ModuleA if ModuleB is loaded via the main init.pl file by issuing the :- use_module(ModuleB) directive there.
Run
?- list_undefined.
to find out which predicates are undefined.