Skip to content

Commit 06bdc0e

Browse files
committed
Lisa fixpoint
1 parent 9469c26 commit 06bdc0e

File tree

8 files changed

+177
-0
lines changed

8 files changed

+177
-0
lines changed

src/fixpoint/dune

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

src/fixpoint/fixpoint.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(** Püsipunktid.
2+
3+
Vt. "Introduction to Compiler Design" õpikust, peatükk 1.5.1.
4+
Vt. Vesali "The Sulund Design Pattern™" slaide. *)
5+
6+
(** Püsipunktid üle suvalise võrreldava tüübi. *)
7+
module Make (D: sig type t [@@deriving eq] end) =
8+
struct
9+
(** Leiab funktsiooni püsipunkti alustades iteratsiooni antud väärtusest. *)
10+
let fp (f: D.t -> D.t) (initial: D.t): D.t =
11+
failwith "TODO"
12+
end
13+
14+
(** Püsipunktid üle hulkade. *)
15+
module MakeSet (D: Set.S) =
16+
struct
17+
include Make (D)
18+
19+
(** Leiab funktsiooni vähima püsipunkti.
20+
Kasutada fp funktsiooni. *)
21+
let lfp (f: D.t -> D.t): D.t =
22+
failwith "TODO"
23+
24+
(** Leiab funktsiooni sulundi, mis sisaldab antud väärtusi.
25+
Kasutada lfp funktsiooni. *)
26+
let closure (f: D.t -> D.t) (initial: D.t): D.t =
27+
failwith "TODO"
28+
end

test/fixpoint/dune

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

test/fixpoint/fixpoint_test.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
open OUnit2
2+
3+
let tests =
4+
"fixpoint" >::: [
5+
Transition_test.tests;
6+
Nfa_eps_test.tests;
7+
]
8+
9+
let () = run_test_tt_main (OUnitTodo.wrap tests)

test/fixpoint/nfa_eps_test.ml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
(** Epsilonsulundi näide "Introduction to Compiler Design" õpikust, peatükk 1.5.1. *)
2+
open OUnit2
3+
open Fixpoint
4+
5+
module IntSet = Set.Make (Int)
6+
7+
let show_intset is = [%show: int list] (IntSet.elements is)
8+
9+
(** Epsilonsammude funktsioon, joonis 1.5. *)
10+
let nfa_eps = function
11+
| 1 -> [2; 5]
12+
| 5 -> [6; 7]
13+
| 8 -> [1]
14+
| _ -> []
15+
16+
(** Epsilonsammude funktsioon hulgal. *)
17+
let nfa_eps_set states =
18+
IntSet.elements states
19+
|> List.concat_map nfa_eps
20+
|> IntSet.of_list
21+
22+
module IntSetFP = MakeSet (IntSet)
23+
24+
let assert_equal = assert_equal ~cmp:IntSet.equal ~printer:show_intset
25+
26+
let test_fp _ =
27+
(* Olekust 1. *)
28+
let f1 x = IntSet.union (IntSet.singleton 1) (nfa_eps_set x) in
29+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7]) (IntSetFP.fp f1 IntSet.empty);
30+
(* Olekust 2. *)
31+
let f2 x = IntSet.union (IntSet.singleton 2) (nfa_eps_set x) in
32+
assert_equal (IntSet.of_list [2]) (IntSetFP.fp f2 IntSet.empty);
33+
(* Olekust 8. *)
34+
let f8 x = IntSet.union (IntSet.singleton 8) (nfa_eps_set x) in
35+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7; 8]) (IntSetFP.fp f8 IntSet.empty)
36+
37+
let test_lfp _ =
38+
(* Olekust 1. *)
39+
let f1 x = IntSet.union (IntSet.singleton 1) (nfa_eps_set x) in
40+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7]) (IntSetFP.lfp f1);
41+
(* Olekust 2. *)
42+
let f2 x = IntSet.union (IntSet.singleton 2) (nfa_eps_set x) in
43+
assert_equal (IntSet.of_list [2]) (IntSetFP.lfp f2);
44+
(* Olekust 8. *)
45+
let f8 x = IntSet.union (IntSet.singleton 8) (nfa_eps_set x) in
46+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7; 8]) (IntSetFP.lfp f8)
47+
48+
let test_closure _ =
49+
(* Olekust 1. *)
50+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7]) (IntSetFP.closure nfa_eps_set (IntSet.singleton 1));
51+
(* Olekust 2. *)
52+
assert_equal (IntSet.of_list [2]) (IntSetFP.closure nfa_eps_set (IntSet.singleton 2));
53+
(* Olekust 8. *)
54+
assert_equal (IntSet.of_list [1; 2; 5; 6; 7; 8]) (IntSetFP.closure nfa_eps_set (IntSet.singleton 8))
55+
56+
let tests =
57+
"nfa_eps" >::: [
58+
"fp" >:: test_fp;
59+
"lfp" >:: test_lfp;
60+
"closure" >:: test_closure;
61+
]

test/fixpoint/nfa_eps_test.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val tests: OUnit2.test

test/fixpoint/transition_test.ml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(** Transitiivse sulundi näide Vesali "The Sulund Design Pattern™" slaididelt. *)
2+
open OUnit2
3+
open Fixpoint
4+
5+
module Transition =
6+
struct
7+
type t = int * string * int [@@deriving ord, show]
8+
end
9+
10+
module TransitionSet =
11+
struct
12+
include Set.Make (Transition) (* Taaskasutame standardset hulga moodulit. *)
13+
(* Aga lisame mõne funktsiooni juurde. *)
14+
15+
(** Relatsioonide kompositsioon. *)
16+
let compose rel1 rel2 =
17+
fold (fun (s1, l1, t1) acc ->
18+
fold (fun (s2, l2, t2) acc ->
19+
if t1 = s2 then
20+
add (s1, l1 ^ l2, t2) acc
21+
else
22+
acc
23+
) rel2 acc
24+
) rel1 empty
25+
26+
let show ts = [%show: Transition.t list] (elements ts)
27+
end
28+
29+
(** Algne relatsioon. *)
30+
let initial = TransitionSet.of_list [
31+
(0, "a", 1);
32+
(1, "b", 2);
33+
(1, "c", 3);
34+
(3, "d", 4);
35+
]
36+
37+
(** Oodatav transitiivne sulund. *)
38+
let expected = TransitionSet.of_list [
39+
(0, "a", 1);
40+
(1, "b", 2);
41+
(1, "c", 3);
42+
(3, "d", 4);
43+
(0, "ab", 2);
44+
(0, "ac", 3);
45+
(1, "cd", 4);
46+
(0, "acd", 4);
47+
]
48+
49+
module TransitionSetFP = MakeSet (TransitionSet)
50+
51+
let assert_equal = assert_equal ~cmp:TransitionSet.equal ~printer:TransitionSet.show
52+
53+
let test_fp _ =
54+
let f ts = TransitionSet.union initial (TransitionSet.compose ts initial) in
55+
assert_equal expected (TransitionSetFP.fp f TransitionSet.empty)
56+
57+
let test_lfp _ =
58+
let f ts = TransitionSet.union initial (TransitionSet.compose ts initial) in
59+
assert_equal expected (TransitionSetFP.lfp f)
60+
61+
let test_closure _ =
62+
let f ts = TransitionSet.compose ts initial in
63+
assert_equal expected (TransitionSetFP.closure f initial)
64+
65+
let tests =
66+
"transition" >::: [
67+
"fp" >:: test_fp;
68+
"lfp" >:: test_lfp;
69+
"closure" >:: test_closure;
70+
]

test/fixpoint/transition_test.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val tests: OUnit2.test

0 commit comments

Comments
 (0)