5

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!

Raoul
  • 1,872
  • 3
  • 26
  • 48
  • 3
    Do everything at runtime: no macros and no eval. You need to write a function `simplify` that can simplify expressions. Then you can write `(simplify (quasiquote (op ...))` (where quasiquote is used, because backtick is tricky to write in comments. – soegaard Dec 26 '18 at 17:36
  • 3
    If your main goal is to write origami programs - then reuse an existing CAS. If you want to play with symbolic calculations - then check "Computer algebra and symbolic computation" by Cohen. – soegaard Dec 26 '18 at 17:40
  • 2
    @soegaard FYI: we can enclose backtick-containing expression in double backticks, like so: ``(simplify `(op ...))`` – Will Ness Dec 29 '18 at 11:23
  • @soegaard me too. Had to be told that by someone too, many years ago. – Will Ness Dec 29 '18 at 12:23
  • this sounds really cool. Did it work out ok? Is there a public repository somewhere? – Riley Jun 24 '20 at 01:36
  • 2
    @Riley thanks. Unfortunately, life caught up with me and this has gone nowhere. However, this is a whole field of research and see for instance http://apps.amandaghassaei.com/OrigamiSimulator/ and associated references to get you started. – Raoul Jun 25 '20 at 04:20

0 Answers0