-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ml
155 lines (127 loc) · 2.54 KB
/
util.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
let debug_output = ref false;;
let verbose_output = ref false;;
let verboseOutput a =
if !verbose_output then
Printf.printf a
else
Printf.ifprintf stdout a
;;
let debugOutput a =
if !debug_output then
Printf.printf a
else
Printf.ifprintf stdout a
;;
let rec trmap_aux f l a =
match l with
| [] -> List.rev a
| h :: t ->
trmap_aux f t ((f h) :: a)
;;
let trmap f l =
trmap_aux f l []
;;
let rec trreverse_h l a =
match l with
| [] -> a
| hd :: tl ->
trreverse_h tl (hd :: a)
;;
let trreverse l =
trreverse_h l []
;;
let rec trfoldmap_h f a l res =
match l with
| [] -> trreverse res
| hd :: tl ->
let next = f hd a in
trfoldmap_h f next tl (next :: res)
;;
let rec trconcat_aux ll a =
match ll with
| [] -> a
| h :: t ->
trconcat_aux t (List.rev_append h a)
;;
let trconcat ll =
trconcat_aux ll []
;;
let f res e =
if List.mem e res then
res
else
e::res;;
(* return a list without duplicates *)
let unique list = List.fold_left f [] list;;
(* create a list containing no times the element elem *)
let rec create_list elem no =
if no = 0 then
[]
else
elem :: (create_list elem (no - 1))
;;
let rec create_consecutive start no =
if no = 0 then
[]
else
start :: (create_consecutive (start + 1) (no - 1))
;;
let counter = ref 0;;
let fresh_string prefix =
let result = prefix ^ (string_of_int !counter) in
(
counter := !counter + 1;
result
)
;;
let fresh_variable () =
fresh_string "X"
;;
let fresh_axiom () =
fresh_string "axiom"
;;
let combine list1 list2 =
trconcat (trmap (function x ->
(trmap (function y -> (x, y)) list2)) list1)
;;
let list_diff big small =
List.filter (function x -> not (List.mem x small)) big
;;
let list_intersect list1 list2 =
List.filter (function x -> List.mem x list2) list1
;;
let rec is_prefix small big = match (small, big) with
| ([], _) -> true
| (s :: sr, b :: br) when s = b -> (is_prefix sr br)
| _ -> false
;;
(* iterate f on a until a fixpoint is reached *)
let rec iterate f a =
let next = f a in
if next = a then
a
else
iterate f next
;;
(* iterate "f" on "a" "n" times *)
let rec iterate_n n f a =
if n = 0 then
a
else
iterate_n (n - 1) f (f a)
;;
let rec take n list =
if n = 0 then
[]
else
match list with
| hd :: tl -> hd :: take (n - 1) tl
| [] -> []
;;
let rec all_prefixes = function
| [] -> []
| h :: t -> [] :: (trmap (fun x -> h :: x) (all_prefixes t))
;;
let show_string_list list =
String.concat ", " list
;;