How can I obtain type errors 
Author Message
 How can I obtain type errors

Hello.

I'm a beginner in Lisp.  I'm using Allegro Common Lisp and I want to
define a function with arguments of a specific type.  For example, the
typical function:

(defun df* (a b)
     (declare (double-float a b))
     (* a b))

should fail with (df* 2 3), but it works!  How can I get a type error?
Does somebody know it?

Thank you in advance.



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:
> Hello.

> I'm a beginner in Lisp.  I'm using Allegro Common Lisp and I want to
> define a function with arguments of a specific type.  For example, the
> typical function:

> (defun df* (a b)
>      (declare (double-float a b))
>      (* a b))

> should fail with (df* 2 3), but it works!  How can I get a type error?
> Does somebody know it?

> Thank you in advance.

In CMUCL

==============================================================================
* (defun df* (a b)
     (declare (double-float a b))
     (* a b))
DF*
* (compile 'df*)
DF*
NIL
NIL
* (df* 2 3)
Invoking de{*filter*}...

        ... 2 is not of type DOUBLE-FLOAT
==============================================================================

You can tune this behavior.

OTOH

==============================================================================
* (defun df* (a b)
     (declare (double-float a b))
     (* a b))
DF*
* (df* 2 3)
6
==============================================================================

Which is what you'd expect, given that the "compilation" process done
at "interpreter" level is not the full one.

If you want something along the lines you seem expect, you should use
ML (alas with many more constraints on types)

==============================================================================

Standard ML of New Jersey, Version 0.93, February 15, 1993
val it = () : unit
- fun dfstar a b : real = a * b;
val dfstar = fn : real -> real -> real
==============================================================================

However

==============================================================================
- dfstar 2 3.0;
std_in:3.1-3.12 Error: operator and operand don't agree (tycon mismatch)
  operator domain: real
  operand:         int
  in expression:
    dfstar 2
-
==============================================================================

while

==============================================================================
* (defun df-g-* (a b)
     (declare (double-float b))
     (* a b))
DF-G-*
* (compile 'df-g-*)
DF-G-*
NIL
NIL
* (df-g-* 2 3.0d0)
6.0d0
==============================================================================

Of course I am lagging way behinf ML development and therefore things
may have changed.

Going back to CMUCL first version of df*

==============================================================================
* (defun df* (a b)
     (declare (type double-float a b))
     (* a b))
DF*
* (defun use-df* (x)
    (df* x 4))
USE-DF*
* (compile 'df*)
DF*
NIL
NIL
* (compile 'use-df*)
In: LAMBDA (X)
  (DF* X 4)
Warning: This is not a DOUBLE-FLOAT:
  4

Compilation unit finished.
  1 warning

USE-DF*
T
T
*
==============================================================================

Cheers

--
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.*-*-*.com/ ~marcoxa



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors
Marco Antoniotti escribi:

Quote:
> In CMUCL

> ==============================================================================
> * (defun df* (a b)
>      (declare (double-float a b))
>      (* a b))
> DF*
> * (compile 'df*)
> DF*
> NIL
> NIL
> * (df* 2 3)
> Invoking de{*filter*}...

>         ... 2 is not of type DOUBLE-FLOAT
> ==============================================================================

> You can tune this behavior.

Thanks to Marco Antoniotti, but it doesn't work yet.

How can I tune the behavior of Allegro Common Lisp to obtain the error "... 2 is
not of type DOUBLE-FLOAT".

Thank you



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors

    Antonio> Marco Antoniotti escribi:
    >> In CMUCL
    >>
    >> ==============================================================================
    >> * (defun df* (a b)
    >> (declare (double-float a b)) (* a b)) DF*
    >> * (compile 'df*)
    >> DF* NIL NIL
    >> * (df* 2 3)
    >> Invoking de{*filter*}...
    >>
    >> ... 2 is not of type DOUBLE-FLOAT
    >> ==============================================================================
    >>
    >> You can tune this behavior.

    Antonio> Thanks to Marco Antoniotti, but it doesn't work yet.

    Antonio> How can I tune the behavior of Allegro Common Lisp to
    Antonio> obtain the error "... 2 is not of type DOUBLE-FLOAT".

Can't you do something like:

(defun df* (a b)
  (declare (double-float a b))
  (check-type a double-float)
  (check-type b double-float)
  (* a b))

Maybe an ASSERT instead of CHECK-TYPE would work for you too:

(assert (and (typep a 'double-float) (typep b 'double-float)))

Ray



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:
>Can't you do something like:

>(defun df* (a b)
>  (declare (double-float a b))
>  (check-type a double-float)
>  (check-type b double-float)
>  (* a b))

>Maybe an ASSERT instead of CHECK-TYPE would work for you too:

>(assert (and (typep a 'double-float) (typep b 'double-float)))

The proper way to do this is:

(defun df* (a b)
  (check-type a double-float)
  (check-type b double-float)
  (locally
    (declare (double-float a b))
    (* a b)))

Remember, in Common Lisp, type declarations are a promise by the programmer
that the variables will be of the specified type.  They are *not* a request
that type checks be inserted.  The intent is to speed up code by allowing
the compiler to make assumptions rather than performing runtime checks.

In some implementations, if you declare high SAFETY optimization, type
checks will be inserted.  It might also be necessary to declare low SPEED.
But this is not required by the language spec and not portable.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:
> Marco Antoniotti escribi:

> > In CMUCL

> > ===========================================================================
> > * (defun df* (a b)
> >      (declare (double-float a b))
> >      (* a b))
> > DF*
> > * (compile 'df*)
> > DF*
> > NIL
> > NIL
> > * (df* 2 3)
> > Invoking de{*filter*}...

> >         ... 2 is not of type DOUBLE-FLOAT
> > ===========================================================================

> > You can tune this behavior.

> Thanks to Marco Antoniotti, but it doesn't work yet.

> How can I tune the behavior of Allegro Common Lisp to obtain the error "... 2 is
> not of type DOUBLE-FLOAT".

You are welcome. But.  I am using CMUCL, which has a very good
compiler when it comes to type checking. I do not know whether ACL has
the same behavior.

CL is not required to signal an error in that case, and the compiler
may use the information you give to generate good code.  Note that
what I reported is a runtime error, not a compile time one.

In the second example I gave, I get a compiler *warning* about the
"wrong" type.

Note however that when programming with CL, typing is something you
usually worry about at a rather late stage.

So the real question to you is: why do you need to be so precise about
types?  C/C++ heritage, or MLish attitude? :)

Cheers

--
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.*-*-*.com/ ~marcoxa



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors
I'm somewhat surprised no one yet has suggested the THE macro.

CL-USER 5 > (the double-float 2)
Error: The value 2 does not satisfy the type specifier DOUBLE-FLOAT.

CL-USER 13 > (the double-float 2.0)
Error: The value 2.0 does not satisfy the type specifier DOUBLE-FLOAT.

CL-USER 15 > (the double-float 2.0d0)
2.0D0

AFAIK, it will cause your code to slow down because of extra type checking,
and may turn out to be harder to eliminate than type declarations that
others have suggested. OTOH, the behavior is specified by the ANSI spec
( http://www.*-*-*.com/ )
so at least you know what behavior to expect across implementations.

Sunil


Quote:
> Marco Antoniotti escribi:

> > In CMUCL

> > ==============================================================================
> > * (defun df* (a b)
> >      (declare (double-float a b))
> >      (* a b))
> > DF*
> > * (compile 'df*)
> > DF*
> > NIL
> > NIL
> > * (df* 2 3)
> > Invoking de{*filter*}...

> >         ... 2 is not of type DOUBLE-FLOAT
> > ==============================================================================

> > You can tune this behavior.

> Thanks to Marco Antoniotti, but it doesn't work yet.

> How can I tune the behavior of Allegro Common Lisp to obtain the error "... 2 is
> not of type DOUBLE-FLOAT".

> Thank you



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors

| I'm using Allegro Common Lisp and I want to define a function with
| arguments of a specific type.

  why?

| For example, the typical function:
|
| (defun df* (a b)
|      (declare (double-float a b))
|      (* a b))
|
| should fail with (df* 2 3), but it works!

  great, isn't it?

| How can I get a type error?

  you can do

(defun fd* (a b)
  (check-type a double-float)
  (check-type a double-float)
  (* a b))

  if you think you are going to win a speed contest with your type
  "checking", you aren't.  is that why you want this stuff?

#:Erik



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:
>I'm somewhat surprised no one yet has suggested the THE macro.

Because it doesn't solve the problem.  THE is a way of adding a type
declaration to an expression, just as DECLARE specifies a type declaration
of a variable, but it doesn't add type checking.  Some implementations
perform type checking when high safety is enabled, but it's not required by
the language.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors

Quote:

> Because it doesn't solve the problem.  THE is a way of adding a type
> declaration to an expression, just as DECLARE specifies a type declaration
> of a variable, but it doesn't add type checking.  Some implementations
> perform type checking when high safety is enabled, but it's not required by
> the language.

Heh, sorry, misinterpreted the definition the first time I read it.

Sunil



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors

Quote:

> I'm a beginner in Lisp.  I'm using Allegro Common Lisp and I want to
> define a function with arguments of a specific type.  For example, the
> typical function:

> (defun df* (a b)
>      (declare (double-float a b))
>      (* a b))

> should fail with (df* 2 3), but it works!  How can I get a type error?
> Does somebody know it?

Lisp is not required to check the types, nor is the compiler required to
use the type information to improve code efficiency.

You can, however, force type checking by using the the CHECK-TYPE call
explictly:

(defun df* (a b)
    (declare (double-float a b))
    (check-type a double-float)
    (check-type b double-float)
    (* a b))

TEST:TEST-THEORY> (df* 2 3)
Error: the value of A is 2, which is not of type DOUBLE-FLOAT.
  [condition type: TYPE-ERROR]

Restart actions (select using :continue):
 0: supply a new value for A.
 1: Return to Top Level (an "abort" restart)
[1]TEST:TEST-THEORY> :reset
TEST:TEST-THEORY> (df* 2.0 3)
Error: the value of A is 2.0, which is not of type DOUBLE-FLOAT.
  [condition type: TYPE-ERROR]

Restart actions (select using :continue):
 0: supply a new value for A.
 1: Return to Top Level (an "abort" restart)
[1]TEST:TEST-THEORY> :reset
TEST:TEST-THEORY> (df* 2.0d0 3)
Error: the value of B is 3, which is not of type DOUBLE-FLOAT.
  [condition type: TYPE-ERROR]

Restart actions (select using :continue):
 0: supply a new value for B.
 1: Return to Top Level (an "abort" restart)
[1]TEST:TEST-THEORY> (df* 2.0d0 3.0d0)
6.0d0

--



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:
>You can, however, force type checking by using the the CHECK-TYPE call
>explictly:

>(defun df* (a b)
>    (declare (double-float a b))
>    (check-type a double-float)
>    (check-type b double-float)
>    (* a b))

This is not correct.  The declaration tells the compiler that the
CHECK-TYPEs are guaranteed to succeed, so it can optimize them away!  I
don't have CMUCL so I can't test it, but I suspect that its type
propagation code may actually be able to do this.

You need to put the type checks outside the scope of the declaration.  As I
wrote in an earlier post, the way to code this is:

(defun df* (a b)
  (check-type a double-float)
  (check-type b double-float)
  (locally (declare (double-float a b))
    (* a b)))

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 18 Nov 2001 03:00:00 GMT  
 How can I obtain type errors


Quote:

> Incidentally, no one has said it, but check-type only really guarantees
> run-time checking.  Earlier checking might be done, but there is no
> requirement of it.  So in a trival sense, the question of how you can
> force an error in the original case, if you assume the error is to be
> forced at compile time, is: you can't.  Lisp doesn't do strong typing.
> It doesn't pretend to try.

This leads to something I've been curious about for a while: has anyone ever
built a language using lisp parenthesized syntax but with statically-typed
semantics similar to ML?  I'm not trying to get into a debate over whether
statically-typed languages are "better" than dynamically-typed ones (IMHO it
depends on a number of factors, including what stage of program development
you're in and how often the resulting code is likely to need to change).  But
I find it curious that all the ML family of languages (as well as lazy
languages like Haskell and Miranda) have abandoned the lisp syntax, despite
the numerous advantages which have been described in a different thread.  I
can think of no good reason why this has to be the case.  Any thoughts on
this?  I think it would be cool if there was a statically-typed dialect of
lisp, since I prefer having my type errors caught at compile-time.  OTOH you
do lose some flexibility that way, so the ultimate solution would be a system
where you could select either typing system, perhaps on a file-by-file basis.

Mike

-------------------------------------------------------------------------

Department of Computation and Neural Systems, Caltech 216-76
Will optimize nonlinear functions with complex parameter spaces for food.



Sun, 18 Nov 2001 03:00:00 GMT  
 
 [ 43 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. I obtain error -10003 from AI hardware config when I use AC coupling

2. Obtained number of the line when an error occurs

3. x/0 as a type error [Extremely static typing]

4. Intel error: type of actual argument differs from type of dummy argument

5. APP Open error 11: Parameter typing error

6. Unknown Error Posted:6:Parameter Typing Error

7. Unknown Error Posted: 10: Parameter typing error

8. Unknown error: 6: Parameter typing error?

9. CW2.0 error 10: Parameter typing error

10. CA Cans VO ?

11. It's not bad canned meat...

12. It's not bad canned meat...

 

 
Powered by phpBB® Forum Software