-
Notifications
You must be signed in to change notification settings - Fork 16
/
base.ml
315 lines (262 loc) · 9.78 KB
/
base.ml
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
302
303
304
305
306
307
308
309
310
311
312
313
314
open Dolmen
module Ast = Dolmen.Term
(* Merging builtin parser functions *)
(* ************************************************************************ *)
let noop _ _ _ _ = None
let rec merge l env ast s args =
match l with
| [] -> None
| f :: r ->
begin match f env ast s args with
| (Some _) as ret -> ret
| None -> merge r env ast s args
end
(* Smtlib logic merging *)
exception Unknown_logic of string
let smtlib_logic
~arrays ~bv ~core ~ints ~reals ~reals_ints = function
| "AUFLIA" -> merge [core; ints; arrays]
| "AUFLIRA" -> merge [core; reals_ints; arrays]
| "AUFNIRA" -> merge [core; reals_ints; arrays]
| "LIA" -> merge [core; ints]
| "LRA" -> merge [core; reals]
| "QF_ABV" -> merge [core; bv; arrays]
| "QF_AUFBV" -> merge [core; bv; arrays]
| "QF_AUFLIA" -> merge [core; ints; arrays]
| "QF_AX" -> merge [core; arrays]
| "QF_BV" -> merge [core; bv]
| "QF_IDL" -> merge [core; ints]
| "QF_LIA" -> merge [core; ints]
| "QF_NIA" -> merge [core; ints]
| "QF_NRA" -> merge [core; reals]
| "QF_RDL" -> merge [core; reals]
| "QF_UF" -> merge [core]
| "QF_UFBV" -> merge [core; bv]
| "QF_UFIDL" -> merge [core; ints]
| "QF_UFLIA" -> merge [core; ints]
| "QF_UFLRA" -> merge [core; reals]
| "QF_LRA" -> merge [core; reals]
| "QF_UFNRA" -> merge [core; reals]
| "UFLIA" -> merge [core; ints]
| "UFLRA" -> merge [core; reals]
| "UFNIA" -> merge [core; ints]
| "UFNRA" -> merge [core; reals]
| logic -> raise (Unknown_logic logic)
(* Building builtins parser functions *)
(* ************************************************************************ *)
type ('env, 'args, 'ret) helper =
(module Tff_intf.S with type env = 'env) ->
'env -> Dolmen.Term.t -> string -> Dolmen.Term.t list ->
('args -> 'ret) -> 'ret option
let make_op0
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
match args with
| [] -> Some (ret ())
| _ ->
let err = Type.Bad_op_arity (op, 0, List.length args) in
raise (Type.Typing_error (err, env, ast))
let make_op1
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
match args with
| [t1] -> Some (ret t1)
| _ ->
let err = Type.Bad_op_arity (op, 1, List.length args) in
raise (Type.Typing_error (err, env, ast))
let make_op2
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
match args with
| [t1; t2] -> Some (ret (t1, t2))
| _ ->
let err = Type.Bad_op_arity (op, 2, List.length args) in
raise (Type.Typing_error (err, env, ast))
let make_op3
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
match args with
| [t1; t2; t3] -> Some (ret (t1, t2, t3))
| _ ->
let err = Type.Bad_op_arity (op, 3, List.length args) in
raise (Type.Typing_error (err, env, ast))
let make_opn n
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
let l = List.length args in
if l = n then
Some (ret args)
else begin
let err = Type.Bad_op_arity (op, 1, List.length args) in
raise (Type.Typing_error (err, env, ast))
end
let make_assoc
(type env) (module Type: Tff_intf.S with type env = env)
env ast op args ret =
match args with
| [] | [_] ->
let err = Type.Bad_op_arity (op, 2, List.length args) in
raise (Type.Typing_error (err, env, ast))
| _ -> Some (ret args)
let fold_left_assoc mk = function
| h :: r -> List.fold_left mk h r
| _ -> raise (Invalid_argument "Base.fold_left_assoc")
let rec fold_right_assoc mk = function
| [x] -> x
| h :: r -> mk h (fold_right_assoc mk r)
| _ -> raise (Invalid_argument "Base.fold_right_assoc")
let make_chain = make_assoc
let map_chain
(type t) (module Type: Tff_intf.S with type T.t = t) mk args =
let rec aux mk = function
| [] -> assert false
| [_] -> []
| x :: ((y :: _) as r) -> mk x y :: aux mk r
in
Type.T._and (aux mk args)
(* Alt-ergo builtins *)
(* ************************************************************************ *)
module Ae = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Ae_Base with type t = Type.Ty.t)
(T : Dolmen.Intf.Term.Ae_Base with type t = Type.T.t) = struct
let parse env ast s args =
match s with
| Type.Builtin Term.Bool ->
make_op0 (module Type) env ast "bool" args
(fun () -> Type.Ty Ty.bool)
| Type.Builtin Term.Unit ->
make_op0 (module Type) env ast "unit" args
(fun () -> Type.Ty Ty.unit)
| Type.Builtin Term.Void ->
make_op0 (module Type) env ast "void" args
(fun () -> Type.Term T.void)
| _ -> None
end
end
(* TPTP builtins ($i, $o, etc..) *)
(* ************************************************************************ *)
module Tptp = struct
module Tff
(Type : Tff_intf.S)
(Ty : Dolmen.Intf.Ty.Tptp_Base with type t = Type.Ty.t)
(T : Dolmen.Intf.Term.Tptp_Base with type t = Type.T.t) = struct
let parse env ast s args =
match s with
(*
| Type.Id ({ Id.name = "$_"; ns = Id.Term } as id) ->
Some (Type.wildcard env ast id args)
*)
| Type.Id { Id.name = "$tType"; ns = Id.Term } ->
make_op0 (module Type) env ast "$tType" args
(fun () -> Type.Ttype)
| Type.Id { Id.name = "$o"; ns = Id.Term } ->
make_op0 (module Type) env ast "$o" args
(fun () -> (Type.Ty Ty.prop))
| Type.Id { Id.name = "$i"; ns = Id.Term } ->
make_op0 (module Type) env ast "$i" args
(fun () -> (Type.Ty Ty.base))
| Type.Id { Id.name = "$true"; ns = Id.Term } ->
make_op0 (module Type) env ast "$true" args
(fun () -> Type.Term T._true)
| Type.Id { Id.name = "$false"; ns = Id.Term } ->
make_op0 (module Type) env ast "$false" args
(fun () -> Type.Term T._false)
| Type.Id id when Id.equal id Id.tptp_role ->
Some (Type.Tags [])
| _ -> None
end
end
(* Smtlib builtins (bool, =, etc...) *)
(* ************************************************************************ *)
module Smtlib = struct
module Tff
(Type : Tff_intf.S)
(Tag : Dolmen.Intf.Tag.Smtlib_Base with type 'a t = 'a Type.Tag.t)
(Ty : Dolmen.Intf.Ty.Smtlib_Base with type t = Type.Ty.t)
(T : Dolmen.Intf.Term.Smtlib_Base with type t = Type.T.t) = struct
let app_left env ast args name mk =
make_assoc (module Type) env ast name args
(fun l -> Type.Term (fold_left_assoc mk (List.map (Type.parse_term env) l)))
let app_right env ast args name mk =
make_assoc (module Type) env ast name args
(fun l -> Type.Term (fold_right_assoc mk (List.map (Type.parse_term env) l)))
let parse_f env ast cstr args =
let loc = Term.(ast.loc) in
let t = Term.apply ?loc cstr args in
Type.Term (Type.parse_term env t)
let parse env ast s args =
match s with
(* Bool sort and constants *)
| Type.Id { Id.name = "Bool"; ns = Id.Sort } ->
make_op0 (module Type) env ast "Bool" args
(fun () -> (Type.Ty Ty.prop))
| Type.Id { Id.name = "true"; ns = Id.Term } ->
make_op0 (module Type) env ast "true" args
(fun () -> (Type.Term Type.T._true))
| Type.Id { Id.name = "false"; ns = Id.Term } ->
make_op0 (module Type) env ast "false" args
(fun () -> (Type.Term Type.T._false))
(* Boolean operators *)
| Type.Id { Id.name = "not"; ns = Id.Term } ->
make_op1 (module Type) env ast "not" args
(fun t -> Type.Term (Type.T.neg (Type.parse_term env t)))
| Type.Id { Id.name = "and"; ns = Id.Term } ->
Some (parse_f env ast (Term.and_t ()) args)
| Type.Id { Id.name = "or"; ns = Id.Term } ->
Some (parse_f env ast (Term.or_t ()) args)
| Type.Id { Id.name = "xor"; ns = Id.Term } ->
app_left env ast args "xor" Type.T.xor
| Type.Id { Id.name = "=>"; ns = Id.Term } ->
app_right env ast args "=>" Type.T.imply
(* If-then-else *)
| Type.Id { Id.name = "ite"; ns = Id.Term } ->
make_op3 (module Type) env ast "ite" args
(fun (c, a, b) ->
let loc = ast.Term.loc in
let ast = Term.ite ?loc c a b in
Type.Term (Type.parse_term env ast))
(* Equality *)
| Type.Id { Id.name = "distinct"; ns = Id.Term } ->
Some (parse_f env ast (Term.neq_t ()) args)
| Type.Id { Id.name = "="; ns = Id.Term } ->
let l = List.map (Type.parse_term env) args in
Some (Type.Term (T.eqs l))
(* Rewrite rules *)
| Type.Id id when Id.equal id Id.rwrt_rule ->
Some (Type.Tags [Type.Any (Tag.rwrt, ())])
| _ -> None
end
end
(* Zipperposition builtins *)
(* ************************************************************************ *)
module Zf = struct
module Tff
(Type : Tff_intf.S)
(Tag : Dolmen.Intf.Tag.Zf_Base with type 'a t = 'a Type.Tag.t) = struct
let parse _env _ast s args =
match s with
| Type.Id id when Id.equal id Id.rwrt_rule ->
Some (Type.Tags [Type.Any (Tag.rwrt, ())])
| Type.Id { Id.name = "infix"; ns = Id.Term } ->
begin match args with
| [ { Term.term = Term.Symbol { Id.name; _ }; _ } ] ->
Some (Type.Tags [
Type.Any (Tag.name, Tag.exact name);
Type.Any (Tag.pos, Tag.infix);
])
| _ -> assert false
end
| Type.Id { Id.name = "prefix"; ns = Id.Term } ->
begin match args with
| [ { Term.term = Term.Symbol { Id.name; _ }; _ } ] ->
Some (Type.Tags [
Type.Any (Tag.name, Tag.exact name);
Type.Any (Tag.pos, Tag.prefix);
])
| _ -> assert false
end
| _ -> None
end
end