Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct can_be/2 for partial lists #57

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/prolog/lib/error.pl
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
Expand Down Expand Up @@ -47,16 +48,13 @@
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.

Expand All @@ -66,9 +64,18 @@

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.
Expand Down