From 373546e83d07ba89a6a8a268c85cac0f9f42e547 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Fri, 28 Sep 2018 18:16:10 +0200 Subject: [PATCH] correct can_be/2 for partial lists Example: ?- can_be(list, [a,b|Ls]). %@ true. --- src/prolog/lib/error.pl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/prolog/lib/error.pl b/src/prolog/lib/error.pl index f58a3d90..21955f50 100644 --- a/src/prolog/lib/error.pl +++ b/src/prolog/lib/error.pl @@ -1,4 +1,5 @@ -:- module(error, [must_be/2, can_be/2]). +:- module(error, [must_be/2, + can_be/2]). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Written September 2018 by Markus Triska (triska@metalevel.at) @@ -47,16 +48,13 @@ ilist([]). ilist([_|Ls]) :- ilist(Ls). - /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - can_be(Type, Term) This predicate is intended for type-checks of built-in predicates. - It asserts that: - - 1) Term is either a variable or instantiated *and* - 2) _if_ it is instantiated, then it is an instance of Type. + It asserts that there is a substitution which, if applied to Term, + makes it an instance of Type. It corresponds to usage mode ?Term. @@ -66,9 +64,18 @@ ilist([_|Ls]) :- ilist(Ls). can_be(Type, Term) :- ( var(Term) -> true - ; must_be(Type, Term) + ; can_(Type, Term) -> true + ; type_error(Type, Term) ). +can_(integer, Term) :- integer(Term). +can_(atom, Term) :- atom(Term). +can_(list, Term) :- list_or_partial_list(Term). + +list_or_partial_list(Var) :- var(Var). +list_or_partial_list([]). +list_or_partial_list([_|Ls]) :- + list_or_partial_list(Ls). /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Shorthands for throwing ISO errors. -- 2.54.0