qi icon indicating copy to clipboard operation
qi copied to clipboard

Symmetry operator of -<

Open chansey97 opened this issue 7 months ago • 3 comments

The operator -< currently is not symmetrical, i.e. lacks its mirror operator >-.

Two examples are provided below to demonstrate why it is useful.

The 1st one is the matrix addition.

Here is a possible solution:

;; 1 8 5
;; 3 4 3
;; 8 2 1
(define-flow D
  (~> (-< (~> (== (* 1) (* 8) (* 5)) +)
          (~> (== (* 3) (* 4) (* 3)) +)
          (~> (== (* 8) (* 2) (* 1)) +))))
          
;; 9 4 2
;; 5 4 7
;; 8 4 7
(define-flow E
  (~> (-< (~> (== (* 9) (* 4) (* 2)) +)
          (~> (== (* 5) (* 4) (* 7)) +)
          (~> (== (* 8) (* 4) (* 7)) +))))

(define-flow D+E
  (~>> (-< (~> E ▽) (~> D ▽)) (map +)))

The D+E seems ugly, because we have to collect them into a list, but I would like to write it like the following, if possible:

(define-flow D+E
  (~> (-< D E) (>- +)))

This example is inspired from Graphical Linear Algebra. In GLA, Pawel introduced a syntactic sugar called "m-wire". so the the string diagram of matrix addition can be simplified to:

Pasted_image_20231204062326

Image from https://www.cs.ox.ac.uk/qpl2015/slides/sobocinski-tutorial.pdf p.25

The 2nd comes from meru.rkt

(define-flow meru-step
  (~>> (-< (~> (-< 0 _) ▽) 
           (~> (-< _ 0) ▽))
       (map +) △))

Using the expected operator >-, the solution would be more concise:

(define-flow meru-step
  (~>> (-< (~> (-< 0 _))
           (~> (-< _ 0)))
       (>- +) △))

P.S. This issue has been discussed on Racket Discord and @countvajhula proposed a solution. Currently we could write the following macro to workaround it.

(require (for-syntax racket/base syntax/parse))

(define-qi-syntax-rule (-<> f ... ((~datum >-) comb))
  (~>> (-< (~> f ▽) ...) (map comb)))

(define-flow D+E
  (~> (-<> D E (>- +))))

More details see https://discord.com/channels/571040468092321801/979642553471221790/1182246331922780251

chansey97 avatar Dec 08 '23 13:12 chansey97