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.
:- 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).
//
error(syntax_error(Error), Location)
//
Location | is a term file(Name, Line, LinePos, CharNo)
or
stream(Stream, Line, LinePos, CharNo) if no file is
associated to the stream RestLazyList. Finally, if the Lazy list is
fully materialized (ends in [] ), Location is
unified with end_of_file-CharCount . |
//