Skip to content

Commit 479495d

Browse files
committed
Lahenda modelcheck/program praktikumis
1 parent 02d457f commit 479495d

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/crashcourse/basics.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ let rec triangular (n: int): int =
7777
Võib eeldada, et y pole negatiivne.
7878
Kasutada rekursiooni, mitte ** operaatorit. *)
7979
let rec pow (x: int) (y: int): int =
80-
failwith "TODO"
80+
match y with
81+
| 0 -> 1
82+
| _ -> x * pow x (y - 1)
8183

8284
(** Näide:
8385
Kas täht on täishäälik (inglise keeles)? *)

src/modelcheck/program/modelcheck_program.ml

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ struct
3939
let initial: t = (P2, Env.singleton "x" 1)
4040

4141
let step ((point, env): t): t list =
42-
failwith "TODO"
42+
match point with
43+
| P2 -> nondet P3 "y" (-10, 10) env
44+
| P3 -> nondet P4 "z" (-10, 10) env
45+
| P4 ->
46+
if Env.find "z" env > 5 then
47+
[(P5, env)]
48+
else
49+
[(P7, env)]
50+
| P5 -> [(P8, Env.add "x" (Env.find "y" env) env)]
51+
| P7 -> [(P8, Env.add "x" (Env.find "x" env + 1) env)]
52+
| P8 -> []
4353

4454
let is_error ((point, env): t): bool =
45-
failwith "TODO"
55+
match point with
56+
| P8 -> Env.find "x" env = 0
57+
| _ -> false
4658
end
4759

4860
(** Mudel järgmise programmi jaoks:
@@ -64,10 +76,22 @@ struct
6476
let initial: t = (P1, Env.empty)
6577

6678
let step ((point, env): t): t list =
67-
failwith "TODO"
79+
match point with
80+
| P1 -> nondet P2 "x" (0, 1500) env
81+
| P2 -> [(P3, Env.add "y" (Env.find "x" env) env)]
82+
| P3 ->
83+
if Env.find "x" env < 1024 then
84+
[(P4, env)]
85+
else
86+
[(P7, env)]
87+
| P4 -> [(P5, Env.add "x" (Env.find "x" env + 1) env)]
88+
| P5 -> [(P3, Env.add "y" (Env.find "y" env + 1) env)]
89+
| P7 -> []
6890

6991
let is_error ((point, env): t): bool =
70-
failwith "TODO"
92+
match point with
93+
| P7 -> Env.find "x" env <> Env.find "y" env
94+
| _ -> false
7195
end
7296

7397
(** Mudel järgmise programmi jaoks:
@@ -98,11 +122,33 @@ struct
98122
let initial: t = (P2, Env.singleton "y" 1)
99123

100124
let step ((point, env): t): t list =
101-
failwith "TODO"
125+
match point with
126+
| P2 -> nondet P3 "x0" (0, 5) env
127+
| P3 -> nondet P4 "n0" (1, 5) env
128+
| P4 -> [(P5, Env.add "x" (Env.find "x0" env) env)]
129+
| P5 -> [(P6, Env.add "n" (Env.find "n0" env) env)]
130+
| P6 ->
131+
if Env.find "n" env > 1 then
132+
[(P7, env)]
133+
else
134+
[(P15, env)]
135+
| P7 ->
136+
if Env.find "n" env mod 2 = 0 then
137+
[(P8, env)]
138+
else
139+
[(P10, env)]
140+
| P8 -> [(P13, Env.add "n" (Env.find "n" env / 2) env)]
141+
| P10 -> [(P11, Env.add "y" (Env.find "x" env * Env.find "y" env) env)]
142+
| P11 -> [(P13, Env.add "n" ((Env.find "n" env + 1) / 2) env)]
143+
| P13 -> [(P6, Env.add "x" (Env.find "x" env * Env.find "x" env) env)]
144+
| P15 -> [(P16, Env.add "y" (Env.find "x" env * Env.find "y" env) env)]
145+
| P16 -> []
102146

103147
(** Vihje: Kasuta Crashcourse.Basics.pow funktsiooni. *)
104148
let is_error ((point, env): t): bool =
105-
failwith "TODO"
149+
match point with
150+
| P16 -> Env.find "y" env <> Crashcourse.Basics.pow (Env.find "x0" env) (Env.find "n0" env)
151+
| _ -> false
106152
end
107153

108154
(** Mudel parandatud PowProgram jaoks. *)
@@ -112,7 +158,7 @@ struct
112158

113159
let step ((point, env): t): t list =
114160
match point with (* Siin saab üle defineerida ühe juhu. *)
115-
161+
| P11 -> [(P13, Env.add "n" ((Env.find "n" env - 1) / 2) env)]
116162
| _ -> step (point, env)
117163
end
118164

0 commit comments

Comments
 (0)