Source file abstract_format.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
(*
 * Copyright yutopp 2017 - .
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *)

open! Base

module Sf = Simple_term_format
module Z = Aux.Z

(* line number *)
type line_t = int
[@@deriving sexp_of]

(* http://erlang.org/doc/apps/erts/absform.html *)
type t =
  | AbstractCode of form_t

and form_t =
  | ModDecl of form_t list

  | AttrExport of {line: line_t;  function_arity_list: (string * int) list}
  | AttrExportType of {line: line_t; type_arity_list: (string * int) list}
  | AttrImport of {line: line_t; module_name: string; function_arity_list: (string * int) list}
  | AttrMod of {line: line_t; module_name: string}
  | AttrFile of {line: line_t; file: string; file_line: line_t}
  | DeclFun of {line: line_t; function_name: string; arity: int; clauses: clause_t list}
  | SpecFun of {line: line_t; module_name: string option; function_name: string; arity: int; specs: type_t list}
  | Callback of {line: line_t; function_name: string; arity: int; specs: type_t list}
  | DeclRecord of {line: line_t; fields: record_field_t list}
  | DeclType of {line: line_t; name: string; tvars: (line_t * string) list; ty: type_t}
  | DeclOpaqueType of {line: line_t; name: string; tvars: (line_t * string) list; ty: type_t}
  | AttrWild of {line: line_t; attribute: string; term: Sf.t}

  | FormEof

and record_field_t =
  | RecordField of
      {line: line_t; line_field_name: line_t; field_name: string; ty: type_t option; default_expr: expr_t option}

and literal_t =
  | LitAtom of {line: line_t; atom: string}
  | LitChar of {line: line_t; uchar: Uchar.t}
  | LitFloat of {line: line_t; float: float}
  | LitInteger of {line: line_t; integer: int}
  | LitBigInt of {line: line_t; bigint: Z.t}
  | LitString of {line: line_t; str: chars}
and chars =
  | Asciis of string
  (* The char in the [Ascii s] is in 0-255 range chars and length is less than or equal to 65535.
     See also http://erlang.org/doc/apps/erts/erl_ext_dist.html#string_ext *)
  | CharList of int list

and pattern_t =
  | PatBitstr of {line: line_t; elements: pattern_bin_element_t list}
  | PatCompound of {line: line_t; lhs: pattern_t; rhs: pattern_t}
  | PatCons of {line: line_t; head: pattern_t; tail: pattern_t}
  | PatNil of {line: line_t}
  | PatMap of {line: line_t;  assocs: pattern_assoc_t list}
  | PatBinOp of {line: line_t; op: string; lhs: pattern_t; rhs: pattern_t}
  | PatUnaryOp of {line: line_t; op: string; operand: pattern_t}
  | PatRecordFieldIndex of {line: line_t; name: string; line_field_name: line_t; field_name: string}
  | PatRecord of {line: line_t; name: string; record_fields: (line_t * atom_or_wildcard * pattern_t) list}
  | PatTuple of {line: line_t; pats: pattern_t list}
  | PatUniversal of {line: line_t}
  | PatVar of {line: line_t; id: string}
  | PatLit of {lit: literal_t}
and pattern_assoc_t =
  | PatAssocExact of {line: line_t; key: pattern_t; value: pattern_t}
and pattern_bin_element_t =
  | PatBinElement of {pattern: pattern_t; size: expr_t option; tsl: (type_spec_t list) option}

and expr_t =
  | ExprBody of {exprs: expr_t list}
  | ExprBitstr of {line: line_t; elements: expr_bin_element_t list}
  | ExprBitstrComprehension of {line: line_t; expr: expr_t; qualifiers: qualifier_t list}
  | ExprBlock of {line: line_t; exprs: expr_t list}
  | ExprCase of {line: line_t; expr: expr_t; clauses: clause_t list}
  | ExprCatch of {line: line_t; expr: expr_t}
  | ExprCons of {line: line_t; head: expr_t; tail: expr_t}
  | ExprNil of {line: line_t}
  | ExprListComprehension of {line: line_t; expr: expr_t; qualifiers: qualifier_t list}
  | ExprLocalFunRef of {line: line_t; function_name: string; arity: int}
  | ExprRemoteFunRef of {line: line_t; module_name: atom_or_var_t; function_name: atom_or_var_t; arity: integer_or_var_t}
  | ExprFun of {line: line_t; name: string option; clauses: clause_t list}
  | ExprLocalCall of {line: line_t; function_expr: expr_t; args: expr_t list}
  | ExprRemoteCall of {line: line_t;  line_remote: line_t; module_expr: expr_t; function_expr: expr_t; args: expr_t list}
  | ExprIf of {line: line_t; clauses: clause_t list} (* `clauses` must be a list of if-clauses (ClsIf) *)
  | ExprMapCreation of {line: line_t; assocs: expr_assoc_t list}
  | ExprMapUpdate of {line: line_t; map: expr_t; assocs: expr_assoc_t list}
  | ExprMatch of {line: line_t; pattern: pattern_t; body: expr_t}
  | ExprBinOp of {line: line_t; op: string; lhs: expr_t; rhs: expr_t}
  | ExprUnaryOp of {line: line_t; op: string; operand: expr_t}
  | ExprReceive of {line: line_t; clauses: clause_t list} (* `clauses` must be a list of case-clauses (ClsCase) *)
  | ExprReceiveAfter of {line: line_t; clauses: clause_t list; timeout: expr_t; body: expr_t list} (* `clauses` must be a list of case-clauses (ClsCase) *)
  | ExprRecord of {line: line_t; name: string; record_fields: record_field_for_expr list}
  | ExprRecordFieldAccess of {line: line_t; expr: expr_t; name: string; line_field_name: line_t; field_name: string}
  | ExprRecordFieldIndex of {line: line_t; name: string; line_field_name: line_t; field_name: string}
  | ExprRecordUpdate of {line: line_t; expr: expr_t; name: string; update_fields: record_field_for_expr list}
  | ExprTuple of {line: line_t; elements: expr_t list}
  | ExprTry of {line: line_t; exprs: expr_t list; case_clauses: clause_t list; catch_clauses: clause_t list; after: expr_t list}
  | ExprVar of {line: line_t; id: string}
  | ExprLit of {lit: literal_t}
and expr_assoc_t =
  | ExprAssoc of {line: line_t; key: expr_t; value: expr_t}
  | ExprAssocExact of {line: line_t; key: expr_t; value: expr_t}
and qualifier_t =
  | QualifierGenerator of {line: line_t; pattern: pattern_t; expr: expr_t}
  | QualifierFilter of {filter: expr_t}
  | QualifierBitstrGenerator of {line: line_t; pattern: pattern_t; expr: expr_t}
and atom_or_var_t =
  | AtomVarAtom of {line: line_t; atom: string}
  | AtomVarVar of {line: line_t; id: string}
and integer_or_var_t =
  | IntegerVarInteger of {line: line_t; integer: int}
  | IntegerVarVar of {line: line_t; id: string}
and record_field_for_expr =
  | RecordFieldForExpr of {line: line_t; line_name: line_t; name: string; value: expr_t}
and type_spec_t =
  | TypeSpec of {atom: string; value: int option}
and expr_bin_element_t =
  | ExprBinElement of {expr: expr_t; size: expr_t option; tsl: (type_spec_t list) option}

and clause_t =
  | ClsCase of {line: line_t; pattern: pattern_t; guard_sequence: guard_sequence_t option; body: expr_t}
  | ClsCatch of {line: line_t; line_cls: line_t; line_stacktrace: line_t;
                 exception_class: atom_or_var_t; pattern: pattern_t; stacktrace: string;
                 guard_sequence: guard_sequence_t option; body: expr_t}
  | ClsFun of {line: line_t; patterns: pattern_t list; guard_sequence: guard_sequence_t option; body: expr_t}
  | ClsIf of {line: line_t; guard_sequence: guard_sequence_t; body: expr_t} (* guard_sequence must not be empty *)
and guard_sequence_t =
  | GuardSeq of {guards: guard_t list}
and guard_t =
  | Guard of {guard_tests: guard_test_t list}
and guard_test_t =
  | GuardTestBitstr of {line: line_t; elements: guard_test_bin_element_t list}
  | GuardTestCons of {line: line_t; head: guard_test_t; tail: guard_test_t}
  | GuardTestCall of {line: line_t; function_name: literal_t; args: guard_test_t list}
  | GuardTestRemoteCall of {line: line_t; line_remote: line_t; line_module_name: line_t; module_name: string; (* `module_name` must be "erlang" *)
                            line_function_name: line_t; function_name: string; args: guard_test_t list}
  | GuardTestMapCreation of {line: line_t; assocs: guard_test_assoc_t list}
  | GuardTestMapUpdate of {line: line_t; map: guard_test_t; assocs: guard_test_assoc_t list}
  | GuardTestNil of {line: line_t}
  | GuardTestBinOp of {line: line_t; op: string; lhs: guard_test_t; rhs: guard_test_t}
  | GuardTestUnaryOp of {line: line_t; op: string; operand: guard_test_t}
  | GuardTestRecord of {line: line_t; name: string; record_fields: (line_t * atom_or_wildcard * guard_test_t) list}
  | GuardTestRecordFieldAccess of
      {line: line_t; record: guard_test_t; name: string; line_field_name: line_t; field_name: string}
  | GuardTestRecordFieldIndex of {line: line_t; name: string; line_field_name: line_t; field_name: string}
  | GuardTestTuple of {line: line_t; elements: guard_test_t list}
  | GuardTestVar of {line: line_t; id: string}
  | GuardTestLit of {lit: literal_t}
and guard_test_assoc_t =
  | GuardTestAssoc of {line: line_t; key: guard_test_t; value: guard_test_t}
  | GuardTestAssocExact of {line: line_t; key: guard_test_t; value: guard_test_t}
and atom_or_wildcard = (* atom or _ for the fields of a record creation in guard tests *)
  | AtomWildcardAtom of {line: line_t; atom: string}
  | AtomWildcardWildcard of {line: line_t}
and guard_test_bin_element_t =
  | GuardTestBinElement of {guard_test: guard_test_t; size: guard_test_t option; tsl: (type_spec_t list) option}

and type_t =
  | TyAnn of {line: line_t; annotation: type_t; tyvar: type_t}
  | TyBitstring of {line: line_t; m: type_t; n: type_t}
  | TyPredef of {line: line_t; name: string; args: type_t list}
  | TyBinOp of {line: line_t; op: string; lhs: type_t; rhs: type_t} (* lhs and rhs must be an integer, char, binop or unaryop *)
  | TyUnaryOp of {line: line_t; op: string; operand: type_t} (* operand must be an integer, char, binop or unaryop *)
  | TyRange of {line: line_t; low: type_t; high: type_t} (* low and high must be an integer, char, binop or unaryop *)
  | TyAnyMap of {line: line_t}
  | TyMap of {line: line_t; assocs: type_assoc_t list}
  | TyVar of {line: line_t; id: string}
  | TyFunAny of {line: line_t}
  | TyFunAnyArity of {line: line_t; line_any: line_t; ret: type_t}
  | TyContFun of {line: line_t; function_type: type_t; constraints: type_func_cont_t}
  | TyFun of {line: line_t; line_params: line_t; params: type_t list; ret: type_t}
  | TyRecord of {line: line_t; line_name: line_t; name: string; field_types: record_field_type_t list}
  | TyRemote of
      {line: line_t; line_module_name: line_t; module_name: string; line_type_name: line_t; type_name: string;
       params: type_t list}
  | TyAnyTuple of {line: line_t}
  | TyTuple of {line: line_t; elements: type_t list}
  | TyUnion of {line: line_t; elements: type_t list}
  | TyUser of {line: line_t; name: string; args: type_t list}
  | TyLit of {lit: literal_t}
and type_assoc_t =
  | TyAssoc of {line: line_t; key: type_t; value: type_t}
  | TyAssocExact of {line: line_t; key: type_t; value: type_t}

and type_func_cont_t =
  | TyCont of {constraints: type_func_cont_t list}
  | TyContRel of {line: line_t; constraint_kind: type_func_cont_t; lhs: type_t; rhs: type_t}
  | TyContIsSubType of {line: line_t}
and record_field_type_t =
  | RecordFieldType of {line: line_t; line_name: line_t; name: string; ty: type_t}
[@@deriving sexp_of]

type err_t = Sf.t Err.t
[@@deriving sexp_of]

let track ~loc result =
  Result.map_error ~f:(Err.record_backtrace ~loc:loc) result

(* bitstring element type specifiers *)
let tsl_of_sf sf =
  let open Result.Let_syntax in
  match sf with
  | Sf.List sf_tss ->
     let ts_of_sf = function
       | Sf.Atom atom -> TypeSpec {atom; value=None} |> return
       | Sf.Tuple (2, [Sf.Atom atom; Sf.Integer value]) -> TypeSpec {atom; value=Some value} |> return
       | _ -> Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of type specifier", sf)) |> Result.fail
     in
     sf_tss |> List.map ~f:ts_of_sf |> Result.all |> track ~loc:[%here]
  | _ ->
     Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of type specifiers", sf)) |> Result.fail

(* bitstring element *)
(* This function is used at bitstring constructor expression, pattern, and guard test. *)
(* NOTE: This function cannot be contained in big mutual recursions started at `of_sf` without explicit type signature *)
(*       because type inference for polymorphic recursion is undecidable. *)
(*       ref: https://discuss.ocaml.org/t/value-restriction-and-mutually-recursive-functions/2432 *)
let bin_element_of_sf ~value_of_sf ~size_of_sf sf =
  let open Result.Let_syntax in
  match sf with
  | Sf.Tuple (5, [Sf.Atom "bin_element"; Sf.Integer line; sf_value; sf_size; sf_tsl]) ->
     let default_or of_sf = function
       | Sf.Atom "default" -> None |> return
       | sf -> sf |> of_sf |> Result.map ~f:(fun e -> Some e) |> track ~loc:[%here]
     in
     let%bind value = sf_value |> value_of_sf |> track ~loc:[%here] in
     let%bind size = sf_size |> default_or size_of_sf |> track ~loc:[%here] in
     let%bind tsl = sf_tsl |> default_or tsl_of_sf |> track ~loc:[%here] in
     (value, size, tsl) |> return
  | _ ->
     Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of bin_element", sf)) |> Result.fail

(*
 * Entry
 *)
let rec of_sf sf : (t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  | Sf.Tuple (2, [Sf.Atom "raw_abstract_v1"; sf_forms]) ->
     let%bind forms =
       sf_forms |> form_of_sf |> track ~loc:[%here]
     in
     AbstractCode forms |> return

  (* is it suitable here? *)
  | Sf.Tuple (3, [Sf.Atom "debug_info_v1";
                  Sf.Atom "erl_abstract_code";
                  Sf.Tuple (2, [sf_forms; _options])]) ->
     let%bind forms =
       sf_forms |> form_of_sf |> track ~loc:[%here]
     in
     AbstractCode forms |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("root", sf)) |> Result.fail

(*
 * 8.1  Module Declarations and Forms
 *)
and form_of_sf sf : (form_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* module declaration *)
  | Sf.List sf_forms ->
     let%bind forms = sf_forms |> List.map ~f:form_of_sf |> Result.all |> track ~loc:[%here] in
     ModDecl forms |> return

  (* attribute -export *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "export";
                  Sf.List sf_function_arity_list
             ]) ->
     let%bind function_arity_list =
       sf_function_arity_list |> List.map ~f:name_and_arity_of_sf |> Result.all |> track ~loc:[%here]
     in
     AttrExport {line; function_arity_list} |> return

  (* attribute -export_type *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "export_type";
                  Sf.List sf_type_arity_list
             ]) ->
     let%bind type_arity_list =
       sf_type_arity_list |> List.map ~f:name_and_arity_of_sf |> Result.all |> track ~loc:[%here]
     in
     AttrExportType {line; type_arity_list} |> return

  (* attribute -import *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "import";
                  Sf.Tuple (2, [Sf.Atom module_name;
                                Sf.List sf_function_arity_list])
             ]) ->
     let%bind function_arity_list =
       sf_function_arity_list |> List.map ~f:name_and_arity_of_sf |> Result.all |> track ~loc:[%here]
     in
     AttrImport {line; module_name; function_arity_list} |> return

  (* attribute -module *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "module";
                  Sf.Atom module_name
             ]) ->
     AttrMod {line; module_name} |> return

  (* attribute -file *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "file";
                  Sf.Tuple (2, [Sf.String file; Sf.Integer file_line])
             ]) ->
     AttrFile {line; file_line; file} |> return

  (* function declaration *)
  | Sf.Tuple (5, [Sf.Atom "function";
                  Sf.Integer line;
                  Sf.Atom function_name;
                  Sf.Integer arity;
                  Sf.List sf_clauses
             ]) ->
     let%bind clauses =
       sf_clauses |> List.map ~f:(cls_of_sf ~in_function:true) |> Result.all |> track ~loc:[%here]
     in
     DeclFun {line; function_name; arity; clauses} |> return

  (* function specification *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "spec";
                  Sf.Tuple (2, [Sf.Tuple (2, [Sf.Atom function_name; Sf.Integer arity]);
                                Sf.List sf_specs])
             ]) ->
     let%bind specs = sf_specs |> List.map ~f:fun_type_of_sf |> Result.all |> track ~loc:[%here] in
     let module_name = None in
     SpecFun {line; module_name; function_name; arity; specs} |> return

  (* function specification (callback attribute) *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "callback";
                  Sf.Tuple (2, [Sf.Tuple (2, [Sf.Atom function_name; Sf.Integer arity]);
                                Sf.List sf_specs])
             ]) ->
     let%bind specs = sf_specs |> List.map ~f:fun_type_of_sf |> Result.all |> track ~loc:[%here] in
     Callback {line; function_name; arity; specs} |> return

  (* function specification(Mod) *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "spec";
                  Sf.Tuple (2, [Sf.Tuple (3, [Sf.Atom module_name;
                                              Sf.Atom function_name;
                                              Sf.Integer arity]);
                                Sf.List sf_specs])
             ]) ->
     let%bind specs = sf_specs |> List.map ~f:fun_type_of_sf |> Result.all |> track ~loc:[%here] in
     let module_name = Some module_name in
     SpecFun {line; module_name; function_name; arity; specs} |> return

  (* record declaration *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "record";
                  Sf.Tuple (2, [Sf.Atom name; Sf.List sf_fields])
             ]) ->
     let%bind fields =
       sf_fields |> List.map ~f:record_field_of_sf |> Result.all |> track ~loc:[%here]
     in
     DeclRecord {line; fields} |> return

  (* type declaration *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "type";
                  Sf.Tuple (3, [Sf.Atom name;
                                sf_ty;
                                Sf.List sf_tvars
                           ]);
             ]) ->
     let%bind ty = sf_ty |> type_of_sf |> track ~loc:[%here] in
     let%bind tvars = sf_tvars |> List.map ~f:tvar_of_sf |> Result.all |> track ~loc:[%here] in
     DeclType {line; name; tvars; ty} |> return

  (* opaque type declaration *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom "opaque";
                  Sf.Tuple (3, [Sf.Atom name;
                                sf_ty;
                                Sf.List sf_tvars
                           ]);
             ]) ->
     let%bind ty = sf_ty |> type_of_sf |> track ~loc:[%here] in
     let%bind tvars = sf_tvars |> List.map ~f:tvar_of_sf |> Result.all |> track ~loc:[%here] in
     DeclOpaqueType {line; name; tvars; ty} |> return

  (* wild attribute *)
  | Sf.Tuple (4, [Sf.Atom "attribute";
                  Sf.Integer line;
                  Sf.Atom attribute;
                  term]) ->
     AttrWild {line; attribute; term} |> return

  (* eof *)
  | Sf.Tuple (2, [Sf.Atom "eof"; Sf.Integer line]) ->
     FormEof |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("form", sf)) |> Result.fail

and name_and_arity_of_sf sf : ((string * int), err_t) Result.t =
  match sf with
  | Sf.Tuple (2, [Sf.Atom name; Sf.Integer arity]) ->
     Ok (name, arity)

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("name_and_arity", sf)) |> Result.fail

and record_field_of_sf sf : (record_field_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  | Sf.Tuple (3, [Sf.Atom "record_field";
                  Sf.Integer line;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name]);
             ]) ->
     RecordField {line; line_field_name; field_name; ty=None; default_expr=None} |> return

  | Sf.Tuple (4, [Sf.Atom "record_field";
                  Sf.Integer line;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name]);
                  sf_e
             ]) ->
     let%bind e = sf_e |> expr_of_sf |> track ~loc:[%here] in
     RecordField {line; line_field_name; field_name; ty=None; default_expr=Some e} |> return

  | Sf.Tuple (3, [Sf.Atom "typed_record_field";
                  Sf.Tuple (3, [Sf.Atom "record_field";
                                Sf.Integer line;
                                Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])
                           ]);
                  sf_t
             ]) ->
     let%bind t = sf_t |> type_of_sf |> track ~loc:[%here] in
     RecordField {line; line_field_name; field_name; ty=Some t; default_expr=None} |> return

  | Sf.Tuple (3, [Sf.Atom "typed_record_field";
                  Sf.Tuple (4, [Sf.Atom "record_field";
                                Sf.Integer line;
                                Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name]);
                                sf_e
                           ]);
                  sf_t
             ]) ->
     let%bind e = sf_e |> expr_of_sf |> track ~loc:[%here] in
     let%bind t = sf_t |> type_of_sf |> track ~loc:[%here] in
     RecordField {line; line_field_name; field_name; ty=Some t; default_expr=Some e} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("record_field", sf)) |> Result.fail

and tvar_of_sf sf : ((line_t * string), err_t) Result.t =
  match sf with
  | Sf.Tuple (3, [Sf.Atom "var";
                  Sf.Integer line;
                  Sf.Atom tvar
             ]) ->
     Ok (line, tvar)

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("tvar", sf)) |> Result.fail

(*
 * 8.2  Atomic Literals
 *)
and lit_of_sf sf : (literal_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  | Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line; Sf.Atom atom]) ->
     LitAtom {line; atom} |> return

  | Sf.Tuple (3, [Sf.Atom "char"; Sf.Integer line; Sf.Integer c]) ->
     begin match Uchar.of_scalar c with
     | None ->
        Err.create ~loc:[%here] (Err.Invalid_input ("not a valid unicode scalar value", sf)) |> Result.fail
     | Some uchar ->
        LitChar {line; uchar} |> return
     end

  | Sf.Tuple (3, [Sf.Atom "float"; Sf.Integer line; Sf.Float float]) ->
     LitFloat {line; float} |> return

  | Sf.Tuple (3, [Sf.Atom "integer"; Sf.Integer line; Sf.Integer integer]) ->
     LitInteger {line; integer} |> return

  | Sf.Tuple (3, [Sf.Atom "integer"; Sf.Integer line; Sf.BigInt bigint]) ->
     LitBigInt {line; bigint} |> return

  | Sf.Tuple (3, [Sf.Atom "string"; Sf.Integer line; Sf.String s]) ->
     LitString {line; str=Asciis s} |> return

  | Sf.Tuple (3, [Sf.Atom "string"; Sf.Integer line; Sf.List sf_chars]) ->
     let f = function
       | Sf.Integer char -> return char
       | _ ->
          Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of a string literal", sf)) |> Result.fail
     in
     let%bind chars = sf_chars |> List.map ~f |> Result.all |> track ~loc:[%here] in
     LitString {line; str=CharList chars} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("lit", sf)) |> Result.fail

(*
 * 8.3  Patterns
 *)
and pat_of_sf sf : (pattern_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* a bitstring pattern *)
  | Sf.Tuple (3, [Sf.Atom "bin"; Sf.Integer line; Sf.List sf_elements]) ->
     let%bind elements =
       sf_elements
       |> List.map ~f:(bin_element_of_sf ~value_of_sf:pat_of_sf ~size_of_sf:expr_of_sf)
       |> Result.all
       |> Result.map ~f:(List.map ~f:(fun (pattern, size, tsl) -> PatBinElement {pattern; size; tsl}))
       |> track ~loc:[%here]
     in
     PatBitstr {line; elements} |> return

  (* a compound pattern *)
  | Sf.Tuple (4, [Sf.Atom "match"; Sf.Integer line; sf_lhs; sf_rhs]) ->
     let%bind lhs = sf_lhs |> pat_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> pat_of_sf |> track ~loc:[%here] in
     PatCompound {line; lhs; rhs} |> return

  (* a cons pattern *)
  | Sf.Tuple (4, [Sf.Atom "cons"; Sf.Integer line; sf_head; sf_tail]) ->
     let%bind head = sf_head |> pat_of_sf |> track ~loc:[%here] in
     let%bind tail = sf_tail |> pat_of_sf |> track ~loc:[%here] in
     PatCons {line; head; tail} |> return

  (* a nil pattern *)
  | Sf.Tuple (2, [Sf.Atom "nil"; Sf.Integer line]) ->
     PatNil {line} |> return

  (* a map pattern *)
  | Sf.Tuple (3, [Sf.Atom "map"; Sf.Integer line; Sf.List sf_assocs]) ->
     let%bind assocs = sf_assocs |> List.map ~f:pat_assoc_of_sf |> Result.all |> track ~loc:[%here] in
     PatMap {line; assocs} |> return

  (* a binary operator pattern *)
  | Sf.Tuple (5, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; sf_lhs; sf_rhs]) ->
     let%bind lhs = sf_lhs |> pat_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> pat_of_sf |> track ~loc:[%here] in
     PatBinOp {line; op; lhs; rhs} |> return

  (* a unary operator pattern *)
  | Sf.Tuple (4, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; sf_operand]) ->
     let%bind operand = sf_operand |> pat_of_sf |> track ~loc:[%here] in
     PatUnaryOp {line; op; operand} |> return

  (* a record field index pattern : #user.name *)
  | Sf.Tuple (4, [Sf.Atom "record_index";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])]) ->
     PatRecordFieldIndex {line; name; line_field_name; field_name} |> return

  (* a record pattern : #user{name = "Taro", admin = true} *)
  | Sf.Tuple (4, [Sf.Atom "record";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_record_fields]) ->
     let field_of_sf sf =
       begin match sf with
       | Sf.Tuple (4, [Sf.Atom "record_field";
                       Sf.Integer line;
                       sf_atom_or_wildcard;
                       sf_pattern]) ->
          let%bind field_name = atom_or_wildcard_of_sf sf_atom_or_wildcard |> track ~loc:[%here] in
          let%bind rhs = pat_of_sf sf_pattern |> track ~loc:[%here] in
          (line, field_name, rhs) |> return
       | _ ->
          Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of a field of record pattern", sf)) |> Result.fail
       end
     in
     let%bind record_fields = sf_record_fields |> List.map ~f:field_of_sf |> Result.all |> track ~loc:[%here] in
     PatRecord {line; name; record_fields} |> return

  (* a tuple pattern *)
  | Sf.Tuple (3, [Sf.Atom "tuple"; Sf.Integer line; Sf.List sf_pats]) ->
     let%bind pats = sf_pats |> List.map ~f:pat_of_sf |> Result.all |> track ~loc:[%here] in
     PatTuple {line; pats} |> return

  (* a variable pattern *)
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom "_"]) ->
     PatUniversal {line} |> return

  (* a variable pattern *)
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) ->
     PatVar {line; id} |> return

  (* atomic literal *)
  | sf_lit ->
     let%bind lit = sf_lit |> lit_of_sf |> track ~loc:[%here] in
     PatLit {lit} |> return

and pat_assoc_of_sf sf : (pattern_assoc_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* an exact association *)
  | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; sf_key; sf_value]) ->
     let%bind key = sf_key |> pat_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> pat_of_sf |> track ~loc:[%here] in
     PatAssocExact {line; key; value} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("pat_assoc", sf)) |> Result.fail

(*
 * 8.4  Expressions
 *)
and expr_of_sf sf : (expr_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  | Sf.List sf_exprs ->
     let%bind exprs = sf_exprs |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprBody {exprs} |> return

  (* a bitstring constructor *)
  | Sf.Tuple (3, [Sf.Atom "bin"; Sf.Integer line; Sf.List sf_elements]) ->
     let%bind elements =
       sf_elements
       |> List.map ~f:(bin_element_of_sf ~value_of_sf:expr_of_sf ~size_of_sf:expr_of_sf)
       |> Result.all
       |> Result.map ~f:(List.map ~f:(fun (expr, size, tsl) -> ExprBinElement {expr; size; tsl}))
       |> track ~loc:[%here]
     in
     ExprBitstr {line; elements} |> return

  (* a bitstring comprehension *)
  | Sf.Tuple (4, [Sf.Atom "bc"; Sf.Integer line; sf_expr; Sf.List sf_qualifiers]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind qualifiers = sf_qualifiers |> List.map ~f:qualifier_of_sf |> Result.all |> track ~loc:[%here] in
     ExprBitstrComprehension {line; expr; qualifiers} |> return

  (* a block expression *)
  | Sf.Tuple (3, [Sf.Atom "block"; Sf.Integer line; Sf.List sf_exprs]) ->
     let%bind exprs = sf_exprs |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprBlock {line; exprs} |> return

  (* a case expression *)
  | Sf.Tuple (4, [Sf.Atom "case"; Sf.Integer line; sf_expr; Sf.List sf_clauses]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind clauses = sf_clauses |> List.map ~f:cls_of_sf |> Result.all |> track ~loc:[%here] in
     ExprCase {line; expr; clauses} |> return

  (* a catch expression *)
  | Sf.Tuple (3, [Sf.Atom "catch"; Sf.Integer line; sf_expr]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     ExprCatch {line; expr} |> return

  (* a cons expression *)
  | Sf.Tuple (4, [Sf.Atom "cons"; Sf.Integer line; sf_head; sf_tail]) ->
     let%bind head = sf_head |> expr_of_sf |> track ~loc:[%here] in
     let%bind tail = sf_tail |> expr_of_sf |> track ~loc:[%here] in
     ExprCons {line; head; tail} |> return

  (* a nil expression *)
  | Sf.Tuple (2, [Sf.Atom "nil"; Sf.Integer line]) ->
     ExprNil {line} |> return

  (* a list comprehension *)
  | Sf.Tuple (4, [Sf.Atom "lc";
                  Sf.Integer line;
                  sf_expr;
                  Sf.List sf_qualifiers]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind qualifiers = sf_qualifiers |> List.map ~f:qualifier_of_sf |> Result.all |> track ~loc:[%here] in
     ExprListComprehension {line; expr; qualifiers} |> return

  (* a local function reference *)
  | Sf.Tuple (3, [Sf.Atom "fun";
                  Sf.Integer line;
                  Sf.Tuple (3, [Sf.Atom "function";
                                Sf.Atom function_name;
                                Sf.Integer arity])]) ->
    ExprLocalFunRef {line; function_name; arity} |> return

  (* a remote function reference *)
  | Sf.Tuple (3, [Sf.Atom "fun";
                  Sf.Integer line;
                  Sf.Tuple (4, [Sf.Atom "function";
                                sf_module_name;
                                sf_function_name;
                                sf_arity])]) ->
    let%bind module_name = sf_module_name |> atom_or_var_of_sf |> track ~loc:[%here] in
    let%bind function_name = sf_function_name |> atom_or_var_of_sf |> track ~loc:[%here] in
    let%bind arity = sf_arity |> integer_or_var_of_sf |> track ~loc:[%here] in
    ExprRemoteFunRef {line; module_name; function_name; arity} |> return

  (* a function expression *)
  | Sf.Tuple (3, [Sf.Atom "fun";
                  Sf.Integer line;
                  Sf.Tuple (2, [Sf.Atom "clauses";
                                Sf.List sf_clauses])]) ->
    let%bind clauses = sf_clauses |> List.map ~f:(cls_of_sf ~in_function:true) |> Result.all |> track ~loc:[%here] in
    ExprFun {line; name = None; clauses} |> return

  (* a named function expression *)
  | Sf.Tuple (4, [Sf.Atom "named_fun";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_clauses]) ->
    let%bind clauses = sf_clauses |> List.map ~f:(cls_of_sf ~in_function:true) |> Result.all |> track ~loc:[%here] in
    ExprFun {line; name = Some name; clauses} |> return

  (* a function call (remote) *)
  | Sf.Tuple (4, [Sf.Atom "call";
                  Sf.Integer line;
                  Sf.Tuple (4, [Sf.Atom "remote"; Sf.Integer line_remote; sf_module_expr; sf_function_expr]);
                  Sf.List sf_args]) ->
     let%bind module_expr = sf_module_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind function_expr = sf_function_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind args = sf_args |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprRemoteCall {line; line_remote; module_expr; function_expr; args} |> return

  (* a function call (local) *)
  | Sf.Tuple (4, [Sf.Atom "call"; Sf.Integer line; sf_function_expr; Sf.List sf_args]) ->
     let%bind function_expr = sf_function_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind args = sf_args |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprLocalCall {line; function_expr; args} |> return

  (* an if expression *)
  | Sf.Tuple (3, [Sf.Atom "if";
                  Sf.Integer line;
                  Sf.List sf_clauses]) ->
     let%bind clauses = sf_clauses |> List.map ~f:(cls_of_sf ~in_function:false) |> Result.all |> track ~loc:[%here] in
     ExprIf {line; clauses} |> return

  (* a map creation *)
  | Sf.Tuple (3, [Sf.Atom "map";
                  Sf.Integer line;
                  Sf.List sf_assocs]) ->
     let%bind assocs = sf_assocs |> List.map ~f:expr_assoc_of_sf |> Result.all |> track ~loc:[%here] in
     ExprMapCreation {line; assocs} |> return

  (* a map update *)
  | Sf.Tuple (4, [Sf.Atom "map";
                  Sf.Integer line;
                  sf_map;
                  Sf.List sf_assocs]) ->
     let%bind map = sf_map |> expr_of_sf |> track ~loc:[%here] in
     let%bind assocs = sf_assocs |> List.map ~f:expr_assoc_of_sf |> Result.all |> track ~loc:[%here] in
     ExprMapUpdate {line; map; assocs} |> return

  (* match operator expression *)
  | Sf.Tuple (4, [Sf.Atom "match";
                  Sf.Integer line;
                  sf_pattern;
                  sf_body]) ->
     let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ExprMatch {line; pattern; body} |> return

  (* an operator expression binary *)
  | Sf.Tuple (5, [Sf.Atom "op";
                  Sf.Integer line;
                  Sf.Atom op;
                  sf_lhs;
                  sf_rhs]) ->
     let%bind lhs = sf_lhs |> expr_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> expr_of_sf |> track ~loc:[%here] in
     ExprBinOp {line; op; lhs; rhs} |> return

  (* an operator expression unary *)
  | Sf.Tuple (4, [Sf.Atom "op";
                  Sf.Integer line;
                  Sf.Atom op;
                  sf_operand]) ->
     let%bind operand = sf_operand |> expr_of_sf |> track ~loc:[%here] in
     ExprUnaryOp {line; op; operand} |> return

  (* a receive expression *)
  | Sf.Tuple (3, [Sf.Atom "receive";
                  Sf.Integer line;
                  Sf.List sf_clauses]) ->
     let%bind clauses = sf_clauses |> List.map ~f:cls_of_sf |> Result.all |> track ~loc:[%here] in
     ExprReceive {line; clauses} |> return

  (* a receive-after expression *)
  | Sf.Tuple (5, [Sf.Atom "receive";
                  Sf.Integer line;
                  Sf.List sf_clauses;
                  sf_timeout;
                  Sf.List sf_body]) ->
     let%bind clauses = sf_clauses |> List.map ~f:cls_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind timeout = sf_timeout |> expr_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprReceiveAfter {line; clauses; timeout; body} |> return

  (* a record creation : #user{name = "Taro", admin = true} *)
  | Sf.Tuple (4, [Sf.Atom "record";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_record_fields]) ->
     let%bind record_fields = sf_record_fields |> List.map ~f:record_field_for_expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprRecord {line; name; record_fields} |> return

  (* a record field access : U#user.name *)
  | Sf.Tuple (5, [Sf.Atom "record_field";
                  Sf.Integer line;
                  sf_expr;
                  Sf.Atom name;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     ExprRecordFieldAccess {line; expr; name; line_field_name; field_name} |> return

  (* a record field index : #user.name *)
  | Sf.Tuple (4, [Sf.Atom "record_index";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])]) ->
     ExprRecordFieldIndex {line; name; line_field_name; field_name} |> return

  (* a record update : U#user{admin = true} *)
  | Sf.Tuple (5, [Sf.Atom "record";
                  Sf.Integer line;
                  sf_expr;
                  Sf.Atom name;
                  Sf.List sf_update_fields]) ->
     let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
     let%bind update_fields = sf_update_fields |> List.map ~f:record_field_for_expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprRecordUpdate {line; expr; name; update_fields} |> return

  (* a tuple skeleton *)
  | Sf.Tuple (3, [Sf.Atom "tuple";
                  Sf.Integer line;
                  Sf.List sf_elements]) ->
     let%bind elements = sf_elements |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprTuple {line; elements} |> return

  (* a try expression *)
  | Sf.Tuple (6, [Sf.Atom "try";
                  Sf.Integer line;
                  Sf.List sf_exprs;
                  Sf.List sf_case_clauses;
                  Sf.List sf_catch_clauses;
                  Sf.List sf_after]) ->
     let%bind exprs = sf_exprs |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind case_clauses = sf_case_clauses |> List.map ~f:cls_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind catch_clauses = sf_catch_clauses |> List.map ~f:cls_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind after = sf_after |> List.map ~f:expr_of_sf |> Result.all |> track ~loc:[%here] in
     ExprTry {line; exprs; case_clauses; catch_clauses; after} |> return

  (* a variable *)
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) ->
     ExprVar {line; id} |> return

  (* atomic literal *)
  | sf_lit ->
     let%bind lit = sf_lit |> lit_of_sf |> track ~loc:[%here] in
     ExprLit {lit} |> return

and expr_assoc_of_sf sf : (expr_assoc_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* an association *)
  | Sf.Tuple (4, [Sf.Atom "map_field_assoc"; Sf.Integer line; sf_key; sf_value]) ->
     let%bind key = sf_key |> expr_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> expr_of_sf |> track ~loc:[%here] in
     ExprAssoc {line; key; value} |> return

  (* an exact association *)
  | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; sf_key; sf_value]) ->
     let%bind key = sf_key |> expr_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> expr_of_sf |> track ~loc:[%here] in
     ExprAssocExact {line; key; value} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("expr_assoc", sf)) |> Result.fail

and qualifier_of_sf sf : (qualifier_t, err_t) Result.t =
    let open Result.Let_syntax in
    match sf with
    (* generator qualifier *)
    | Sf.Tuple (4, [Sf.Atom "generate"; Sf.Integer line; sf_pattern; sf_expr]) ->
       let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
       let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
       QualifierGenerator {line; pattern; expr} |> return
    (* bitstring generator qualifier *)
    | Sf.Tuple (4, [Sf.Atom "b_generate"; Sf.Integer line; sf_pattern; sf_expr]) ->
       let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
       let%bind expr = sf_expr |> expr_of_sf |> track ~loc:[%here] in
       QualifierBitstrGenerator {line; pattern; expr} |> return
    (* filter qualifier *)
    | sf_filter ->
       let%bind filter = sf_filter |> expr_of_sf |> track ~loc:[%here] in
       QualifierFilter {filter} |> return

and atom_or_var_of_sf sf : (atom_or_var_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* atom *)
  | (Sf.Tuple (3, [(Sf.Atom "atom");
                   (Sf.Integer line);
                   (Sf.Atom atom)])) ->
    AtomVarAtom {line; atom} |> return
  (* variable *)
  | (Sf.Tuple (3, [(Sf.Atom "var");
                   (Sf.Integer line);
                   (Sf.Atom id)])) ->
    AtomVarVar {line; id} |> return
  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("atom_or_var", sf)) |> Result.fail

and record_field_for_expr_of_sf sf =
  let open Result.Let_syntax in
  match record_field_of_sf sf with
  | Ok (RecordField {line; line_field_name; field_name; ty=None; default_expr=Some value}) ->
     RecordFieldForExpr {line; line_name=line_field_name; name=field_name; value} |> return
  | Ok _ ->
     Err.create ~loc:[%here] (Err.Invalid_input ("the field of a record expr must have an expression", sf)) |> Result.fail
  | Error e -> Error e |> track ~loc:[%here]

and integer_or_var_of_sf sf =
  let open Result.Let_syntax in
  match sf with
  (* integer *)
  | (Sf.Tuple (3, [(Sf.Atom "integer");
                   (Sf.Integer line);
                   (Sf.Integer integer)])) ->
    IntegerVarInteger {line; integer} |> return
  (* variable *)
  | (Sf.Tuple (3, [(Sf.Atom "var");
                   (Sf.Integer line);
                   (Sf.Atom id)])) ->
    IntegerVarVar {line; id} |> return
  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("integer_or_var", sf)) |> Result.fail

(*
 * 8.5  Clauses
 *)
and cls_of_sf ?(in_function=false) sf : (clause_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf, in_function with
  (* catch clause P -> B or E:P -> B or E:P:S -> B *)
  | Sf.Tuple  (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List [Sf.Tuple (3, [Sf.Atom "tuple";
                                        Sf.Integer line_cls;
                                        Sf.List [sf_exception_class;
                                                 sf_pattern;
                                                 Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line_stacktrace; Sf.Atom stacktrace])]])];
                 Sf.List [];
                 sf_body
              ]), false ->
     let%bind exception_class = sf_exception_class |> atom_or_var_of_sf |> track ~loc:[%here] in
     let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsCatch {line; line_cls; line_stacktrace; exception_class; pattern; stacktrace; guard_sequence=None; body} |> return

  (* catch clause P when Gs -> B or E:P when Gs -> B or E:P:S when Gs -> B *)
  | Sf.Tuple  (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List [Sf.Tuple (3, [Sf.Atom "tuple";
                                        Sf.Integer line_cls;
                                        Sf.List [sf_exception_class;
                                                 sf_pattern;
                                                 Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line_stacktrace; Sf.Atom stacktrace])]])];
                 sf_guard_sequence;
                 sf_body
              ]), false ->
     let%bind exception_class = sf_exception_class |> atom_or_var_of_sf |> track ~loc:[%here] in
     let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
     let%bind guard_sequence = sf_guard_sequence |> guard_sequence_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsCatch {line; line_cls; line_stacktrace; exception_class; pattern; stacktrace; guard_sequence=Some guard_sequence; body} |> return

  (* case clause P -> B *)
  | Sf.Tuple (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List [sf_pattern];
                 Sf.List [];
                 sf_body
             ]), false ->
     let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsCase {line; pattern; guard_sequence = None; body} |> return

  (* case clause P -> B when Gs *)
  | Sf.Tuple (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List [sf_pattern];
                 sf_guard_sequence;
                 sf_body
             ]), false ->
     let%bind pattern = sf_pattern |> pat_of_sf |> track ~loc:[%here] in
     let%bind guard_sequence = sf_guard_sequence |> guard_sequence_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsCase {line; pattern; guard_sequence = Some guard_sequence; body} |> return

  (* if clause Gs -> B *)
  | Sf.Tuple (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List [];
                 sf_guard_sequence;
                 sf_body
             ]), false ->
     let%bind guard_sequence = sf_guard_sequence |> guard_sequence_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsIf {line; guard_sequence; body} |> return

  (* function clause ( Ps ) -> B *)
  | Sf.Tuple (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List sf_patterns;
                 Sf.List [];
                 sf_body
             ]), true ->
     let%bind patterns = sf_patterns |> List.map ~f:pat_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsFun {line; patterns; guard_sequence = None; body} |> return

  (* function clause ( Ps ) when Gs -> B *)
  | Sf.Tuple (5, [
                 Sf.Atom "clause";
                 Sf.Integer line;
                 Sf.List sf_patterns;
                 sf_guard_sequence;
                 sf_body
             ]), true ->
     let%bind patterns = sf_patterns |> List.map ~f:pat_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind guard_sequence = sf_guard_sequence |> guard_sequence_of_sf |> track ~loc:[%here] in
     let%bind body = sf_body |> expr_of_sf |> track ~loc:[%here] in
     ClsFun {line; patterns; guard_sequence = Some guard_sequence; body} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("cls", sf)) |> Result.fail

(*
 * 8.6  Guards
 *)
and guard_sequence_of_sf sf : (guard_sequence_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* empty or non-empty sequence *)
  | Sf.List sf_guards ->
     let%bind guards = sf_guards |> List.map ~f:guard_of_sf |> Result.all |> track ~loc:[%here] in
     GuardSeq {guards} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("guard_sequence", sf)) |> Result.fail

and guard_of_sf sf : (guard_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* non-empty sequence *)
  | Sf.List sf_guard_tests when List.length sf_guard_tests > 0 ->
     let%bind guard_tests = sf_guard_tests |> List.map ~f:guard_test_of_sf |> Result.all |> track ~loc:[%here] in
     Guard {guard_tests} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("guard", sf)) |> Result.fail

and guard_test_of_sf sf : (guard_test_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* bitstring constructor *)
  | Sf.Tuple (3, [Sf.Atom "bin"; Sf.Integer line; Sf.List sf_elements]) ->
     let%bind elements =
       sf_elements
       |> List.map ~f:(bin_element_of_sf ~value_of_sf:guard_test_of_sf ~size_of_sf:guard_test_of_sf)
       |> Result.all
       |> Result.map ~f:(List.map ~f:(fun (guard_test, size, tsl) -> GuardTestBinElement {guard_test; size; tsl}))
       |> track ~loc:[%here]
     in
     GuardTestBitstr {line; elements} |> return

  (* cons skeleton *)
  | Sf.Tuple (4, [Sf.Atom "cons"; Sf.Integer line; sf_head; sf_tail]) ->
     let%bind head = sf_head |> guard_test_of_sf |> track ~loc:[%here] in
     let%bind tail = sf_tail |> guard_test_of_sf |> track ~loc:[%here] in
     GuardTestCons {line; head; tail} |> return

  (* remote function call *)
  | Sf.Tuple (4, [
                 Sf.Atom "call";
                 Sf.Integer line;
                 Sf.Tuple (4, [Sf.Atom "remote";
                               Sf.Integer line_remote;
                               Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_module_name; Sf.Atom module_name]);
                               Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_function_name; Sf.Atom function_name])]);
                 Sf.List sf_args
             ]) ->
     let%bind args = sf_args |> List.map ~f:guard_test_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestRemoteCall {line; line_remote; line_module_name; module_name; line_function_name; function_name; args} |> return

  (* function call *)
  | Sf.Tuple (4, [
                 Sf.Atom "call";
                 Sf.Integer line;
                 sf_function_name;
                 Sf.List sf_args
             ]) ->
     let%bind function_name = sf_function_name |> lit_of_sf |> track ~loc:[%here] in
     let%bind args =  sf_args |> List.map ~f:guard_test_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestCall {line; function_name; args} |> return

  (* a map creation *)
  | Sf.Tuple (3, [Sf.Atom "map"; Sf.Integer line; Sf.List sf_assocs]) ->
     let%bind assocs = sf_assocs |> List.map ~f:guard_test_assoc_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestMapCreation {line; assocs} |> return

  (* a map update *)
  | Sf.Tuple (4, [Sf.Atom "map"; Sf.Integer line; sf_map; Sf.List sf_assocs]) ->
     let%bind map = sf_map |> guard_test_of_sf |> track ~loc:[%here] in
     let%bind assocs = sf_assocs |> List.map ~f:guard_test_assoc_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestMapUpdate {line; map; assocs} |> return

  (* nil *)
  | Sf.Tuple (2, [Sf.Atom "nil"; Sf.Integer line]) ->
     GuardTestNil {line} |> return

  (* a binary operator *)
  | Sf.Tuple (5, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; sf_lhs; sf_rhs]) ->
     let%bind lhs = sf_lhs |> guard_test_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> guard_test_of_sf |> track ~loc:[%here] in
     GuardTestBinOp {line; op; lhs; rhs} |> return

 (* a unary operator *)
  | Sf.Tuple (4, [Sf.Atom "op"; Sf.Integer line; Sf.Atom op; sf_operand]) ->
     let%bind operand = sf_operand |> guard_test_of_sf |> track ~loc:[%here] in
     GuardTestUnaryOp {line; op; operand} |> return

  (* a record creation : #user{name = "Taro", admin = true} *)
  | Sf.Tuple (4, [Sf.Atom "record";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_record_fields]) ->
     let field_of_sf sf =
       begin match sf with
       | Sf.Tuple (4, [Sf.Atom "record_field";
                       Sf.Integer line;
                       sf_atom_or_wildcard;
                       sf_guard_test]) ->
          let%bind field_name = atom_or_wildcard_of_sf sf_atom_or_wildcard |> track ~loc:[%here] in
          let%bind rhs = guard_test_of_sf sf_guard_test |> track ~loc:[%here] in
          (line, field_name, rhs) |> return
       | _ ->
          Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of record_field", sf)) |> Result.fail
       end
     in
     let%bind record_fields = sf_record_fields |> List.map ~f:field_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestRecord {line; name; record_fields} |> return

  (* a record field access : U#user.name *)
  | Sf.Tuple (5, [Sf.Atom "record_field";
                  Sf.Integer line;
                  sf_record;
                  Sf.Atom name;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])]) ->
     let%bind record = sf_record |> guard_test_of_sf |> track ~loc:[%here] in
     GuardTestRecordFieldAccess {line; record; name; line_field_name; field_name} |> return

  (* a record field index : #user.name *)
  | Sf.Tuple (4, [Sf.Atom "record_index";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_field_name; Sf.Atom field_name])]) ->
     GuardTestRecordFieldIndex {line; name; line_field_name; field_name} |> return

  (* a tuple skeleton *)
  | Sf.Tuple (3, [Sf.Atom "tuple"; Sf.Integer line; Sf.List sf_elements]) ->
     let%bind elements = sf_elements |> List.map ~f:guard_test_of_sf |> Result.all |> track ~loc:[%here] in
     GuardTestTuple {line; elements} |> return

  (* variable pattern *)
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) ->
     GuardTestVar {line; id} |> return

  (* atomic literal *)
  | sf_lit ->
     let%bind lit = sf_lit |> lit_of_sf |> track ~loc:[%here] in
     GuardTestLit {lit} |> return

and guard_test_assoc_of_sf sf : (guard_test_assoc_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* an association *)
  | Sf.Tuple (4, [Sf.Atom "map_field_assoc"; Sf.Integer line; sf_key; sf_value]) ->
     let%bind key = sf_key |> guard_test_of_sf |> track ~loc:[%here]in
     let%bind value = sf_value |> guard_test_of_sf |> track ~loc:[%here] in
     GuardTestAssoc {line; key; value} |> return

  (* an exact association *)
  | Sf.Tuple (4, [Sf.Atom "map_field_exact"; Sf.Integer line; sf_key; sf_value]) ->
     let%bind key = sf_key |> guard_test_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> guard_test_of_sf |> track ~loc:[%here]in
     GuardTestAssocExact {line; key; value} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("guard_test_assoc", sf)) |> Result.fail

and atom_or_wildcard_of_sf sf =
  match sf with
  | Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line; Sf.Atom field_name]) ->
     AtomWildcardAtom {line; atom=field_name} |> Result.return
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom "_"]) ->
     AtomWildcardWildcard {line} |> Result.return
  | _ ->
     Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of atom_or_wildcard", sf)) |> Result.fail

(*
 * 8.7  Types
 *)
and type_of_sf sf : (type_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* annotated type *)
  | Sf.Tuple (3, [Sf.Atom "ann_type";
                  Sf.Integer line;
                  Sf.List [sf_annotation; sf_tyvar]]) ->
     let%bind annotation = sf_annotation |> type_of_sf |> track ~loc:[%here] in
     let%bind tyvar = sf_tyvar |> type_of_sf |> track ~loc:[%here] in
     TyAnn {line; annotation; tyvar} |> return

  (* bitstring type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "binary";
                  Sf.List [sf_m; sf_n]]) ->
     let%bind m = sf_m |> type_of_sf |> track ~loc:[%here] in
     let%bind n = sf_n |> type_of_sf |> track ~loc:[%here] in
     TyBitstring {line; m; n} |> return

  (* fun type (any) *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "fun";
                  Sf.List []]) ->
     TyFunAny {line} |> return

  (* fun type (any arity) *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "fun";
                  Sf.List [Sf.Tuple (3, [
                                       Sf.Atom "type";
                                       Sf.Integer line_any;
                                       Sf.Atom "any";
                                    ]);
                           sf_ret]]) ->
     let%bind ret = sf_ret |> type_of_sf |> track ~loc:[%here] in
     TyFunAnyArity {line; line_any; ret} |> return

  (* map type (any) *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "map";
                  Sf.Atom "any"]) ->
     TyAnyMap {line} |> return

  (* map type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "map";
                  Sf.List sf_assocs]) ->
     let%bind assocs =
       sf_assocs |> List.map ~f:type_assoc_of_sf |> Result.all |> track ~loc:[%here]
     in
     TyMap {line; assocs} |> return

  (* operator type for a binary operator *)
  | Sf.Tuple (5, [Sf.Atom "op";
                  Sf.Integer line;
                  Sf.Atom op;
                  sf_lhs; sf_rhs]) ->
     let%bind lhs = sf_lhs |> type_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> type_of_sf |> track ~loc:[%here] in
     TyBinOp {line; lhs; op; rhs} |> return

  (* operator type for a unary operator *)
  | Sf.Tuple (4, [Sf.Atom "op";
                  Sf.Integer line;
                  Sf.Atom op;
                  sf_operand]) ->
     let%bind operand = sf_operand |> type_of_sf |> track ~loc:[%here] in
     TyUnaryOp {line; op; operand} |> return

  (* range type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "range";
                  Sf.List [sf_low; sf_high]]) ->
     let%bind low = sf_low |> type_of_sf |> track ~loc:[%here] in
     let%bind high = sf_high |> type_of_sf |> track ~loc:[%here] in
     TyRange {line; low; high} |> return

  (* record type : t(A) = #state{name :: A} *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "record";
                  Sf.List (
                      Sf.Tuple(3, [Sf.Atom "atom"; Sf.Integer line_name; Sf.Atom name])
                      :: sf_field_types)]) ->
     let%bind field_types = sf_field_types |> List.map ~f:record_field_type_of_sf |> Result.all |> track ~loc:[%here] in
     TyRecord {line; line_name; name; field_types} |> return

  (* remote type : dict:dict(integer(), any()) *)
  | Sf.Tuple (3, [Sf.Atom "remote_type";
                 Sf.Integer line;
                 Sf.List [
                     Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_module_name; Sf.Atom module_name]);
                     Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_type_name; Sf.Atom type_name]);
                     Sf.List sf_params;
             ]]) ->
     let%bind params = sf_params |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here] in
     TyRemote {line; line_module_name; module_name; line_type_name; type_name; params} |> return

  (* tuple type (any) *)
  | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "tuple"; Sf.Atom "any"]) ->
     TyAnyTuple {line} |> return

  (* tuple type *)
  | Sf.Tuple (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "tuple"; Sf.List sf_elements]) ->
     let%bind elements = sf_elements |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here] in
     TyTuple {line; elements} |> return

  (* union type *)
  | Sf.Tuple  (4, [Sf.Atom "type"; Sf.Integer line; Sf.Atom "union"; Sf.List sf_elements]) ->
     let%bind elements = sf_elements |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here] in
     TyUnion {line; elements} |> return

  (* predefined (or built-in) type OR fun type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_args]) ->
     begin match fun_type_of_sf sf with
     | Ok fn -> Ok fn
     | _ ->
        let%bind args =
          sf_args |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here]
        in
        TyPredef {line; name; args} |> return
     end

  (* type variable *)
  | Sf.Tuple (3, [Sf.Atom "var"; Sf.Integer line; Sf.Atom id]) ->
     TyVar {line; id} |> return

  (* user defined type *)
  | Sf.Tuple (4, [Sf.Atom "user_type";
                  Sf.Integer line;
                  Sf.Atom name;
                  Sf.List sf_args]) ->
     let%bind args =
       sf_args |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here]
     in
     TyUser {line; name; args} |> return

  (* atomic literal *)
  | sf_lit ->
     let%bind lit = sf_lit |> lit_of_sf |> track ~loc:[%here] in
     TyLit {lit} |> return

and fun_type_of_sf sf : (type_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* constrained function type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "bounded_fun";
                  Sf.List [sf_function_type; sf_constraints]
             ]) ->
     let%bind function_type = sf_function_type |> type_of_sf |> track ~loc:[%here] in
     let%bind constraints = sf_constraints |> type_fun_cont_of_sf |> track ~loc:[%here] in
     TyContFun {line; function_type; constraints} |> return

  (* function type *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "fun";
                  Sf.List [Sf.Tuple (4, [Sf.Atom "type";
                                         Sf.Integer line_params;
                                         Sf.Atom "product";
                                         Sf.List sf_params]);
                           sf_ret]]) ->
     let%bind params = sf_params |> List.map ~f:type_of_sf |> Result.all |> track ~loc:[%here] in
     let%bind ret = sf_ret |> type_of_sf |> track ~loc:[%here] in
     TyFun {line; line_params; params; ret} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("fun_type", sf)) |> Result.fail

and type_fun_cont_of_sf sf : (type_func_cont_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  | Sf.List sf_constraints ->
     let%bind constraints =
       sf_constraints |> List.map ~f:type_fun_cont_of_sf |> Result.all |> track ~loc:[%here]
     in
     TyCont {constraints} |> return

  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "constraint";
                  Sf.List [sf_constraint_kind; Sf.List [sf_lhs; sf_rhs]]
             ]) ->
     let%bind constraint_kind = sf_constraint_kind |> type_fun_cont_of_sf |> track ~loc:[%here] in
     let%bind lhs = sf_lhs |> type_of_sf |> track ~loc:[%here] in
     let%bind rhs = sf_rhs |> type_of_sf |> track ~loc:[%here] in
     TyContRel {line; constraint_kind; lhs; rhs} |> return

  | Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line; Sf.Atom "is_subtype"]) ->
     TyContIsSubType {line} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("type_fun_cont", sf)) |> Result.fail

and type_assoc_of_sf sf : (type_assoc_t, err_t) Result.t =
  let open Result.Let_syntax in
  match sf with
  (* an association *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "map_field_assoc";
                  Sf.List [sf_key; sf_value]]) ->
     let%bind key = sf_key |> type_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> type_of_sf |> track ~loc:[%here] in
     TyAssoc {line; key; value} |> return

  (* an exact association *)
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "map_field_exact";
                  Sf.List [sf_key; sf_value]]) ->
     let%bind key = sf_key |> type_of_sf |> track ~loc:[%here] in
     let%bind value = sf_value |> type_of_sf |> track ~loc:[%here] in
     TyAssocExact {line; key; value} |> return

  | _ ->
     Err.create ~loc:[%here] (Err.Not_supported_absform ("type_assoc", sf)) |> Result.fail

and record_field_type_of_sf sf =
  let open Result.Let_syntax in
  match sf with
  | Sf.Tuple (4, [Sf.Atom "type";
                  Sf.Integer line;
                  Sf.Atom "field_type";
                  Sf.List [
                      Sf.Tuple (3, [Sf.Atom "atom"; Sf.Integer line_name; Sf.Atom field_name]);
                      sf_ty;
                    ]
             ]) ->
     let%bind ty = sf_ty |> type_of_sf |> track ~loc:[%here] in
     RecordFieldType {line; line_name; name=field_name; ty} |> return
  | _ ->
     Err.create ~loc:[%here] (Err.Invalid_input ("invalid form of a record field type", sf)) |> Result.fail

(**)
let of_etf etf : (t, err_t) Result.t =
  etf |> Sf.of_etf |> of_sf