-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInit.lean
More file actions
219 lines (183 loc) · 6.09 KB
/
Copy pathInit.lean
File metadata and controls
219 lines (183 loc) · 6.09 KB
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
import Lean.Server.Utils
import cvc5
namespace cvc5
namespace Test
/-! ## A few sanity tests -/
example [Monad m] : MonadLiftT m (EnvT m) := inferInstance
example : MonadLift IO Env := inferInstance
example : MonadLift BaseIO Env := inferInstance
/-! ## Helpers for the rest of the tests -/
def IO.run : IO Unit → IO Unit :=
id
def fail {α : outParam Type} (msg : String) : IO α :=
IO.throwServerError msg
protected def pref (hint : String) : String :=
if hint.isEmpty then "" else "[" ++ hint ++ "] "
def assertEq [ToString α] [BEq α] (lft rgt : α) (hint := "") : IO Unit := do
if lft != rgt then
IO.eprintln s!"{Test.pref hint}comparison failed: `{lft}` is different from `{rgt}`"
fail "assertion failed"
def assertTrue (b : Bool) (hint := "") : IO Unit :=
assertEq true b hint
def assertFalse (b : Bool) (hint := "") : IO Unit :=
assertEq false b hint
def assertNone [ToString α] (a? : Option α) (hint := "") : IO Unit := do
if let some a := a? then
IO.eprintln s!"{Test.pref hint}expected none, got {a}"
fail "assertion failed"
def assertSome (a? : Option α) (hint := "") : IO α := do
let some a := a?
| IO.eprintln s!"{Test.pref hint}expected non-none value, got none"
fail "assertion failed"
return a
def assertSomeDiscard (a? : Option α) (hint := "") : IO Unit := do
let _ ← assertSome a? (hint := hint)
def assertNe [ToString α] [BEq α] (lft rgt : α) (hint := "") : IO Unit := do
if lft == rgt then
IO.eprintln s!"{Test.pref hint}comparison failed: `{lft}` is the same as `{rgt}`"
fail "assertion failed"
def assertOk
(code : Env α)
(hint : String := "")
: Env α := do
try code catch e =>
IO.eprintln s!"{Test.pref hint}expected `.ok` result, got `{e}`"
fail "assertion failed"
def assertOkDiscard
(result : Env α)
(hint : String := "")
: Env Unit := do
let _ ← assertOk result hint
return ()
def assertAnyError
(expected : String)
(code : Env α)
(errorDo : Error → Env Unit := fun _ => return ())
(hint : String := "")
: Env Unit := do
try
let _ ← code
IO.eprintln s!"{Test.pref hint}expected {expected}, got `.ok` result"
fail "assertion failed"
catch e => errorDo e
def assertError
(expected : String)
(code : Env α)
(hint : String := "")
: Env Unit :=
assertAnyError expected
code
(hint := hint)
fun
| .error err => do
if err.trimAscii == expected.trimAscii then
return ()
else
IO.eprintln s!"{Test.pref hint}expected cvc5 error `{expected}`, got cvc5 error `{err}`"
fail "test failed"
| .recoverable err => do
IO.eprintln s!"{Test.pref hint}expected error `{expected}`, got recoverable error `{err}`"
fail "test failed"
| .unsupported err => do
IO.eprintln s!"{Test.pref hint}expected error `{expected}`, got unsupported error `{err}`"
fail "test failed"
| .option err => do
IO.eprintln s!"{Test.pref hint}expected error `{expected}`, got option error `{err}`"
fail "test failed"
| .missingValue => do
IO.eprintln s!"{Test.pref hint}expected cvc5 error `{expected}`, got missing value error"
fail "test failed"
end Test
namespace Result
def toOption (res : Result) : Option Bool :=
if res.isSat then true
else if res.isUnsat then false
else none
end Result
namespace Solver variable (solver : Solver)
variable [Monad m]
def checkSat? : Env (Option Bool) :=
Result.toOption <$> solver.checkSat
def checkSatAssuming? (terms : Array Term) : Env (Option Bool) :=
Result.toOption <$> solver.checkSatAssuming terms
end Solver
namespace Test
scoped syntax
docComment ?
"test! " ("[" declId ", " declId "] ")? (ident (ident)? " => ")? term : command
scoped syntax
docComment ?
"test? " ("[" declId ", " declId "] ")? (ident (ident)? " => ")? term : command
macro_rules
| `(command|
$[ $outputComment:docComment ]?
test! $[ [ $fileId:ident , $testId:ident ] ]?
$[$tmIdent?:ident $[$solverIdent?:ident]? =>]?
$code:term
) => do
let errPrefStrLit :=
match (fileId, testId) with
| (some fileId, some testId) =>
Lean.Syntax.mkStrLit s!"[{fileId}.{testId}] test failed:\n"
| _ => Lean.Syntax.mkStrLit "test failed"
let runFun ← `(term| cvc5.Env.run)
let toRun ←
if let some tmIdent := tmIdent? then
let boolIdent := `bool |> Lean.mkIdent
let intIdent := `int |> Lean.mkIdent
let realIdent := `real |> Lean.mkIdent
let uninterpretedIdent := `uninterpreted |> Lean.mkIdent
if let some (some solverIdent) := solverIdent? then
`(do
let $tmIdent:ident ← TermManager.new
let $solverIdent:ident ← Solver.new $tmIdent
let $boolIdent ← $tmIdent:ident |>.getBooleanSort
let $intIdent ← $tmIdent:ident |>.getIntegerSort
let $realIdent ← $tmIdent:ident |>.getRealSort
let $uninterpretedIdent ← $tmIdent:ident |>.mkUninterpretedSort "u"
$code:term
)
else
`(do
let $tmIdent:ident ← TermManager.new
$code:term
)
else `(do $code:term)
`(
$[ $outputComment:docComment ]?
#guard_msgs in #eval IO.run do
let res ← $runFun:term $toRun
match res with
| .ok res => return res
| .error e =>
IO.eprintln ($errPrefStrLit ++ (toString e))
return default
)
| `(command|
$[ $_outputComment:docComment ]?
test? $[ [ $fileId:ident, $testId:ident ] ]? $[$tmIdent?:ident =>]? $code:term
) => do
let errPrefStrLit :=
match (fileId, testId) with
| (some fileId, some testId) =>
Lean.Syntax.mkStrLit s!"[{fileId}.{testId}] test failed:\n"
| _ => Lean.Syntax.mkStrLit "test failed"
let runFun ← `(term| cvc5.Env.run)
let mut code ←
if let some tmIdent := tmIdent? then
`(doSeq|
let $tmIdent:ident ← TermManager.new
$code:term
)
else `(doSeq| $code:term)
`(
#eval IO.run do
let res ← $runFun:term do $code
match res with
| .ok res => return res
| .error e =>
IO.eprintln ($errPrefStrLit ++ (toString e))
return default
)
end Test
end cvc5