-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.rkt
218 lines (188 loc) · 7.07 KB
/
score.rkt
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
#lang racket
(require "data/bit-vec.rkt"
"data/fp.rkt"
"data/eval.rkt")
(provide (all-defined-out))
(define Hamming-distance
(λ (bv1 bv2)
(let ([bs1 (BitVec->bits bv1)] [bs2 (BitVec->bits bv2)])
(foldl (λ (t r) (+ r (bitwise-xor (car t) (cdr t))))
0
(map cons bs1 bs2)))))
; smt equality/inequality
; bv's equality
(define (score/bv= c bv1 bv2)
(if (bv= bv1 bv2)
1
(* c (- 1 (/ (Hamming-distance bv1 bv2) (BitVec-width bv1))))))
; fp's equality
(define (score/fp= c fp1 fp2)
(cond
[(and (fp/nan? fp1) (fp/nan? fp2)) 1]
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(bv= (FloatingPoint->BitVec fp1) (FloatingPoint->BitVec fp2)) 1]
[else (fp-dist-score c fp1 fp2 #f)]))
(define ((score/= c) v1 v2)
(match v1
[(struct BitVec _) (score/bv= c v1 v2)]
[(struct FloatingPoint _) (score/fp= c v1 v2)]
[_ (error "unimplemented type")]))
; bv's inequality
(define score/bv≠ (λ (bv1 bv2) (if (bv= bv1 bv2) 0 1)))
; fp's inequality
(define score/fp≠
(λ (fp1 fp2)
(cond
[(and (fp/nan? fp1) (fp/nan? fp2)) 0]
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[else
(score/bv≠ (FloatingPoint->BitVec fp1) (FloatingPoint->BitVec fp2))])))
(define (score/≠ v1 v2)
(match v1
[(struct BitVec _) (score/bv≠ v1 v2)]
[(struct FloatingPoint _) (score/fp≠ v1 v2)]
[_ (error "unimplemented type")]))
; fp's equality/inequality
(define ((score/fpeq c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(and (fp/zero? fp1) (fp/zero? fp2)) 1]
[else (score/fp= c fp1 fp2)]))
(define (score/fp!eq fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[(and (fp/zero? fp1) (fp/zero? fp2)) 0]
[else (score/bv≠ (FloatingPoint->BitVec fp1) (FloatingPoint->BitVec fp2))]))
(define bv-dist-score
(λ (c bv1 bv2 eq)
(define dist
(+ (abs (- (BitVec-value bv1) (BitVec-value bv2))) (if eq 1 0)))
(* c (- 1 (/ dist (expt 2 (BitVec-width bv1)))))))
; bv's lt
; score = c*(1-(|bv1-bv2|+1)/2^n)
(define ((score/bv< c) bv1 bv2)
(if (bv< bv1 bv2) 1 (bv-dist-score c bv1 bv2 #t)))
; bv's geq
; score = c*(1-|bv1-bv2|/2^n)
(define ((score/bv≥ c) bv1 bv2)
(if (bv≥ bv1 bv2) 1 (bv-dist-score c bv1 bv2 #f)))
; bv's gt
; score = c*(1-(|bv1-bv2|+1)/2^n)
(define ((score/bv> c) bv1 bv2)
(if (bv> bv1 bv2) 1 (bv-dist-score c bv1 bv2 #t)))
; bv's leq
; score = c*(1-|bv1-bv2|/2^n)
(define ((score/bv≤ c) bv1 bv2)
(if (bv≤ bv1 bv2) 1 (bv-dist-score c bv1 bv2 #f)))
(define get/fp-pos
(λ (fp)
(define exp-width (FloatingPoint-exp-width fp))
(define sig-width (FloatingPoint-sig-width fp))
(if (fp/positive? fp)
(BitVec-value (FloatingPoint->BitVec fp))
(- 0
(- (BitVec-value (FloatingPoint->BitVec fp))
(expt 2 (- (+ exp-width sig-width) 1)))))))
(define fp-dist-score
(λ (c fp1 fp2 eq)
(define dist (+ (abs (- (get/fp-pos fp1) (get/fp-pos fp2))) (if eq 1 0)))
(* c
(- 1
(/ dist
(expt 2
(+ (FloatingPoint-exp-width fp1)
(FloatingPoint-sig-width fp2))))))))
; note that we're not pursuing a minimal set of operations here
; instead we give each operation its score as well as for its negation
; fp's lt
(define ((score/fplt c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(fp< fp1 fp2) 1]
;; fp1 >= fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #t)]))
(define ((score/fp!lt c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[(fp≥ fp1 fp2) 1]
;; fp1 < fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #f)]))
(define ((score/fpleq c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(fp≤ fp1 fp2) 1]
;; fp1 > fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #f)]))
(define ((score/fp!leq c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[(fp> fp1 fp2) 1]
;; fp1 <= fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #t)]))
(define ((score/fpgt c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(fp> fp1 fp2) 1]
;; fp1 <= fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #t)]))
(define ((score/fp!gt c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[(fp≤ fp1 fp2) 1]
;; fp1 > fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #f)]))
(define ((score/fpgeq c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 0]
[(fp≥ fp1 fp2) 1]
;; fp1 < fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #f)]))
(define ((score/fp!geq c) fp1 fp2)
(cond
[(or (fp/nan? fp1) (fp/nan? fp2)) 1]
[(fp< fp1 fp2) 1]
;; fp1 >= fp2 and fp1 != nan and fp2 != nan
[else (fp-dist-score c fp1 fp2 #t)]))
(define score2
(λ (op1 op2 assignment env score-bf)
(let ([bv1 (eval op1 assignment env)] [bv2 (eval op2 assignment env)])
(score-bf bv1 bv2))))
; bool's score function
; note that bool is treated as bv1
(define score-bool (λ (v) (eval/id v)))
(define score-bool! (λ (v) (- 1 (score-bool v))))
(define score-seq (λ (es sf cf) (foldl (λ (e s) (cf s (sf e))) 0 es)))
(define ((score c assignment [env '()]) formula)
(define extend-env (λ (sym val env) (cons (cons sym val) env)))
(let ([env (if (empty? env) (hash->list assignment) env)])
(match formula
[`⊤ 1]
[`⊥ 0]
[`(let (,bindings ...) ,body)
(define new-env
(foldl (λ (binding env)
(extend-env (car binding)
(eval (car (cdr binding)) assignment env)
env))
env
bindings))
((score c assignment new-env) body)]
[`(∨ ,es ...) (score-seq es (score c assignment env) max)]
[`(∧ ,es ...) (/ (score-seq es (score c assignment env) +) (length es))]
[`(¬ (= ,op1 ,op2)) (score2 op1 op2 assignment env score/≠)]
[`(= ,op1 ,op2) (score2 op1 op2 assignment env (score/= c))]
[`(¬ (bvult ,op1 ,op2)) (score2 op1 op2 assignment env (score/bv≥ c))]
[`(bvult ,op1 ,op2) (score2 op1 op2 assignment env (score/bv< c))]
[`(¬ (fp.lt ,op1 ,op2)) (score2 op1 op2 assignment env (score/fp!lt c))]
[`(fp.lt ,op1 ,op2) (score2 op1 op2 assignment env (score/fplt c))]
[`(¬ (fp.leq ,op1 ,op2)) (score2 op1 op2 assignment env (score/fp!leq c))]
[`(fp.leq ,op1 ,op2) (score2 op1 op2 assignment env (score/fpleq c))]
[`(¬ (fp.gt ,op1 ,op2)) (score2 op1 op2 assignment env (score/fp!gt c))]
[`(fp.gt ,op1 ,op2) (score2 op1 op2 assignment (score/fpgt c))]
[`(¬ (fp.geq ,op1 ,op2)) (score2 op1 op2 assignment env (score/fp!geq c))]
[`(fp.geq ,op1 ,op2) (score2 op1 op2 assignment env (score/fpgeq c))]
[`(¬ (fp.eq ,op1 ,op2)) (score2 op1 op2 assignment env score/fp!eq)]
[`(fp.eq ,op1 ,op2) (score2 op1 op2 assignment env (score/fpeq c))]
[`(¬ ,b) (score-bool! (eval b assignment env))]
[else (score-bool (eval formula assignment env))])))
;(define assignment (hash-set (hash-set (make-immutable-hash) "a" (mkBV 3 1)) "b" (mkBV 3 2)))