From: Ming Gong
Subject: A question: Dynamic type check
Date: 
Message-ID: <10026@uqcspe.cs.uq.oz.au>
I want to know *at run time* whether or not a expression is a function. 
Can some knowledgeable people tell me how?

For example, I have a lisp function PLUS implementing 
     lambda x. lambda y. x + y
then (funcall (PLUS) 3) is a function and will printout something like
     #<Function PLUS ....>.
The questions are: can I access such information within a lisp program
and how?

Please email answers to ····@cs.uq.oz.au.

Thank you in advance.

Ming Gong

From: Len Charest
Subject: Re: A question: Dynamic type check
Date: 
Message-ID: <1992Aug28.192233.20104@jpl-devvax.jpl.nasa.gov>
In article <·····@uqcspe.cs.uq.oz.au>, ····@cs.uq.oz.au (Ming Gong) writes:
|> I want to know *at run time* whether or not a expression is a function. 
|> Can some knowledgeable people tell me how?

If the expression is bound to a variable X then
	(functionp x)
should do the job.

|> For example, I have a lisp function PLUS implementing 
|>      lambda x. lambda y. x + y
|> then (funcall (PLUS) 3) is a function and will printout something like
|>      #<Function PLUS ....>.

Some observations: if PLUS is a Lisp function then the form (PLUS) is a call to that function with no arguments; furthermore (funcall (PLUS) 3) is a call to the *result* of (PLUS) with the argument 3. I'll assume that you mean (PLUS) returns the function object and that object is what prints as #<Function PLUS ....>. This suggests that PLUS is not an ordinary Lisp function object, but rather some custom data structure that you implemented yourself.

|> The questions are: can I access such information within a lisp program
|> and how?

If the functions in question are true Lisp function objects, then FUNCTIONP should work as described above. Otherwise, the answer depends on your implementation of these 'functions'. For example, if you used DEFSTRUCT to define the data structure then a type-discriminating predicate would be automatically created. E.g.,

(defstruct function
  name
  lambda-list
  code)

> (setq test (make-function :name 'plus :lambda-list '(x y)
                            :code '(+ x y)))
TEST
> (function-p test)
T
..................................................
                                  Len Charest, Jr.
                 JPL Artificial Intelligence Group
                          ·······@aig.jpl.nasa.gov
From: David Gadbois
Subject: Re: A question: Dynamic type check
Date: 
Message-ID: <19920828225713.6.GADBOIS@CLIO.MCC.COM>
    Date: Fri, 28 Aug 1992 14:22 CDT
    From: ·······@Aig.Jpl.Nasa.Gov (Len Charest)

    In article <·····@uqcspe.cs.uq.oz.au>, ····@cs.uq.oz.au (Ming Gong) writes:
    |> I want to know *at run time* whether or not a expression is a
    |> function.  Can some knowledgeable people tell me how?

    [...]

    If the functions in question are true Lisp function objects, then
    FUNCTIONP should work as described above. Otherwise, the answer
    depends on your implementation of these 'functions'. For example, if
    you used DEFSTRUCT to define the data structure then a
    type-discriminating predicate would be automatically created. E.g.,

    (defstruct function
      name
      lambda-list
      code)

    > (setq test (make-function :name 'plus :lambda-list '(x y)
				:code '(+ x y)))
    TEST
    > (function-p test)
    T

Note that since FUNCTION is already a type name, you can lose big
redefining it as such even if your implementation lets you.  It would be
not unlike redefining CAR.  It would be OK to use a name like
MY-FUNCTION, or to define it in a package that has its own symbol named
"FUNCTION".

--David Gadbois
From: Barry Margolin
Subject: Re: A question: Dynamic type check
Date: 
Message-ID: <17m5qjINNgll@early-bird.think.com>
In article <······················@jpl-devvax.jpl.nasa.gov> ·······@aig.jpl.nasa.gov writes:
>In article <·····@uqcspe.cs.uq.oz.au>, ····@cs.uq.oz.au (Ming Gong) writes:
>|> For example, I have a lisp function PLUS implementing 
>|>      lambda x. lambda y. x + y
>|> then (funcall (PLUS) 3) is a function and will printout something like
>|>      #<Function PLUS ....>.
>

>Some observations: if PLUS is a Lisp function then the form (PLUS) is a
>call to that function with no arguments; furthermore (funcall (PLUS) 3) is
>a call to the *result* of (PLUS) with the argument 3. I'll assume that you
>mean (PLUS) returns the function object and that object is what prints as
>#<Function PLUS ....>. This suggests that PLUS is not an ordinary Lisp
>function object, but rather some custom data structure that you
>implemented yourself.

It looks to me like he's experimenting with curried functions.  Presumably
his definition of PLUS is something like:

(defun plus ()
  #'(lambda (x)
      #'(lambda (y) (+ x y))))

When I use this in Lucid CL I see:

> (funcall (plus) 3)
#<Compiled-Function (:INTERNAL (:INTERNAL PLUS 0) 0) C349EE>
> (plus)
#<Compiled-Function (:INTERNAL PLUS 0) C347E6>
> (funcall (plus) 3)
#<Compiled-Function (:INTERNAL (:INTERNAL PLUS 0) 0) C34C8E>
> (funcall (funcall (plus) 3) 4)
7

I can easily imagine other Lisp implementations printing out the first two
results as "#<Function PLUS ...>".
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar