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.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Associated types (ADR 0047): declarations, definitions and their rules. |
| 2 | |
| 3 | import std/list |
| 4 | import std/process.{type Subject, type proc} |
| 5 | import std/ref.{type Ref} |
| 6 | |
| 7 | pub trait Container<c> |
| 8 | { |
| 9 | type Elem |
| 10 | fn first :: c: c -> Option<Elem<c>> |
| 11 | } |
| 12 | |
| 13 | impl Container<List<a>> |
| 14 | { |
| 15 | type Elem = a |
| 16 | fn first :: c |
| 17 | { |
| 18 | match c { [x, ..] -> Some(x), [] -> None } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Inferred through projections. |
| 23 | fn first_of :: c |
| 24 | { |
| 25 | first c |
| 26 | } |
| 27 | //~ TYPE first_of: fn(a) -> Option<Elem<a>> where Container<a> |
| 28 | |
| 29 | // Definitions: each impl defines each, once, with its own variables. |
| 30 | pub type One { One } |
| 31 | pub type Two { Two } |
| 32 | pub type Three<a> { Three(a) } |
| 33 | pub type Four<a> { Four(a) } |
| 34 | |
| 35 | impl Container<One> //~ ERROR E0407 must define associated type `Elem` |
| 36 | { |
| 37 | fn first :: _ |
| 38 | { |
| 39 | None |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Container<Two> |
| 44 | { |
| 45 | type Elem = Int |
| 46 | type Extra = Int //~ ERROR E0406 not an associated type |
| 47 | fn first :: _ |
| 48 | { |
| 49 | None |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Container<Three<a>> |
| 54 | { |
| 55 | type Elem = b //~ ERROR E0211 type variable `b` |
| 56 | fn first :: _ |
| 57 | { |
| 58 | None |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Reduction must end: a definition's projections take the impl's variables. |
| 63 | impl Container<Four<a>> |
| 64 | { |
| 65 | type Elem = Elem<List<a>> //~ ERROR E0415 might never be computed |
| 66 | fn first :: _ |
| 67 | { |
| 68 | None |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub trait Defaulted<c> |
| 73 | { |
| 74 | type Elem2 = Int //~ ERROR E0120 defined by each impl |
| 75 | fn get :: c: c -> Elem2<c> |
| 76 | } |
| 77 | |
| 78 | impl Container<Bool> |
| 79 | { |
| 80 | type Elem //~ ERROR E0120 must define associated type `Elem` |
| 81 | fn first :: _ |
| 82 | { |
| 83 | None |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // A projection needs its trait, and does not determine its argument. |
| 88 | fn unproved :: //~ ERROR E0402 `Container<c>` is not known to hold |
| 89 | c: c |
| 90 | -> Option<Elem<c>> |
| 91 | { |
| 92 | None |
| 93 | } |
| 94 | |
| 95 | fn undetermined :: //~ ERROR E0403 associated types |
| 96 | -> Option<Elem<c>> |
| 97 | where Container<c> |
| 98 | { |
| 99 | None |
| 100 | } |
| 101 | |
| 102 | // Concrete arguments: the impl decides. |
| 103 | fn concrete :: x: Elem<List<Int>> -> Int |
| 104 | { |
| 105 | x + 1 |
| 106 | } |
| 107 | |
| 108 | // Unknown arguments: the equality waits for them. In an inferred |
| 109 | // function it becomes part of its type. |
| 110 | fn forced :: c |
| 111 | { |
| 112 | let x: Option<Int> = first c |
| 113 | x |
| 114 | } |
| 115 | //~ TYPE forced: fn(a) -> Option<Int> where Container<a, Elem = Int> |
| 116 | |
| 117 | fn forced_ok :: -> Option<Int> |
| 118 | { |
| 119 | forced [1] |
| 120 | } |
| 121 | |
| 122 | fn forced_wrong :: -> Option<Int> |
| 123 | { |
| 124 | forced ["one"] //~ ERROR E0202 type mismatch |
| 125 | } |
| 126 | |
| 127 | // In an annotated function nothing determines `x`'s type. |
| 128 | fn undecided :: -> Int |
| 129 | { |
| 130 | let vs = [] |
| 131 | let a = match vs |
| 132 | { [v, ..] -> first v //~ ERROR E0403 which type should implement `Container` |
| 133 | , [] -> None |
| 134 | } |
| 135 | let x: Option<Int> = a //~ ERROR E0403 cannot tell what |
| 136 | 0 |
| 137 | } |
| 138 | |
| 139 | // Each equality is decided under its own arm's equalities: `Elem<r>` is |
| 140 | // `Int` in one arm and `String` in the other. |
| 141 | type Kind<r> { Ints -> Kind<List<Int>>, Texts -> Kind<List<String>> } |
| 142 | |
| 143 | fn per_arm :: k: Kind<r> x: r -> Int where Container<r> |
| 144 | { |
| 145 | let vs = [] |
| 146 | let a = match vs { [v, ..] -> first v, [] -> None } |
| 147 | let n = match k |
| 148 | { Ints -> |
| 149 | { |
| 150 | let i: Option<Int> = a |
| 151 | 0 |
| 152 | } |
| 153 | , Texts -> |
| 154 | { |
| 155 | let s: Option<String> = a |
| 156 | 1 |
| 157 | } |
| 158 | } |
| 159 | let _ = [x, ..vs] |
| 160 | n |
| 161 | } |
| 162 | |
| 163 | fn per_arm_wrong :: k: Kind<r> x: r -> Int where Container<r> |
| 164 | { |
| 165 | let vs = [] |
| 166 | let a = match vs { [v, ..] -> first v, [] -> None } |
| 167 | let n = match k |
| 168 | { Ints -> |
| 169 | { |
| 170 | let i: Option<String> = a //~ ERROR E0202 type mismatch |
| 171 | 0 |
| 172 | } |
| 173 | , Texts -> |
| 174 | { |
| 175 | let s: Option<String> = a |
| 176 | 1 |
| 177 | } |
| 178 | } |
| 179 | let _ = [x, ..vs] |
| 180 | n |
| 181 | } |
| 182 | |
| 183 | // Bindings: what a projection is, in a `where` clause. |
| 184 | fn total :: c: c -> Int where Container<c, Elem = Int> |
| 185 | { |
| 186 | 0 |
| 187 | } |
| 188 | |
| 189 | fn total_ok :: -> Int |
| 190 | { |
| 191 | total [1, 2] |
| 192 | } |
| 193 | |
| 194 | fn total_wrong :: -> Int |
| 195 | { |
| 196 | total ["one"] //~ ERROR E0202 type mismatch |
| 197 | } |
| 198 | |
| 199 | fn bound_var :: c: c -> Option<e> where Container<c, Elem = e> |
| 200 | { |
| 201 | first c |
| 202 | } |
| 203 | |
| 204 | fn unbound_var :: -> Option<e> where Container<c, Elem = e> |
| 205 | //~^ ERROR E0403 only in the `where` clause |
| 206 | { |
| 207 | None |
| 208 | } |
| 209 | |
| 210 | fn cyclic :: c: c -> Int where Container<c, Elem = List<Elem<c>>> |
| 211 | //~^ ERROR E0416 refers to itself |
| 212 | { |
| 213 | 0 |
| 214 | } |
| 215 | |
| 216 | fn twice :: //~ ERROR E0416 bound twice |
| 217 | c: c |
| 218 | -> Int |
| 219 | where Container<c, Elem = Int>, Container<c, Elem = String> |
| 220 | { |
| 221 | 0 |
| 222 | } |
| 223 | |
| 224 | fn against_impl :: x: Int -> Int where Container<List<Int>, Elem = String> |
| 225 | //~^ ERROR E0416 by its impl |
| 226 | { |
| 227 | x |
| 228 | } |
| 229 | |
| 230 | fn unknown_name :: c: c -> Int where Container<c, Size = Int> |
| 231 | //~^ ERROR E0406 no associated type `Size` |
| 232 | { |
| 233 | 0 |
| 234 | } |
| 235 | |
| 236 | fn misplaced :: x: Option<Int, Elem = Int> -> Int |
| 237 | //~^ ERROR E0416 can only follow a trait's arguments |
| 238 | { |
| 239 | 0 |
| 240 | } |
| 241 | |
| 242 | // Quantified bindings of generic associated types hold for every type. |
| 243 | pub trait Mappable<c> |
| 244 | { |
| 245 | type Mapped<x> |
| 246 | fn map_all :: c: c f: fn(Int) -> x -> Mapped<c, x> |
| 247 | } |
| 248 | |
| 249 | pub type Duo { Duo(Int, Int) } |
| 250 | |
| 251 | impl Mappable<Duo> |
| 252 | { |
| 253 | type Mapped<x> = (x, x) |
| 254 | fn map_all :: d f |
| 255 | { |
| 256 | match d { Duo(a, b) -> (f a, f b) } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | pub type IntList { IntList(List<Int>) } |
| 261 | |
| 262 | impl Mappable<IntList> |
| 263 | { |
| 264 | type Mapped<x> = List<x> |
| 265 | fn map_all :: c f |
| 266 | { |
| 267 | match c { IntList(ns) -> list.map ns f } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | fn needs_list :: c: c -> Int where Mappable<c, Mapped<y> = List<y>> |
| 272 | { |
| 273 | 0 |
| 274 | } |
| 275 | //~ TYPE needs_list: fn(c) -> Int where Mappable<c, Mapped<y> = List<y>> |
| 276 | |
| 277 | fn needs_list_ok :: -> Int |
| 278 | { |
| 279 | needs_list IntList([1]) |
| 280 | } |
| 281 | |
| 282 | fn needs_list_wrong :: -> Int |
| 283 | { |
| 284 | needs_list //~ ERROR E0202 type mismatch |
| 285 | Duo(1, 2) |
| 286 | } |
| 287 | |
| 288 | fn shadowing :: c: c y: y -> Int where Mappable<c, Mapped<y> = List<y>> |
| 289 | //~^ ERROR E0416 already names a type variable |
| 290 | { |
| 291 | 0 |
| 292 | } |
| 293 | |
| 294 | // Not an index of a constructor, and no derive. |
| 295 | type Indexed<a> |
| 296 | { ByElem(Int) |
| 297 | -> Indexed< //~ ERROR E0214 cannot mention an associated type |
| 298 | Elem<List<Int>> |
| 299 | > |
| 300 | } |
| 301 | |
| 302 | @derivable count |
| 303 | trait Sized2<a> //~ ERROR E0412 has associated types |
| 304 | { |
| 305 | type Unit |
| 306 | fn size :: x: a -> Int |
| 307 | } |
| 308 | |
| 309 | fn count :: c: Constructor<Int> -> Int |
| 310 | { |
| 311 | 0 |
| 312 | } |
| 313 | |
| 314 | // `Send` looks through projections. |
| 315 | pub trait Holds<c> |
| 316 | { |
| 317 | type Held |
| 318 | fn held :: c: c -> Held<c> |
| 319 | } |
| 320 | |
| 321 | pub type Cell<h> { Cell(Ref<h, Int>) } |
| 322 | |
| 323 | impl Holds<Cell<h>> |
| 324 | { |
| 325 | type Held = Ref<h, Int> |
| 326 | fn held :: c |
| 327 | { |
| 328 | match c { Cell(r) -> r } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | fn sends_held :: c: Cell<h> to: Subject<Held<Cell<h>>> -> () / <proc> |
| 333 | { |
| 334 | process.send to (held c) //~ ERROR E0704 cannot be sent |
| 335 | } |
| 336 | |
| 337 | fn sends_generic :: c: c to: Subject<Held<c>> -> () / <proc> where Holds<c> |
| 338 | { |
| 339 | process.send //~ ERROR E0402 `Held<c>` is not known to implement `Send` |
| 340 | to |
| 341 | (held c) |
| 342 | } |
| 343 | |
| 344 | fn sends_bounded :: |
| 345 | c: c |
| 346 | to: Subject<Held<c>> |
| 347 | -> () / <proc> |
| 348 | where Holds<c>, Send<Held<c>> |
| 349 | { |
| 350 | process.send to (held c) |
| 351 | } |