| Did you know ... | Search Documentation: |
| pure_input.pl -- Pure Input from files and streams |
This module is part of pio.pl, dealing with pure input: processing input streams from the outside world using pure predicates, notably grammar rules (DCG). Using pure predicates makes non-deterministic processing of input much simpler.
Pure input uses attributed variables to read input from the external source into a list on demand. The overhead of lazy reading is more than compensated for by using block reads based on read_pending_codes/3.
Ulrich Neumerkel came up with the idea to use coroutining for creating a lazy list. His implementation repositioned the file to deal with re-reading that can be necessary on backtracking. The current implementation uses destructive assignment together with more low-level attribute handling to realise pure input on any (buffered) stream.
phrase_from_file(:Grammar, +File) is nondet
phrase_from_file(:Grammar, +File, +Options) is nondet
Options are passed to open/4 and phrase_from_stream/3. The latter
notable allows for as(chars) to call a DCG compiled for dealing with
character lists rather than codes.
Below is an example that counts the number of times a string appears in a file. The library dcg/basics provides string//1 matching an arbitrary string and remainder//1 which matches the remainder of the input without parsing.
:- use_module(library(dcg/basics)).
file_contains(File, Pattern) :-
phrase_from_file(match(Pattern), File).
match(Pattern) -->
string(_),
string(Pattern),
remainder(_).
match_count(File, Pattern, Count) :-
aggregate_all(count, file_contains(File, Pattern), Count).
This can be called as (note that the pattern must be a string (code list)):
?- match_count('pure_input.pl', `file`, Count).
phrase_from_stream(:Grammar, +Stream)
phrase_from_stream(:Grammar, +Stream, +Options)codes (default) or chars. See also
stream_to_lazy_codes/2 and stream_to_lazy_chars/2.
syntax_error(+Error)//
lazy_list_location(-Location)// is det
lazy_list_character_count(-CharCount)//
stream_to_lazy_codes(+Stream, -List) is det
stream_to_lazy_chars(+Stream, -List) is det
stream_to_lazy_list(+Stream, -List) is detThe following predicates are exported from this file while their implementation is defined in imported modules or non-module files loaded by this module.
phrase_from_file(:Grammar, +File) is nondet
phrase_from_file(:Grammar, +File, +Options) is nondet
Options are passed to open/4 and phrase_from_stream/3. The latter
notable allows for as(chars) to call a DCG compiled for dealing with
character lists rather than codes.
Below is an example that counts the number of times a string appears in a file. The library dcg/basics provides string//1 matching an arbitrary string and remainder//1 which matches the remainder of the input without parsing.
:- use_module(library(dcg/basics)).
file_contains(File, Pattern) :-
phrase_from_file(match(Pattern), File).
match(Pattern) -->
string(_),
string(Pattern),
remainder(_).
match_count(File, Pattern, Count) :-
aggregate_all(count, file_contains(File, Pattern), Count).
This can be called as (note that the pattern must be a string (code list)):
?- match_count('pure_input.pl', `file`, Count).
phrase_from_stream(:Grammar, +Stream)
phrase_from_stream(:Grammar, +Stream, +Options)codes (default) or chars. See also
stream_to_lazy_codes/2 and stream_to_lazy_chars/2.
stream_to_lazy_codes(+Stream, -List) is det
stream_to_lazy_chars(+Stream, -List) is det
stream_to_lazy_list(+Stream, -List) is det
stream_to_lazy_codes(+Stream, -List) is det
stream_to_lazy_chars(+Stream, -List) is det
stream_to_lazy_list(+Stream, -List) is det