Source file glue.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
let _dbg = Verbose.get_flag "Glue"
let lucky_type_of teff = (
if (teff = CkTypeEff.boolean) then Type.BoolT
else if (teff = CkTypeEff.integer) then Type.IntT
else if (teff = CkTypeEff.real) then Type.FloatT
else (raise (LutErrors.Internal_error (
"Glue.lucky_type_of",
"Unexpected type"^(CkTypeEff.to_string teff)
)))
)
let lucky_exp_zero = Exp.Numer (Exp.Ival (Num.num_of_int 0))
let lucky_exp_of_value = function
| Value.B true -> Exp.Formu Exp.True
| Value.B false -> Exp.Formu Exp.False
| Value.N (Value.I i) -> Exp.Numer (Exp.Ival i)
| Value.N (Value.F f) -> Exp.Numer (Exp.Fval f)
let lucky_exp_var_ref (x: Exp.var) = (
match Var.typ x with
| Type.BoolT -> Exp.Formu (Exp.Bvar x)
| Type.IntT -> Exp.Numer (Exp.Ivar x)
| Type.FloatT -> Exp.Numer (Exp.Fvar x)
| _ -> assert false
)
type id2exp = bool -> CoAlgExp.node -> Exp.t
let lucky_exp_of (eval:bool) (id2exp:id2exp) (e: CoAlgExp.t) = (
let rec _lucky_exp_of e = (
let t = CoAlgExp.get_type e in
if t = CkTypeEff.boolean then
Exp.Formu (_lucky_formula_of e)
else if t = CkTypeEff.integer then
Exp.Numer (_lucky_numexp_of e)
else if t = CkTypeEff.real then
Exp.Numer (_lucky_numexp_of e)
else failwith ("XXX :"^(CkTypeEff.to_string t)^":"^(CoAlgExp.lus_dumps e)^"\n")
)
and _lucky_formula_of e = (
let raw_res = (
let nat = e.CoAlgExp.ae_val in
match nat with
| CoAlgExp.AE_true -> Exp.True
| CoAlgExp.AE_false -> Exp.False
| CoAlgExp.AE_pre id
| CoAlgExp.AE_support id
| CoAlgExp.AE_alias id -> (
match id2exp eval nat with
| Exp.Formu f -> f
| e -> (
let msg = "unexpected exp type for var \""^(CoIdent.to_string id)^"\""^
" bool exp is expected but get \""^(Exp.to_string e)^"\""
in
raise (LutErrors.Internal_error ("Glue.lucky_formula_of", msg))
)
)
| CoAlgExp.AE_call (id, ops) -> (
match (id, ops) with
| ("not", [o]) -> Exp.Not (_lucky_formula_of o)
| ("and", [o1;o2]) -> Exp.And (_lucky_formula_of o1, _lucky_formula_of o2)
| ("or", [o1;o2]) -> Exp.Or (_lucky_formula_of o1, _lucky_formula_of o2)
| ("xor", [o1;o2]) -> Exp.Xor (_lucky_formula_of o1, _lucky_formula_of o2)
| ("nxor", l) -> Exp.NXor (List.map _lucky_formula_of l)
| ("nor", l) -> Exp.Nor (List.map _lucky_formula_of l)
| ("#", l) -> Exp.Diese (List.map _lucky_formula_of l)
| ("impl", [o1;o2]) -> Exp.Impl (_lucky_formula_of o1, _lucky_formula_of o2)
| ("ite", [o1;o2;o3]) -> Exp.IteB (_lucky_formula_of o1,
_lucky_formula_of o2, _lucky_formula_of o3)
| ("lt", [o1;o2]) -> Exp.Inf (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("lte", [o1;o2]) -> Exp.InfEq (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("gt", [o1;o2]) -> Exp.Sup (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("gte", [o1;o2]) -> Exp.SupEq (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("neq", [o1;o2])
| ("eq", [o1;o2]) -> (
let ty = CoAlgExp.get_type o1 in
let eqexp = if (ty = CkTypeEff.boolean ) then
Exp.EqB (_lucky_formula_of o1, _lucky_formula_of o2)
else if ((ty = CkTypeEff.integer) || (ty = CkTypeEff.real)) then (
Exp.Eq (_lucky_numexp_of o1, _lucky_numexp_of o2)
) else (
assert false
) in
if (id = "neq") then Exp.Not eqexp else eqexp
)
| _ -> assert false
)
| CoAlgExp.AE_rconst x ->
raise (LutErrors.Internal_error (
"lucky_formula_of", "unexpected AE_rconst "^x^" CoAlgExp.t" ))
| CoAlgExp.AE_iconst x ->
raise (LutErrors.Internal_error (
"lucky_formula_of", "unexpected AE_iconst "^x^" CoAlgExp.t" ))
| CoAlgExp.AE_rval x ->
raise (LutErrors.Internal_error (
"lucky_formula_of", "unexpected AE_rval "^(string_of_float x)^" CoAlgExp.t" ))
| CoAlgExp.AE_ival x ->
raise (LutErrors.Internal_error (
"lucky_formula_of", "unexpected AE_ival "^
(string_of_int x)^" CoAlgExp.t" ))
| CoAlgExp.AE_const x ->
raise (LutErrors.Internal_error ( "lucky_formula_of", "unexpected AE_const "^x^" CoAlgExp.t" ))
| CoAlgExp.AE_external_call (_id, _ei, _prof, _ops) -> (
raise ( LutErrors.Internal_error (
"Glue.lucky_formula_of",
"sorry, bool type in external function not yet implemented"))
)
) in if eval then ExpEval.simp_formula raw_res else raw_res
)
and _lucky_numexp_of e = (
let raw_res = (
let nat = e.CoAlgExp.ae_val in
match nat with
| CoAlgExp.AE_iconst s -> Exp.Ival (Num.num_of_string s)
| CoAlgExp.AE_rconst s -> Exp.Fval (float_of_string s)
| CoAlgExp.AE_ival i -> Exp.Ival (Num.num_of_int i)
| CoAlgExp.AE_rval r -> Exp.Fval r
| CoAlgExp.AE_pre id
| CoAlgExp.AE_support id
| CoAlgExp.AE_alias id -> (
match id2exp eval nat with
| Exp.Numer n -> n
| e -> (
let msg = "unexpected exp type for var \""^(CoIdent.to_string id)^"\""^
" numerical exp is expected but get \""^(Exp.to_string e)^"\""
in
raise (LutErrors.Internal_error ("Glue._lucky_formula_of", msg))
)
)
| CoAlgExp.AE_call (id, ops) -> (
match (id, ops) with
| ("uminus", [o]) -> Exp.Uminus (_lucky_numexp_of o)
| ("plus", [o1;o2]) -> Exp.Sum (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("minus", [o1;o2]) -> Exp.Diff (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("times", [o1;o2]) -> Exp.Prod (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("slash", [o1;o2]) -> Exp.Quot (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("mod", [o1;o2]) -> Exp.Mod (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("div", [o1;o2]) -> Exp.Div (_lucky_numexp_of o1, _lucky_numexp_of o2)
| ("ite", [o1;o2;o3]) -> Exp.Ite (_lucky_formula_of o1, _lucky_numexp_of o2, _lucky_numexp_of o3)
| ("interval_continue", [o1;o2;o3]) ->
Exp.Icont (_lucky_numexp_of o1, _lucky_numexp_of o2, _lucky_numexp_of o3)
| ("interval_stop", [o1;o2;o3]) ->
Exp.Istop (_lucky_numexp_of o1, _lucky_numexp_of o2, _lucky_numexp_of o3)
| ("gauss_continue", [o1;o2;o3]) ->
Exp.Gcont (_lucky_numexp_of o1, _lucky_numexp_of o2, _lucky_numexp_of o3)
| ("gauss_stop", [o1;o2;o3]) ->
Exp.Gstop (_lucky_numexp_of o1, _lucky_numexp_of o2, _lucky_numexp_of o3)
| _ -> raise ( LutErrors.Internal_error (
"Glue.lucky_numexp_of",
"Unexpected expression \""^(CoAlgExp.lus_dumps e)^"\""
))
)
| CoAlgExp.AE_external_call (id, ei, prof, ops) -> (
let args = List.map _lucky_exp_of ops in
let atypes = List.map lucky_type_of (CkTypeEff.params_of_prof prof) in
let fcargs = (id, ei.CkIdentInfo.ed_sym, atypes, ei.CkIdentInfo.ed_lib_name, args) in
let rtype = match (CkTypeEff.res_of_prof prof) with
| [t] -> lucky_type_of t
| _ -> assert false
in
match rtype with
| Type.IntT -> Exp.IFC fcargs
| Type.FloatT -> Exp.FFC fcargs
| Type.BoolT -> raise ( LutErrors.Internal_error (
"lucky_num_exp_of",
"sorry, bool type in external function not yet implemented"))
| _ -> (
assert false
)
)
| _ -> assert false
) in if eval then ExpEval.simp_num raw_res else raw_res
) in
_lucky_exp_of e
)
let lucky_var_of (id2exp: id2exp)(si : Expand.support_info) = (
let nme = CoIdent.to_string si.Expand.si_ident in
let ty = lucky_type_of si.Expand.si_type in
let mode = match si.Expand.si_nature with
| Expand.Input -> Var.Input
| Expand.Output -> Var.Output
| Expand.LocalOut -> Var.Local
| Expand.LocalIn -> Var.Input
in
let res = Var.make "" nme ty mode in
let res = match si.Expand.si_init with
| None -> res
| Some e -> Var.set_init res (lucky_exp_of true id2exp e)
in
let res = match si.Expand.si_range with
| None -> res
| Some (low, high) ->
let res = Var.set_min res (lucky_exp_of true id2exp low) in
let res = Var.set_max res (lucky_exp_of true id2exp high) in
res
in
res
)