-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1sample.ml
More file actions
145 lines (123 loc) · 4.59 KB
/
hw1sample.ml
File metadata and controls
145 lines (123 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
(* Copyright 2006-2023 Paul Eggert.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. *)
let subset_test0 = subset [] [1;2;3]
let subset_test1 = subset [3;1;3] [1;2;3]
let subset_test2 = not (subset [1;3;7] [4;1;3])
let equal_sets_test0 = equal_sets [1;3] [3;1;3]
let equal_sets_test1 = not (equal_sets [1;3;4] [3;1;3])
let set_union_test0 = equal_sets (set_union [] [1;2;3]) [1;2;3]
let set_union_test1 = equal_sets (set_union [3;1;3] [1;2;3]) [1;2;3]
let set_union_test2 = equal_sets (set_union [] []) []
let set_all_union_test0 =
equal_sets (set_all_union []) []
let set_all_union_test1 =
equal_sets (set_all_union [[3;1;3]; [4]; [1;2;3]]) [1;2;3;4]
let set_all_union_test2 =
equal_sets (set_all_union [[5;2]; []; [5;2]; [3;5;7]]) [2;3;5;7]
let computed_fixed_point_test0 =
computed_fixed_point (=) (fun x -> x / 2) 1000000000 = 0
let computed_fixed_point_test1 =
computed_fixed_point (=) (fun x -> x *. 2.) 1. = infinity
let computed_fixed_point_test2 =
computed_fixed_point (=) sqrt 10. = 1.
let computed_fixed_point_test3 =
((computed_fixed_point (fun x y -> abs_float (x -. y) < 1.)
(fun x -> x /. 2.)
10.)
= 1.25)
let computed_periodic_point_test0 =
computed_periodic_point (=) (fun x -> x / 2) 0 (-1) = -1
let computed_periodic_point_test1 =
computed_periodic_point (=) (fun x -> x *. x -. 1.) 2 0.5 = -1.
(* An example grammar for a small subset of Awk. *)
type awksub_nonterminals =
| Expr | Lvalue | Incrop | Binop | Num
let awksub_rules =
[Expr, [T"("; N Expr; T")"];
Expr, [N Num];
Expr, [N Expr; N Binop; N Expr];
Expr, [N Lvalue];
Expr, [N Incrop; N Lvalue];
Expr, [N Lvalue; N Incrop];
Lvalue, [T"$"; N Expr];
Incrop, [T"++"];
Incrop, [T"--"];
Binop, [T"+"];
Binop, [T"-"];
Num, [T"0"];
Num, [T"1"];
Num, [T"2"];
Num, [T"3"];
Num, [T"4"];
Num, [T"5"];
Num, [T"6"];
Num, [T"7"];
Num, [T"8"];
Num, [T"9"]]
let awksub_grammar = Expr, awksub_rules
let awksub_test0 =
filter_blind_alleys awksub_grammar = awksub_grammar
let awksub_test1 =
filter_blind_alleys (Expr, List.tl awksub_rules) = (Expr, List.tl awksub_rules)
let awksub_test2 =
filter_blind_alleys (Expr,
[Expr, [N Num];
Expr, [N Lvalue];
Expr, [N Expr; N Lvalue];
Expr, [N Lvalue; N Expr];
Expr, [N Expr; N Binop; N Expr];
Lvalue, [N Lvalue; N Expr];
Lvalue, [N Expr; N Lvalue];
Lvalue, [N Incrop; N Lvalue];
Lvalue, [N Lvalue; N Incrop];
Incrop, [T"++"]; Incrop, [T"--"];
Binop, [T"+"]; Binop, [T"-"];
Num, [T"0"]; Num, [T"1"]; Num, [T"2"]; Num, [T"3"]; Num, [T"4"];
Num, [T"5"]; Num, [T"6"]; Num, [T"7"]; Num, [T"8"]; Num, [T"9"]])
= (Expr,
[Expr, [N Num];
Expr, [N Expr; N Binop; N Expr];
Incrop, [T"++"]; Incrop, [T"--"];
Binop, [T "+"]; Binop, [T "-"];
Num, [T "0"]; Num, [T "1"]; Num, [T "2"]; Num, [T "3"]; Num, [T "4"];
Num, [T "5"]; Num, [T "6"]; Num, [T "7"]; Num, [T "8"]; Num, [T "9"]])
let awksub_test3 =
filter_blind_alleys (Expr, List.tl (List.tl (List.tl awksub_rules))) =
filter_blind_alleys (Expr, List.tl (List.tl awksub_rules))
type giant_nonterminals =
| Conversation | Sentence | Grunt | Snore | Shout | Quiet
let giant_grammar =
Conversation,
[Snore, [T"ZZZ"];
Quiet, [];
Grunt, [T"khrgh"];
Shout, [T"aooogah!"];
Sentence, [N Quiet];
Sentence, [N Grunt];
Sentence, [N Shout];
Conversation, [N Snore];
Conversation, [N Sentence; T","; N Conversation]]
let giant_test0 =
filter_blind_alleys giant_grammar = giant_grammar
let giant_test1 =
filter_blind_alleys (Sentence, List.tl (snd giant_grammar)) =
(Sentence,
[Quiet, []; Grunt, [T "khrgh"]; Shout, [T "aooogah!"];
Sentence, [N Quiet]; Sentence, [N Grunt]; Sentence, [N Shout]])
let giant_test2 =
filter_blind_alleys (Sentence, List.tl (List.tl (snd giant_grammar))) =
(Sentence,
[Grunt, [T "khrgh"]; Shout, [T "aooogah!"];
Sentence, [N Grunt]; Sentence, [N Shout]])