Implement your own Abstract Predicate Streams

18+ Years ago I remember these predicates existed as the building blocks for writing your own Sockets in some Prolog I cannot remember.

Installation using SWI-Prolog 7.1 or later:

?- pack_install('').

Source code available and pull requests accepted at

Example usages

?- with_output_to_predicate({}/[X]>>assert(saved_output(X)),
     (write("hi there"),nl,writeln("how are you?"))),

saved_output("hi there\n").
saved_output("how are you?\n").
?- with_input_from_predicate(=('hello.\n'), read(World)).
World = hello.


% this works
?- call(({}/[X]>>(repeat,X='Y')),Y).
Y = 'Y' ;
Y = 'Y' ;
Y = 'Y' ;
Y = 'Y' ;
Y = 'Y' ;
Y = 'Y' .

% but not this ?
?- with_input_from_predicate(({}/[X]>>(repeat,X='YN')),(get_char(C0),get_char(C1),get_char(C2),get_char(C3))).
C0 = 'Y',
C1 = 'N',
C2 = C3, C3 = end_of_file.

C0 = C2, C1 = C3,
C2 = 'Y',
C3 = 'N'.

% Auto presses Y Multiple times
?- with_input_from_predicate({}/[X]>>X='YYYYYYYYYYYYYYYYYYYYYYYY', poor_interactive_goal).

?- with_error_to_predicate(write,threads).
... writes thread info to stdout instead of stderr...
?- with_output_to_predicate(print_as_html_pre,
    (writeln("hi there"),writeln("how are you?"))).

<pre>hi there
<pre>how are you?

Some TODOs

Document this pack! Write tests

BSD 2-Clause License

Copyright (c) 2017, Douglas Miles <> and TeamSPoon All rights reserved.

Not obligated to maintain a git fork just to contribute

Dislike having tons of forks that are several commits behind the main git repo?

Be old school - Please ask to be added to TeamSPoon and Contribute directly ! Still, we wont stop you from doing it the Fork+PullRequest method

Prolog files

pack.plShow source
prolog/INDEX.plShow source
prolog/broadcast_streams.plShow source
prolog/concatenated_input_streams.plShow source
prolog/  -- predicate_streams - Abstract Predicate StreamsShow source
t/sanity_tests.plShow source
t/test123.plShow source