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

Re: #\a octothorpe syntax vs SRFI 10



 | From: Aubrey Jaffer <agj@xxxxxxxxxxxx>
 | Date: Mon,  3 Jan 2005 13:46:06 -0500 (EST)
 | 
 | Heterogeneous vectors support UNQUOTE because the elements of vectors
 | can contain reference to expressions.  Homogeneous-vectors would not
 | easily support UNQUOTE.  From SRFI-4:
 | 
 |    Homogeneous vectors can appear in quasiquotations but must not
 |    contain unquote or unquote-splicing forms (i.e. `(,x #u8(1 2)) is
 |    legal but `#u8(1 ,x 2) is not).  This restriction is to accomodate
 |    the many Scheme systems that use the read procedure to parse
 |    programs.
 | 
 | Similarly, heterogeneous arrays could support UNQUOTE; but uniform
 | arrays would not easily do so.
 | 
 | That vectors can support UNQUOTE-SPLICING is a fortuitous consequence
 | of their having only one row, whose length is thus unconstrained.  The
 | rows of multi-row arrays must all be the same length; supporting
 | UNQUOTE-SPLICING would require some heavy lifting.

It is useful to be able to specify data structures by template, as
QUASIQUOTE does.  We can do better than my first attempt by adding a
procedure to SRFI-47 array:

 -- Function: array rank protothunk contents

     RANK must be a nonnegative integer.  PROTOTHUNK must be a
     procedure of no arguments returning an array.  CONTENTS must be a
     RANK-deep nesting of lists.  For RANK = 0, CONTENTS can be any
     object.

     `array' creates and returns an array of the type returned by
     calling PROTOTHUNK with no arguments.  The returned array has
     RANK dimensions; and elements taken from CONTENTS in one-to-one
     correspondence, the innermost lists corresponding to the rows
     (the last index).

Here is how the `array' procedure could be used to return matrices for
calculating optical interference (Fresnel equations):

;;; Returns 2x2 matrix coding phase difference between reflected and
;;; transmitted paths.
;;;
;;;  (  -i*d_n    0    )
;;;  ( e               )
;;;  (           i*d_n )
;;;  (    0     e      )
(define (layer-phase h_j n_j th_j w)
  (define phase (exp (/ (* +2i pi h_j n_j (cos th_j)) w)))
  (array 2 A:complex-64 `((,(/ phase)      0)
                          (0          ,phase))))

;;; Returns 2x2 matrix:
;;;
;;;            (   1      r      )
;;;    1       (           n-1,n )
;;; -------- * (                 )
;;;  t         ( r          1    )
;;;   n-1,n    (  n-1,n          )
(define (layer-interface n1 n2 th1 s-polarization?)
  (define cos-i (cos th1))
  (define cos-t (cos (Snell-law n1 n2 th1)))
  (let ((transmit (E_T n1 n2 cos-i cos-t s-polarization?))
        (reflect (E_R n1 n2 cos-i cos-t s-polarization?)))
    (let ((r/t (/ reflect transmit))
          (tinv (/ transmit)))
      (array 2 A:complex-64 `((,tinv  ,r/t)
                              (,r/t  ,tinv))))))

                              -=-=-=-=-

* The `array' procedure allows one to use quasiquote to create any
  array, homogeneous or heterogeneous.

* The `array' procedure allows one to use array templates for both
  static and dynamically generated data.

* Calls to `array' look very much like SRFI-10 reader syntax:

  (define ident2 (array 2 A:real-32 '((1.0 0.0) (0.0 1.0))))
  (define ident2 #,(array 2 A:real-32 ((1.0 0.0) (0.0 1.0))))

* The `array' procedure can be used to implement SRFI-10 syntax for
  arrays.