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
|
(* TEST
expect;
*)
let id x = x
let apply x f = f x
let pair x y = x, y
module Id = struct
let (let+) = apply
let (and+) = pair
end;;
[%%expect{|
val id : 'a -> 'a = <fun>
val apply : 'a -> ('a -> 'b) -> 'b = <fun>
val pair : 'a -> 'b -> 'a * 'b = <fun>
module Id :
sig
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : 'a -> 'b -> 'a * 'b
end
|}];;
let res =
Id.(
let+ x = 1
and+ y = 2
and+ z = 3 in
[x; y; z]
);;
[%%expect{|
val res : int list = [1; 2; 3]
|}];;
let res2 =
Id.(
let+ x = 1 in
x + 2
);;
[%%expect{|
val res2 : int = 3
|}];;
module List = struct
let map l f = List.map f l
let concat_map l f =
let l = List.map f l in
List.concat l
let product xs ys =
List.fold_right
(fun x acc -> (List.map (fun y -> (x, y)) ys) @ acc)
xs []
let (let+) = map
let (and+) = product
let ( let* ) = concat_map
let ( and* ) = product
end;;
[%%expect{|
module List :
sig
val map : 'a list -> ('a -> 'b) -> 'b list
val concat_map : 'a list -> ('a -> 'b list) -> 'b list
val product : 'a list -> 'b list -> ('a * 'b) list
val ( let+ ) : 'a list -> ('a -> 'b) -> 'b list
val ( and+ ) : 'a list -> 'b list -> ('a * 'b) list
val ( let* ) : 'a list -> ('a -> 'b list) -> 'b list
val ( and* ) : 'a list -> 'b list -> ('a * 'b) list
end
|}];;
let map =
List.(
let+ x = [1; 2; 3] in
x + 1
);;
[%%expect{|
val map : int list = [2; 3; 4]
|}];;
let map_and =
List.(
let+ x = [1; 2; 3]
and+ y = [7; 8; 9] in
x + y
);;
[%%expect{|
val map_and : int list = [8; 9; 10; 9; 10; 11; 10; 11; 12]
|}];;
let bind =
List.(
let* x = [1; 2; 3] in
let* y = [7; 8; 9] in
[x + y]
);;
[%%expect{|
val bind : int list = [8; 9; 10; 9; 10; 11; 10; 11; 12]
|}];;
let bind_and =
List.(
let* x = [1; 2; 3]
and* y = [7; 8; 9] in
[x + y]
);;
[%%expect{|
val bind_and : int list = [8; 9; 10; 9; 10; 11; 10; 11; 12]
|}];;
let bind_map =
List.(
let* x = [1; 2; 3] in
let+ y = [7; 8; 9] in
x + y
);;
[%%expect{|
val bind_map : int list = [8; 9; 10; 9; 10; 11; 10; 11; 12]
|}];;
module Let_unbound = struct
end;;
[%%expect{|
module Let_unbound : sig end
|}];;
let let_unbound =
Let_unbound.(
let+ x = 1 in
x + y
);;
[%%expect{|
Line 3, characters 4-8:
3 | let+ x = 1 in
^^^^
Error: Unbound value "(let+)"
|}];;
module And_unbound = struct
let (let+) = Id.(let+)
end;;
[%%expect{|
module And_unbound : sig val ( let+ ) : 'a -> ('a -> 'b) -> 'b end
|}];;
let and_unbound =
And_unbound.(
let+ x = 1
and+ y = 2 in
x + y
);;
[%%expect{|
Line 4, characters 4-8:
4 | and+ y = 2 in
^^^^
Error: Unbound value "(and+)"
|}];;
module Ill_typed_1 = struct
let (let+) = fun x f -> f (not x)
end;;
[%%expect{|
module Ill_typed_1 : sig val ( let+ ) : bool -> (bool -> 'a) -> 'a end
|}];;
let ill_typed_1 =
Ill_typed_1.(
let+ x = 1 in
x + y
);;
[%%expect{|
Line 3, characters 13-14:
3 | let+ x = 1 in
^
Error: The constant "1" has type "int" but an expression was expected of type
"bool"
|}];;
module Ill_typed_2 = struct
let (let+) = apply
let (and+) = fun x y -> x +. y, x -. y
end;;
[%%expect{|
module Ill_typed_2 :
sig
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : float -> float -> float * float
end
|}];;
let ill_typed_2 =
Ill_typed_2.(
let+ x = 1
and+ y = 2 in
x + y
);;
[%%expect{|
Line 3, characters 13-14:
3 | let+ x = 1
^
Error: The constant "1" has type "int" but an expression was expected of type
"float"
Hint: Did you mean "1."?
|}];;
module Ill_typed_3 = struct
let (let+) = 7
end;;
[%%expect{|
module Ill_typed_3 : sig val ( let+ ) : int end
|}];;
let ill_typed_3 =
Ill_typed_3.(
let+ x = 1 in
x + y
);;
[%%expect{|
Line 3, characters 4-8:
3 | let+ x = 1 in
^^^^
Error: The operator "let+" has type "int" but it was expected to have type
"'a -> ('b -> 'c) -> 'd"
|}];;
module Ill_typed_4 = struct
let (let+) = apply
let (and+) = not
end;;
[%%expect{|
module Ill_typed_4 :
sig val ( let+ ) : 'a -> ('a -> 'b) -> 'b val ( and+ ) : bool -> bool end
|}];;
let ill_typed_4 =
Ill_typed_4.(
let+ x = 1
and+ y = 2 in
x + y
);;
[%%expect{|
Line 4, characters 4-8:
4 | and+ y = 2 in
^^^^
Error: The operator "and+" has type "bool -> bool"
but it was expected to have type "bool -> 'a -> 'b"
Type "bool" is not compatible with type "'a -> 'b"
|}];;
module Ill_typed_5 = struct
let (let+) = (fun x f -> not x)
let (and+) = pair
end;;
[%%expect{|
module Ill_typed_5 :
sig
val ( let+ ) : bool -> 'a -> bool
val ( and+ ) : 'a -> 'b -> 'a * 'b
end
|}];;
let ill_typed_5 =
Ill_typed_5.(
let+ x = 1
and+ y = 2
and+ z = 3 in
x + y + z
);;
[%%expect{|
Lines 3-5, characters 9-14:
3 | .........x = 1
4 | and+ y = 2
5 | and+ z = 3...
Error: These bindings have type "(int * int) * int"
but bindings were expected of type "bool"
|}];;
module Ill_typed_6 = struct
let (let+) = apply
let (and+) = fun x y -> x + 1, y
end;;
[%%expect{|
module Ill_typed_6 :
sig
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : int -> 'a -> int * 'a
end
|}];;
let ill_typed_6 =
Ill_typed_6.(
let+ x = 1
and+ y = 2
and+ z = 3 in
x + y + z
);;
[%%expect{|
Lines 3-4, characters 9-14:
3 | .........x = 1
4 | and+ y = 2
Error: These bindings have type "int * int" but bindings were expected of type
"int"
|}];;
module Ill_typed_7 = struct
let (let+) f x = f (x + 1)
let (and+) = pair
end;;
[%%expect{|
module Ill_typed_7 :
sig
val ( let+ ) : (int -> 'a) -> int -> 'a
val ( and+ ) : 'a -> 'b -> 'a * 'b
end
|}];;
let ill_typed_7 =
Ill_typed_7.(
let+ x = 1
and+ y = 2 in
x + y
);;
[%%expect{|
Line 3, characters 4-8:
3 | let+ x = 1
^^^^
Error: The operator "let+" has type "(int -> 'a) -> int -> 'a"
but it was expected to have type "(int -> 'a) -> ('b * 'c -> 'd) -> 'e"
Type "int" is not compatible with type "'b * 'c -> 'd"
|}];;
module Indexed_monad = struct
type opened = private Opened
type closed = private Closed
type (_, _, _) t =
| Return : 'a -> ('s, 's, 'a) t
| Map : ('s1, 's2, 'a) t * ('a -> 'b) -> ('s1, 's2, 'b) t
| Both : ('s1, 's2, 'a) t * ('s2, 's3, 'b) t -> ('s1, 's3, 'a * 'b) t
| Bind : ('s1, 's2, 'a) t * ('a -> ('s2, 's3, 'b) t) -> ('s1, 's3, 'b) t
| Open : string -> (closed, opened, unit) t
| Read : (opened, opened, string) t
| Close : (opened, closed, unit) t
let return x = Return x
let map m f = Map(m, f)
let both m1 m2 = Both(m1, m2)
let bind m f = Bind(m, f)
let open_ s = Open s
let read = Read
let close = Close
type 'a state =
| Opened : in_channel -> opened state
| Closed : closed state
let run (type a) (m : (closed, closed, a) t) : a =
let rec loop : type a s1 s2. s1 state -> (s1, s2, a) t -> s2 state * a =
fun state m ->
match m, state with
| Return x, _ -> state, x
| Map(m, f), _ ->
let state2, x = loop state m in
state2, f x
| Both(m1, m2), _ ->
let state2, x = loop state m1 in
let state3, y = loop state2 m2 in
state3, (x, y)
| Bind(m, f), _ ->
let state2, x = loop state m in
loop state2 (f x)
| Open filename, Closed ->
let ic = open_in filename in
Opened ic, ()
| Read, Opened ic ->
let c = input_line ic in
state, c
| Close, Opened ic ->
close_in ic;
Closed, ()
in
let Closed, result = loop Closed m in
result
let ( let+ ) = map
let ( and+ ) = both
let ( let* ) = bind
let ( and* ) = both
end;;
[%%expect {|
module Indexed_monad :
sig
type opened = private Opened
type closed = private Closed
type (_, _, _) t =
Return : 'a -> ('s, 's, 'a) t
| Map : ('s1, 's2, 'a) t * ('a -> 'b) -> ('s1, 's2, 'b) t
| Both : ('s1, 's2, 'a) t * ('s2, 's3, 'b) t -> ('s1, 's3, 'a * 'b) t
| Bind : ('s1, 's2, 'a) t *
('a -> ('s2, 's3, 'b) t) -> ('s1, 's3, 'b) t
| Open : string -> (closed, opened, unit) t
| Read : (opened, opened, string) t
| Close : (opened, closed, unit) t
val return : 'a -> ('b, 'b, 'a) t
val map : ('a, 'b, 'c) t -> ('c -> 'd) -> ('a, 'b, 'd) t
val both : ('a, 'b, 'c) t -> ('b, 'd, 'e) t -> ('a, 'd, 'c * 'e) t
val bind : ('a, 'b, 'c) t -> ('c -> ('b, 'd, 'e) t) -> ('a, 'd, 'e) t
val open_ : string -> (closed, opened, unit) t
val read : (opened, opened, string) t
val close : (opened, closed, unit) t
type 'a state =
Opened : in_channel -> opened state
| Closed : closed state
val run : (closed, closed, 'a) t -> 'a
val ( let+ ) : ('a, 'b, 'c) t -> ('c -> 'd) -> ('a, 'b, 'd) t
val ( and+ ) : ('a, 'b, 'c) t -> ('b, 'd, 'e) t -> ('a, 'd, 'c * 'e) t
val ( let* ) : ('a, 'b, 'c) t -> ('c -> ('b, 'd, 'e) t) -> ('a, 'd, 'e) t
val ( and* ) : ('a, 'b, 'c) t -> ('b, 'd, 'e) t -> ('a, 'd, 'c * 'e) t
end
|}];;
let indexed_monad1 =
Indexed_monad.(
let+ () = open_ "foo"
and+ first = read
and+ second = read
and+ () = close in
first ^ second
);;
[%%expect{|
val indexed_monad1 :
(Indexed_monad.closed, Indexed_monad.closed, string) Indexed_monad.t =
Indexed_monad.Map
(Indexed_monad.Both
(Indexed_monad.Both
(Indexed_monad.Both (Indexed_monad.Open "foo", Indexed_monad.Read),
Indexed_monad.Read),
Indexed_monad.Close),
<fun>)
|}];;
let indexed_monad2 =
Indexed_monad.(
let* () = open_ "foo" in
let* first = read in
let* second = read in
let* () = close in
return (first ^ second)
);;
[%%expect{|
val indexed_monad2 :
(Indexed_monad.closed, Indexed_monad.closed, string) Indexed_monad.t =
Indexed_monad.Bind (Indexed_monad.Open "foo", <fun>)
|}];;
let indexed_monad3 =
Indexed_monad.(
let+ first = read
and+ () = open_ "foo"
and+ second = read
and+ () = close in
first ^ second
);;
[%%expect{|
Line 4, characters 14-25:
4 | and+ () = open_ "foo"
^^^^^^^^^^^
Error: This expression has type
"(Indexed_monad.closed, Indexed_monad.opened, unit) Indexed_monad.t"
but an expression was expected of type
"(Indexed_monad.opened, 'a, 'b) Indexed_monad.t"
Type "Indexed_monad.closed" is not compatible with type
"Indexed_monad.opened"
|}];;
let indexed_monad4 =
Indexed_monad.(
let* () = open_ "foo" in
let* first = read in
let* () = close in
let* second = read in
return (first ^ second)
);;
[%%expect{|
Lines 6-7, characters 4-29:
6 | ....let* second = read in
7 | return (first ^ second)
Error: This expression has type
"(Indexed_monad.opened, Indexed_monad.opened, string) Indexed_monad.t"
but an expression was expected of type
"(Indexed_monad.closed, 'a, 'b) Indexed_monad.t"
Type "Indexed_monad.opened" is not compatible with type
"Indexed_monad.closed"
|}];;
(* Test principality using constructor disambiguation *)
module A = struct
type t = A
end
module Let_principal = struct
let ( let+ ) (x : A.t) f = f x
end;;
[%%expect{|
module A : sig type t = A end
module Let_principal : sig val ( let+ ) : A.t -> (A.t -> 'a) -> 'a end
|}];;
let let_principal =
Let_principal.(
let+ A = A in
()
);;
[%%expect{|
val let_principal : unit = ()
|}];;
module And_principal = struct
let ( let+ ) = apply
let ( and+ ) (x : A.t) y = x, y
end;;
[%%expect{|
module And_principal :
sig
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : A.t -> 'a -> A.t * 'a
end
|}];;
let and_principal =
And_principal.(
let+ _ = A
and+ () = () in
()
);;
[%%expect{|
val and_principal : unit = ()
|}];;
module Let_not_principal = struct
let ( let+ ) = apply
end;;
[%%expect{|
module Let_not_principal : sig val ( let+ ) : 'a -> ('a -> 'b) -> 'b end
|}];;
let let_not_principal =
Let_not_principal.(
let+ A = A.A in
()
);;
[%%expect{|
val let_not_principal : unit = ()
|}, Principal{|
Line 3, characters 9-10:
3 | let+ A = A.A in
^
Warning 18 [not-principal]: this type-based constructor disambiguation is not principal.
val let_not_principal : unit = ()
|}];;
module And_not_principal = struct
let ( let+ ) = apply
let ( and+ ) x y = if true then x,y else y,x
end;;
[%%expect{|
module And_not_principal :
sig
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : 'a -> 'a -> 'a * 'a
end
|}];;
let and_not_principal =
And_not_principal.(
fun x y ->
let+ A.A = x
and+ A = y in
()
);;
[%%expect{|
val and_not_principal : A.t -> A.t -> unit = <fun>
|}, Principal{|
Line 5, characters 11-12:
5 | and+ A = y in
^
Warning 18 [not-principal]: this type-based constructor disambiguation is not principal.
val and_not_principal : A.t -> A.t -> unit = <fun>
|}];;
module Let_not_propagated = struct
let ( let+ ) = apply
end;;
[%%expect{|
module Let_not_propagated : sig val ( let+ ) : 'a -> ('a -> 'b) -> 'b end
|}];;
let let_not_propagated : A.t =
Let_not_propagated.(
let+ x = 3 in
A
);;
[%%expect{|
Line 4, characters 4-5:
4 | A
^
Error: Unbound constructor "A"
|}];;
module Side_effects_ordering = struct
let r = ref []
let msg s =
r := !r @ [s]
let output () = !r
let ( let+ ) x f = msg "Let operator"; f x
let ( and+ ) a b = msg "First and operator"; a, b
let ( and++ ) a b = msg "Second and operator"; a, b
end;;
[%%expect{|
module Side_effects_ordering :
sig
val r : string list ref
val msg : string -> unit
val output : unit -> string list
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : 'a -> 'b -> 'a * 'b
val ( and++ ) : 'a -> 'b -> 'a * 'b
end
|}];;
let side_effects_ordering =
Side_effects_ordering.(
let+ () = msg "First argument"
and+ () = msg "Second argument"
and++ () = msg "Third argument" in
output ()
);;
[%%expect{|
val side_effects_ordering : string list =
["First argument"; "Second argument"; "First and operator";
"Third argument"; "Second and operator"; "Let operator"]
|}];;
module GADT_ordering = struct
type point = { x : int; y : int }
type _ is_point =
| Is_point : point is_point
let (let+) = apply
let (and+) = pair
end;;
[%%expect{|
module GADT_ordering :
sig
type point = { x : int; y : int; }
type _ is_point = Is_point : point is_point
val ( let+ ) : 'a -> ('a -> 'b) -> 'b
val ( and+ ) : 'a -> 'b -> 'a * 'b
end
|}];;
let gadt_ordering =
GADT_ordering.(
fun (type a) (is_point : a is_point) (a : a) ->
let+ Is_point : a is_point = is_point
and+ { x; y } : a = a in
x + y
);;
[%%expect{|
val gadt_ordering : 'a GADT_ordering.is_point -> 'a -> int = <fun>
|}];;
(* This example doesn't produce a good error location. To fix this we need to handle the
patterns directly rather than elaborating them to tuples. We'd like to do this in
future but it is quite a bit of work, so for now we leave the location as it is. It
should only appear in principal mode when using GADTs anyway. *)
let bad_location =
GADT_ordering.(
fun (type a) (is_point : a is_point) (a : a) ->
let+ Is_point = is_point
and+ { x; y } = a in
x + y
);;
[%%expect{|
val bad_location : 'a GADT_ordering.is_point -> 'a -> int = <fun>
|}, Principal{|
Line 4, characters 11-19:
4 | let+ Is_point = is_point
^^^^^^^^
Warning 18 [not-principal]: typing this pattern requires considering GADT_ordering.point and a as equal.
But the knowledge of these types is not principal.
Line 5, characters 11-19:
5 | and+ { x; y } = a in
^^^^^^^^
Error: This pattern matches values of type "GADT_ordering.point"
but a pattern was expected which matches values of type
"a" = "GADT_ordering.point"
This instance of "GADT_ordering.point" is ambiguous:
it would escape the scope of its equation
|}];;
|