use new predicates from library(error) to throw type and domain errors

This commit is contained in:
Markus Triska
2020-04-28 17:34:56 +02:00
parent 97115a9c1c
commit b139620fba
3 changed files with 13 additions and 13 deletions

View File

@@ -3,7 +3,7 @@
write_term_to_chars/3]). write_term_to_chars/3]).
:- use_module(library(iso_ext)). :- use_module(library(iso_ext)).
:- use_module(library(error)).
fabricate_var_name(VarType, VarName, N) :- fabricate_var_name(VarType, VarName, N) :-
char_code('A', AC), char_code('A', AC),
@@ -58,17 +58,17 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
char_type(Char, Type) :- char_type(Char, Type) :-
( var(Char) -> throw(error(instantiation_error, char_type/2)) ( var(Char) -> instantiation_error(char_type/2)
; atom_length(Char, 1) -> ; atom_length(Char, 1) ->
( ground(Type) -> ( ground(Type) ->
( ctype(Type) -> ( ctype(Type) ->
'$char_type'(Char, Type) '$char_type'(Char, Type)
; throw(error(domain_error(char_type, Type), char_type/2)) ; domain_error(char_type, Type, char_type/2)
) )
; ctype(Type), ; ctype(Type),
'$char_type'(Char, Type) '$char_type'(Char, Type)
) )
; throw(error(type_error(in_character, Char), char_type/2)) ; type_error(in_character, Char, char_type/2)
). ).
@@ -101,13 +101,13 @@ ctype(whitespace).
get_single_char(C) :- get_single_char(C) :-
( var(C) -> '$get_single_char'(C) ( var(C) -> '$get_single_char'(C)
; atom_length(C, 1) -> '$get_single_char'(C) ; atom_length(C, 1) -> '$get_single_char'(C)
; throw(error(type_error(in_character, C), get_single_char/1)) ; type_error(in_character, C, get_single_char/1)
). ).
read_term_from_chars(Chars, Term) :- read_term_from_chars(Chars, Term) :-
( var(Chars) -> ( var(Chars) ->
throw(error(instantiation_error, read_term_from_chars/2)) instantiation_error(read_term_from_chars/2)
; nonvar(Term) -> ; nonvar(Term) ->
throw(error(uninstantiation_error(Term), read_term_from_chars/2)) throw(error(uninstantiation_error(Term), read_term_from_chars/2))
; '$skip_max_list'(_, -1, Chars, Chars0), ; '$skip_max_list'(_, -1, Chars, Chars0),
@@ -115,23 +115,23 @@ read_term_from_chars(Chars, Term) :-
partial_string(Chars) -> partial_string(Chars) ->
true true
; ;
throw(error(type_error(complete_string, Chars), read_term_from_chars/2)) type_error(complete_string, Chars, read_term_from_chars/2)
), ),
'$read_term_from_chars'(Chars, Term). '$read_term_from_chars'(Chars, Term).
write_term_to_chars(_, Options, _) :- write_term_to_chars(_, Options, _) :-
var(Options), throw(error(instantiation_error, write_term_to_chars/3)). var(Options), instantiation_error(write_term_to_chars/3).
write_term_to_chars(Term, Options, Chars) :- write_term_to_chars(Term, Options, Chars) :-
'$skip_max_list'(_, -1, Options, Options0), '$skip_max_list'(_, -1, Options, Options0),
( var(Options0) -> ( var(Options0) ->
throw(error(instantiation_error, write_term_to_chars/3)) instantiation_error(write_term_to_chars/3)
; nonvar(Chars) -> ; nonvar(Chars) ->
throw(error(uninstantiation_error(Chars), write_term_to_chars/3)) throw(error(uninstantiation_error(Chars), write_term_to_chars/3))
; Options0 == [] -> ; Options0 == [] ->
true true
; ;
throw(error(type_error(list, Options), write_term_to_chars/3)) type_error(list, Options, write_term_to_chars/3)
), ),
builtins:inst_member_or(Options, ignore_ops(IgnoreOps), ignore_ops(false)), builtins:inst_member_or(Options, ignore_ops(IgnoreOps), ignore_ops(false)),
builtins:inst_member_or(Options, numbervars(NumberVars), numbervars(false)), builtins:inst_member_or(Options, numbervars(NumberVars), numbervars(false)),

View File

@@ -21,9 +21,9 @@ length(Xs, N) :-
; var(Xs0) -> R is N-M, length_rundown(Xs0, R)). ; var(Xs0) -> R is N-M, length_rundown(Xs0, R)).
length(_, N) :- length(_, N) :-
integer(N), !, integer(N), !,
throw(error(domain_error(not_less_than_zero, N), length/2)). domain_error(not_less_than_zero, N, length/2).
length(_, N) :- length(_, N) :-
throw(error(type_error(integer, N), length/2)). type_error(integer, N, length/2).
length_addendum([], N, N). length_addendum([], N, N).
length_addendum([_|Xs], N, M) :- length_addendum([_|Xs], N, M) :-

View File

@@ -22,7 +22,7 @@ random(R) :-
random_integer(Lower, Upper, R) :- random_integer(Lower, Upper, R) :-
var(R), var(R),
( (var(Lower) ; var(Upper)) -> ( (var(Lower) ; var(Upper)) ->
throw(error(instantiation_error, random_integer/3)) instantiation_error(random_integer/3)
; \+ integer(Lower) -> ; \+ integer(Lower) ->
domain_error(integer, Lower, random_integer/3) domain_error(integer, Lower, random_integer/3)
; \+ integer(Upper) -> ; \+ integer(Upper) ->