[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [r6rs-discuss] [Scheme-reports] do we need to redefine eqv?



On Fri, 2010-12-24 at 13:32 -0500, Andre van Tonder wrote:

> I think one of the principal ways CASE was originally intended to be used
> was for fast dispatch on symbols or possibly numbers used as typetags in
> objects represented as lists, e.g.,
> 
>    (case (car object)
>      ((node) .....)
>      ((leaf) .....))
> 
> I believe there are some Schemes, like STALIN, that highly optimize such
> CASE expressions.  This would be harder to do in a higher-order CASE.

Well, my use-case was more like the following

(case object
(((node1 ,x ,y) (node2 ,x ,y)) (recur x y))
(((leaf ,x)) (recur x)))

(or, more readable)

(case object
| (node1 ,x ,y) | (node2 ,x ,y) => (recur x y)
| (leaf ,x) => (recur x))

(so, eqv? here being expanded to include user-specified semantics 
and data-types)

which should translate, modulo destructuring bindings of course, 
to exact same sequence of EQV? comparisons. Bindings would be moved 
inside switch arms, and dispatch would reduce to trying EQV? on each
tag, just like with original CASE. 

If STALIN could do a switch on a constant symbol value before, what
prevents it from doing the same here? 

P.S. Actually, I have tried the following program with STALIN, but
couldn't find any switch on symbol value in the C output.
Defunctionalized for-each calls for 3 cases though do all look like
pretty well optimized, so I think the final assembly would look quite 
similar...

(define foo1
   (lambda (x)
      (case x
((a b) 1000)
((c d) 2000)
(else 3000))))
(define foo2
   (lambda (x)
      (cond 
((or (eqv? x 'a)
      (eqv? x 'b)) 1001)
((or (eqv? x 'c)
      (eqv? x 'd)) 2002)
(else 3003))))
(define foo3
   (lambda (x)
      (if (or (eqv? x 'a)
      (eqv? x 'b))
  1010
  (if (or (eqv? x 'c)
      (eqv? x 'd))
      2020
      3030))))

(for-each (lambda (x) (write (foo1 x))(newline)) '(a d e))
(for-each (lambda (x) (write (foo2 x))(newline)) '(a d e))
(for-each (lambda (x) (write (foo3 x))(newline)) '(a d e))





_______________________________________________
r6rs-discuss mailing list
r6rs-discuss@x
http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss