From: Alexander
Subject: Type question
Date: 
Message-ID: <1140520146.663575.318450@g47g2000cwa.googlegroups.com>
Are there simple way to determine arbitrary expression's type?
For example
(defmacro mymacro (x &environment env)
  (assert (subtype (type-of-ex x env) 'number))
  'ok)

(mymacro 10) => ok
(mymacro (+ 1 2)) => ok
(mymacro "bla-bal") => error
(mymacro (subseq "bla-bla" :start 0 :end 2)) => error
(mymacro (cosh (+ 1 2 3 (* 10 20))) => ok
(let ((x 10))
  (declare (type number x))
    (mymacro x))) => ok

(let ((x "bla-bla"))
  (declare (type string x))
    (mymacro x))) => error

From: Frank Buss
Subject: Re: Type question
Date: 
Message-ID: <1a4usv4jhvmpi$.e5hhbzuygfk5.dlg@40tude.net>
Alexander wrote:

> Are there simple way to determine arbitrary expression's type?
> For example
> (defmacro mymacro (x &environment env)
>   (assert (subtype (type-of-ex x env) 'number))
>   'ok)

Do you want to detect it at runtime? Then use typep:

http://www.lisp.org/HyperSpec/Body/fun_typep.html

A macro doesn't evaluate the arguments. If you want to detect it at
compile-time, you have to use one of the type inference systems for Lisp.

But usually you don't need to detect the type, because there are better
ways with defgeneric etc. Where do you need it?

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Alexander
Subject: Re: Type question
Date: 
Message-ID: <1140530511.549457.78610@g47g2000cwa.googlegroups.com>
>If you want to detect it at
compile-time, you have to use one of the type inference systems for
Lisp.

Does such library exist?

>But usually you don't need to detect the type, because there are better
ways with defgeneric etc. Where do you need it?

Macro what do argument typecheck and optimizations depending on
argument's real types
From: Thomas A. Russ
Subject: Re: Type question
Date: 
Message-ID: <ymislqcd17z.fsf@sevak.isi.edu>
"Alexander" <········@gmail.com> writes:

> >But usually you don't need to detect the type, because there are better
> ways with defgeneric etc. Where do you need it?
> 
> Macro what do argument typecheck and optimizations depending on
> argument's real types

Well, part of the problem is that you often don't know until run time
what the types of the arguments will be.  That more-or-less forces you
to wait until runtime for doing some of the type checking.

A bit of a middle-ground, which will work for situations where you have
constant arguments is to use compiler macros.  This won't work with
expressions unless there has been some constant folding, and it won't
work with declared types.  There isn't any portable way of accessing the
type declaration information, it is compiler and implementation specific.

So, I would think you are probably better served by using an
implementation (like CMUCL or SBCL) that has good support for numeric
optimizations and also a fairly extensive type inference system.  That
way you can let the implementation do the work for you.


-- 
Thomas A. Russ,  USC/Information Sciences Institute