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
type num = I of Num.num | F of float
type t = B of bool | N of num
let to_string e =
match e with
| N(I(i)) ->((Num.string_of_num i) ^ " ")
| N(F(f)) -> ((Util.my_string_of_float f) ^ " ")
| B(true) -> "T "
| B(false) -> "F "
let _to_string2 e =
match e with
| N(I(i)) ->((Num.string_of_num i) ^ " ")
| N(F(f)) -> ((string_of_float f) ^ " ")
| B(true) -> "T "
| B(false) -> "F "
let list_to_string l sep =
String.concat sep (List.map to_string l)
let print oc e = output_string oc (to_string e)
let (to_data_val : t -> Data.v) =
function
B b -> Data.B b
| N(I i)-> Data.I (Util.int_of_num i)
| N(F f)-> Data.F f
let (from_data_val : Data.v -> t) =
function
Data.B b -> B b
| Data.I i -> N(I (Num.num_of_int i))
| Data.F f -> N(F f)
| Data.U -> failwith "undefined variable"
| (Data.E (_, _)|Data.A _|Data.S _|Data.Str _) -> assert false
let (num_value_to_string : num -> string) =
fun n ->
match n with
| I(i) -> Num.string_of_num i
| F(f) -> Util.my_string_of_float f
type name = string
module IdentMap = struct
include Map.Make(
struct
type t = name
let compare = compare
end
)
end
module OfIdent = struct
type value = t
let value_to_string = to_string
type t = value IdentMap.t
let empty:t = IdentMap.empty
let get (n2v:t) (n:name) = IdentMap.find n n2v
let add (n2v:t) ((n,v):name * value) = IdentMap.add n v n2v
let add2 (n2v:t) (n : name) (v:value) = IdentMap.add n v n2v
let add_list (n2v:t) (l:(name * value) list) = List.fold_left add n2v l
let add_list2 (n2v:t) (nl: name list) (vl: value list) =
try List.fold_left2 add2 n2v nl vl
with _ -> assert false
let from_list (l:(name * value) list) = List.fold_left add empty l
let union (x1:t) (x2:t) = IdentMap.fold (fun n v x -> add x (n,v)) x1 x2
let support (x:t) = IdentMap.fold (fun n _v acc -> n::acc) x []
let partition f (x:t) = IdentMap.fold
(fun n v (yes, no) ->
if f (n,v) then (add yes (n,v), no) else (yes, add no (n,v))) x (empty,empty)
let content (x:t) = (
List.fast_sort (fun (vn1, _) (vn2, _) -> compare vn1 vn2)
(IdentMap.fold (fun n v acc -> (n,v)::acc) x [])
)
let to_string (pfx:string) (x:t) = (
if x = empty then pfx^"empty\n"
else (
let nv2s (n,v) = pfx ^ "\t" ^ (n) ^ " = " ^ (value_to_string v) ^ "\n" in
let str_l = List.map nv2s (content x) in
String.concat "" str_l
)
)
let to_short_string (x:t) = (
let nv2s (n,v) = (n) ^ "=" ^ (value_to_string v) in
let str_l = List.map nv2s (content x) in
"{"^(String.concat ";" str_l)^"}"
)
let print (x:t) (oc:out_channel) = output_string oc (to_string "" x)
let mapi = IdentMap.mapi
let iter = IdentMap.iter
let fold = IdentMap.fold
end
let (num_is_int: num -> bool) =
function I _ -> true | F _ -> false
let (add_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
| (I(i1), I(i2)) -> I(Num.add_num i1 i2)
| (F(f1), F(f2)) -> F(f1 +. f2)
| _ -> assert false
let (diff_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
(I(i1), I(i2)) -> I(Num.sub_num i1 i2)
| (F(f1), F(f2)) -> F(f1 -. f2)
| _ -> assert false
let (mult_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
(I(i1), I(i2)) -> I(Num.mult_num i1 i2)
| (F(f1), F(f2)) -> F(f1 *. f2)
| _ -> assert false
let (quot_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
(I(i1), I(i2)) -> I(Num.quo_num i1 i2)
| (F(f1), F(f2)) -> F(f1 /. f2)
| _ -> assert false
let (div_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
(I(i1), I(i2)) -> I(Num.quo_num i1 i2)
| (F(f1), F(f2)) -> F(f1 /. f2)
| _ -> assert false
let (modulo_num : num -> num -> num) =
fun n1 n2 ->
match (n1, n2) with
(I(i1), I(i2)) -> I(Num.mod_num i1 i2)
| (F(f1), F(f2)) -> F(mod_float f1 f2)
| _ -> assert false
let zero = Num.num_of_int 0
let (num_eq_zero : num -> bool) =
fun n ->
match n with
I(i) -> i = zero
| F(f) -> f = 0.
let (num_sup_zero : num -> bool) =
fun n ->
match n with
I(i) -> Num.gt_num i zero
| F(f) -> f > 0.
let (num_supeq_zero : num -> bool) =
fun n ->
match n with
I(i) -> Num.ge_num i zero
| F(f) -> f >= 0.
let (neg : num -> num) =
fun n ->
match n with
I(i) -> I(Num.minus_num i)
| F(f) -> F(-.f)
let (eq_num: num -> num -> bool) =
fun n1 n2 ->
match n1, n2 with
| I n1, I n2 -> Num.eq_num n1 n2
| I _ , F _ | F _, I _ -> false
| F f1, F f2 -> f1 = f2