aboutsummaryrefslogtreecommitdiffstats
path: root/csc/compare.csc
blob: e8e68d3a7590be75bb3e7084857ef413cb9a78f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(define-library (csc compare)
  (export diff apply-transformer)
  (import (scheme base)
          (only (scheme write)
            display
            write)
          (only (csc strings)
            contains?
            split))
  (begin
    ; compare is inspired by Go's cmp.Diff.


    (define (print w . xs)
      (unless (null? xs)
        (let ((x (car xs)))
          (if (or (string? x)
                  (symbol? x))
            (display x w)
            (write x w)))
        (apply print w (cdr xs))))


    (define (apply-transformer t x)
      (if (list? t)
        (let loop ((transformers t)
                   (x x))
          (if (null? transformers)
            x
            (loop (cdr transformers)
                  (apply-transformer (car transformers) x))))
        (if ((car t) x)
          ((cdr t) x)
          x)))


    (define (alist? l)
      (and (list? l)
           (let loop ((l l))
             (cond
               ((null? l) #t)
               ((not (and (pair? (car l))
                          (symbol? (caar l))))
                 #f)
               (else (loop (cdr l)))))))


    (define (transform x transformers)
      (define x* (apply-transformer transformers x))
      (cond
        ((alist? x*)
          (map (lambda (elem)
                 (define elem* (apply-transformer transformers elem))
                 (if (pair? elem*)
                   (cons (car elem) (transform (cdr elem) transformers))
                   elem*))
               x*))
        ((list? x*)
          (map (lambda (elem) (transform elem transformers))
               x*))
        (else x*)))


    ; As always, I copied the algorithm from Wikipedia
    (define (lcs x y cmp)
      (define x-vals (list->vector x))
      (define y-vals (list->vector y))
      (define table (make-vector (* (+ 1 (vector-length x-vals)) (+ 1 (vector-length y-vals))) 0))
      (define (index i j)
        (+ j (* i (+ 1 (vector-length y-vals)))))
      (let loop-i ((i 1))
        (when (<= i (vector-length x-vals))
          (let loop-j ((j 1))
            (when (<= j (vector-length y-vals))
              (if (cmp (vector-ref x-vals (- i 1)) (vector-ref y-vals (- j 1)))
                (vector-set! table (index i j) (+ 1 (vector-ref table (index (- i 1) (- j 1)))))
                (vector-set! table (index i j) (max (vector-ref table (index (- i 1) j))
                                                    (vector-ref table (index i (- j 1))))))
              (loop-j (+ 1 j))))
          (loop-i (+ 1 i))))
      (let loop ((i (vector-length x-vals))
                 (j (vector-length y-vals))
                 (lcs '()))
        (cond
          ((or (= 0 i) (= 0 j)) lcs)
          ((cmp (vector-ref x-vals (- i 1)) (vector-ref y-vals (- j 1)))
            (loop (- i 1) (- j 1) (cons (vector-ref x-vals (- i 1)) lcs)))
          ((> (vector-ref table (index i (- j 1))) (vector-ref table (index (- i 1) j)))
            (loop i (- j 1) lcs))
          (else (loop (- i 1) j lcs)))))


    (define (pretty w x indent)
      (cond
        ((list? x)
          (print w indent "  (\n")
          (let ((new-indent (string-append indent "  ")))
            (for-each (lambda (v)
                        (pretty w v new-indent))
              x))
          (print w indent "  )\n"))
        ((and (pair? x)
              (symbol? (car x)))
          (print w indent "  (" (car x) " .\n")
          (pretty w (cdr x) (string-append indent "  "))
          (print w indent "  )\n"))
        (else (print w indent "  " x "\n"))))


    (define (diff* w x y indent)
      (cond
        ((or (and (boolean? x) (boolean? y))
             (and (char? x) (char? y))
             (and (symbol? x) (symbol? y))
             (and (number? x) (number? y))
             (and (string? x) (string? y)))
          (if (equal? x y)
            (begin
              (print w indent "   " x "\n")
              #t)
            (begin
              (print w indent "-  " x "\n" indent "+  " y "\n")
              #f)))
        ((and (alist? x) (alist? y))
          (print w indent "   (\n")
          (let ((key-lcs (lcs (map car x) (map car y) symbol=?))
                (indent* (string-append indent "  "))
                (equal #t))
            (let loop ()
              (unless (and (null? key-lcs)
                           (null? x)
                           (null? y))
                (cond
                  ((and (null? key-lcs)
                        (null? x))
                    (pretty w (car y) (string-append indent* "+"))
                    (set! y (cdr y))
                    (set! equal #f))
                  ((null? key-lcs)
                    (pretty w (car x) (string-append indent* "-"))
                    (set! x (cdr x))
                    (set! equal #f))
                  ((symbol=? (caar x) (caar y) (car key-lcs))
                    (print w indent* "   (" (car key-lcs) " .\n")
                    (set! equal (and (diff* w (cdar x) (cdar y) (string-append indent* "  "))
                                     equal))
                    (print w indent* "   )\n")
                    (set! x (cdr x))
                    (set! y (cdr y))
                    (set! key-lcs (cdr key-lcs)))
                  ((symbol=? (caar x) (car key-lcs))
                    (pretty w (car y) (string-append indent* "+"))
                    (set! y (cdr y))
                    (set! equal #f))
                  (else
                    (pretty w (car x) (string-append indent* "-"))
                    (set! x (cdr x))
                    (set! equal #f)))
                (loop)))
            (print w indent "   )\n")
            equal))
        ((and (list? x) (list? y))
          (print w indent "   (\n")
          (let* ((common (lcs x y equal?))
                 (indent* (string-append indent "  "))
                 (equal #t))
            (let loop ()
              (unless (and (null? common)
                           (null? x)
                           (null? y))
                (cond
                  ((and (null? common)
                        (null? x))
                    (pretty w (car y) (string-append indent* "+"))
                    (set! y (cdr y))
                    (set! equal #f))
                  ((and (null? common)
                        (null? y))
                    (pretty w (car x) (string-append indent* "-"))
                    (set! x (cdr x))
                    (set! equal #f))
                  ((null? common)
                    (diff* w (car x) (car y) indent*)
                    (set! x (cdr x))
                    (set! y (cdr y))
                    (set! equal #f))
                  ((equal? (car x) (car y))
                    (pretty w (car x) (string-append indent* " "))
                    (set! x (cdr x))
                    (set! y (cdr y))
                    (set! common (cdr common)))
                  ((equal? (car x) (car common))
                    (pretty w (car y) (string-append indent* "+"))
                    (set! y (cdr y))
                    (set! equal #f))
                  ((equal? (car y) (car common))
                    (pretty w (car x) (string-append indent* "-"))
                    (set! x (cdr x))
                    (set! equal #f))
                  (else
                    (diff* w (car x) (car y) indent*)
                    (set! x (cdr x))
                    (set! y (cdr y))
                    (set! equal #f)))
                (loop)))
            (print w indent "   )\n")
            equal))
        (else
          (pretty w x (string-append indent "-"))
          (pretty w y (string-append indent "+"))
          #f)))


    (define default-transformers
      (list
        (cons (lambda (s)
                (and (string? s)
                     (contains? s "\n")))
              (lambda (s)
                (list (cons '!type 'string)
                      (cons 'value (split s "\n")))))
        (cons vector? (lambda (v)
                        (list (cons '!type 'vector)
                              (cons 'value (vector->list v)))))
        (cons bytevector? (lambda (b)
                            (list (cons '!type 'bytevector)
                                  (cons 'value (let loop ((i 0)
                                                          (acc '()))
                                                 (when (< i (bytevector-length b))
                                                   (loop (+ 1 i)
                                                         (cons (string-append "0x" (number->string
                                                                                     (bytevector-u8-ref b i)
                                                                                     16))
                                                               acc)))
                                                 (reverse acc))))))))


    ; Returns a string diff between x and y. Diff knows how to compare lists,
    ; alists and other primitive types. Any user-defined type needs a
    ; transformer. Each transformer is a pair of type predicate that the
    ; transformer matches on, and a function that transforms a value of that
    ; type into an alist.
    (define (diff x y . transformers)
      (define d (open-output-string))
      (define t* (cons default-transformers transformers))
      (if (diff* d (transform x t*) (transform y t*) "")
        ""
        (get-output-string d)))))