Re: ANN: Implement your own prolog streams with predicate sources and sinks

17 views
Skip to first unread message

Douglas Miles

unread,
Feb 8, 2017, 10:03:43 AM2/8/17
to GoogleSWI-Prolog
Hi,


?- see('rtrace_tests.plt').
true.

?- current_input(X),stream_property(X,file_name(Y)).
X = <stream>(0x1026b60),
Y = 'rtrace_tests.plt'.

?- stream_property(X,alias(current_input)).
X = <stream>(0x1026b60).

?- current_input(X),stream_property(X,alias(Y)).
false.

Is current input alias set correctly here?

?- stream_property(X,alias(user_input)).
X = <stream>(0x7faa44e7fde0).



On Tue, Feb 7, 2017 at 9:16 PM, Douglas Miles <logi...@gmail.com> wrote:

Implement your own Abstract Predicate Streams

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

Installation using SWI-Prolog 7.1 or later:

?- pack_install(predicate_streams).

or

?- pack_install('https://github.com/TeamSPoon/predicate_streams.git').

Source code available and pull requests accepted at http://github.com/TeamSPoon/predicate_streams

Example usages

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

saved_output("hi there\n").
saved_output("how are you?\n").
?- with_input_from_predicate(=('hello.\n'), read(World)).
World = hello.
% Auto presses Y<Enter>
?- with_input_from_predicate({}/[X]>>X='Y\n', 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>
<pre>how are you?
</pre>
Reply all
Reply to author
Forward
0 new messages