File tree Expand file tree Collapse file tree 2 files changed +37
-4
lines changed Expand file tree Collapse file tree 2 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -58,14 +58,31 @@ let bool_of_int expr = Syntax.(expr <> ~$0)
58
58
Vihje: let open Syntax in ...
59
59
Vihje: int_of_bool. *)
60
60
let eval_binary (l : Expr.expr ) (b : Ast.binary ) (r : Expr.expr ): Expr.expr =
61
- failwith " TODO"
61
+ let open Syntax in
62
+ match b with
63
+ | Add -> l + r
64
+ | Sub -> l - r
65
+ | Mul -> l * r
66
+ | Div -> l / r
67
+ | Mod -> l mod r
68
+ | Eq -> int_of_bool (l = r)
69
+ | Ne -> int_of_bool (l <> r)
70
+ | Lt -> int_of_bool (l < r)
71
+ | Le -> int_of_bool (l < = r)
72
+ | Gt -> int_of_bool (l > r)
73
+ | Ge -> int_of_bool (l > = r)
62
74
63
75
(* * Teisendab avaldise Z3 avaldiseks.
64
76
Rand avaldist pole vaja toetada, sest see pole nii lihtne kui võib arvata.
65
77
Vihje: let open Syntax in ...
66
78
Vihje: eval_binary. *)
67
79
let rec eval_expr (expr : Ast.expr ): Expr.expr =
68
- failwith " TODO"
80
+ let open Syntax in
81
+ match expr with
82
+ | Num i -> ~$ i
83
+ | Var x -> ! x
84
+ | Rand (l , u ) -> failwith " Rand"
85
+ | Binary (l , b , r ) -> eval_binary (eval_expr l) b (eval_expr r)
69
86
70
87
71
88
module Env = Eval.Common. Env
Original file line number Diff line number Diff line change @@ -9,7 +9,19 @@ open Symbolic
9
9
Vihje: Expr.substitute_one.
10
10
Vihje: bool_of_int. *)
11
11
let rec wp (stmt : Ast.stmt ) (post : Expr.expr ): Expr.expr =
12
- failwith " TODO"
12
+ let open Syntax in
13
+ match stmt with
14
+ | Nop -> post
15
+ | Error -> false_
16
+ | Assign (x , e ) ->
17
+ let e' = eval_expr e in
18
+ Expr. substitute_one post ! x e'
19
+ | Seq (s1 , s2 ) -> wp s1 (wp s2 post)
20
+ | If (c , s1 , s2 ) ->
21
+ let c' = bool_of_int (eval_expr c) in
22
+ implies c' (wp s1 post) && implies (not c') (wp s2 post)
23
+ | While (Num 1 , Nop) -> true_
24
+ | _ -> failwith " TODO"
13
25
14
26
15
27
(* * Korrektsuse kontrolli tulemus. *)
@@ -25,4 +37,8 @@ type verdict =
25
37
Vihje: match-i Solver.check tulemust.
26
38
Vihje: env_of_model. *)
27
39
let check (stmt : Ast.stmt ): verdict =
28
- failwith " TODO"
40
+ let formula = Syntax. (not (wp stmt true_)) in
41
+ match Solver. check solver [formula] with
42
+ | SATISFIABLE -> Incorrect (env_of_model (Option. get (Solver. get_model solver)))
43
+ | UNSATISFIABLE -> Correct
44
+ | UNKNOWN -> Unknown
You can’t perform that action at this time.
0 commit comments