Using lazy_lists module to give out hash of a Swipl installer in Windows

The lazy_lists are mysterious to me … the page should have few simple examples in Lazy list handling.

Well I was motivated today to learn about them and got a grip this time …

The Swi-Prolog download page download site does show this SHA256.
I was really impressed how fast this is! I first tried to load the executable with read_file_to_codes/3 and get the sha256 with that, it took like 15 seconds (in my Windows 10 laptop)

Then checked how fast the linux sha256sum command is, it is lighting fast.
This is also lightning fast compared to 15 seconds … lazy_list_materialize … sounds like some Harry Potter command :slight_smile: … it has zero explanations at documentaion.

tst5(M):-
    open('../../downloads/swipl-9.1.10-1.x64.exe',read,Stream,[type(binary)]),
    stream_to_lazy_list(Stream,LList),
    lazy_list_materialize(LList),
    sha_hash(LList, Hash, [algorithm(sha256),encoding(octet)]),
    hash_atom(Hash,M),
    close(Stream).

The download page uses this to compute the hashes. Note that it computes them only once such that they stay the same, even if someone manages to hack the server and modify the download file.

compute_file_checksum(Path, Sum) :-
	crypto_file_hash(Path, Sum,
			 [ encoding(octet),
			   algorithm(sha256)
			 ]),
	assert_sha256(Path, Sum).

Using a Prolog list as intermediate is rather slow …

Anyway, lazy lists are basically lists that end in an attributed variable rather than []. If you unify this variable to something it is replaced by the next chunk of the list that (again) ends in an attributed variable. If the lazy list is finite, the last chunk ends the normal list way ([]).

They are mostly there to process huge amounts of data in finite memory. This implies they are mostly interesting for running algorithms that consider the list as an (possibly infinite) stream.

The thing you typically do not want to do with a lazy list is to materialize it :slight_smile:

The library is a generalization of library(pure_input).