I am doing symbolic computations for origami, involving computing the parametric equation of a crease given two points, as below. I am hoping to turn that into a very simple origami theorem prover/CAS hybrid.
; number -> number -> vertex
(struct vertex (x y))
; lambda _ -> crease
(struct crease (equation))
; λ -> vertex -> vertex -> vertex
(define (vop-symbol op v1 v2)
(vertex `(,op ,(vertex-x v1) ,(vertex-x v2))
`(,op ,(vertex-y v1) ,(vertex-y v2))))
; vertex -> vertex -> vertex
(define (vadd-symbol v1 v2)
(vop-symbol + v1 v2))
; vertex -> vertex -> vertex
(define (vsub-symbol v1 v2)
(vop-symbol - v1 v2))
; number -> vertex -> vertex
(define (vsmul-symbol s v)
(vertex `(* ,s ,(vertex-x v)) `(* ,s ,(vertex-y v))))
; vertex -> vertex -> crease
(define (axiom1 v1 v2)
(crease (λ (s)(vadd-symbol v1 (vsmul-symbol s (vsub-symbol v2 v1))))))
This is very simple, but has the disadvantage that using concrete numbers instead of symbols requires eval
on user input:
> (define crease1 (axiom1 (vertex 0 0) (vertex 3 3)))
> (vertex-y ((crease-equation crease1) 1/2))
'(#<procedure:+> 0 (* 1/2 (#<procedure:-> 3 0)))
> (eval (vertex-y ((crease-equation crease1) 1/2)))
1 1/2
So my question is, is it better practice as in easier/more maintainable to perform that sort of symbolic manipulation at runtime (as in the SICP function derivation examples), or compile-time? I.e. is the trouble of using macros worth avoiding eval
? Or is there a workaround with functions?
Thank you!