-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTests.hs
301 lines (173 loc) · 6.32 KB
/
Tests.hs
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{-# LANGUAGE GADTs,RebindableSyntax,CPP,FlexibleContexts,FlexibleInstances,ConstraintKinds #-}
{-# OPTIONS_GHC -dcore-lint #-}
{-
- The idea of this test suite is that it should be compiled
- with the -fplugin=Herbie and -dcore-lint flags.
- Then we check to make sure GHC didn't throw any errors during
- the core type checking process.
-}
module Main
where
import SubHask
--------------------------------------------------------------------------------
-- This section tests that Herbie obeys the code annotations
{-# ANN ann "NoHerbie" #-}
ann :: Float -> Float
ann x = x+x+x*x*x*x
noann :: Float -> Float
noann x = x+x+x*x*x*x
--------------------------------------------------------------------------------
-- This section tests that Herbie gets run on the correct types.
-- Herbie should be run on all the functions below.
#define f1(x) (sqrt ((x)+1) - sqrt (x))
herbie1 :: Real a => a -> a
herbie1 x = f1(x)
herbie2 :: Real a => a -> a -> a -> a -> a
herbie2 a b c d = f1(a)+f1(b)+f1(c)+f1(d)
herbie3 :: Float -> Float
herbie3 x = f1(x)
herbie4 :: String -> String
herbie4 str = show $ f1(x1)
where
x1 = fromIntegral (length str) :: Float
herbie5 :: (Show a, Real a) => String -> a -> String
herbie5 str x1 = show $ f1(x1)
herbie6 :: (Show a, Real a) => a -> String -> String
herbie6 x1 str = show $ f1(x1)
herbie7 :: Semigroup a => a -> a
herbie7 x1 = x1+x1+x1+x1+x1
herbie8 :: Float -> Float
herbie8 x1 = case x1 of
1.0 -> f1(x1)
2.0 -> x1
herbie9 :: Float -> Float
herbie9 x1 = go 4 x1
where
go :: Float -> Float -> Float
go 0 b = b
go a b = go (a-1) (sqrt (b-1))
herbie10 :: String -> String
herbie10 str = show ( sqrt (1+fromIntegral (length str))
- sqrt (fromIntegral (length str))
:: Float
)
-- Herbie should not get run on any of the functions in this section.
#define f2(a,b) a+b*(a+b*a)+a*b
noherbie1 :: String -> String
noherbie1 x = x++"hello world"
noherbie2 :: Rational -> Rational -> Rational
noherbie2 a b = f2(a,b)
noherbie3 :: Int -> Int -> Int
noherbie3 a b = f2(a,b)
noherbie4 :: x -> Int -> Int -> Int
noherbie4 x a b = f2(a,b)
--------------------------------------------------------------------------------
-- Herbie shouldn't process these because the expression size is too small.
-- We're unlikely to get any benefit, and it might take a long time.
toosmall1 :: Float -> Float
toosmall1 a = a+a
toosmall2 :: Float -> Float -> Float -> Float
toosmall2 a b c = a+b*c
-- These are big enough and should get processed
bigenough1 :: Float -> Float
bigenough1 a = a+a*a
bigenough2 :: Float -> Float -> Float -> Float
bigenough2 a b c = a+b*(c+a)
bigenough3 :: Float -> Float -> Float -> Float
bigenough3 a b c = f1(c)
--------------------------------------------------------------------------------
-- This section contains lots of examples of expressions that the Herbie plugin can parse
-- and find improved versions.
example1 x1 x2 = sqrt (x1*x1 + x2*x2)
example2 x = exp(log(x)+8)
example3 x = sqrt(x*x +1) -1
example4 x = exp(x)-1
example5 x = log(1+x)
example6 x y = sqrt(x+ y) - sqrt(y)
example7 k r a = k*(r-a)^3
example8 k r a = k*(r-a)^2
example9 x y = sin(x - y)
example10 p1x p2x p1y p2y = sqrt((p1x - p2x) * (p1x - p2x) + (p1y - p2y) * (p1y - p2y))
example11 x = sin(x)-x
example12 x = 1-cos(x)
example13 x1 x2 = sqrt((x1 - x2) * (x1 - x2))
example14 x y z = sqrt(x*x + y*y + z*z)
example15 x y z c = sqrt(x*x + y*y + z*z)/c
example16 tdx dx tdy dy = (tdx * dx + tdy * dy) / (dx * dx + dy * dy)
example17 tdx dx tdy dy sl2 = (tdx * dx + tdy * dy) / sl2
example18 x = (x + 0.1)-x
example19 x = log(x) - sin(x+1)
example20 a b = exp(1+log(a) + log(b))
example21 x = (1+sqrt(x-1))/(x-1)^2
example22 x = (1+sqrt(x))/(x-1)^2
example23 a b c d e f = a+b+(((d-c)*(d-c))*e*f/(e+f))
example24 q = sqrt(q*(q-1))
example25 a = sqrt(a^2-1)
example26 a b c d = ((a*b)+(c*d))/(a+c)
example27 x = sqrt(x^2)
example28 x y = sqrt(x) * y * y
example29 x y z = sqrt(x*x+y*y+z*z)
example30 x y = 1.75 * x * y*y + sqrt(x/y)
example31 x = exp(3*log(x)+2)
example32 x = exp(2*log(x))
example33 x = sqrt(1/x + 1) - sqrt(1/x)
example34 left i right count = left + i * ((left - right) / count)
example35 left right count = left + count * ((left - right) / count)
example36 x y = sqrt(x*x) - sqrt(y*y)
example37 x = log(x+1)-log(x)
example38 x = log(x+1)^x
example39 minval minstep val = (minval/minstep + val) * minstep
example40 x = x*x*cos(x/2 - sqrt(x))
example41 x = sqrt(4+x^2+x)
example42 x y z = x / sqrt(x*x + y*y + z*z)
example43 x = sin(sqrt(x+1))
example44 x = sqrt(x-2)-sqrt(x*x-3)
example45 x = (sin(x) - tan(x)) / x
example46 x y = 1 / sqrt(x^2 - y^2)
example47 x1 x2 = sqrt((x1 - x2)^2)
example48 x = x - sin(x)
example49 x = sqrt(x + 1) - 1 + x
example50 a b c = (a*a - c*c)/b
example51 x y = sin(x+y)-cos(x+y)
example52 x = (x + 1)^2 - 1
example53 x = sqrt(1+x) - sqrt(x)
example54 x = sqrt(x + 1) / (x*x)
example55 x = sqrt(x^2 / 3)
example56 a b = 100*(a-b)/a
example57 x = abs(x^3)-x^3
example58 x = log(x) - log(x+1)
example59 x = 1/x - 1/(x+1)
example60 a b c = -b + sqrt(b*b-4*a*c)/(2*a)
example61 a c an cn = log(exp(a)*an + exp(c)*cn) - log(an+cn)
example62 x = sqrt(sin(x)) - sqrt(x)
example63 x = log(1+x)
example64 a b = a * b / (1 - b + a * b)
example65 a b = b*sqrt(a * a + 1.0)
example65' a = sqrt(a * a + 1.0)
example66 x y = x * y * x*pi/y
example67 x = sqrt(x + 1) - sqrt(x - 1)
example68 x = cos(x + 1) * x^2
example69 a b = b*(a/b - log(1 + a/b))
example70 a b = b*(a/b - 1 - log(a/b))
example71 x = (6/(x^99))*(x^101)
example72 x = (1/(x^99))*(x^101)
example73 x = (1/(x^100))*(x^100)
example74 x y z = cos(sqrt(x*x+y*y+z*z))
example75 x = sqrt(sqrt(x*x+1)+1)
example76 a k = a + sqrt(a*a-k)
example77 a k = -a - sqrt(a*a-k)
example78 a b x = x*x*a+x*(a+b) +x*b
example79 x = (x + x) ^ 3 / x
example80 x = sqrt(x+1)-sqrt 1
example81 x = (x+1)-x
example82 x = sqrt(x+100)-sqrt(x)
example83 x = 1-cos(x)
example84 u v = sqrt(sqrt(u^2 + v^2) - u)
example85 x = exp(log(x))
example86 x = sqrt(x + 1) - sqrt x + sin(x - 1)
example87 x = exp x / sqrt(exp x - 1) * sqrt x
example88 x = (exp(x) - 1) / x
example89 x = sqrt(x + 2) - sqrt(x)
--------------------------------------------------------------------------------
-- The main function does nothing
main = return ()