This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
/
PrintTypedData.v
352 lines (319 loc) · 11.9 KB
/
PrintTypedData.v
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
Require Import Ascii.
Require Import String.
Require Import List.
Require Import ErgoSpec.Backend.Qcert.QcertModel.
Require Import ErgoSpec.Utils.Misc.
Require Import ErgoSpec.Backend.QLib.
Require Import ErgoSpec.Common.Result.
Require Import ErgoSpec.Common.Names.
Require Import ErgoSpec.Common.Provenance.
Require Import ErgoSpec.Common.NamespaceContext.
Require Import JsAst.JsNumber. (* XXX To be fixed on Q*cert side - JS *)
Local Open Scope string.
Section PrintTypedData.
Definition print_brand (nsctxt:namespace_ctxt) (b:string) : string :=
match get_local_part b with
| None => "~" ++ b
| Some local_name =>
elift_both
(fun resolved_b =>
if string_dec resolved_b b
then
local_name
else
"~" ++ b)
(fun _ => "~" ++ b)
(resolve_type_name dummy_provenance nsctxt (None,local_name))
end.
Definition print_multiple_brands (nsctxt:namespace_ctxt) (bs:list string) : string :=
"<" ++ (map_concat "," (print_brand nsctxt) bs) ++ ">".
Section Data.
Context {br:brand_relation}.
Definition unpack_output
(out : qcert_data)
: option (qcert_data * list qcert_data * qcert_data) :=
match out with
| (dleft (drec (("__emit", dcoll emits)
::("__response", response)
::("__state", state)
::nil))) =>
Some (response, emits, state)
| _ => None
end.
Definition fmt_nl := String.String (ascii_of_nat 10) EmptyString.
Definition fmt_dq := """".
Definition js_quote_char (a:ascii) : string
:= match a with
| "008"%char => "\b"
| "009"%char => "\t"
| "010"%char => "\n"
| "013"%char => "\r"
| """"%char => "\"""
| "\"%char => "\\"
| _ => (String.String a EmptyString)
end.
Definition js_quote_string (s:string) : string
:= flat_map_string js_quote_char s.
Fixpoint string_of_enum (nsctxt:namespace_ctxt) (d : qcert_data) : string :=
match d with
| dleft (dstring x) => x
| dright d' => string_of_enum nsctxt d'
| _ => "???should be enum???"
end.
Fixpoint string_of_data (nsctxt:namespace_ctxt) (d : qcert_data) : string :=
let string_of_rec : list (string * qcert_data) -> string :=
fun rec =>
("{"
++ (String.concat
", "
(map
(fun item =>
(fst item) ++ ": " ++ (string_of_data nsctxt (snd item)))
rec))
++ "}") in
match d with
| dunit => "unit"
| dnat z => toString z
| dfloat f => """" ++ toString f ++ """"
| dbool true => "true"
| dbool false => "false"
| dstring s => toString (dstring s)
| dcoll arr =>
"["
++ (String.concat
", "
(map (string_of_data nsctxt) arr))
++ "]"
| dleft s => "some(" ++ (string_of_data nsctxt s) ++ ")"
| dright _ => "none"
| dbrand (b::nil) (dleft x) => string_of_enum nsctxt (dleft x)
| dbrand (b::nil) (dright x) => string_of_enum nsctxt (dright x)
| dbrand (b::nil) d' => print_brand nsctxt b ++ (string_of_data nsctxt d')
| dbrand _ _ => "???more than one brand???"
| drec r => string_of_rec r
| dforeign (enhanceddateTimeformat f) =>
"dateTimeFormat(""" ++ DateTimeComponent.DATE_TIME_FORMAT_to_string f ++ """)"
| dforeign (enhanceddateTime dt) =>
"dateTime(""" ++ DateTimeComponent.DATE_TIME_format dt (DateTimeComponent.DATE_TIME_FORMAT_from_string "MM/DD/YYYY") ++ """)"
| dforeign (enhanceddateTimeduration dti) =>
"duration(" ++ DateTimeComponent.DATE_TIME_DURATION_to_string dti ++ ")"
| dforeign (enhanceddateTimeperiod dti) =>
"period(" ++ DateTimeComponent.DATE_TIME_PERIOD_to_string dti ++ ")"
end.
End Data.
Section Types.
Import QcertType.
Context {br:brand_model}.
Fixpoint rtype_to_string
(nsctxt:namespace_ctxt) (t : rtype₀) : string :=
match t with
| Bottom₀ => "Nothing"
| Top₀ => "Any"
| Unit₀ => "Unit"
| Nat₀ => "Integer"
| Float₀ => "Double"
| Bool₀ => "Boolean"
| String₀ => "String"
| Coll₀ r' => (rtype_to_string nsctxt r') ++ "[]"
| Rec₀ k srl =>
let recend : string :=
match k with
| Closed => ""
| Open => " .."
end
in
"{" ++
(String.concat
(", ")
(map (fun sr => ((fst sr) ++ ": " ++ (rtype_to_string nsctxt (snd sr))))
srl)) ++ recend ++ "}"
| Either₀ tl tr => (rtype_to_string nsctxt tl) ++ "?"
| Arrow₀ tin tout => (rtype_to_string nsctxt tin) ++ " -> " ++ (rtype_to_string nsctxt tout)
| Brand₀ (b::nil) => print_brand nsctxt b
| Brand₀ bs => print_multiple_brands nsctxt bs
| Foreign₀ enhancedDateTimeFormat => "DateTimeFormat"
| Foreign₀ enhancedDateTime => "DateTime"
| Foreign₀ enhancedDateTimeDuration => "InternalDuration"
| Foreign₀ enhancedDateTimePeriod => "InternalPeriod"
| Foreign₀ _ => "(unknown foreign type)"
end.
Definition qcert_type_to_string
(nsctxt:namespace_ctxt) (t : qcert_type) : string :=
rtype_to_string nsctxt (qcert_type_unpack t).
Definition string_of_result_type
(nsctxt:namespace_ctxt) (result : option qcert_type)
: string :=
match result with
| None => ""
| Some typ => " : " ++ (qcert_type_to_string nsctxt typ)
end.
Definition unpack_error nsctxt kind out :=
ESystemError dummy_provenance
("Cannot unpack type: "
++ (string_of_result_type nsctxt (Some out))
++ " (expected "
++ kind ++ ")").
Definition unpack_failure_type
(nsctxt:namespace_ctxt)
(out : qcert_type)
: eresult qcert_type :=
let osuccess :=
match unteither out with
| None => None
| Some (tl, tr) => Some (tl, tr)
end
in
eresult_of_option
(lift snd osuccess)
(unpack_error nsctxt "either" out)
nil.
Definition unpack_success_type
(nsctxt:namespace_ctxt)
(out:qcert_type)
(warnings: list ewarning)
: eresult (qcert_type * qcert_type * qcert_type) :=
let osuccess :=
match unteither out with
| None => None
| Some (tl, tr) => Some (tl, tr)
end
in
let success :=
eresult_of_option
(lift fst osuccess)
(unpack_error nsctxt "either" out)
nil
in
let response :=
elift fst
(eolift
(fun success =>
(eresult_of_option (qcert_type_infer_unary_op (OpDot this_response) success)
(unpack_error nsctxt this_response out))
nil)
success)
in
let emit :=
elift fst
(eolift
(fun success =>
(eresult_of_option (qcert_type_infer_unary_op (OpDot this_emit) success)
(unpack_error nsctxt this_emit out))
nil)
success)
in
let state :=
elift fst
(eolift
(fun success =>
(eresult_of_option (qcert_type_infer_unary_op (OpDot this_state) success)
(unpack_error nsctxt this_state out))
warnings)
success)
in
elift3 (fun r e s => (r,e,s))
response emit state.
Definition unpack_output_type
(nsctxt:namespace_ctxt)
(out:qcert_type)
(warnings:list ewarning)
: eresult (qcert_type * qcert_type * qcert_type * qcert_type) :=
elift2
(fun x y =>
let '(respt,emitt,statet) := x in
(respt,emitt,statet,y))
(unpack_success_type nsctxt out warnings)
(unpack_failure_type nsctxt out).
End Types.
Section Both.
Context {br:brand_model}.
Definition string_of_response
(nsctxt:namespace_ctxt)
(response:qcert_data)
(response_type:option qcert_type) : string :=
"Response. " ++ (string_of_data nsctxt response) ++ (string_of_result_type nsctxt response_type).
Definition string_of_emits
(nsctxt:namespace_ctxt)
(emits:list qcert_data)
(emit_type:option qcert_type) : string :=
match emits with
| nil => ""
| e1 :: erest =>
(fold_right
(fun new old =>
(old ++ fmt_nl ++ "Emit. " ++ new))
("Emit. " ++ string_of_data nsctxt e1)
(map (string_of_data nsctxt) erest))
++ (string_of_result_type nsctxt emit_type) ++ fmt_nl
end.
Definition string_of_state
(nsctxt:namespace_ctxt)
(old_state : option qcert_data)
(new_state : qcert_data)
(state_type: option qcert_type)
: string :=
let jsonify := string_of_data nsctxt in
match old_state with
| None => fmt_nl ++ "State. " ++ (jsonify new_state) ++ (string_of_result_type nsctxt state_type)
| Some actual_old_state =>
if Data.data_eq_dec new_state actual_old_state
then ""
else
fmt_nl ++ "State. " ++ (jsonify new_state) ++ (string_of_result_type nsctxt state_type)
end.
Definition string_of_typed_data
(nsctxt:namespace_ctxt)
(old_state : option qcert_data)
(data: qcert_data)
(typ: option qcert_type) : string :=
match data with
| dright msg =>
let failure_type :=
match typ with
| None => None
| Some typ => elift_both Some (fun _ => None) (unpack_failure_type nsctxt typ)
end
in
"Failure. " ++ (string_of_data nsctxt msg) ++ (string_of_result_type nsctxt failure_type)
| out =>
match unpack_output out with
| Some (response, emits, state) =>
let '(response_type, emit_type, state_type) :=
match typ with
| None => (None, None, None)
| Some typ =>
elift_both
(fun res => let '(r,e,s) := res in (Some r, Some e, Some s))
(fun _ => (None, None, None))
(unpack_success_type nsctxt typ nil)
end
in
(string_of_emits nsctxt emits emit_type)
++ (string_of_response nsctxt response response_type)
++ (string_of_state nsctxt old_state state state_type)
| None => (string_of_data nsctxt out)
end
end.
Definition string_of_typed_result
(nsctxt:namespace_ctxt)
(old_state : option qcert_data)
(result : option qcert_type * option qcert_data) : string :=
match result with
| (_, None) => ""
| (typ, Some dat) => (string_of_typed_data nsctxt old_state dat typ) ++ fmt_nl
end.
End Both.
End PrintTypedData.