From b0a5df2838242ca788c835ccf291195cdc7fcd36 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 25 Apr 2020 19:59:38 +0200 Subject: [PATCH 1/4] use an anonymous variable --- src/prolog/lib/charsio.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prolog/lib/charsio.pl b/src/prolog/lib/charsio.pl index e09c2185..df0e050b 100644 --- a/src/prolog/lib/charsio.pl +++ b/src/prolog/lib/charsio.pl @@ -47,7 +47,7 @@ extend_var_list(Value, VarList, NewVarList, VarType) :- term_variables(Value, Vars), extend_var_list_(Vars, 0, VarList, NewVarList, VarType). -extend_var_list_([], N, VarList, VarList, _). +extend_var_list_([], _, VarList, VarList, _). extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :- ( var_list_contains_variable(VarList, V) -> extend_var_list_(Vs, N, VarList, NewVarList, VarType) From 40619c8184b9886fd709d0a046de21a0ac609f43 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 25 Apr 2020 19:59:12 +0200 Subject: [PATCH 2/4] ENHANCED: Throw domain errors in char_type/2 for wrong types This is especially important because a few of our names diverge from ctype(3), and we better inform programmers when the type they are using is not available at all. --- src/prolog/lib/charsio.pl | 58 ++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/prolog/lib/charsio.pl b/src/prolog/lib/charsio.pl index df0e050b..ead86e23 100644 --- a/src/prolog/lib/charsio.pl +++ b/src/prolog/lib/charsio.pl @@ -60,36 +60,44 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :- char_type(Char, Type) :- ( var(Char) -> throw(error(instantiation_error, char_type/2)) ; atom_length(Char, 1) -> - ( ground(Type) -> '$char_type'(Char, Type) - ; Type = symbolic_control, '$char_type'(Char, Type) - ; Type = layout, '$char_type'(Char, Type) - ; Type = symbolic_hexadecimal, Char = x - ; Type = octal_digit, '$char_type'(Char, Type) - ; Type = binary_digit, '$char_type'(Char, Type) - ; Type = hexadecimal_digit, '$char_type'(Char, Type) - ; Type = exponent, '$char_type'(Char, Type) - ; Type = sign, '$char_type'(Char, Type) - ; Type = upper, '$char_type'(Char, Type) - ; Type = lower, '$char_type'(Char, Type) - ; Type = graphic, '$char_type'(Char, Type) - ; Type = alpha, '$char_type'(Char, Type) - ; Type = decimal_digit, '$char_type'(Char, Type) - ; Type = alnum, '$char_type'(Char, Type) - ; Type = meta, '$char_type'(Char, Type) - ; Type = solo, '$char_type'(Char, Type) - ; Type = prolog, '$char_type'(Char, Type) - ; Type = alphabetic, '$char_type'(Char, Type) - ; Type = whitespace, '$char_type'(Char, Type) - ; Type = control, '$char_type'(Char, Type) - ; Type = numeric, '$char_type'(Char, Type) - ; Type = ascii, '$char_type'(Char, Type) - ; Type = ascii_punctuation, '$char_type'(Char, Type) - ; Type = ascii_graphic, '$char_type'(Char, Type) + ( ground(Type) -> + ( ctype(Type) -> + '$char_type'(Char, Type) + ; throw(error(domain_error(char_type, Type), char_type/2)) + ) + ; ctype(Type), + '$char_type'(Char, Type) ) ; throw(error(type_error(in_character, Char), char_type/2)) ). +ctype(alnum). +ctype(alpha). +ctype(alphabetic). +ctype(ascii). +ctype(ascii_graphic). +ctype(ascii_punctuation). +ctype(binary_digit). +ctype(control). +ctype(decimal_digit). +ctype(exponent). +ctype(graphic). +ctype(hexadecimal_digit). +ctype(layout). +ctype(lower). +ctype(meta). +ctype(numeric). +ctype(octal_digit). +ctype(prolog). +ctype(sign). +ctype(solo). +ctype(symbolic_control). +ctype(symbolic_hexadecimal). +ctype(upper). +ctype(whitespace). + + get_single_char(C) :- ( var(C) -> '$get_single_char'(C) ; atom_length(C, 1) -> '$get_single_char'(C) From f27597cab52acd1b09290cb139c1be9989258731 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 25 Apr 2020 20:01:04 +0200 Subject: [PATCH 3/4] update toplevel interaction --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 64768c23..52b29734 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,6 @@ REPL: ``` ?- [user]. -(type Enter + Ctrl-D to terminate the stream when finished) :- module(test, [local_member/2]). :- use_module(library(lists)). From b604177ca1e49c134071c7c53d23ff0ebf62edb4 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sat, 25 Apr 2020 20:08:56 +0200 Subject: [PATCH 4/4] include library(pio) and library(charsio) in the description --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 52b29734..337b5407 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,12 @@ The modules that ship with Scryer Prolog are also called `time/1` reports the CPU time of a goal. It is useful for measuring the performance of your code. +To read contents of external files, use `phrase_from_file/2` from +[`library(pio)`](src/prolog/lib/pio.pl) to apply a DCG to +file contents. The predicates in +[`library(charsio)`](src/prolog/lib/charsio.pl) are also useful for +parsing. + To use predicates provided by the `lists` library, write: ```