1/* Part of SWI-Prolog 2 3 WWW: http://www.swi-prolog.org 4 Copyright (c) 2021, SWI-Prolog Solutions b.v. 5 All rights reserved. 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions 9 are met: 10 11 1. Redistributions of source code must retain the above copyright 12 notice, this list of conditions and the following disclaimer. 13 14 2. Redistributions in binary form must reproduce the above copyright 15 notice, this list of conditions and the following disclaimer in 16 the documentation and/or other materials provided with the 17 distribution. 18 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 29 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 POSSIBILITY OF SUCH DAMAGE. 31*/ 32 33:- module(types, 34 [ must_be/4, % +Term, +Type, +Goal, +ArgNo 35 illarg/3, % +ErrorTerm, +Goal, +ArgNo 36 illarg/4 % +ErrorTerm, +Goal, +ArgNo, +Culprit 37 ]). 38:- use_module(library(error)). 39 40/** <module> SICStus 4 library(types). 41 42@see https://sicstus.sics.se/sicstus/docs/4.6.0/html/sicstus/lib_002dtypes.html 43*/ 44 45%! must_be(+Term, +Type, +Goal, +ArgNo) is det. 46% 47% Similar to must_be/2. This emulation currently only accepts 48% types that SWI-Prolog must_be/2 understands natively. 49% 50% The Goal and ArgNo arguments are currently ignored. 51 52must_be(Term, Type, _Goal, _ArgNo) :- must_be(Type, Term). 53 54%! illarg(+ErrorTerm, +Goal, +ArgNo) is det. 55% 56% Same as illarg/4, with Culprit set to argument number ArgNo 57% of Goal. The Goal and ArgNo arguments are otherwise not included 58% in the thrown error. 59 60illarg(ErrorTerm, Goal, ArgNo) :- 61 arg(ArgNo, Goal, Culprit), 62 illarg(ErrorTerm, Goal, ArgNo, Culprit). 63 64%! illarg(+ErrorTerm, +Goal, +ArgNo, +Culprit) is det. 65% 66% Throw a SICStus standard error described by ErrorTerm. 67% If possible, errors are thrown in the corresponding SWI-Prolog 68% format using library(error). If a SICStus error has no 69% SWI-Prolog counterpart, it is thrown in the same format that 70% SICStus would use. 71% 72% The Goal and ArgNo arguments are currently always ignored. 73% Depending on the requested error type, Culprit and/or parts of 74% ErrorTerm may also be ignored. 75 76illarg(var, _Goal, _ArgNo, Culprit) :- 77 instantiation_error(Culprit). 78illarg(type(ErrorType), Goal, ArgNo, Culprit) :- 79 % Equivalent according to SICStus docs. 80 must_be(Culprit, ErrorType, Goal, ArgNo). 81illarg(domain(ErrorType, ErrorDomain), Goal, ArgNo, Culprit) :- 82 must_be(Culprit, ErrorType, Goal, ArgNo), 83 domain_error(ErrorDomain, Culprit). 84illarg(force_type(ExpType), _Goal, _ArgNo, Culprit) :- 85 type_error(ExpType, Culprit). 86illarg(context(ContextType, CommandType), _Goal, _ArgNo, _Culprit) :- 87 throw(error(context_error(ContextType, CommandType), _)). 88illarg(existence(ObjType, Culprit, _Message), _Goal, _ArgNo, _CulpritOther) :- 89 existence_error(ObjType, Culprit). 90illarg(permission(Operation, ObjType, _Message), _Goal, _ArgNo, Culprit) :- 91 permission_error(Operation, ObjType, Culprit). 92illarg(representation(ErrorType), _Goal, _ArgNo, _Culprit) :- 93 representation_error(ErrorType). 94illarg(evaluation(ErrorType), _Goal, _ArgNo, _Culprit) :- 95 throw(error(evaluation_error(ErrorType), _)). 96illarg(consistency(Culprit1, Culprit2, Message), _Goal, _ArgNo, _CulpritOther) :- 97 throw(error(consistency_error(Culprit1, Culprit2, Message), _)). 98illarg(syntax(_Pos, Msg, _Tokens, _AfterError), _Goal, _ArgNo, _Culprit) :- 99 syntax_error(Msg). 100illarg(resource(Resource), _Goal, _ArgNo, _Culprit) :- 101 resource_error(Resource). 102illarg(system(_Message), _Goal, _ArgNo, _Culprit) :- 103 throw(error(system_error, _))