Skip to content

Commit 78816c3

Browse files
committed
Lisa modelcheck
1 parent 27d7f1f commit 78816c3

6 files changed

Lines changed: 152 additions & 0 deletions

File tree

src/modelcheck/checker.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
open Model
2+
3+
(** Mudelkontrollija, mis kasutab püsipunkti.
4+
Vt. Fixpoint moodul. *)
5+
module MakeNaive (Model: Model) =
6+
struct
7+
module StateSet = Set.Make (Model)
8+
module StateSetFP = Fixpoint.MakeSet (StateSet)
9+
10+
11+
12+
(** Tagastab kõik saavutatavad olekud. *)
13+
let all_states (): StateSet.t =
14+
failwith "TODO"
15+
16+
(** Tagastab kõik saavutatavad veaolekud.
17+
Vihje: StateSet.filter. *)
18+
let error_states (): StateSet.t =
19+
failwith "TODO"
20+
21+
(** Kas mõni veaolek on saavutatav? *)
22+
let has_error (): bool =
23+
failwith "TODO"
24+
25+
(** Kas veaolekud on mittesaavutatavad? *)
26+
let is_correct (): bool =
27+
failwith "TODO"
28+
end
29+
30+

src/modelcheck/dune

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(library
2+
(name modelcheck)
3+
(libraries fixpoint)
4+
(preprocess (pps ppx_deriving.std)))

src/modelcheck/model.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module type Model =
2+
sig
3+
(** Oleku tüüp. *)
4+
type t [@@deriving ord]
5+
6+
(** Algolek. *)
7+
val initial: t
8+
9+
(** Tagastab olekule järgnevad võimalikud olekud. *)
10+
val step: t -> t list
11+
12+
(** Kontrollib, kas on veaolek. *)
13+
val is_error: t -> bool
14+
end

src/modelcheck/modelcheck.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include Model
2+
3+
module Checker = Checker

test/modelcheck/dune

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(test
2+
(name modelcheck_test)
3+
(libraries ounit2 ounittodo modelcheck)
4+
(preprocess (pps ppx_deriving.std)))

test/modelcheck/modelcheck_test.ml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
open OUnit2
2+
open Modelcheck
3+
4+
module BadModel =
5+
struct
6+
type t = int [@@deriving ord, show]
7+
8+
let initial = 42
9+
10+
let step i =
11+
if i >= 0 then
12+
[i - 1; i - 2; i - 3]
13+
else
14+
[]
15+
16+
let is_error i = i < 0
17+
end
18+
19+
module BadModelChecker = Checker.MakeNaive (BadModel)
20+
21+
let test_bad_correct _ =
22+
assert_equal ~printer:string_of_bool true (BadModelChecker.has_error ());
23+
assert_equal ~printer:string_of_bool false (BadModelChecker.is_correct ())
24+
25+
let test_bad_all_states _ =
26+
let all_states = BadModelChecker.all_states () in
27+
let assert_mem expected state =
28+
assert_equal ~printer:string_of_bool ~msg:(BadModel.show state) expected (BadModelChecker.StateSet.mem state all_states)
29+
in
30+
assert_mem true 42;
31+
assert_mem false 43;
32+
assert_mem true 0;
33+
assert_equal ~printer:string_of_int 46 (BadModelChecker.StateSet.cardinal all_states)
34+
35+
let test_bad_error_states _ =
36+
let error_states = BadModelChecker.error_states () in
37+
let assert_mem expected state =
38+
assert_equal ~printer:string_of_bool ~msg:(BadModel.show state) expected (BadModelChecker.StateSet.mem state error_states)
39+
in
40+
(* Kõik veaolekud. *)
41+
assert_mem true (-1);
42+
assert_mem true (-2);
43+
assert_mem true (-3);
44+
assert_equal ~printer:string_of_int 3 (BadModelChecker.StateSet.cardinal error_states)
45+
46+
47+
module GoodModel =
48+
struct
49+
type t = int [@@deriving ord, show]
50+
51+
let initial = 42
52+
53+
let step i =
54+
if i >= 3 then
55+
[i - 1; i - 2; i - 3]
56+
else
57+
[]
58+
59+
let is_error i = i < 0
60+
end
61+
62+
module GoodModelChecker = Checker.MakeNaive (GoodModel)
63+
64+
let test_good_correct _ =
65+
assert_equal ~printer:string_of_bool false (GoodModelChecker.has_error ());
66+
assert_equal ~printer:string_of_bool true (GoodModelChecker.is_correct ())
67+
68+
let test_good_all_states _ =
69+
let all_states = GoodModelChecker.all_states () in
70+
let assert_mem expected state =
71+
assert_equal ~printer:string_of_bool ~msg:(GoodModel.show state) expected (GoodModelChecker.StateSet.mem state all_states)
72+
in
73+
assert_mem true 42;
74+
assert_mem false 43;
75+
assert_mem true 0;
76+
assert_equal ~printer:string_of_int 43 (GoodModelChecker.StateSet.cardinal all_states)
77+
78+
let test_good_error_states _ =
79+
let error_states = GoodModelChecker.error_states () in
80+
assert_equal ~printer:string_of_int 0 (GoodModelChecker.StateSet.cardinal error_states)
81+
82+
83+
let tests =
84+
"modelcheck" >::: [
85+
"bad" >::: [
86+
"correct" >:: test_bad_correct;
87+
"all_states" >:: test_bad_all_states;
88+
"error_states" >:: test_bad_error_states;
89+
];
90+
"good" >::: [
91+
"correct" >:: test_good_correct;
92+
"all_states" >:: test_good_all_states;
93+
"error_states" >:: test_good_error_states;
94+
];
95+
]
96+
97+
let () = run_test_tt_main (OUnitTodo.wrap tests)

0 commit comments

Comments
 (0)