Debian Codegraph @

lang/interpret_test/TestAstFunc2/very-complex-example.txtar

raw
1 -- main.mcl --
2 # This code should fail during type-checking.
3 #
4 # $id1 and $id2 are both polymorphic functions, which can be specialized to a
5 # different monomorphic type at every use site. In simple code, this
6 # specialization happens when the polymorphic function is called, but in more
7 # complex code, the specialization happens earlier, when the polymorphic
8 # function is passed as an argument to higher-order functions such as $generate
9 # and iter.map.
10 #
11 # In:
12 #
13 # $id1 = func($x str) { # definition site
14 # $x
15 # }
16 # $id2 = func($x str) {
17 # $x + $x
18 # }
19 #
20 # $generate = func($idn) {
21 # $idn("foo") # 1 call site, 2 calls
22 # }
23 #
24 # $foo = iter.map([$id1, $id2,], $generate)
25 #
26 # $generate is specialized to `func(func(str) str) str`, and $id1 and $id2 are
27 # specialized to `func(str) str`.
28 #
29 # In:
30 #
31 # $id1 = func($x) { # definition site
32 # $x
33 # }
34 # $id2 = func($x) {
35 # $x + $x
36 # }
37 #
38 # $generate = func($idn) {
39 # fmt.printf("%s %d",
40 # $idn("foo"), # 1 call site, 2 calls
41 # $idn(42)
42 # )
43 # }
44 #
45 # $foo = iter.map([$id1, $id2,], $generate)
46 #
47 # $idn cannot be given a monomorphic type, since it is used both as a
48 # `func(str) str` and as a `func(int) int`. Therefore, $generate cannot be
49 # given a monomorphic type either, and neither can the call to iter.map.
50
51 import "fmt"
52 import "iter"
53
54 # function expression
55 $id1 = func($x) { # definition site
56 $x
57 }
58 $id2 = func($x) {
59 $x + $x
60 }
61
62 #$str = $id1("foo")
63 #$int = $id1(42)
64
65 $generate = func($idn) {
66 fmt.printf("%s %d",
67 $idn("foo"), # 1 call site, 2 calls
68 $idn(42)
69 )
70 }
71
72 # this code should be rejected during type unification
73 $foo = iter.map([$id1, $id2,], $generate)
74
75 $name = $foo[0] || "fail"
76 test "${name}" {}
77 -- OUTPUT --
78 # err: errUnify: unify error with: param(idn): type error: str != int