tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

177 lignes · 2849 octetsBrut · Historique
1// Edge cases a sound checker must get right.
2
3// Rigid variables are distinct from each other.
4pub fn coerce :: x: a -> b
5{
6 x //~ ERROR E0202 expected `b`, found `a`
7}
8
9// A generalized function can be used at several types...
10fn ident :: x
11{
12 x
13}
14fn two_uses ::
15{
16 (ident 1, ident "a")
17}
18//~ TYPE two_uses: fn() -> (Int, String)
19
20// ...but within its own recursive group it is monomorphic.
21fn ping :: n
22{
23 pong n
24}
25fn pong :: n
26{
27 let s = ping "x"
28 ping 1 //~ ERROR E0202 expected `String`, found `Int`
29}
30
31// Constraints flow through impls with `where` clauses.
32pub type Box<a> { Box(a) }
33
34impl Show<Box<a>> where Show<a>
35{
36 fn show :: b
37 {
38 match b { Box(x) -> "Box(" <> show x <> ")" }
39 }
40}
41
42pub fn show_box :: b: Box<Int> -> String
43{
44 show b
45}
46
47pub fn show_fn_box :: b: Box<fn() -> Int> -> String
48{
49 show
50 //~^ ERROR E0401 `fn() -> Int` does not implement `Show` (needed for `Show<Box<fn() -> Int>>`)
51 b
52}
53
54pub type Wrap<a> { Wrap(a) }
55
56impl Show<Wrap<a>>
57{
58 fn show :: w
59 {
60 match w
61 { Wrap(x) -> show x //~ ERROR E0402 `a` is not known to implement `Show`
62 }
63 }
64}
65
66// An inferred constraint is satisfied at a concrete call site.
67fn render :: x
68{
69 "<" <> show x <> ">"
70}
71pub fn render_int :: -> String
72{
73 render 42
74}
75
76// Never fits any expected type, in every checking position.
77effect fail
78{
79 final ctl fail :: reason: String -> Never
80}
81
82// An inferred function that never returns gets a fully polymorphic
83// result, like Gleam's `panic`: it can stand in for any type.
84fn boom ::
85{
86 fail "x"
87}
88//~ TYPE boom: fn() -> a / <fail>
89
90pub fn never_positions :: flag: Bool -> Int / <fail>
91{
92 let a: Int = boom ()
93 let xs = [fail "a", 1]
94 let y = if flag { fail "b" } else { 2 }
95 match xs { [] -> fail "c", _ -> a + y }
96}
97
98// ...but not inside a structure.
99pub fn never_list :: xs: List<Never> -> List<Int>
100{
101 xs //~ ERROR E0202 expected `List<Int>`, found `List<Never>`
102}
103
104// A handler answers for every type an operation's own variables can take
105// (issue #169): its clauses see them as rigid.
106effect pick
107{
108 fn pick :: x: a -> a
109}
110
111fn lie :: -> String / <pick>
112{
113 pick "hello"
114}
115
116pub fn forged :: -> String
117{
118 handle
119 {
120 lie ()
121 }
122 with
123 { pick _ -> 42 //~ ERROR E0202 expected `a`, found `Int`
124 }
125}
126
127pub fn honest :: -> String
128{
129 handle
130 {
131 lie ()
132 }
133 with
134 { pick x -> x }
135}
136
137effect choice
138{
139 ctl choose :: x: a y: a -> a
140}
141
142pub fn resumed_with_a_fixed_type :: -> Int
143{
144 handle
145 {
146 choose 1 2
147 }
148 with
149 { choose _ _ -> resume "one" //~ ERROR E0202 found `String`
150 }
151}
152
153pub fn resumed_with_an_argument :: -> Int
154{
155 handle
156 {
157 choose 1 2
158 }
159 with
160 { choose x _ -> resume x }
161}
162
163// The effect's own parameters are fixed by the handled label.
164effect cell<s>
165{
166 fn read :: -> s
167}
168
169pub fn read_cell :: -> Int
170{
171 handle
172 {
173 read () + 1
174 }
175 with
176 { read -> 41 }
177}