Jonathan S. Shapiro wrote:
> What we now have in mind is to introduce a new constraint:
>
> (has-field 'a name 'b)
>
> which is satisfied when the type 'a has a field with the given /name/,
> and that field in turn has type 'b. So the type of:
>
> (define (get-x v) v.x)
> get-x (forall ((has-field 'a v 'b))
> (fn 'a -> 'b))
>
> that is: we will now do polymorphic inference over field names.
How do you intend to implement such a function? One option would be to
require an offset for each HAS-FIELD constraint:
(defstruct point-2d (x float) (y float)) )
(define (get-x-impl x-offset v-addr)
(+ v-addr x-offset)) ;; returns the address of the pointer plus the
offset
That could work, but this would require a lot of offsets... I'm also not
sure about returning an address..., but I guess an address can always be
converted into any type. Perhaps you need to implement two versions of
the function, one for value-types and one for pointer types. If not, and
one knows at the call-site that 'b is a float (for example), then
(define (test (pt point-2d))
(get-x pt))
Would result in:
(define (test-impl pt-addr) (ld-float (get-x-impl 0 pt-addr))) ;;
(ld-float [addr]) loads a floating-point and returns the value.
<=>
(define (test-impl pt-addr) (ld-float (+ pt-addr 0)))
<=>
(define (test-impl pt-addr) (ld-float pt-addr))
In order to deal with the increasing number of offsets, one could use
offset tables. Consider:
(defstruct color-point-2d (color color) (x float) (y float))
(define (draw pt)
(draw-point pt.x pt.y)) ;; draw-point is a regular function.
Here, the draw function would have to be implemented with two offsets
(for x and for y). The draw function implementation would be something like:
(define (draw-impl pt-dict pt-addr)
(let ((x-off (get-offset pt-dict "x")) ;; Look up the offset of x
for pt.
(y-off (get-offset pt-dict "y")))
(draw-point (+ pt-addr x-off) (+ pt-addr y-off))))
Or, if draw-point requires floating point arguments:
(define (draw-impl pt-dict pt-addr)
(let ((x-off (get-offset pt-dict "x"))
(y-off (get-offset pt-dict "y")))
(draw-point (ld-float (+ pt-addr x-off)) ;; Assuming (ld-float
addr) loads
(ld-float (+ pt-addr y-off))) ;; a floating point
value at [addr].
))
Thanks,
PKE.
--
Pål-Kristian Engstad ([EMAIL PROTECTED]),
Lead Graphics & Engine Programmer,
Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North,
Santa Monica, CA 90404, USA. Ph.: (310) 633-9112.
"It is better to have 100 functions operate on one data structure
than 10 functions [each operate] on 10 data structures." -- NN
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev