From: Kyle McGivney
Subject: ignore that last typecase post
Date: 
Message-ID: <a7a5682d-1af7-423d-bb20-6fe99be4ae0d@64g2000hsw.googlegroups.com>
I just now had a problem that I thought typecase would work well for,
even though I've never used it. I decided to try it, although I didn't
read the documentation on it first.

assuming that it works like "case", I wrote the following
(etypecase my-thing
    (null nil)
    (list (do-something my-thing))
    ((function symbol) (do-something-else my-thing)))

...but apparently that third clause does not work. It seems strange to
me that the first part of the clause is evaluated, even though in the
macro case, the clause is not evaluated, and it can represent a list
of possible values. Why was this design chosen for typecase? It seems
inconsistent with case. Also, is there a way to do this using
typecase?

From: Kyle McGivney
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <46a26dee-5b09-4588-8ae4-757887cff726@q78g2000hsh.googlegroups.com>
On Feb 18, 9:05 am, Kyle McGivney <·······@gmail.com> wrote:
> I just now had a problem that I thought typecase would work well for,
> even though I've never used it. I decided to try it, although I didn't
> read the documentation on it first.
>
> assuming that it works like "case", I wrote the following
> (etypecase my-thing
>     (null nil)
>     (list (do-something my-thing))
>     ((function symbol) (do-something-else my-thing)))
>
> ...but apparently that third clause does not work. It seems strange to
> me that the first part of the clause is evaluated, even though in the
> macro case, the clause is not evaluated, and it can represent a list
> of possible values. Why was this design chosen for typecase? It seems
> inconsistent with case. Also, is there a way to do this using
> typecase?

As for the subject - I had a keyboard mishap, and I though I had
posted this accidentally before I was finished writing it. I appear to
be mistaken. So just ignore the subject then.
From: Ken Tilton
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <47b99910$0$15200$607ed4bc@cv.net>
Kyle McGivney wrote:
> I just now had a problem that I thought typecase would work well for,
> even though I've never used it. I decided to try it, although I didn't
> read the documentation on it first.
> 
> assuming that it works like "case", I wrote the following
> (etypecase my-thing
>     (null nil)
>     (list (do-something my-thing))
>     ((function symbol) (do-something-else my-thing)))
> 
> ...but apparently that third clause does not work. It seems strange to
> me that the first part of the clause is evaluated, even though in the
> macro case, the clause is not evaluated, and it can represent a list
> of possible values. Why was this design chosen for typecase? It seems
> inconsistent with case. Also, is there a way to do this using
> typecase?

Try: ((or function symbol) ...)

That might give you an idea as to why typecase varies a little from case.

hth, kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Kyle McGivney
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <73ce5705-2007-43e1-abca-89ed084a00db@e23g2000prf.googlegroups.com>
On Feb 18, 9:41 am, Ken Tilton <···········@optonline.net> wrote:
> Kyle McGivney wrote:
> > I just now had a problem that I thought typecase would work well for,
> > even though I've never used it. I decided to try it, although I didn't
> > read the documentation on it first.
>
> > assuming that it works like "case", I wrote the following
> > (etypecase my-thing
> >     (null nil)
> >     (list (do-something my-thing))
> >     ((function symbol) (do-something-else my-thing)))
>
> > ...but apparently that third clause does not work. It seems strange to
> > me that the first part of the clause is evaluated, even though in the
> > macro case, the clause is not evaluated, and it can represent a list
> > of possible values. Why was this design chosen for typecase? It seems
> > inconsistent with case. Also, is there a way to do this using
> > typecase?
>
> Try: ((or function symbol) ...)
>
> That might give you an idea as to why typecase varies a little from case.
>
> hth, kenny
>
> --http://smuglispweeny.blogspot.com/http://www.theoryyalgebra.com/
>
> "In the morning, hear the Way;
>   in the evening, die content!"
>                      -- Confucius- Hide quoted text -
>
> - Show quoted text -

Thanks, that's what I was looking for.
From: Ken Tilton
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <47b9bc30$0$15159$607ed4bc@cv.net>
Kyle McGivney wrote:
> On Feb 18, 9:41 am, Ken Tilton <···········@optonline.net> wrote:
> 
>>Kyle McGivney wrote:
>>
>>>I just now had a problem that I thought typecase would work well for,
>>>even though I've never used it. I decided to try it, although I didn't
>>>read the documentation on it first.
>>
>>>assuming that it works like "case", I wrote the following
>>>(etypecase my-thing
>>>    (null nil)
>>>    (list (do-something my-thing))
>>>    ((function symbol) (do-something-else my-thing)))
>>
>>Try: ((or function symbol) ...)

I might have mentioned that that works because '(or function symbol) is 
a single type, if you will: (typep 42 '(or fixnum string)) ->t

And one can have programmable types via SATISFIES:

(defun signed-number? (mx)
   (and (signed? mx)
     (or (typep .op2 'mx-number)
       (unsigned-numeric-fraction? .op2))))

(deftype signed-number ()
   `(or mx-number (satisfies signed-number?)))

IANALL, but as to your other concern about inconsistency with case, I 
guess there would be an ambiguity as to whether one meant with a list a 
list of types or a compound type and since a compound OR type was almost 
succinct as listing the types separately etc etc.

kt

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/

"In the morning, hear the Way;
  in the evening, die content!"
                     -- Confucius
From: Pascal J. Bourguignon
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <7clk5ibadf.fsf@pbourguignon.anevia.com>
Kyle McGivney <·······@gmail.com> writes:

> I just now had a problem that I thought typecase would work well for,
> even though I've never used it. I decided to try it, although I didn't
> read the documentation on it first.
>
> assuming that it works like "case", 

No, it does not.

> I wrote the following
> (etypecase my-thing
>     (null nil)
>     (list (do-something my-thing))
>     ((function symbol) (do-something-else my-thing)))
>
> ...but apparently that third clause does not work. 

Indeed, (function symbol) is invalid syntax for a type.  Try:
(function (symbol) t)  for a function taking a symbol argument, or
(function (&rest t) symbol) for a function returning a symbol.  

But in any case, functions cannot be handled by TYPEP nor by
[E]TYPECASE.

 (etypecase (lambda (x) (declare (type symbol x)) x)
     (null nil)
     (list (do-something my-thing))
     ((function (symbol) symbol) (do-something-else my-thing))) ; still doesn't work

However, SUBTYPEP do work, so you may use it, keeping somewhere the
type descriptor of your functions.

> It seems strange to
> me that the first part of the clause is evaluated, even though in the
> macro case, the clause is not evaluated, and it can represent a list
> of possible values. Why was this design chosen for typecase? It seems
> inconsistent with case. Also, is there a way to do this using
> typecase?

Read again the last etypecase above.  How could the compiler, in
general, know that (lambda (x) (declare (type symbol x)) x) does
return a symbol, and not an integer?  For a simple function like this
one, no problem, but what about:

(lambda (x)
   (declare (type symbol x))
   (when (oddp (random 2)) (eval (read)))
   (f x))

Imagine the user typing: (defun f (x) 42) or (defun f (x) "XLII")


-- 
__Pascal Bourguignon__
From: Kyle McGivney
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <539d1806-fe3b-4093-af68-881b9d2af265@i12g2000prf.googlegroups.com>
> But in any case, functions cannot be handled by TYPEP nor by
> [E]TYPECASE.

The examples that you're using of anonymous functions seem a little
more complex than anything I would ever employ in my code. I don't
know if we're using the same language here, but this returns 42 in
Allegro 8:

(typecase #'foo
  (list 1)
  (function 42))

So typecase has to be able to "handle" at least some functions, right?
Maybe just named functions?
From: Pascal Bourguignon
Subject: Re: ignore that last typecase post
Date: 
Message-ID: <87d4qudmki.fsf@thalassa.informatimago.com>
Kyle McGivney <·······@gmail.com> writes:

>> But in any case, functions cannot be handled by TYPEP nor by
>> [E]TYPECASE.
>
> The examples that you're using of anonymous functions seem a little
> more complex than anything I would ever employ in my code. I don't
> know if we're using the same language here, but this returns 42 in
> Allegro 8:
>
> (typecase #'foo
>   (list 1)
>   (function 42))
>
> So typecase has to be able to "handle" at least some functions, right?
> Maybe just named functions?

Yep. Since (function symbol) was meaningless in (typecase x ((function
symbol) 42)), (it is not a type description, which is what typecase
expects there), I infered wrongly you wanted to mean a function OF
symbol, while I left to Kenny to infer what you probably meant (as
indicated by your reference to CASE), function OR symbol.

FUNCTION is a type specifier for all functions, and TYPEP and
[E]TYPECASE can test for an object to be a function in general.  It
just cannot check if the function is of a subtype of a more precise
function type, including signature (type of its arguments and results).

(declaim (ftype (function (symbol) symbol) foo))
(defun foo (s)
   (declare (symbol s))
   s)

(typecase (function foo)
  ((function (symbol) symbol) 42))  ; still raises an error.



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

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.