Source file invariants.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
open Shared_ast
open Ast
open Catala_utils
type invariant_status = Fail | Pass | Ignore
type invariant_expr = decl_ctx -> typed expr -> invariant_status
let check_invariant (inv : string * invariant_expr) (p : typed program) : bool =
let result = ref true in
let name, inv = inv in
let total = ref 0 in
let ok = ref 0 in
let p' =
Program.map_exprs p ~varf:Fun.id ~f:(fun e ->
let rec f e =
let r =
match inv p.decl_ctx e with
| Ignore -> true
| Fail ->
Message.raise_spanned_error (Expr.pos e)
"@[<v 2>Invariant @{<magenta>%s@} failed.@,%a@]" name
(Print.expr ()) e
| Pass ->
incr ok;
incr total;
true
in
Expr.map_gather e ~acc:r ~join:( && ) ~f
in
let res, e' = f e in
result := res && !result;
e')
in
assert (Bindlib.free_vars p' = Bindlib.empty_ctxt);
Message.emit_result "Invariant %s checked.@ result: [%d/%d]" name !ok !total;
!result
let invariant_default_no_arrow () : string * invariant_expr =
( "default_no_arrow",
fun _ctx e ->
match Mark.remove e with
| EDefault _ -> begin
match Mark.remove (Expr.ty e) with TArrow _ -> Fail | _ -> Pass
end
| _ -> Ignore )
let invariant_no_partial_evaluation () : string * invariant_expr =
( "no_partial_evaluation",
fun _ctx e ->
match Mark.remove e with
| EApp { f; args; _ } -> (
match Mark.remove (Expr.ty f) with
| TArrow (tl, _) when List.length args = List.length tl -> Pass
| _ -> Fail)
| _ -> Ignore )
let invariant_no_return_a_function () : string * invariant_expr =
( "no_return_a_function",
fun _ctx e ->
match Mark.remove e with
| EAbs _ -> begin
match Mark.remove (Expr.ty e) with
| TArrow (_, (TArrow _, _)) -> Fail
| _ -> Pass
end
| _ -> Ignore )
let invariant_app_inversion () : string * invariant_expr =
( "app_inversion",
fun _ctx e ->
match Mark.remove e with
| EApp { f = EAbs { binder; _ }, _; args; _ } ->
if Bindlib.mbinder_arity binder = List.length args then Pass else Fail
| EApp { f = EVar _, _; _ } -> Pass
| EApp { f = EStructAccess _, _; _ } -> Pass
| EApp { f = EExternal _, _; _ } -> Pass
| EApp _ -> Fail
| _ -> Ignore )
(** the arity of constructors when matching is always one. *)
let invariant_match_inversion () : string * invariant_expr =
( "match_inversion",
fun _ctx e ->
match Mark.remove e with
| EMatch { cases; _ } ->
if
EnumConstructor.Map.for_all
(fun _ case ->
match Mark.remove case with
| EAbs { binder; _ } -> Bindlib.mbinder_arity binder = 1
| _ -> false)
cases
then Pass
else Fail
| _ -> Ignore )
let rec check_typ_no_default ctx ty =
match Mark.remove ty with
| TLit _ -> true
| TTuple ts -> List.for_all (check_typ_no_default ctx) ts
| TStruct n ->
let s = StructName.Map.find n ctx.ctx_structs in
StructField.Map.for_all (fun _k ty -> check_typ_no_default ctx ty) s
| TEnum n ->
let s = EnumName.Map.find n ctx.ctx_enums in
EnumConstructor.Map.for_all (fun _k ty -> check_typ_no_default ctx ty) s
| TOption ty -> check_typ_no_default ctx ty
| TArrow (args, res) ->
List.for_all (check_typ_no_default ctx) args && check_typ_no_default ctx res
| TArray ty -> check_typ_no_default ctx ty
| TDefault _t -> false
| TAny ->
Message.raise_internal_error
"Some Dcalc invariants are invalid: TAny was found whereas it should be \
fully resolved."
| TClosureEnv ->
Message.raise_internal_error
"Some Dcalc invariants are invalid: TClosureEnv was found whereas it \
should only appear later in the compilation process."
let check_type_thunked_or_nodefault ctx ty =
check_typ_no_default ctx ty
||
match Mark.remove ty with
| TArrow (args, res) -> (
List.for_all (check_typ_no_default ctx) args
&&
match Mark.remove res with
| TDefault ty -> check_typ_no_default ctx ty
| _ -> check_typ_no_default ctx ty)
| _ -> false
let check_type_root ctx ty =
check_type_thunked_or_nodefault ctx ty
||
match Mark.remove ty with
| TStruct n ->
let s = StructName.Map.find n ctx.ctx_structs in
ScopeName.Map.exists
(fun _k info -> StructName.equal info.in_struct_name n)
ctx.ctx_scopes
&& StructField.Map.for_all
(fun _k ty -> check_type_thunked_or_nodefault ctx ty)
s
| TArrow ([(TStruct n, _)], res) ->
let s = StructName.Map.find n ctx.ctx_structs in
ScopeName.Map.exists
(fun _k info -> StructName.equal info.in_struct_name n)
ctx.ctx_scopes
&& StructField.Map.for_all
(fun _k ty -> check_type_thunked_or_nodefault ctx ty)
s
&& check_typ_no_default ctx res
| TDefault arg -> check_typ_no_default ctx arg
| _ -> false
let invariant_typing_defaults () : string * invariant_expr =
( "typing_defaults",
fun ctx e ->
if check_type_root ctx (Expr.ty e) then Pass
else (
Message.emit_warning "typing error %a@." (Print.typ ctx) (Expr.ty e);
Fail) )
let check_all_invariants prgm =
List.fold_left ( && ) true
[
check_invariant (invariant_default_no_arrow ()) prgm;
check_invariant (invariant_no_partial_evaluation ()) prgm;
check_invariant (invariant_no_return_a_function ()) prgm;
check_invariant (invariant_app_inversion ()) prgm;
check_invariant (invariant_match_inversion ()) prgm;
check_invariant (invariant_typing_defaults ()) prgm;
]