Source file language.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
open! Stdppx
open! Import
include Language_intf.Definitions
open Result.Let_syntax
module Type = struct
include Type
let equal_witness_non_tuple
: type a b. a non_tuple -> b non_tuple -> (a, b) Stdlib.Type.eq option
=
fun t1 t2 ->
match t1, t2 with
| Kind, Kind -> Some Equal
| Mode, Mode -> Some Equal
| Modality, Modality -> Some Equal
| Alloc, Alloc -> Some Equal
| Synchro, Synchro -> Some Equal
| (Kind | Mode | Modality | Alloc | Synchro), _ -> None
;;
let sexp_of_non_tuple : type a. a non_tuple -> Sexp.t = function
| Kind -> Atom "Kind"
| Mode -> Atom "Mode"
| Modality -> Atom "Modality"
| Alloc -> Atom "Alloc"
| Synchro -> Atom "Synchro"
;;
let rec equal_witness : type a b. a t -> b t -> (a, b) Stdlib.Type.eq option =
fun t1 t2 ->
match t1, t2 with
| Non_tuple b1, Non_tuple b2 ->
(match equal_witness_non_tuple b1 b2 with
| Some Equal -> Some Equal
| None -> None)
| Tuple tp1, Tuple tp2 ->
(match equal_tuple_witness tp1 tp2 with
| None -> None
| Some Equal -> Some Equal)
| (Non_tuple _ | Tuple _), _ -> None
and equal_tuple_witness : type a b. a tuple -> b tuple -> (a, b) Stdlib.Type.eq option =
fun tp1 tp2 ->
match tp1, tp2 with
| [], [] -> Some Equal
| hd1 :: tl1, hd2 :: tl2 ->
(match equal_witness hd1 hd2 with
| None -> None
| Some Equal ->
(match equal_tuple_witness tl1 tl2 with
| None -> None
| Some Equal -> Some Equal))
| [], _ :: _ | _ :: _, [] -> None
;;
let rec sexp_of_t : type a. a t -> Sexp.t = function
| Non_tuple non_tuple -> sexp_of_non_tuple non_tuple
| Tuple [ (Non_tuple Alloc as t1); (Non_tuple Mode as t2) ] ->
List [ sexp_of_t t1; Atom "@"; sexp_of_t t2 ]
| Tuple [ (Non_tuple Synchro as t1); (Non_tuple Mode as t2) ] ->
List [ sexp_of_t t1; Atom "@"; sexp_of_t t2 ]
| Tuple tp -> List (Atom "Tuple" :: sexp_of_tuple tp)
and sexp_of_tuple : type a. a tuple -> Sexp.t list = function
| [] -> []
| hd :: tl -> sexp_of_t hd :: sexp_of_tuple tl
;;
let compare_packed : packed -> packed -> int = Poly.compare
let sexp_of_packed (P t) = sexp_of_t t
let kind = Non_tuple Kind
let mode = Non_tuple Mode
let modality = Non_tuple Modality
let alloc = Non_tuple Alloc
let synchro = Non_tuple Synchro
let tuple2 t1 t2 = Tuple [ t1; t2 ]
end
module Untyped = struct
include Untyped
module Axis = struct
include Axis
let compare : t -> t -> int = Poly.compare
module Map = Map.Make (struct
type nonrec t = t
let compare = compare
end)
end
module Identifier = struct
include Identifier
let compare t1 t2 = String.compare t1.ident t2.ident
let sexp_of_t t = Atom t.ident
end
module Value = struct
include Value
let rec compare : t -> t -> int =
fun t1 t2 ->
match t1, t2 with
| Identifier ident1, Identifier ident2 -> Identifier.compare ident1 ident2
| Kind_product kinds1, Kind_product kinds2 ->
Nonempty_list.compare kinds1 kinds2 ~cmp:compare
| Kind_mod (kind1, mods1), Kind_mod (kind2, mods2) ->
(match compare kind1 kind2 with
| 0 ->
let mods1 = Nonempty_list.sort_uniq ~cmp:compare mods1 in
let mods2 = Nonempty_list.sort_uniq ~cmp:compare mods2 in
Nonempty_list.compare ~cmp:compare mods1 mods2
| n -> n)
| Tuple ts1, Tuple ts2 -> Nonempty_list.compare ~cmp:compare ts1 ts2
| Identifier _, _ -> -1
| _, Identifier _ -> 1
| Kind_product _, _ -> -1
| _, Kind_product _ -> 1
| Kind_mod _, _ -> -1
| _, Kind_mod _ -> 1
| Tuple _, _ -> .
| _, Tuple _ -> .
;;
let rec sexp_of_t : t -> Sexp.t = function
| Identifier ident -> Identifier.sexp_of_t ident
| Kind_product kinds ->
List [ Atom "Product"; kinds |> Nonempty_list.sexp_of_t sexp_of_t ]
| Kind_mod (kind, mods) ->
List
[ Atom "Mod"
; sexp_of_t kind
; mods
|> Nonempty_list.sort_uniq ~cmp:compare
|> Nonempty_list.sexp_of_t sexp_of_t
]
| Tuple ts ->
List (Atom "Tuple" :: (ts |> Nonempty_list.to_list |> List.map ~f:sexp_of_t))
;;
end
module Pattern = struct
include Pattern
let rec compare : t -> t -> int =
fun t1 t2 ->
match t1, t2 with
| Wildcard, Wildcard -> 0
| Identifier ident1, Identifier ident2 -> Identifier.compare ident1 ident2
| Tuple ts1, Tuple ts2 -> Nonempty_list.compare ~cmp:compare ts1 ts2
| Wildcard, _ -> -1
| _, Wildcard -> 1
| Identifier _, _ -> -1
| _, Identifier _ -> 1
| Tuple _, _ -> .
| _, Tuple _ -> .
;;
let rec sexp_of_t : t -> Sexp.t = function
| Wildcard -> Atom "Wildcard"
| Identifier ident -> Identifier.sexp_of_t ident
| Tuple ts ->
List (Atom "Tuple" :: (ts |> Nonempty_list.to_list |> List.map ~f:sexp_of_t))
;;
end
module Expression = struct
include Expression
let rec compare : t -> t -> int =
fun t1 t2 ->
match t1, t2 with
| Identifier ident1, Identifier ident2 -> Identifier.compare ident1 ident2
| Kind_product kinds1, Kind_product kinds2 ->
Nonempty_list.compare kinds1 kinds2 ~cmp:compare
| Kind_mod (kind1, mods1), Kind_mod (kind2, mods2) ->
(match compare kind1 kind2 with
| 0 ->
let mods1 = Nonempty_list.sort_uniq ~cmp:compare mods1 in
let mods2 = Nonempty_list.sort_uniq ~cmp:compare mods2 in
Nonempty_list.compare ~cmp:compare mods1 mods2
| n -> n)
| Kind_coercion (kind1, coerce_to1), Kind_coercion (kind2, coerce_to2) ->
(match compare kind1 kind2 with
| 0 -> compare coerce_to1 coerce_to2
| n -> n)
| Comma_separated ts1, Comma_separated ts2 ->
Nonempty_list.compare ~cmp:compare ts1 ts2
| Typed (t1, typ1), Typed (t2, typ2) ->
(match compare t1 t2 with
| 0 -> Type.compare_packed typ1 typ2
| n -> n)
| Identifier _, _ -> -1
| _, Identifier _ -> 1
| Kind_product _, _ -> -1
| _, Kind_product _ -> 1
| Kind_mod _, _ -> -1
| _, Kind_mod _ -> 1
| Kind_coercion _, _ -> -1
| _, Kind_coercion _ -> 1
| Comma_separated _, _ -> -1
| _, Comma_separated _ -> 1
| Typed _, _ -> .
| _, Typed _ -> .
;;
let rec sexp_of_t : t -> Sexp.t = function
| Identifier ident -> Identifier.sexp_of_t ident
| Kind_product kinds ->
List (Atom "Product" :: (kinds |> Nonempty_list.to_list |> List.map ~f:sexp_of_t))
| Kind_mod (kind, mods) ->
List
[ Atom "Mod"
; sexp_of_t kind
; mods
|> Nonempty_list.sort_uniq ~cmp:compare
|> Nonempty_list.sexp_of_t sexp_of_t
]
| Kind_coercion (kind, coerce_to) ->
List [ Atom "Kind_coercion"; sexp_of_t kind; sexp_of_t coerce_to ]
| Comma_separated ts ->
List
(Atom "Comma_separated" :: (ts |> Nonempty_list.to_list |> List.map ~f:sexp_of_t)
)
| Typed (t, typ) -> List [ sexp_of_t t; Atom ":"; Type.sexp_of_packed typ ]
;;
end
end
module Typed = struct
include Typed
module Vec = struct
type ('item, 'length) t =
| [] : (_, unit) t
| ( :: ) : 'item * ('item, 'length) t -> ('item, _ * 'length) t
type wrong_length = Wrong_length
let rec of_list_with_length
: type item length.
item list -> length Type.tuple -> ((item, length) t, wrong_length) result
=
fun items length ->
match items, length with
| [], [] -> Ok []
| hd_items :: tl_items, _ :: tl_length ->
let* tl = of_list_with_length tl_items tl_length in
Ok (hd_items :: tl)
| _ :: _, [] | [], _ :: _ -> Error Wrong_length
;;
end
module Axis = struct
include Axis
let compare_packed : packed -> packed -> int = Poly.compare
let of_type : type a. a Type.non_tuple Type.t -> a Type.non_tuple t = function
| Non_tuple Kind -> Singleton Kind
| Non_tuple Mode -> Singleton Mode
| Non_tuple Modality -> Singleton Modality
| Non_tuple Alloc -> Singleton Alloc
| Non_tuple Synchro -> Singleton Synchro
;;
let is_set : type a. a t -> bool = function
| Set _ -> true
| Singleton _ -> false
;;
module Map = Map.Make (struct
type t = packed
let compare = compare_packed
end)
module Sub_axis = struct
include Sub_axis
module Modal = struct
include Modal
let of_mode_identifier : string -> t Or_unrecognized.t = function
| "global" | "local" -> Known Locality
| "nonportable" | "shareable" | "corruptible" | "portable" -> Known Portability
| "uncontended" | "contended" | "corrupted" | "shared" -> Known Contention
| "stateful" | "reading" | "writing" | "stateless" -> Known Statefulness
| "read_write" | "read" | "write" | "immutable" -> Known Visibility
| "many" | "once" -> Known Linearity
| "aliased" | "unique" -> Known Uniqueness
| "unyielding" | "yielding" -> Known Yielding
| "forkable" | "unforkable" -> Known Forkable
| _ -> Unrecognized
;;
let sexp_of_t = function
| Locality -> Atom "Locality"
| Portability -> Atom "Portability"
| Contention -> Atom "Contention"
| Statefulness -> Atom "Statefulness"
| Visibility -> Atom "Visibility"
| Linearity -> Atom "Linearity"
| Uniqueness -> Atom "Uniqueness"
| Yielding -> Atom "Yielding"
| Forkable -> Atom "Forkable"
;;
end
module Mode = struct
include Mode
let default (Mode m) =
match m with
| Locality -> "global"
| Portability -> "nonportable"
| Contention -> "uncontended"
| Statefulness -> "stateful"
| Visibility -> "read_write"
| Linearity -> "many"
| Uniqueness -> "aliased"
| Yielding -> "unyielding"
| Forkable ->
t also applies to [forkable]. *)
"forkable"
;;
let of_mode_identifier str : _ Or_unrecognized.t =
match Modal.of_mode_identifier str with
| Known m -> Known (Mode m)
| Unrecognized -> Unrecognized
;;
let sexp_of_t (Mode m) = Modal.sexp_of_t m
end
module Modality = struct
include Modality
let default (Modality m) =
match m with
| Locality -> "local"
| Portability -> "nonportable"
| Contention -> "uncontended"
| Statefulness -> "stateful"
| Visibility -> "read_write"
| Linearity -> "once"
| Uniqueness -> "unique"
| Yielding -> "yielding"
| Forkable -> "unforkable"
;;
let of_mode_identifier str : _ Or_unrecognized.t =
match Modal.of_mode_identifier str with
| Known m -> Known (Modality m)
| Unrecognized -> Unrecognized
;;
let sexp_of_t (Modality m) = Modal.sexp_of_t m
end
module Or_unrecognized = struct
include Or_unrecognized
let sexp_of_t sexp_of_a = function
| Known a -> sexp_of_a a
| Unrecognized -> Atom "unrecognized"
;;
end
let compare_packed : packed -> packed -> int = Poly.compare
module Map = Stdlib.Map.Make (struct
type t = packed
let compare = compare_packed
end)
let of_identifier : type a. a Type.non_tuple Identifier.t -> a Type.non_tuple t =
fun { ident; type_ = Non_tuple type_ } ->
match type_ with
| Kind -> Kind
| Mode -> Mode (Mode.of_mode_identifier ident)
| Modality -> Modality (Modality.of_mode_identifier ident)
| Alloc -> Alloc
| Synchro -> Synchro
;;
let of_value : type a. a Type.non_tuple Value.t -> a Type.non_tuple t = function
| Identifier ident -> of_identifier ident
| Kind_product _ -> Kind
| Kind_mod _ -> Kind
;;
let default : type a. a Type.non_tuple t -> string Or_unrecognized.t = function
| Kind -> Known "value"
| Mode (Known mode) -> Known (Mode.default mode)
| Modality (Known modality) -> Known (Modality.default modality)
| Mode Unrecognized | Modality Unrecognized -> Unrecognized
| Alloc -> Known "heap"
| Synchro -> Known "unsync"
;;
let sexp_of_t : type a. a t -> Sexp.t = function
| Kind -> Atom "Kind"
| Mode m -> List [ Atom "Mode"; Or_unrecognized.sexp_of_t Mode.sexp_of_t m ]
| Modality m ->
List [ Atom "Modality"; Or_unrecognized.sexp_of_t Modality.sexp_of_t m ]
| Alloc -> Atom "Alloc"
| Synchro -> Atom "Synchro"
;;
end
module Namespace = struct
include Namespace
let rec of_value : type a. is_set:bool -> a Value.t -> a t =
fun ~is_set val_ ->
let to_namespace sub_axis = if is_set then Set sub_axis else Singleton sub_axis in
match val_ with
| Identifier _ -> to_namespace (Sub_axis.of_value val_)
| Kind_product _ -> to_namespace (Sub_axis.of_value val_)
| Kind_mod _ -> to_namespace (Sub_axis.of_value val_)
| Tuple tp -> Tuple (of_value_tuple ~is_set tp)
and of_value_tuple : type a. is_set:bool -> a Value.tuple -> a tuple =
fun ~is_set tp ->
match tp with
| [] -> []
| hd :: tl -> of_value ~is_set hd :: of_value_tuple ~is_set tl
;;
let rec same_namespace : type a b. a t -> b t -> bool =
fun tp1 tp2 ->
match tp1, tp2 with
| Singleton tp1, Singleton tp2 | Set tp1, Set tp2 ->
(match tp1, tp2 with
| Mode (Known (Mode m1)), Modality (Known (Modality m2)) -> m1 = m2
| Modality (Known (Modality m1)), Mode (Known (Mode m2)) -> m1 = m2
| Mode Unrecognized, Modality Unrecognized
| Modality Unrecognized, Mode Unrecognized -> true
| _, _ -> Sub_axis.compare_packed (P tp1) (P tp2) = 0)
| Tuple tp1, Tuple tp2 -> same_namespace_tuple tp1 tp2
| (Singleton _ | Set _), Tuple _
| Tuple _, (Singleton _ | Set _)
| Singleton _, Set _
| Set _, Singleton _ -> false
and same_namespace_tuple : type a b. a tuple -> b tuple -> bool =
fun tp1 tp2 ->
match tp1, tp2 with
| [], [] -> true
| hd1 :: tl1, hd2 :: tl2 -> same_namespace hd1 hd2 && same_namespace_tuple tl1 tl2
| [], _ :: _ | _ :: _, [] -> false
;;
let rec sexp_of_t : type a. a t -> Sexp.t = function
| Singleton t -> Sub_axis.sexp_of_t t
| Set t -> List [ Atom "Set"; Sub_axis.sexp_of_t t ]
| Tuple tp -> List (Atom "Tuple" :: sexp_of_tuple tp)
and sexp_of_tuple : type a. a tuple -> Sexp.t list = function
| [] -> []
| hd :: tl -> sexp_of_t hd :: sexp_of_tuple tl
;;
end
end
module Identifier = struct
include Identifier
let equal_witness { type_; ident } t =
match Type.equal_witness type_ t.type_ with
| Some _ as eq when String.equal ident t.ident -> eq
| _ -> None
;;
end
module Value = struct
include Value
let rec untype : type a. a t -> Untyped.Value.t = function
| Identifier { ident; type_ = _ } -> Identifier { ident }
| Kind_product kinds -> Kind_product (Nonempty_list.map kinds ~f:untype)
| Kind_mod (kind, mods) -> Kind_mod (untype kind, Nonempty_list.map mods ~f:untype)
| Tuple tp -> Tuple (untype_tuple tp)
and untype_tuple : type a b. (a * b) tuple -> Untyped.Value.t Nonempty_list.t
= function
| [ hd ] -> Nonempty_list.singleton (untype hd)
| hd :: (_ :: _ as tl) -> Nonempty_list.cons (untype hd) (untype_tuple tl)
;;
let compare t1 t2 = Untyped.Value.compare (untype t1) (untype t2)
let sexp_of_t t = Untyped.Value.sexp_of_t (untype t)
let rec as_expression : type a. a t -> (a, Expression.singleton) Expression.t
= function
| Identifier ident -> Identifier ident
| Kind_mod (kind, mods) ->
Kind_mod (as_expression kind, Nonempty_list.map mods ~f:as_expression)
| Kind_product kinds -> Kind_product (Nonempty_list.map kinds ~f:as_expression)
| Tuple tp -> Tuple (uneval_tuple tp)
and uneval_tuple : type a. a tuple -> a Expression.tuple = function
| [] -> []
| hd :: tl -> as_expression hd :: uneval_tuple tl
;;
let is_actual_default : type a. a Type.non_tuple t -> bool = function
| Identifier ident ->
(match Axis.Sub_axis.default (Axis.Sub_axis.of_identifier ident) with
| Known default -> String.equal ident.ident default
| Unrecognized -> false)
| Kind_product _ | Kind_mod _ -> false
;;
let is_default_for_standard_mangling : type a. a Type.non_tuple t -> bool = function
| Identifier { type_ = Non_tuple Kind; ident = "value_or_null" } -> true
| _ -> false
;;
let defaultness : type a. a Type.non_tuple t -> Defaultness.t =
fun value ->
if is_actual_default value
then Actual_default
else if is_default_for_standard_mangling value
then Default_for_standard_mangling
else Not_a_default
;;
let rec to_node
: type a. a Type.non_tuple t -> loc:location -> a Type.non_tuple Node.t
=
fun t ~loc ->
match t with
| Identifier { ident; type_ } ->
(match type_ with
| Non_tuple Mode -> Mode { txt = Mode ident; loc }
| Non_tuple Modality -> Modality { txt = Modality ident; loc }
| Non_tuple Kind ->
Jkind_annotation
{ pjka_desc = Pjk_abbreviation ({ txt = Lident ident; loc }, [])
; pjka_loc = loc
}
| Non_tuple Alloc -> Alloc
| Non_tuple Synchro -> Synchro)
| Kind_product kinds ->
let ghost_loc = { loc with loc_ghost = true } in
let kinds =
kinds
|> Nonempty_list.to_list
|> List.map ~f:(fun kind ->
let (Jkind_annotation kind) = to_node ~loc:ghost_loc kind in
kind)
in
Jkind_annotation { pjka_desc = Pjk_product kinds; pjka_loc = loc }
| Kind_mod (kind, mods) ->
let ghost_loc = { loc with loc_ghost = true } in
let (Jkind_annotation kind) = to_node ~loc:ghost_loc kind in
let mods =
mods
|> Nonempty_list.to_list
|> List.sort_uniq ~cmp:compare
|> List.map ~f:(fun (Identifier { ident; type_ = Non_tuple Modality }) ->
{ txt = Mode ident; loc = ghost_loc })
in
Jkind_annotation { pjka_desc = Pjk_mod (kind, mods); pjka_loc = loc }
;;
end
module Pattern = struct
include Pattern
let rec untype : type a. a t -> Untyped.Pattern.t = function
| Wildcard -> Wildcard
| Identifier { ident; type_ = _ } -> Identifier { ident }
| Tuple tp -> Tuple (untype_tuple tp)
and untype_tuple : type a b. (a * b) tuple -> Untyped.Pattern.t Nonempty_list.t
= function
| [ hd ] -> Nonempty_list.singleton (untype hd)
| hd :: (_ :: _ as tl) -> Nonempty_list.cons (untype hd) (untype_tuple tl)
;;
let rec type_check
: type a. Untyped.Pattern.t -> expected:a Type.t -> (a t, Type_error.t) result
=
fun untyped ~expected ->
match untyped, expected with
| Wildcard, _ -> Ok Wildcard
| Identifier { ident }, type_ -> Ok (Identifier { ident; type_ })
| Tuple untyped_tp, Tuple type_tp ->
let* vec =
match Vec.of_list_with_length (Nonempty_list.to_list untyped_tp) type_tp with
| Ok vec -> Ok vec
| Error Wrong_length ->
Error
(Type_error.Tuple_length_mismatch
{ kind = "pattern"
; sexp_of_kind = Untyped.Pattern.sexp_of_t
; value = Tuple untyped_tp
; expected_type = Tuple type_tp
})
in
let+ tuple = type_check_tuple vec ~expected:type_tp in
Tuple tuple
| (Tuple _ as node), (Non_tuple _ as type_) ->
Error
(Type_mismatch
{ kind = "pattern"
; sexp_of_kind = Untyped.Pattern.sexp_of_t
; value = node
; expected_type = type_
; expected_sets = None
; hint = None
})
and type_check_tuple
: type a.
(Untyped.Pattern.t, a) Vec.t
-> expected:a Type.tuple
-> (a tuple, Type_error.t) result
=
fun untyped ~expected ->
match untyped, expected with
| [], [] -> Ok []
| untyped_hd :: untyped_tl, type_hd :: type_tl ->
let* hd = type_check untyped_hd ~expected:type_hd in
let+ tl = type_check_tuple untyped_tl ~expected:type_tl in
hd :: tl
;;
end
module Expression = struct
include Expression
let rec of_parsetree_jkind
: jkind_annotation -> ((Type.kind, _) Expression.t, string loc) result
= function
| { pjka_desc = Pjk_abbreviation ({ txt = Lident ident; _ }, []); _ } ->
Ok (Identifier { ident; type_ = Non_tuple Kind })
| { pjka_desc = Pjk_abbreviation (_, _ :: _); pjka_loc } ->
Error { loc = pjka_loc; txt = "no kind modifiers" }
| { pjka_desc = Pjk_mod (jkind, mode :: modes); _ } ->
let modes =
Nonempty_list.map (mode :: modes) ~f:(fun { txt = Mode ident; _ } ->
Expression.Identifier { ident; type_ = Non_tuple Modality })
in
let+ jkind = of_parsetree_jkind jkind in
Kind_mod (jkind, modes)
| { pjka_desc = Pjk_product (jkind :: jkinds); _ } ->
let+ kinds =
Nonempty_list.Or_first_error.map (jkind :: jkinds) ~f:of_parsetree_jkind
in
Kind_product kinds
| { pjka_desc = _; pjka_loc } ->
Error
{ loc = pjka_loc
; txt = "unrecognized kind shape: only kind abbreviation, mod, or product"
}
;;
let sexp_of_sets : type s. (string, s) allow_set -> Sexp.t = function
| Singleton_only _hint -> Atom "singleton"
| Set_or_singleton -> Atom "set with unions"
;;
let rec type_ : type a is_set. (a, is_set) t -> a Type.t = function
| Identifier { type_; _ } -> type_
| Kind_mod _ -> Type.kind
| Kind_product _ -> Type.kind
| Kind_coercion _ -> Type.kind
| Tuple tp -> Tuple (type_tuple tp)
| Union (hd :: _) -> type_ hd
and type_tuple : type a b. (a * b) tuple -> (a * b) Type.tuple = function
| [ hd ] -> [ type_ hd ]
| hd :: (_ :: _ as tl) -> type_ hd :: type_tuple tl
;;
let rec untype : type a is_set. (a, is_set) t -> Untyped.Expression.t = function
| Identifier { ident; type_ = _ } -> Identifier { ident }
| Kind_mod (kind, mods) -> Kind_mod (untype kind, Nonempty_list.map mods ~f:untype)
| Kind_product kinds -> Kind_product (Nonempty_list.map kinds ~f:untype)
| Kind_coercion (kind, coerce_to) -> Kind_coercion (untype kind, untype coerce_to)
| Tuple tp -> Comma_separated (untype_tuple tp)
| Union ts -> Comma_separated (Nonempty_list.map ts ~f:untype)
and untype_tuple : type a b. (a * b) tuple -> Untyped.Expression.t Nonempty_list.t
= function
| [ hd ] -> [ untype hd ]
| hd :: (_ :: _ as tl) -> untype hd :: Nonempty_list.to_list (untype_tuple tl)
;;
let rec to_set : type a is_set. (a, is_set) t -> (a, set) t = function
| Identifier ident -> Identifier ident
| Kind_mod (kind, mods) -> Kind_mod (to_set kind, mods)
| Kind_product kinds -> Kind_product (Nonempty_list.map kinds ~f:to_set)
| Kind_coercion (kind, coerce_to) -> Kind_coercion (to_set kind, coerce_to)
| Tuple tp -> Tuple tp
| Union ts -> Union (Nonempty_list.map ts ~f:to_set)
;;
let type_mismatch
(type s)
?hint
~value
~expected
~(allow_set : (string, s) Expression.allow_set)
()
=
Error
(Type_error.Type_mismatch
{ kind = "expression"
; sexp_of_kind = Untyped.Expression.sexp_of_t
; value
; expected_type = expected
; expected_sets = Some allow_set
; hint
})
;;
let rec type_check
: type a s.
Untyped.Expression.t
-> expected:a Type.t
-> allow_set:(string, s) allow_set
-> ((a, s) t, Type_error.t) result
=
fun untyped ~expected ~allow_set ->
match untyped, expected with
| Identifier { ident }, type_ -> Ok (Identifier { ident; type_ })
| Kind_product kinds, Non_tuple Kind ->
let+ kinds =
Nonempty_list.Or_first_error.map
kinds
~f:(type_check ~expected:(Non_tuple Kind) ~allow_set)
in
Kind_product kinds
| (Kind_product _ as value), expected ->
type_mismatch ~value ~expected ~allow_set ()
| Kind_mod (kind, mods), Non_tuple Kind ->
let* kind = type_check kind ~expected:(Non_tuple Kind) ~allow_set in
let+ mods =
Nonempty_list.Or_first_error.map
mods
~f:
(type_check
~expected:(Non_tuple Modality)
~allow_set:
(Singleton_only
{ why_no_set = "sets not allowed inside [kind mod] modalities" }))
in
Kind_mod (kind, mods)
| Kind_coercion (kind, coerce_to), Non_tuple Kind ->
let* kind = type_check kind ~expected ~allow_set in
let+ coerce_to = type_check coerce_to ~expected ~allow_set:Set_or_singleton in
Kind_coercion (kind, coerce_to)
| (Kind_coercion _ as value), expected ->
type_mismatch ~value ~expected ~allow_set ()
| (Kind_mod _ as value), expected -> type_mismatch ~value ~expected ~allow_set ()
| Comma_separated untyped_tp, Tuple type_tp ->
let* vec =
match Vec.of_list_with_length (Nonempty_list.to_list untyped_tp) type_tp with
| Ok vec -> Ok vec
| Error Wrong_length ->
Error
(Type_error.Tuple_length_mismatch
{ kind = "expression"
; sexp_of_kind = Untyped.Expression.sexp_of_t
; value = untyped
; expected_type = expected
})
in
let+ tuple = type_check_tuple vec ~expected:type_tp in
Tuple tuple
| Comma_separated untyped_union, Non_tuple _ ->
(match allow_set with
| Singleton_only { why_no_set = hint } ->
type_mismatch ~hint ~value:untyped ~expected ~allow_set ()
| Set_or_singleton ->
let+ subsets =
Nonempty_list.Or_first_error.map
untyped_union
~f:(type_check ~expected ~allow_set:Set_or_singleton)
in
Union (subsets :> (_, set) t Nonempty_list.t))
| (Typed (untyped, P typ) as value), expected ->
(match Type.equal_witness typ expected with
| None -> type_mismatch ~value ~expected ~allow_set ()
| Some Equal -> type_check untyped ~expected ~allow_set)
and type_check_tuple
: type a.
(Untyped.Expression.t, a) Vec.t
-> expected:a Type.tuple
-> (a tuple, Type_error.t) result
=
fun untyped ~expected ->
match untyped, expected with
| [], [] -> Ok []
| untyped_hd :: untyped_tl, type_hd :: type_tl ->
let* hd =
type_check
untyped_hd
~expected:type_hd
~allow_set:(Singleton_only { why_no_set = "sets not allowed inside tuples" })
in
let+ tl = type_check_tuple untyped_tl ~expected:type_tl in
hd :: tl
;;
end
end
module Type_error = struct
include Type_error
let sexp_of_t : t -> Sexp.t = function
| Type_mismatch
{ kind; sexp_of_kind; value; expected_type = type_; expected_sets = is_set; hint }
->
Sexplib0.Sexp.message
"Type mismatch"
([ "kind", Atom kind
; "value", sexp_of_kind value
; "expected type", Type.sexp_of_t type_
]
@ (match is_set with
| Some is_set -> [ "expected sets", Typed.Expression.sexp_of_sets is_set ]
| None -> [])
@
match hint with
| Some hint -> [ "hint", Atom hint ]
| None -> [])
| Tuple_length_mismatch { kind; sexp_of_kind; value; expected_type = type_ } ->
Sexplib0.Sexp.message
"Tuple length mismatch"
[ "kind", Atom kind
; "value", sexp_of_kind value
; "expected type", Type.sexp_of_t type_
]
;;
let to_error ~loc t =
Syntax_error.createf
~loc
"%s"
(Sexp.to_string_hum (List [ Atom "[%template]"; sexp_of_t t ]))
;;
let lift_to_error_result ~loc = function
| Ok _ as ok -> ok
| Error type_error -> Error (to_error ~loc type_error)
;;
end