1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/m_assert.htm
2024-04-01 10:24:07 +02:00

99 lines
8.3 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Macro ASSERT</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="e_storag.htm">
<LINK REL=UP HREF="c_condit.htm">
<LINK REL=NEXT HREF="f_error.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="e_storag.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_condit.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_error.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="assert"><I>Macro</I> <B>ASSERT</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>assert</B> <I>test-form [(<I>place</I><B>*</B>) [datum-form <I>argument-form</I><B>*</B>]]</I><P> =&gt; <I><A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A></I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>test-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; always evaluated. <P>
<I>place</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A>; evaluated if an error is signaled. <P>
<I>datum-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> that evaluates to a <I>datum</I>. Evaluated each time an error is to be signaled, or not at all if no error is to be signaled. <P>
<I>argument-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> that evaluates to an <I>argument</I>. Evaluated each time an error is to be signaled, or not at all if no error is to be signaled. <P>
<I>datum</I>, <I>arguments</I>---<A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designators</I></A> for a <A REL=DEFINITION HREF="26_glo_c.htm#condition"><I>condition</I></A> of default type <A REL=DEFINITION HREF="e_error.htm#error"><B>error</B></A>. (These <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designators</I></A> are the result of evaluating <I>datum-form</I> and each of the <I>argument-forms</I>.) <P>
<P><B>Description:</B><P>
<P>
<A REL=DEFINITION HREF="#assert"><B>assert</B></A> assures that <I>test-form</I> evaluates to <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>. If <I>test-form</I> evaluates to <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>, <A REL=DEFINITION HREF="#assert"><B>assert</B></A> signals a <A REL=DEFINITION HREF="26_glo_c.htm#correctable"><I>correctable</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#error"><I>error</I></A> (denoted by <I>datum</I> and <I>arguments</I>). Continuing from this error using the <A REL=DEFINITION HREF="a_contin.htm#continue"><B>continue</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> makes it possible for the user to alter the values of the <I>places</I> before <A REL=DEFINITION HREF="#assert"><B>assert</B></A> evaluates <I>test-form</I> again. If the value of <I>test-form</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, <A REL=DEFINITION HREF="#assert"><B>assert</B></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
The <I>places</I> are <A REL=DEFINITION HREF="26_glo_g.htm#generalized_reference"><I>generalized references</I></A> to data upon which <I>test-form</I> depends, whose values can be changed by the user in attempting to correct the error. <A REL=DEFINITION HREF="26_glo_s.htm#subform"><I>Subforms</I></A> of each <I>place</I> are only evaluated if an error is signaled, and might be re-evaluated if the error is re-signaled (after continuing without actually fixing the problem). The order of evaluation of the <I>places</I> is not specified; see <A REL=CHILD HREF="05_aaa.htm">Section 5.1.1.1 (Evaluation of Subforms to Places)</A>. If a <I>place</I> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> is supplied that produces more values than there are store variables, the extra values are ignored. If the supplied <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> produces fewer values than there are store variables, the missing values are set to <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
<P><B>Examples:</B><P>
<PRE>
(setq x (make-array '(3 5) :initial-element 3))
=&gt; #2A((3 3 3 3 3) (3 3 3 3 3) (3 3 3 3 3))
(setq y (make-array '(3 5) :initial-element 7))
=&gt; #2A((7 7 7 7 7) (7 7 7 7 7) (7 7 7 7 7))
(defun matrix-multiply (a b)
(let ((*print-array* nil))
(assert (and (= (array-rank a) (array-rank b) 2)
(= (array-dimension a 1) (array-dimension b 0)))
(a b)
&quot;Cannot multiply ~S by ~S.&quot; a b)
(really-matrix-multiply a b))) =&gt; MATRIX-MULTIPLY
(matrix-multiply x y)
&gt;&gt; Correctable error in MATRIX-MULTIPLY:
&gt;&gt; Cannot multiply #&lt;ARRAY ...&gt; by #&lt;ARRAY ...&gt;.
&gt;&gt; Restart options:
&gt;&gt; 1: You will be prompted for one or more new values.
&gt;&gt; 2: Top level.
&gt;&gt; Debug&gt; :continue 1
&gt;&gt; Value for A: x
&gt;&gt; Value for B: (make-array '(5 3) :initial-element 6)
=&gt; #2A((54 54 54 54 54)
(54 54 54 54 54)
(54 54 54 54 54)
(54 54 54 54 54)
(54 54 54 54 54))
</PRE>
</TT> <P>
<PRE>
(defun double-safely (x) (assert (numberp x) (x)) (+ x x))
(double-safely 4)
=&gt; 8
(double-safely t)
&gt;&gt; Correctable error in DOUBLE-SAFELY: The value of (NUMBERP X) must be non-NIL.
&gt;&gt; Restart options:
&gt;&gt; 1: You will be prompted for one or more new values.
&gt;&gt; 2: Top level.
&gt;&gt; Debug&gt; :continue 1
&gt;&gt; Value for X: 7
=&gt; 14
</PRE>
</TT> <P>
<P><B>Affected By:</B><P>
<P>
<A REL=DEFINITION HREF="v_break_.htm#STbreak-on-signalsST"><B>*break-on-signals*</B></A> <P>
The set of active <A REL=DEFINITION HREF="26_glo_c.htm#condition_handler"><I>condition handlers</I></A>. <P>
<P><B>Exceptional Situations:</B> None.
<P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="m_check_.htm#check-type"><B>check-type</B></A>, <A REL=DEFINITION HREF="f_error.htm#error"><B>error</B></A>, <A REL=CHILD HREF="05_a.htm">Section 5.1 (Generalized Reference)</A> <P>
<P><B>Notes:</B><P>
<P>
The debugger need not include the <I>test-form</I> in the error message, and the <I>places</I> should not be included in the message, but they should be made available for the user's perusal. If the user gives the ``continue'' command, the values of any of the references can be altered. The details of this depend on the implementation's style of user interface. <P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss309.htm">SETF-MULTIPLE-STORE-VARIABLES:ALLOW</A><LI> <A REL=CHILD HREF="../Issues/iss279.htm">PUSH-EVALUATION-ORDER:FIRST-ITEM</A><LI> <A REL=CHILD HREF="../Issues/iss016.htm">ASSERT-ERROR-TYPE:ERROR</A><LI> <A REL=CHILD HREF="../Issues/iss170.htm">FORMAT-STRING-ARGUMENTS:SPECIFY</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>