-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathvalid.ml
More file actions
769 lines (689 loc) · 23.6 KB
/
valid.ml
File metadata and controls
769 lines (689 loc) · 23.6 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
open Util
open Source
open Il.Ast
open Ast
open Xl
open Al_util
open Print
open Free
module IlEval = Il.Eval
(* Error *)
type source = string phrase
let error at msg = Error.error at "AL validation" msg
let error_valid error_kind source msg =
error source.at
(Printf.sprintf "%s when validating `%s`\n %s" error_kind source.it msg)
let error_mismatch source typ1 typ2 =
error_valid "type mismatch" source
(Printf.sprintf "%s =/= %s"
(Il.Print.string_of_typ typ1)
(Il.Print.string_of_typ typ2)
)
let error_field source typ field =
error_valid "unknown field" source
(Printf.sprintf "%s ∉ %s" (string_of_atom field) (Il.Print.string_of_typ typ))
let error_struct source typ =
error_valid "invalid struct type" source (Il.Print.string_of_typ typ)
let error_tuple source typ =
error_valid "invalid tuple type" source (Il.Print.string_of_typ typ)
let error_case source typ =
error_valid "invalid case type" source (Il.Print.string_of_typ typ)
let (let*) = Option.bind
module Env = struct
include Map.Make(String)
type t = expr option Map.Make(String).t
(* TODO: pass env *)
let add_bound_var id env = add id None env
let add_bound_vars expr = IdSet.fold add_bound_var (free_expr expr)
let add_bound_param arg env =
match arg.it with
| ExpA e -> add_bound_vars e env
| TypA _ | DefA _ -> env
let add_subst lhs rhs env =
let open Eval in
match get_subst lhs rhs Subst.empty with
| None ->
IdSet.fold (fun id env -> add_bound_var id env) (Free.free_expr lhs) env
| Some subst ->
subst
|> Subst.map Option.some
|> union (fun _ _ _ -> (* TODO *) assert (false)) env
let add id expr = add id (Some expr)
end
(* Type Env *)
module IlEnv = Il.Env
let il_env: IlEnv.t ref = ref IlEnv.empty
let varT s = VarT (s $ no_region, []) $ no_region
let rec is_trivial_mixop = function
| Mixop.Arg () -> true
| Mixop.Seq mixops -> List.for_all is_trivial_mixop mixops
| _ -> false
(* Subtyping *)
let get_deftyps (id: Il.Ast.id) (args: Il.Ast.arg list): deftyp list =
match IlEnv.find_opt_typ !il_env id with
| Some (_, insts) ->
let typ_of_arg arg =
match (IlEval.reduce_arg !il_env arg).it with
| ExpA { it=SubE (_, typ, _); _ } -> typ
| ExpA { note; _ } -> note
| TypA typ -> typ
| a -> failwith ("TODO: " ^ Il.Print.string_of_arg (a $ arg.at))
in
let get_syntax_arg_name arg =
match arg.it with
| Il.Ast.TypA { it=VarT (id, []); _ } -> Some id
| _ -> None
in
let subst_syntax_arg subst arg inst_arg =
let name = get_syntax_arg_name inst_arg in
if Option.is_some name then
let name = Option.get name in
Il.Subst.add_typid subst name (typ_of_arg arg)
else
subst
in
let subst_inst inst =
let InstD (binds, inst_args, deftyp) = inst.it in
let subst = List.fold_left2 subst_syntax_arg Il.Subst.empty args inst_args in
let new_args = Il.Subst.subst_args subst inst_args in
let new_deftyp = Il.Subst.subst_deftyp subst deftyp in
{ inst with it = InstD (binds, new_args, new_deftyp) }
in
let insts = List.map subst_inst insts in
let get_deftyp inst =
let InstD (_, inst_args, deftyp) = inst.it in
let valid_arg arg inst_arg =
IlEval.sub_typ !il_env (typ_of_arg arg) (typ_of_arg inst_arg)
in
if List.for_all2 valid_arg args inst_args then
Some deftyp
else
None
in
(match List.find_map get_deftyp insts with
| Some deftyp -> [deftyp]
| None ->
List.map (fun inst -> let InstD (_, _, deftyp) = inst.it in deftyp) insts
)
| None -> []
let rec unify_deftyp_opt (deftyp: deftyp) : typ option =
match deftyp.it with
| AliasT typ -> Some typ
| StructT _ -> None
| VariantT typcases
when List.for_all (fun (mixop', _, _) -> is_trivial_mixop mixop') typcases ->
typcases |> List.map (fun (_mixop, (typ, _qs, _ps), _hs) -> typ) |> unify_typs_opt
| _ -> None
and unify_deftyps_opt : deftyp list -> typ option = function
| [] -> None
| [deftyp] -> unify_deftyp_opt deftyp
| deftyp :: deftyps ->
let* typ1 = unify_deftyp_opt deftyp in
let* typ2 = unify_deftyps_opt deftyps in
unify_typ_opt typ1 typ2
and unify_typ_opt (typ1: typ) (typ2: typ) : typ option =
let typ1', typ2' = ground_typ_of typ1, ground_typ_of typ2 in
match typ1'.it, typ2'.it with
| VarT (id1, _), VarT (id2, _) when id1 = id2 -> Some typ1'
| BoolT, BoolT | NumT _, NumT _ | TextT, TextT -> Some typ1'
| TupT etl1, TupT etl2 ->
let* etl =
List.fold_right2
(fun et1 et2 acc ->
let* acc = acc in
let* res = unify_typ_opt (snd et1) (snd et2) in
Some ((fst et1, res) :: acc)
)
etl1
etl2
(Some [])
in
Some (TupT etl $ typ1.at)
| IterT (typ1'', iter), IterT (typ2'', _) ->
let* typ = unify_typ_opt typ1'' typ2'' in
Some (IterT (typ, iter) $ typ1.at)
| _ -> None
and unify_typs_opt : typ list -> typ option = function
| [] -> None
| [typ] -> Some typ
| typ :: typs' ->
let* unified_typ = unify_typs_opt typs' in
unify_typ_opt typ unified_typ
and ground_typ_of (typ: typ) : typ =
match typ.it with
| VarT (id, _) when IlEnv.mem_var !il_env id ->
let typ' = IlEnv.find_var !il_env id in
if Il.Eq.eq_typ typ typ' then typ else ground_typ_of typ'
(* NOTE: Consider `fN` as a `NumT` to prevent diverging ground type *)
| VarT (id, _) when id.it = "fN" -> NumT `RealT $ typ.at
| VarT (id, args) ->
get_deftyps id args
|> unify_deftyps_opt
|> Option.map ground_typ_of
|> Option.value ~default:typ
| TupT [_, typ'] -> ground_typ_of typ'
| IterT (typ', iter) -> IterT (ground_typ_of typ', iter) $ typ.at
| _ -> typ
let is_num typ =
match (ground_typ_of typ).it with
| NumT _ -> true
| _ -> false
let rec sub_typ typ1 typ2 =
try
let typ1', typ2' = ground_typ_of typ1, ground_typ_of typ2 in
match typ1'.it, typ2'.it with
| IterT (typ1'', _), IterT (typ2'', _) -> sub_typ typ1'' typ2''
| NumT _, NumT _ -> true
| _, _ -> IlEval.sub_typ !il_env typ1' typ2'
with Util.Error.Error (_, "undeclared type") -> false
let rec matches typ1 typ2 =
try
match (ground_typ_of typ1).it, (ground_typ_of typ2).it with
| IterT (typ1', _), IterT (typ2', _) -> matches typ1' typ2'
| VarT (id1, _), VarT (id2, _) when id1.it = id2.it -> true
| _ -> sub_typ typ1 typ2 || sub_typ typ2 typ1
with Util.Error.Error (_, "undeclared type") -> false
(* Helper functions *)
let get_base_typ typ =
match typ.it with
| IterT (typ', _) -> typ'
| _ -> typ
let unwrap_iter_typ typ =
match typ.it with
| IterT (typ', _) -> typ'
| _ -> assert (false)
let rec get_typfields_of_inst (inst: inst) : typfield list =
let InstD (_, _, dt) = inst.it in
match dt.it with
| StructT typfields -> typfields
| AliasT typ -> get_typfields typ
| VariantT [mixop, (typ, _, _), _] when is_trivial_mixop mixop ->
get_typfields typ
(* TODO: some variants of struct type *)
| VariantT _ -> []
and get_typfields (typ: typ) : typfield list =
match typ.it with
| VarT (id, _) when IlEnv.mem_typ !il_env id ->
let _, insts = IlEnv.find_typ !il_env id in
List.concat_map get_typfields_of_inst insts
| _ -> []
let check_match source typ1 typ2 =
if not (matches typ1 typ2) then error_mismatch source typ1 typ2
let check_num source typ =
if not (is_num typ) then error_mismatch source typ (varT "num")
let check_bool source typ =
match (get_base_typ typ).it with
| BoolT -> ()
| _ -> error_mismatch source typ (varT "bool")
let check_list source typ =
match typ.it with
| IterT (_, iter) when iter <> Opt -> ()
| _ -> error_mismatch source typ (varT "list")
let check_opt source typ =
match typ.it with
| IterT (_, Opt) -> ()
| _ -> error_mismatch source typ (varT "option")
(* TODO: use hint *)
let check_instr source typ =
let typ = get_base_typ typ in
if
not (sub_typ typ (varT "instr")) &&
not (sub_typ typ (varT "admininstr"))
then
error_mismatch source typ (varT "instr")
let check_val source typ =
if not (sub_typ (get_base_typ typ) (varT "val")) then
error_mismatch source typ (varT "val")
let check_evalctx source typ =
match typ.it with
| VarT (id, []) when id.it = "evalctx" -> ()
| _ -> error_mismatch source typ (varT "evalctx")
let check_field source source_typ expr_record typfield =
let atom, (typ, _, _), _ = typfield in
(* TODO: Use record api *)
let f e = e |> fst |> Atom.eq atom in
match List.find_opt f expr_record with
| Some (_, expr_ref) -> check_match source !expr_ref.note typ
| None -> error_field source source_typ atom
let check_struct source typ =
match typ.it with
| VarT (id, args) -> (
let deftyps = get_deftyps id args in
if not (List.exists (fun deftyp -> match deftyp.it with StructT _ -> true | _ -> false) deftyps) then
error_valid "not a struct" source (Il.Print.string_of_typ typ)
)
| _ -> error_valid "not a struct" source (Il.Print.string_of_typ typ)
let check_tuple source exprs typ =
match (ground_typ_of typ).it with
| TupT etl when List.length exprs = List.length etl ->
let f expr (_, typ) = check_match source expr.note typ in
List.iter2 f exprs etl
| _ -> error_tuple source typ
let check_call source id args result_typ =
match IlEnv.find_opt_def !il_env (id $ no_region) with
| Some (params, typ, _) ->
(* TODO: Use local il_environment *)
(* Store global il_enviroment *)
let global_il_env = !il_env in
let check_arg arg param =
match arg.it, param.it with
| ExpA expr, ExpP (_, typ') -> check_match source expr.note typ'
(* Add local variable typ *)
| TypA typ1, TypP id -> il_env := IlEnv.bind_var !il_env id typ1
| DefA aid, DefP (_, pparams, ptyp) ->
(match IlEnv.find_opt_def !il_env (aid $ no_region) with
| Some (aparams, atyp, _) ->
if not (IlEval.sub_typ !il_env atyp ptyp) then
error_valid
"argument's return type is not a subtype of parameter's return type"
source
(Printf.sprintf " %s !<: %s"
(Il.Print.string_of_typ atyp)
(Il.Print.string_of_typ ptyp)
);
List.iter2 (fun aparam pparam ->
(* TODO: only supports ExpP for param of arg/param now *)
let typ_of_param param = match param.it with
| ExpP (_, typ) -> typ
| _ ->
error_valid "argument param is not an expression" source
(Il.Print.string_of_param aparam);
in
let aptyp = typ_of_param aparam in
let pptyp = typ_of_param pparam in
if not (IlEval.sub_typ !il_env pptyp aptyp) then
error_valid
"parameter's parameter type is not a subtype of argument's return type"
source
(Printf.sprintf " %s !<: %s"
(Il.Print.string_of_typ pptyp)
(Il.Print.string_of_typ aptyp)
);
) aparams pparams;
| _ -> error_valid "no function definition" source aid
);
| _ ->
error_valid "argument type mismatch" source
(Printf.sprintf " %s =/= %s"
(string_of_arg arg)
(Il.Print.string_of_param param)
)
in
List.iter2 check_arg args params;
check_match source result_typ typ;
(* Reset global il_enviroment *)
il_env := global_il_env
| None -> error_valid "no function definition" source ""
let check_inv_call source id indices args result_typ =
(* Get typs from result_typ *)
let typs =
match result_typ.it with
| TupT l -> l |> List.split |> snd
| _ -> [result_typ]
in
(* Make arguments with typs *)
let typ2arg typ = ExpA (VarE "" $$ no_region % typ) $ no_region in
let free_args = List.map typ2arg typs in
(* Merge free args and bound args *)
let merge_args args idx =
let free_args, bound_args, merged_args = args in
if Option.is_some idx then
let first_free_arg, new_free_args = Lib.List.split_hd free_args in
new_free_args, bound_args, merged_args @ [first_free_arg]
else
let first_bound_arg, new_bound_args = Lib.List.split_hd bound_args in
free_args, new_bound_args, merged_args @ [first_bound_arg]
in
let free_args', result_args, merged_args =
List.fold_left merge_args (free_args, args, []) indices
in
(* TODO: Use error function *)
assert (List.length free_args' = 0);
(* Set new result typ from the last elements of args *)
let new_result_typ =
match result_args with
| [arg] -> (
match arg.it with
| ExpA exp -> exp.note
| a -> error_valid (Printf.sprintf "wrong result argument")
source (Print.string_of_arg (a $ no_region))
)
| _ ->
let arg2typ arg = (
match arg.it with
| ExpA exp -> ("_" $ no_region, exp.note)
| a -> error_valid (Printf.sprintf "wrong result argument")
source (Print.string_of_arg (a $ no_region))
) in
TupT (List.map arg2typ result_args) $ no_region
in
check_call source id merged_args new_result_typ
let check_case source exprs typ =
match typ.it with
| TupT etl when List.length exprs = List.length etl ->
let f expr (_, typ) = check_match source expr.note typ in
List.iter2 f exprs etl
| _ -> error_case source typ
let find_case source cases op =
match List.find_opt (fun (op', _, _) -> Mixop.eq op' op) cases with
| Some (_op, x, _hints) -> x
| None -> error_valid "unknown case" source (string_of_mixop op)
let get_typcases source typ =
let dt =
match typ.it with
| VarT (id, args) ->
(match get_deftyps id args with
| [ dt ] -> dt
| _ -> error_case source typ
)
| _ -> error_case source typ
in
match dt.it with
| VariantT tcs -> tcs
| _ -> error_case source typ
let access (source: source) (typ: typ) (path: path) : typ =
match path.it with
| IdxP _ -> check_list source typ; unwrap_iter_typ typ
| SliceP _ -> check_list source typ; typ
| DotP atom ->
let typfields = get_typfields typ in
match List.find_opt (fun (field, _, _) -> Atom.eq field atom) typfields with
| Some (_, (typ', _, _), _) -> typ'
| None -> error_field source typ atom
let rec valid_path env path =
let source = string_of_path path $ path.at in
match path.it with
| IdxP expr ->
valid_expr env expr;
check_num source expr.note;
| SliceP (expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_num source expr1.note;
check_num source expr2.note;
| DotP _ -> ()
and valid_arg env arg =
match arg.it with
| ExpA expr -> valid_expr env expr
| _ -> ()
(* Expr validation *)
and valid_expr env (expr: expr) : unit =
(* TODO *)
let source = string_of_expr expr $ expr.at in
(match expr.it with
| VarE id ->
if not (Env.mem id env) then error expr.at ("free identifier " ^ id)
| NumE _ -> check_num source expr.note;
| BoolE _ | IsCaseOfE _ | IsValidE _ | MatchE _ | HasTypeE _ | ContextKindE _ ->
check_bool source expr.note;
| CvtE (expr', _, _) ->
check_num source expr.note;
check_num source expr'.note;
| UnE (#Bool.unop, expr') ->
valid_expr env expr';
check_bool source expr.note;
check_bool source expr'.note;
| UnE (#Num.unop, expr') ->
valid_expr env expr';
check_num source expr.note;
check_num source expr'.note;
| BinE (#Num.binop, expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_num source expr.note;
check_num source expr1.note;
check_num source expr2.note;
| BinE (#Num.cmpop, expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_bool source expr.note;
check_num source expr1.note;
check_num source expr2.note;
| BinE (#Bool.binop, expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_bool source expr.note;
check_bool source expr1.note;
check_bool source expr2.note;
| BinE (#Bool.cmpop, expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_bool source expr.note;
check_match source expr1.note expr2.note;
| AccE (expr', path) ->
valid_expr env expr';
valid_path env path;
access source expr'.note path
|> check_match source expr.note;
| UpdE (expr1, pl, expr2) | ExtE (expr1, pl, expr2, _) ->
valid_expr env expr1;
List.iter (valid_path env) pl;
valid_expr env expr2;
check_match source expr.note expr1.note;
List.fold_left (access source) expr1.note pl
|> check_match source expr2.note;
| StrE r ->
List.iter (fun (_, er) -> valid_expr env !er) r;
let typfields = get_typfields expr.note in
List.iter (check_field source expr.note r) typfields;
| CompE (expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_struct source expr1.note;
check_struct source expr2.note;
check_match source expr.note expr1.note;
check_match source expr1.note expr2.note;
| CatE (expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_list source expr1.note;
check_list source expr2.note;
check_match source expr.note expr1.note;
check_match source expr1.note expr2.note;
| MemE (expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_bool source expr.note;
check_match source expr2.note (iterT expr1.note List);
| LenE expr' ->
valid_expr env expr';
check_list source expr'.note;
check_num source expr.note;
| TupE exprs ->
List.iter (valid_expr env) exprs;
check_tuple source exprs expr.note;
| CaseE (op, exprs) ->
let is_evalctx_id id =
let evalctx_ids = List.filter_map (fun (mixop, _, _) ->
let atom = Mixop.flatten mixop |> List.hd |> List.hd in
match atom.it with
| Atom.Atom s -> Some s
| _ -> None
) (get_typcases source evalctxT) in
List.mem id evalctx_ids
in
(match Mixop.flatten op with
| [[{ it=Atom id; _ }]] when is_evalctx_id id ->
check_case source exprs (TupT [] $ no_region)
| _ ->
List.iter (valid_expr env) exprs;
let tcs = get_typcases source expr.note in
let typ, _qs, _prems = find_case source tcs op in
check_case source exprs typ;
)
| CallE (id, args) ->
List.iter (valid_arg env) args;
check_call source id args expr.note;
| InvCallE (id, indices, args) ->
List.iter (valid_arg env) args;
check_inv_call source id indices args expr.note;
| IterE (expr1, (iter, xes)) -> (* TODO *)
if not (expr1.note.it = BoolT && expr.note.it = BoolT) then
let _new_env = List.fold_right (fun (id, e) -> Env.add id e) xes env in
(match iter with
| Opt ->
check_match source expr.note (iterT expr1.note Opt);
| ListN (expr2, _) ->
check_match source expr.note (iterT expr1.note List);
check_num source expr2.note
| _ ->
check_match source expr.note (iterT expr1.note List);
);
| OptE expr_opt ->
Option.iter (valid_expr env) expr_opt;
check_opt source expr.note;
Option.iter
(fun expr' -> check_match source expr.note (iterT expr'.note Opt))
expr_opt;
| ListE l ->
List.iter (valid_expr env) l;
check_list source expr.note;
let elem_typ = unwrap_iter_typ expr.note in
l
|> List.map note
|> List.iter (check_match source elem_typ)
| LiftE expr1 ->
valid_expr env expr;
check_list source expr.note;
check_opt source expr1.note;
let elem_typ = unwrap_iter_typ expr.note in
let elem1_typ = unwrap_iter_typ expr1.note in
check_match source elem1_typ elem_typ
| GetCurStateE ->
check_evalctx source expr.note
| GetCurContextE _ ->
check_evalctx source expr.note
| ChooseE expr1 ->
valid_expr env expr1;
check_list source expr1.note;
check_match source expr1.note (iterT expr.note List)
| IsDefinedE expr1 ->
valid_expr env expr1;
check_opt source expr1.note;
check_bool source expr.note;
| TopValueE expr_opt ->
Option.iter (valid_expr env) expr_opt;
check_bool source expr.note;
Option.iter (fun expr1 -> check_match source expr1.note (varT "valtype")) expr_opt
| TopValuesE expr1 ->
valid_expr env expr1;
check_bool source expr.note;
check_num source expr1.note
| SubE _ | YetE _ -> error_valid "invalid expression" source ""
)
(* Instr validation *)
let rec valid_instr (env: Env.t) (instr: instr) : Env.t =
let source = string_of_instr instr $ instr.at in
(match instr.it with
| IfI (expr, il1, il2) ->
valid_expr env expr;
check_bool source expr.note;
(* TODO: Merge env *)
let _env1 = valid_instrs env il1 in
let _env2 = valid_instrs env il2 in
env
| EitherI (il1, il2) ->
(* TODO: Merge env *)
let _env1 = valid_instrs env il1 in
let _env2 = valid_instrs env il2 in
env
| EnterI (expr1, expr2, il) ->
valid_expr env expr1;
valid_expr env expr2;
check_evalctx source expr1.note;
check_instr source expr2.note;
valid_instrs env il
| AssertI expr ->
valid_expr env expr;
check_bool source expr.note;
env
| PushI expr ->
valid_expr env expr;
if
not (sub_typ (get_base_typ expr.note) (varT "val"))
then
error_mismatch source (get_base_typ expr.note) (varT "val");
env
| PopI expr | PopAllI expr ->
let new_env = Env.add_bound_vars expr env in
valid_expr new_env expr;
if
not (sub_typ (get_base_typ expr.note) (varT "val"))
then
error_mismatch source (get_base_typ expr.note) (varT "val");
new_env
| LetI (expr1, expr2) ->
let new_env = Env.add_subst expr1 expr2 env in
valid_expr new_env expr1;
valid_expr env expr2;
check_match source expr1.note expr2.note;
new_env
| TrapI | FailI | NopI | ReturnI None | ExitI _ -> env
| ThrowI expr ->
if not (sub_typ (get_base_typ expr.note) (varT "val")) then
error_mismatch source (get_base_typ expr.note) (varT "val");
env
| ReturnI (Some expr) ->
valid_expr env expr;
env
| ExecuteI expr | ExecuteSeqI expr ->
valid_expr env expr;
check_instr source expr.note;
env
| PerformI (id, args) ->
List.iter (valid_arg env) args;
check_call source id args (TupT [] $ no_region);
env
| ReplaceI (expr1, path, expr2) ->
valid_expr env expr1;
valid_path env path;
valid_expr env expr2;
access source expr1.note path
|> check_match source expr2.note;
env
| AppendI (expr1, expr2) ->
valid_expr env expr1;
valid_expr env expr2;
check_list source expr1.note;
env
| ForEachI _ | OtherwiseI _ | YetI _ -> error_valid "invalid instruction" source ""
)
and valid_instrs env = function
| [] -> env
| h :: t -> valid_instrs (valid_instr env h) t
let init_env algo =
let params = Al_util.params_of_algo algo in
let env =
Env.empty
|> Env.add_bound_var "s"
|> List.fold_right Env.add_bound_param params
in
List.iter (valid_arg env) params;
env
let valid_algo (algo: algorithm) =
algo
|> Al_util.params_of_algo
|> List.map string_of_arg
|> String.concat ", "
|> Printf.sprintf "%s(%s)" (Al_util.name_of_algo algo)
|> print_endline;
(* TODO: Use local il_environment *)
(* Store global il_enviroment *)
let global_il_env = !il_env in
(* Add function argument to il_environment *)
(match IlEnv.find_opt_def !il_env (Al_util.name_of_algo algo $ no_region) with
| Some (params, _, _) -> List.iter (fun param ->
(match param.it with
| DefP (id, params', typ') -> il_env := IlEnv.bind_def !il_env id (params', typ', [])
| _ -> ()
)
) params;
| _ -> ()
);
let env = init_env algo in
let _env = valid_instrs env (Al_util.body_of_algo algo) in
(* Reset global il_enviroment *)
il_env := global_il_env
let valid (script: script) =
Lang.al := script;
List.iter valid_algo script