From 6ba374c62baa43e56f8d56dbbf59d9952d502143 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Fri, 26 Nov 2021 17:11:35 +0100 Subject: [PATCH 1/2] ENHANCED: use '$skip_max_list'/4 for fast list tests in can_be/2 and must_be/2 This also (partially) addresses #1108. --- src/lib/error.pl | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib/error.pl b/src/lib/error.pl index d6bf8708..749ad04e 100644 --- a/src/lib/error.pl +++ b/src/lib/error.pl @@ -79,9 +79,12 @@ character(C) :- atom(C), atom_length(C, 1). -ilist(V) :- var(V), instantiation_error(must_be/2). -ilist([]). -ilist([_|Ls]) :- ilist(Ls). +ilist(Ls) :- + '$skip_max_list'(_, -1, Ls, Rs), + ( var(Rs) -> + instantiation_error(must_be/2) + ; Rs == [] + ). type(type). type(integer). @@ -120,10 +123,11 @@ can_(chars, Ls) :- '$is_partial_string'(Ls). can_(list, Term) :- list_or_partial_list(Term). can_(boolean, Term) :- boolean(Term). -list_or_partial_list(Var) :- var(Var). -list_or_partial_list([]). -list_or_partial_list([_|Ls]) :- - list_or_partial_list(Ls). +list_or_partial_list(Ls) :- + '$skip_max_list'(_, -1, Ls, Rs), + ( var(Rs) -> true + ; Rs == [] + ). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Shorthands for throwing ISO errors. From 803e120a5108048bbab10872b4f2933f113c361b Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Fri, 26 Nov 2021 17:11:51 +0100 Subject: [PATCH 2/2] use must_be(list, ...) earlier, since it is now faster --- src/lib/error.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/error.pl b/src/lib/error.pl index 749ad04e..d5827495 100644 --- a/src/lib/error.pl +++ b/src/lib/error.pl @@ -47,13 +47,13 @@ must_be_(integer, Term) :- check_(integer, integer, Term). must_be_(atom, Term) :- check_(atom, atom, Term). must_be_(character, T) :- check_(error:character, character, T). must_be_(chars, Ls) :- - ( ground(Ls), '$is_partial_string'(Ls) -> + must_be(list, Ls), + ( '$is_partial_string'(Ls) -> % The expected case (success) uses a very fast test. % We cannot use partial_string/1 from library(iso_ext), % because that library itself imports library(error). true - ; must_be(list, Ls), - all_characters(Ls) + ; all_characters(Ls) ). must_be_(list, Term) :- check_(error:ilist, list, Term). must_be_(type, Term) :- check_(error:type, type, Term).