pgn_game_lengths.pl

 pgn_game_lengths(+OptsAndFiles)
Calculate lengths of games in PGN files.

Atoms in OptsAndFiles are taken to be PGN files.

This script illustrates how to use the chess_db/2,3 machinery to process PGN in arbitray ways (rather than to just load PGN games to a database). This is trivial in the case where the PGN can be loaded in memory as you the read terms (from pgn/2) can be passed to the counting predicates implemented here without resorting to using chess_db/2, however, it is useful when we are dealing with huge PGNs via incremental processiing (option incr(true)) in chess_db/3.

Opts

debug(Dbg=true)
informational, progress messages
help(Help=false)
help messsage and exit
incr(Incr=true)
as in os_chess/2 option. The default here is true, as this script needs the chess_db/2 machinery more.
output(Out=file)
could also use terminal
postfix(Psx=glens)
postfix for output file, if used
sep(Sep=_)
separator for postfix (see os_postfix/3)
stack_limit(Slm=false)
or set to a number (stack limit will be set to Slm Gbs)
usage(Usg=false)
usage message and exit

Opts are passed to chess_db/2.

Examples

?- pgn_game_lengths([]).
> upsh pgn_game_lengths output=terminal ../data/pgn/18.03-candidates.pgn

% Doing: '../data/pgn/18.03-candidates.pgn', output on: '../data/pgn/18.03-candidates_glens.pgn'
% Calling chess_db/2 options: [incr(false),goal(pgn_games_length),goal_iter([]),goal_return(_46328)]
% Predicate: chess_db/2 at 21:23:57 on 24th of Jan 2026 starting task: PGN load from: ../data/pgn/18.03-candidates.pgn.
% Predicate: chess_db/2 option selected: incr(false).
% Wrote on file: '/home/nicos/pl/packs/src/chess_db/data/pgn/18.03-candidates_glens.pgn'
% Finished: pgn_game_lengths

> upsh pgn_game_lengths.pl lichess_elite_2023-01.pgn incr=false
ERROR: Stack limit (3.9Gb) exceeded
ERROR:   Stack sizes: local: 5Kb, global: 2.8Gb, trail: 0.3Gb
ERROR:   Stack depth: 268,715, last-call: 100%, Choice points: 12
...
ERROR: ?- set_prolog_flag(stack_limit, 8_294_967_296). to double the limit.

> u pgn_game_lengths.pl lichess_elite_2023-01.pgn incr=false stack_limit=16
author
- nicos angelopoulos
version
- 0.1 2026/01/24