Skip to content

Commit e29f035

Browse files
committed
Lahenda wp praktikumis
1 parent 5e8f518 commit e29f035

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/symbolic/symbolic.ml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,31 @@ let bool_of_int expr = Syntax.(expr <> ~$0)
5858
Vihje: let open Syntax in ...
5959
Vihje: int_of_bool. *)
6060
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)
6274

6375
(** Teisendab avaldise Z3 avaldiseks.
6476
Rand avaldist pole vaja toetada, sest see pole nii lihtne kui võib arvata.
6577
Vihje: let open Syntax in ...
6678
Vihje: eval_binary. *)
6779
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)
6986

7087

7188
module Env = Eval.Common.Env

src/wp/wp.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ open Symbolic
99
Vihje: Expr.substitute_one.
1010
Vihje: bool_of_int. *)
1111
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"
1325

1426

1527
(** Korrektsuse kontrolli tulemus. *)
@@ -25,4 +37,8 @@ type verdict =
2537
Vihje: match-i Solver.check tulemust.
2638
Vihje: env_of_model. *)
2739
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

0 commit comments

Comments
 (0)