-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.ml
219 lines (199 loc) · 5.99 KB
/
history.ml
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
type entry = {
mutable first : Inference.Set.elem option;
mutable iterations : (string * Ast.simple_entailment) list;
mutable unfoldings : entry list;
mutable terms : Ast.term list option;
mutable constraints : Ast.pi option;
mutable verdict : bool option;
}
let make_entry () =
{
first = None;
iterations = [];
unfoldings = [];
terms = None;
constraints = None;
verdict = None;
}
let set_first (i, t) hist = hist.first <- Some (i, t)
let add_iteration (label, es) hist =
(* Printf.printf "%s :: %s\n" label (Ast.show_entailment es); *)
hist.iterations <- (label, es) :: hist.iterations
let add_unfolding sub hist = hist.unfoldings <- sub :: hist.unfoldings
let set_terms terms hist = if List.length terms > 0 then hist.terms <- Some terms
let set_constraints constrnt hist = hist.constraints <- Some constrnt
let set_verdict verdict hist = hist.verdict <- Some verdict
let show_entry hist ~verbose =
let rec aux spaces prefix hist =
let first = ref true in
let get_prefix () =
if !first then (
first := false;
prefix)
else
spaces
in
let id x = x in
let print name message =
Printf.sprintf "%s%10s %s┃%s %s%s%s" Colors.yellow name Colors.magenta Colors.reset
(get_prefix ()) message Colors.reset
in
let show_first =
match hist.first with
| None -> id
| Some (i, t) ->
let first =
Printf.sprintf "%s%s, %s%s" Colors.magenta (Signals.show i) Colors.yellow
(match t with
| None -> "_"
| Some t -> Ast.show_term t)
in
List.cons (print "-" first)
in
let show_iterations =
if verbose then
List.fold_right
(fun (name, entailment) acc -> print name (Ast.show_simple_entailment entailment) :: acc)
hist.iterations
else
List.cons
(let name, entailment = List.hd hist.iterations in
print name (Ast.show_simple_entailment entailment))
in
let show_unfoldings =
List.fold_right List.cons
(List.mapi
(fun i x ->
let prefix' = get_prefix () in
if i = 0 then
aux (prefix' ^ " ") (prefix' ^ "└──") x
else
aux (prefix' ^ "│ ") (prefix' ^ "├──") x)
hist.unfoldings)
in
let _show_terms =
match hist.terms with
| None -> id
| Some terms ->
List.cons
(print "(TERMS)"
(Colors.yellow ^ (List.map Ast.show_term terms |> String.concat ", ") ^ Colors.reset))
in
let show_constraints =
match hist.constraints with
| None -> id
| Some True -> id
| Some con -> List.cons (print "(CHECK)" (Ast.show_pi con))
in
let show_verdict =
match hist.verdict with
| None -> id
| Some verdict ->
List.cons
(print "(RESULT)"
(Colors.blue ^ Colors.italic
^ (if verdict then "SUCCESS" else "FAILURE")
^ Colors.reset))
in
[] |> show_first |> show_iterations |> show_unfoldings |> show_constraints |> show_verdict
|> List.rev |> String.concat "\n"
in
aux "" "" hist
type t = entry list list
let from_entries l = l
let roman n =
assert (n >= 0);
let digits = [| ""; "I"; "V"; "X"; "L"; "C"; "D"; "M"; "V"; "X"; "L"; "C"; "D"; "M" |] in
let idx =
[|
[];
[ 1 ];
[ 1; 1 ];
[ 1; 1; 1 ];
[ 1; 2 ];
[ 2 ];
[ 2; 1 ];
[ 2; 1; 1 ];
[ 2; 1; 1; 1 ];
[ 1; 3 ];
|]
in
let rec aux p n =
match (p, n) with
| 0, 0 -> "O"
| _, 0 -> ""
| p, n ->
let d = n mod 10 in
let t = List.fold_left (fun acc x -> acc ^ digits.(p + x)) "" idx.(d) in
aux (p + 2) (n / 10) ^ t
in
aux 0 n
let case_no i j =
assert (i >= 0 && j >= 0);
let i = roman i in
Printf.sprintf "%s-%d" i j
let show hist ~verbose =
let _, output =
List.fold_left
(fun (i, acc) l ->
( i + 1,
let _, subh =
List.fold_left
(fun (j, acc2) sub ->
( j + 1,
let entry = show_entry sub ~verbose in
let label =
Printf.sprintf "%s%-10s %s┃" Colors.bold (case_no i j) Colors.reset
in
entry :: label :: acc2 ))
(1, []) l
in
List.rev subh :: acc ))
(1, []) hist
in
String.concat "\n" (List.concat (List.rev output))
let () =
(* test case_no *)
assert (case_no 0 0 = "O-0");
assert (case_no 0 1 = "O-1");
assert (case_no 1 1 = "I-1");
assert (case_no 2 1 = "II-1");
assert (case_no 3 2 = "III-2");
assert (case_no 4 2 = "IV-2");
assert (case_no 5 3 = "V-3");
assert (case_no 6 3 = "VI-3");
assert (case_no 7 3 = "VII-3");
assert (case_no 8 3 = "VIII-3");
assert (case_no 9 3 = "IX-3");
assert (case_no 10 3 = "X-3");
assert (case_no 11 3 = "XI-3");
assert (case_no 12 3 = "XII-3");
assert (case_no 13 3 = "XIII-3");
assert (case_no 14 3 = "XIV-3");
assert (case_no 15 3 = "XV-3");
assert (case_no 19 3 = "XIX-3");
assert (case_no 20 3 = "XX-3");
assert (case_no 40 3 = "XL-3");
assert (case_no 45 3 = "XLV-3");
assert (case_no 50 3 = "L-3");
assert (case_no 60 3 = "LX-3");
assert (case_no 90 3 = "XC-3");
assert (case_no 99 3 = "XCIX-3");
assert (case_no 100 3 = "C-3");
assert (case_no 200 3 = "CC-3");
assert (case_no 400 3 = "CD-3");
assert (case_no 450 3 = "CDL-3");
assert (case_no 455 3 = "CDLV-3");
assert (case_no 456 3 = "CDLVI-3");
assert (case_no 456 3 = "CDLVI-3");
assert (case_no 495 3 = "CDXCV-3");
assert (case_no 499 3 = "CDXCIX-3");
assert (case_no 999 3 = "CMXCIX-3");
assert (case_no 1000 3 = "M-3");
assert (case_no 1100 3 = "MC-3");
assert (case_no 1500 3 = "MD-3");
assert (case_no 2500 3 = "MMD-3");
assert (case_no 3999 3 = "MMMCMXCIX-3");
assert (case_no 4000 3 = "MV-3");
assert (case_no 9000 3 = "MX-3");
()