/
source.ml
7276 lines (6027 loc) · 177 KB
/
source.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
[@@@foo]
let (x[@foo]) : unit [@foo] = ()[@foo]
[@@foo]
type t =
| Foo of (t[@foo]) [@foo]
[@@foo]
[@@@foo]
module M = struct
type t = {
l : (t [@foo]) [@foo]
}
[@@foo]
[@@foo]
[@@@foo]
end[@foo]
[@@foo]
module type S = sig
include (module type of (M[@foo]))[@foo] with type t := M.t[@foo]
[@@foo]
[@@@foo]
end[@foo]
[@@foo]
[@@@foo]
type 'a with_default
= ?size:int (** default [42] *)
-> ?resizable:bool (** default [true] *)
-> 'a
type obj = <
meth1 : int -> int;
(** method 1 *)
meth2: unit -> float (** method 2 *);
>
type var = [
| `Foo (** foo *)
| `Bar of int * string (** bar *)
]
[%%foo let x = 1 in x]
let [%foo 2+1] : [%foo bar.baz] = [%foo "foo"]
[%%foo module M = [%bar] ]
let [%foo let () = () ] : [%foo type t = t ] = [%foo class c = object end]
[%%foo: 'a list]
let [%foo: [`Foo] ] : [%foo: t -> t ] = [%foo: < foo : t > ]
[%%foo? _ ]
[%%foo? Some y when y > 0]
let [%foo? (Bar x | Baz x) ] : [%foo? #bar ] = [%foo? { x }]
[%%foo: module M : [%baz]]
let [%foo: include S with type t = t ]
: [%foo: val x : t val y : t]
= [%foo: type t = t ]
let int_with_custom_modifier =
1234567890_1234567890_1234567890_1234567890_1234567890z
let float_with_custom_modifier =
1234567890_1234567890_1234567890_1234567890_1234567890.z
let int32 = 1234l
let int64 = 1234L
let nativeint = 1234n
let hex_without_modifier = 0x32f
let hex_with_modifier = 0x32g
let float_without_modifer = 1.2e3
let float_with_modifer = 1.2g
let%foo x = 42
let%foo _ = () and _ = ()
let%foo _ = ()
(* Expressions *)
let () =
let%foo[@foo] x = 3
and[@foo] y = 4 in
(let module%foo[@foo] M = M in ()) ;
(let open%foo[@foo] M in ()) ;
(fun%foo[@foo] x -> ()) ;
(function%foo[@foo] x -> ()) ;
(try%foo[@foo] () with _ -> ()) ;
(if%foo[@foo] () then () else ()) ;
while%foo[@foo] () do () done ;
for%foo[@foo] x = () to () do () done ;
assert%foo[@foo] true ;
lazy%foo[@foo] x ;
object%foo[@foo] end ;
begin%foo[@foo] 3 end ;
new%foo[@foo] x ;
match%foo[@foo] () with
(* Pattern expressions *)
| lazy%foo[@foo] x -> ()
| exception%foo[@foo] x -> ()
(* Class expressions *)
class x =
fun[@foo] x ->
let[@foo] x = 3 in
object[@foo]
inherit[@foo] x
val[@foo] x = 3
val[@foo] virtual x : t
val![@foo] mutable x = 3
method[@foo] x = 3
method[@foo] virtual x : t
method![@foo] private x = 3
initializer[@foo] x
end
(* Class type expressions *)
class type t =
object[@foo]
inherit[@foo] t
val[@foo] x : t
val[@foo] mutable x : t
method[@foo] x : t
method[@foo] private x : t
constraint[@foo] t = t'
[@@@abc]
[%%id]
[@@@aaa]
end
(* Type expressions *)
type t =
(module%foo[@foo] M)
(* Module expressions *)
module M =
functor[@foo] (M : S) ->
(val[@foo] x)
(struct[@foo] end)
(* Module type expression *)
module type S =
functor[@foo] (M:S) ->
(module type of[@foo] M) ->
(sig[@foo] end)
(* Structure items *)
let%foo[@foo] x = 4
and[@foo] y = x
type%foo[@foo] t = int
and[@foo] t = int
type%foo[@foo] t += T
class%foo[@foo] x = x
class type%foo[@foo] x = x
external%foo[@foo] x : _ = ""
exception%foo[@foo] X
module%foo[@foo] M = M
module%foo[@foo] rec M : S = M
and[@foo] M : S = M
module type%foo[@foo] S = S
include%foo[@foo] M
open%foo[@foo] M
(* Signature items *)
module type S = sig
val%foo[@foo] x : t
external%foo[@foo] x : t = ""
type%foo[@foo] t = int
and[@foo] t' = int
type%foo[@foo] t += T
exception%foo[@foo] X
module%foo[@foo] M : S
module%foo[@foo] rec M : S
and[@foo] M : S
module%foo[@foo] M = M
module type%foo[@foo] S = S
include%foo[@foo] M
open%foo[@foo] M
class%foo[@foo] x : t
class type%foo[@foo] x = x
end
type t = ..;;
type t += A;;
[%extension_constructor A];;
([%extension_constructor A] : extension_constructor);;
module M = struct
type extension_constructor = int
end;;
open M;;
([%extension_constructor A] : extension_constructor);;
(* By using two types we can have a recursive constraint *)
type 'a class_name = .. constraint 'a = < cast: 'a. 'a name -> 'a; ..>
and 'a name =
Class : 'a class_name -> (< cast: 'a. 'a name -> 'a; ..> as 'a) name
;;
exception Bad_cast
;;
class type castable =
object
method cast: 'a.'a name -> 'a
end
;;
(* Lets create a castable class with a name*)
class type foo_t =
object
inherit castable
method foo: string
end
;;
type 'a class_name += Foo: foo_t class_name
;;
class foo: foo_t =
object(self)
method cast: type a. a name -> a =
function
Class Foo -> (self :> foo_t)
| _ -> ((raise Bad_cast) : a)
method foo = "foo"
end
;;
(* Now we can create a subclass of foo *)
class type bar_t =
object
inherit foo
method bar: string
end
;;
type 'a class_name += Bar: bar_t class_name
;;
class bar: bar_t =
object(self)
inherit foo as super
method cast: type a. a name -> a =
function
Class Bar -> (self :> bar_t)
| other -> super#cast other
method bar = "bar"
[@@@id]
[%%id]
end
;;
(* Now lets create a mutable list of castable objects *)
let clist :castable list ref = ref []
;;
let push_castable (c: #castable) =
clist := (c :> castable) :: !clist
;;
let pop_castable () =
match !clist with
c :: rest ->
clist := rest;
c
| [] -> raise Not_found
;;
(* We can add foos and bars to this list, and retrive them *)
push_castable (new foo);;
push_castable (new bar);;
push_castable (new foo);;
let c1: castable = pop_castable ();;
let c2: castable = pop_castable ();;
let c3: castable = pop_castable ();;
(* We can also downcast these values to foos and bars *)
let f1: foo = c1#cast (Class Foo);; (* Ok *)
let f2: foo = c2#cast (Class Foo);; (* Ok *)
let f3: foo = c3#cast (Class Foo);; (* Ok *)
let b1: bar = c1#cast (Class Bar);; (* Exception Bad_cast *)
let b2: bar = c2#cast (Class Bar);; (* Ok *)
let b3: bar = c3#cast (Class Bar);; (* Exception Bad_cast *)
type foo = ..
;;
type foo +=
A
| B of int
;;
let is_a x =
match x with
A -> true
| _ -> false
;;
(* The type must be open to create extension *)
type foo
;;
type foo += A of int (* Error type is not open *)
;;
(* The type parameters must match *)
type 'a foo = ..
;;
type ('a, 'b) foo += A of int (* Error: type parameter mismatch *)
;;
(* In a signature the type does not have to be open *)
module type S =
sig
type foo
type foo += A of float
end
;;
(* But it must still be extensible *)
module type S =
sig
type foo = A of int
type foo += B of float (* Error foo does not have an extensible type *)
end
;;
(* Signatures can change the grouping of extensions *)
type foo = ..
;;
module M = struct
type foo +=
A of int
| B of string
type foo +=
C of int
| D of float
end
;;
module type S = sig
type foo +=
B of string
| C of int
type foo += D of float
type foo += A of int
end
;;
module M_S = (M : S)
;;
(* Extensions can be GADTs *)
type 'a foo = ..
;;
type _ foo +=
A : int -> int foo
| B : int foo
;;
let get_num : type a. a foo -> a -> a option = fun f i1 ->
match f with
A i2 -> Some (i1 + i2)
| _ -> None
;;
(* Extensions must obey constraints *)
type 'a foo = .. constraint 'a = [> `Var ]
;;
type 'a foo += A of 'a
;;
let a = A 9 (* ERROR: Constraints not met *)
;;
type 'a foo += B : int foo (* ERROR: Constraints not met *)
;;
(* Signatures can make an extension private *)
type foo = ..
;;
module M = struct type foo += A of int end
;;
let a1 = M.A 10
;;
module type S = sig type foo += private A of int end
;;
module M_S = (M : S)
;;
let is_s x =
match x with
M_S.A _ -> true
| _ -> false
;;
let a2 = M_S.A 20 (* ERROR: Cannot create a value using a private constructor *)
;;
(* Extensions can be rebound *)
type foo = ..
;;
module M = struct type foo += A1 of int end
;;
type foo += A2 = M.A1
;;
type bar = ..
;;
type bar += A3 = M.A1 (* Error: rebind wrong type *)
;;
module M = struct type foo += private B1 of int end
;;
type foo += private B2 = M.B1
;;
type foo += B3 = M.B1 (* Error: rebind private extension *)
;;
type foo += C = Unknown (* Error: unbound extension *)
;;
(* Extensions can be rebound even if type is closed *)
module M : sig type foo type foo += A1 of int end
= struct type foo = .. type foo += A1 of int end
type M.foo += A2 = M.A1
(* Rebinding handles abbreviations *)
type 'a foo = ..
;;
type 'a foo1 = 'a foo = ..
;;
type 'a foo2 = 'a foo = ..
;;
type 'a foo1 +=
A of int
| B of 'a
| C : int foo1
;;
type 'a foo2 +=
D = A
| E = B
| F = C
;;
(* Extensions must obey variances *)
type +'a foo = ..
;;
type 'a foo += A of (int -> 'a)
;;
type 'a foo += B of ('a -> int)
(* ERROR: Parameter variances are not satisfied *)
;;
type _ foo += C : ('a -> int) -> 'a foo
(* ERROR: Parameter variances are not satisfied *)
;;
type 'a bar = ..
;;
type +'a bar += D of (int -> 'a) (* ERROR: type variances do not match *)
;;
(* Exceptions are compatible with extensions *)
module M : sig
type exn +=
Foo of int * float
| Bar : 'a list -> exn
end = struct
exception Bar : 'a list -> exn
exception Foo of int * float
end
;;
module M : sig
exception Bar : 'a list -> exn
exception Foo of int * float
end = struct
type exn +=
Foo of int * float
| Bar : 'a list -> exn
end
;;
exception Foo of int * float
;;
exception Bar : 'a list -> exn
;;
module M : sig
type exn +=
Foo of int * float
| Bar : 'a list -> exn
end = struct
exception Bar = Bar
exception Foo = Foo
end
;;
(* Test toplevel printing *)
type foo = ..
;;
type foo +=
Foo of int * int option
| Bar of int option
;;
let x = Foo(3, Some 4), Bar(Some 5) (* Prints Foo and Bar successfully *)
;;
type foo += Foo of string
;;
let y = x (* Prints Bar but not Foo (which has been shadowed) *)
;;
exception Foo of int * int option
;;
exception Bar of int option
;;
let x = Foo(3, Some 4), Bar(Some 5) (* Prints Foo and Bar successfully *)
;;
type foo += Foo of string
;;
let y = x (* Prints Bar and part of Foo (which has been shadowed) *)
;;
(* Test Obj functions *)
type foo = ..
;;
type foo +=
Foo
| Bar of int
;;
let extension_name e = Obj.extension_name (Obj.extension_constructor e);;
let extension_id e = Obj.extension_id (Obj.extension_constructor e);;
let n1 = extension_name Foo
;;
let n2 = extension_name (Bar 1)
;;
let t = (extension_id (Bar 2)) = (extension_id (Bar 3)) (* true *)
;;
let f = (extension_id (Bar 2)) = (extension_id Foo) (* false *)
;;
let is_foo x = (extension_id Foo) = (extension_id x)
type foo += Foo
;;
let f = is_foo Foo
;;
let _ = Obj.extension_constructor 7 (* Invald_arg *)
;;
let _ = Obj.extension_constructor (object method m = 3 end) (* Invald_arg *)
;;
(* Typed names *)
module Msg : sig
type 'a tag
type result = Result : 'a tag * 'a -> result
val write : 'a tag -> 'a -> unit
val read : unit -> result
type 'a tag += Int : int tag
module type Desc = sig
type t
val label : string
val write : t -> string
val read : string -> t
end
module Define (D : Desc) : sig
type 'a tag += C : D.t tag
end
end = struct
type 'a tag = ..
type ktag = T : 'a tag -> ktag
type 'a kind =
{ tag : 'a tag;
label : string;
write : 'a -> string;
read : string -> 'a; }
type rkind = K : 'a kind -> rkind
type wkind = { f : 'a . 'a tag -> 'a kind }
let readTbl : (string, rkind) Hashtbl.t = Hashtbl.create 13
let writeTbl : (ktag, wkind) Hashtbl.t = Hashtbl.create 13
let read_raw () : string * string = raise (Failure "Not implemented")
type result = Result : 'a tag * 'a -> result
let read () =
let label, content = read_raw () in
let K k = Hashtbl.find readTbl label in
let body = k.read content in
Result(k.tag, body)
let write_raw (label : string) (content : string) =
raise (Failure "Not implemented")
let write (tag : 'a tag) (body : 'a) =
let {f} = Hashtbl.find writeTbl (T tag) in
let k = f tag in
let content = k.write body in
write_raw k.label content
(* Add int kind *)
type 'a tag += Int : int tag
let ik =
{ tag = Int;
label = "int";
write = string_of_int;
read = int_of_string }
let () = Hashtbl.add readTbl "int" (K ik)
let () =
let f (type t) (i : t tag) : t kind =
match i with
Int -> ik
| _ -> assert false
in
Hashtbl.add writeTbl (T Int) {f}
(* Support user defined kinds *)
module type Desc = sig
type t
val label : string
val write : t -> string
val read : string -> t
end
module Define (D : Desc) = struct
type 'a tag += C : D.t tag
let k =
{ tag = C;
label = D.label;
write = D.write;
read = D.read }
let () = Hashtbl.add readTbl D.label (K k)
let () =
let f (type t) (c : t tag) : t kind =
match c with
C -> k
| _ -> assert false
in
Hashtbl.add writeTbl (T C) {f}
end
end;;
let write_int i = Msg.write Msg.Int i;;
module StrM = Msg.Define(struct
type t = string
let label = "string"
let read s = s
let write s = s
end);;
type 'a Msg.tag += String = StrM.C;;
let write_string s = Msg.write String s;;
let read_one () =
let Msg.Result(tag, body) = Msg.read () in
match tag with
Msg.Int -> print_int body
| String -> print_string body
| _ -> print_string "Unknown";;
(* Example of algorithm parametrized with modules *)
let sort (type s) set l =
let module Set = (val set : Set.S with type elt = s) in
Set.elements (List.fold_right Set.add l Set.empty)
let make_set (type s) cmp =
let module S = Set.Make(struct
type t = s
let compare = cmp
end) in
(module S : Set.S with type elt = s)
let both l =
List.map
(fun set -> sort set l)
[ make_set compare; make_set (fun x y -> compare y x) ]
let () =
print_endline (String.concat " " (List.map (String.concat "/")
(both ["abc";"xyz";"def"])))
(* Hiding the internal representation *)
module type S = sig
type t
val to_string: t -> string
val apply: t -> t
val x: t
end
let create (type s) to_string apply x =
let module M = struct
type t = s
let to_string = to_string
let apply = apply
let x = x
end in
(module M : S with type t = s)
let forget (type s) x =
let module M = (val x : S with type t = s) in
(module M : S)
let print x =
let module M = (val x : S) in
print_endline (M.to_string M.x)
let apply x =
let module M = (val x : S) in
let module N = struct
include M
let x = apply x
end in
(module N : S)
let () =
let int = forget (create string_of_int succ 0) in
let str = forget (create (fun s -> s) (fun s -> s ^ s) "X") in
List.iter print (List.map apply [int; apply int; apply (apply str)])
(* Existential types + type equality witnesses -> pseudo GADT *)
module TypEq : sig
type ('a, 'b) t
val apply: ('a, 'b) t -> 'a -> 'b
val refl: ('a, 'a) t
val sym: ('a, 'b) t -> ('b, 'a) t
end = struct
type ('a, 'b) t = unit
let apply _ = Obj.magic
let refl = ()
let sym () = ()
end
module rec Typ : sig
module type PAIR = sig
type t
type t1
type t2
val eq: (t, t1 * t2) TypEq.t
val t1: t1 Typ.typ
val t2: t2 Typ.typ
end
type 'a typ =
| Int of ('a, int) TypEq.t
| String of ('a, string) TypEq.t
| Pair of (module PAIR with type t = 'a)
end = struct
module type PAIR = sig
type t
type t1
type t2
val eq: (t, t1 * t2) TypEq.t
val t1: t1 Typ.typ
val t2: t2 Typ.typ
end
type 'a typ =
| Int of ('a, int) TypEq.t
| String of ('a, string) TypEq.t
| Pair of (module PAIR with type t = 'a)
end
open Typ
let int = Int TypEq.refl
let str = String TypEq.refl
let pair (type s1) (type s2) t1 t2 =
let module P = struct
type t = s1 * s2
type t1 = s1
type t2 = s2
let eq = TypEq.refl
let t1 = t1
let t2 = t2
end in
let pair = (module P : PAIR with type t = s1 * s2) in
Pair pair
module rec Print : sig
val to_string: 'a Typ.typ -> 'a -> string
end = struct
let to_string (type s) t x =
match t with
| Int eq -> string_of_int (TypEq.apply eq x)
| String eq -> Printf.sprintf "%S" (TypEq.apply eq x)
| Pair p ->
let module P = (val p : PAIR with type t = s) in
let (x1, x2) = TypEq.apply P.eq x in
Printf.sprintf "(%s,%s)" (Print.to_string P.t1 x1)
(Print.to_string P.t2 x2)
end
let () =
print_endline (Print.to_string int 10);
print_endline (Print.to_string (pair int (pair str int)) (123, ("A", 456)))
(* #6262: first-class modules and module type aliases *)
module type S1 = sig end
module type S2 = S1
let _f (x : (module S1)) : (module S2) = x
module X = struct
module type S
end
module Y = struct include X end
let _f (x : (module X.S)) : (module Y.S) = x
(* PR#6194, main example *)
module type S3 = sig val x : bool end;;
let f = function
| Some (module M : S3) when M.x ->1
| Some _ [@foooo]-> 2
| None -> 3
;;
print_endline (string_of_int (f (Some (module struct let x = false end))));;
type 'a ty =
| Int : int ty
| Bool : bool ty
let fbool (type t) (x : t) (tag : t ty) =
match tag with
| Bool -> x
;;
(* val fbool : 'a -> 'a ty -> 'a = <fun> *)
(** OK: the return value is x of type t **)
let fint (type t) (x : t) (tag : t ty) =
match tag with
| Int -> x > 0
;;
(* val fint : 'a -> 'a ty -> bool = <fun> *)
(** OK: the return value is x > 0 of type bool;
This has used the equation t = bool, not visible in the return type **)
let f (type t) (x : t) (tag : t ty) =
match tag with
| Int -> x > 0
| Bool -> x
(* val f : 'a -> 'a ty -> bool = <fun> *)
let g (type t) (x : t) (tag : t ty) =
match tag with
| Bool -> x
| Int -> x > 0
(* Error: This expression has type bool but an expression was expected of type
t = int *)
let id x = x;;
let idb1 = (fun id -> let _ = id true in id) id;;
let idb2 : bool -> bool = id;;
let idb3 ( _ : bool ) = false;;
let g (type t) (x : t) (tag : t ty) =
match tag with
| Bool -> idb3 x
| Int -> x > 0
let g (type t) (x : t) (tag : t ty) =
match tag with
| Bool -> idb2 x
| Int -> x > 0
(* Encoding generics using GADTs *)
(* (c) Alain Frisch / Lexifi *)
(* cf. http://www.lexifi.com/blog/dynamic-types *)
(* Basic tag *)
type 'a ty =
| Int: int ty
| String: string ty
| List: 'a ty -> 'a list ty
| Pair: ('a ty * 'b ty) -> ('a * 'b) ty
;;
(* Tagging data *)
type variant =