Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

function type

104 views
Skip to first unread message

jimka

unread,
Nov 20, 2013, 5:43:15 AM11/20/13
to
Can someone give me some examples of using function type specifiers.

http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm

I was under the impression that it was possible to specify a function type as a mapping of input types to output types, but from my reading of the spec that doesn't seem to be the case. For example, I thought it was possible to say that binary function F maps (X Y) -> Z and (A B) -> C.

Thanks
Jim

Damien Diederen

unread,
Nov 20, 2013, 11:38:57 AM11/20/13
to

Hi Jim,
If I understand correctly, you are looking for a compound type specifier
akin to:

(independent-domains-and-ranges
((number symbol) (values (integer 42 42)))
((string symbol) (values (member foo))))

as opposed to:

(function ((or number string) symbol)
(values (or (integer 42 42) (member foo))))

I don't think such a thing exists in Common Lisp.

Cheers, -D

--
http://crosstwine.com
tel: +49 89 2189 2939
cell: +49 174 3489 428

“Strong Opinions, Weakly Held”
— Bob Johansen

tar...@google.com

unread,
Nov 20, 2013, 5:19:15 PM11/20/13
to
On Wednesday, November 20, 2013 2:43:15 AM UTC-8, jimka wrote:
| Can someone give me some examples of using function type specifiers.
|
| http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm
|

Some examples might be:

(declaim (ftype (function (number number) number) add))
(defun add (x y)
(+ x y))

| I was under the impression that it was possible to specify a function type as
| a mapping of input types to output types, but from my reading of the spec
| that doesn't seem to be the case. For example, I thought it was possible to
| say that binary function F maps (X Y) -> Z and (A B) -> C.

You can only specify a single mapping, not a set of alternate (type-descriminated) mappings.

Pascal J. Bourguignon

unread,
Nov 21, 2013, 5:39:18 PM11/21/13
to
(deftype f () '(or (function (x y) z)
(function (a b) c)))

as in:

cl-user> (deftype x () 'integer)
(deftype y () 'integer)
(deftype z () 'integer)
(deftype a () 'character)
(deftype b () 'string)
(deftype c () 'string)

C
cl-user> (deftype f () '(or (function (x y) z)
(function (a b) c)))
F
cl-user> (typep (lambda (x y) (declare (integer x y)) (+ x y)) 'f)
T
cl-user>

Now of course, that doesn't mean much, the implementation may be lying
to you:

cl-user> (typep (lambda (x y) (declare (real x y)) (+ x y)) 'f)
T
cl-user> (typep (lambda (x y) (cons (car x) (cdr y))) 'f)
T

Basically, function types used this way are all equivalent to FUNCTION,
since CL doesn't impose the implementations to provide a termination
proving program.


--
__Pascal Bourguignon__
http://www.informatimago.com/

Damien Diederen

unread,
Nov 22, 2013, 4:31:39 AM11/22/13
to

Hi Pascal, Jim,

"Pascal J. Bourguignon" <p...@informatimago.com> writes:
> jimka <ji...@rdrop.com> writes:
>
>> Can someone give me some examples of using function type specifiers.
>>
>> http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm
>>
>> I was under the impression that it was possible to specify a function
>> type as a mapping of input types to output types, but from my reading
>> of the spec that doesn't seem to be the case. For example, I thought
>> it was possible to say that binary function F maps (X Y) -> Z and (A
>> B) -> C.
>
> (deftype f () '(or (function (x y) z)
> (function (a b) c)))

This is is not quite the same as INDEPENDENT-DOMAINS-AND-RANGES (cf. my
other post): there is no guarantee you can call an F-typed function with
an A as the first parameter, it may only accept Xs there.

Perhaps AND fits that bill. Do you think it would be a reasonable
interpretation of "intersection of the typespecs"?

(and (function (x y) z)
(function (a b) c))

(For some context: Jim and I have been discussing CL-like type
specifiers for Cadence SKILL on a different forum:
http://www.cadence.com/Community/forums/p/27846/1328863.aspx#1328863 )

> cl-user> (typep (lambda (x y) (declare (integer x y)) (+ x y)) 'f)
> T
>
> Now of course, that doesn't mean much, the implementation may be lying
> to you

It's worse than that:

"An error of type error is signaled if type-specifier is values, or a
type specifier list whose first element is either function or
values." (From http://clhs.lisp.se/Body/f_typep.htm.)

SBCL will tell you as much :) Which, I guess, is better than lying.

> Basically, function types used this way are all equivalent to FUNCTION,
> since CL doesn't impose the implementations to provide a termination
> proving program.

They're still useful outside of the language's runtime library;
e.g. just as documentation or for type inference:

(defun foo (fn u)
(declare (f fn))
(funcall fn u ...))

=> U is (OR X A)

(foo ... (definitely-an-a))

=> Result is of type C.

Damien Diederen

unread,
Nov 22, 2013, 5:21:39 AM11/22/13
to

Hi,

Damien Diederen <d...@crosstwine.com> writes:
> "Pascal J. Bourguignon" <p...@informatimago.com> writes:
>> jimka <ji...@rdrop.com> writes:
>>
>>> Can someone give me some examples of using function type specifiers.
>>>
>>> http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm
>>>
>>> I was under the impression that it was possible to specify a function
>>> type as a mapping of input types to output types, but from my reading
>>> of the spec that doesn't seem to be the case. For example, I thought
>>> it was possible to say that binary function F maps (X Y) -> Z and (A
>>> B) -> C.
>>
>> (deftype f () '(or (function (x y) z)
>> (function (a b) c)))
>
> This is is not quite the same as INDEPENDENT-DOMAINS-AND-RANGES (cf. my
> other post): there is no guarantee you can call an F-typed function with
> an A as the first parameter, it may only accept Xs there.
>
> Perhaps AND fits that bill. Do you think it would be a reasonable
> interpretation of "intersection of the typespecs"?
>
> (and (function (x y) z)
> (function (a b) c))

Bj�rn Lindberg pointed me to the first bullet of:

http://www.lispworks.com/documentation/HyperSpec/Body/t_fn.htm

which seems to preclude that interpretation in CL.
0 new messages