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 | // Traits with several parameters (ADR 0018). |
| 2 | |
| 3 | trait Convert<a, b> |
| 4 | { |
| 5 | fn convert :: x: a -> b |
| 6 | } |
| 7 | |
| 8 | impl Convert<Int, String> |
| 9 | { |
| 10 | fn convert :: n |
| 11 | { |
| 12 | show n |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl Convert<List<a>, List<b>> where Convert<a, b> |
| 17 | { |
| 18 | fn convert :: xs |
| 19 | { |
| 20 | map xs convert |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | fn map :: xs f |
| 25 | { |
| 26 | match xs { [] -> [], [x, ..rest] -> [f x, ..map rest f] } |
| 27 | } |
| 28 | |
| 29 | // Constraints on generalized variables become `where` clauses. |
| 30 | fn convert_each :: xs |
| 31 | { |
| 32 | map xs convert |
| 33 | } |
| 34 | //~ TYPE convert_each: fn(List<a>) -> List<b> where Convert<a, b> |
| 35 | |
| 36 | fn to_strings :: xs: List<Int> -> List<String> |
| 37 | { |
| 38 | convert xs |
| 39 | } |
| 40 | |
| 41 | // Every argument must be known: there are no functional dependencies. |
| 42 | fn unknown_result :: -> String |
| 43 | { |
| 44 | show //~ ERROR E0403 cannot tell which type should implement `Show` |
| 45 | (convert 1) |
| 46 | //~^ ERROR E0403 cannot tell which impl of `Convert` to use here: `Convert<Int, _>` has unknown types |
| 47 | } |
| 48 | |
| 49 | fn known_result :: -> String |
| 50 | { |
| 51 | let s: String = convert 1 |
| 52 | s |
| 53 | } |
| 54 | |
| 55 | pub fn no_impl :: -> String |
| 56 | { |
| 57 | convert True //~ ERROR E0401 no impl of `Convert<Bool, String>` exists |
| 58 | } |
| 59 | |
| 60 | pub fn nested_no_impl :: -> List<String> |
| 61 | { |
| 62 | convert //~ ERROR E0401 no impl of `Convert<Bool, String>` exists |
| 63 | [True] |
| 64 | } |
| 65 | |
| 66 | pub fn missing_given :: x: a -> String where Show<a> |
| 67 | { |
| 68 | convert x //~ ERROR E0402 `Convert<a, String>` is not known to hold |
| 69 | } |
| 70 | |
| 71 | pub fn given :: x: a -> b where Convert<a, b> |
| 72 | { |
| 73 | convert x |
| 74 | } |
| 75 | |
| 76 | // A variable only in the `where` clause can never be determined. |
| 77 | pub fn through :: x: a -> c where Convert<a, b>, Convert<b, c> |
| 78 | //~^ ERROR E0403 type variable `b` appears only in the `where` clause of `through` |
| 79 | { |
| 80 | panic "unused" |
| 81 | } |
| 82 | |
| 83 | trait Empty<c, e> |
| 84 | { |
| 85 | fn empty :: -> c |
| 86 | //~^ ERROR E0403 method `empty` does not mention the trait's type parameter `e` |
| 87 | } |
| 88 | |
| 89 | trait Twice<a, a> //~ ERROR E0411 must be distinct |
| 90 | { |
| 91 | fn twice :: x: a -> a |
| 92 | } |
| 93 | |
| 94 | // Two impls overlap exactly when their head types are equal. |
| 95 | impl Convert<Option<a>, Bool> |
| 96 | { |
| 97 | fn convert :: _ |
| 98 | { |
| 99 | True |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl Convert< |
| 104 | //~^ ERROR E0404 `Convert` is implemented more than once for `Option, Bool` |
| 105 | Option<b> |
| 106 | , Bool |
| 107 | > |
| 108 | { |
| 109 | fn convert :: _ |
| 110 | { |
| 111 | False |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Every argument is a named type applied to distinct variables. |
| 116 | impl Convert<a, Int> //~ ERROR E0409 distinct type variables |
| 117 | { |
| 118 | fn convert :: _ |
| 119 | { |
| 120 | 0 |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl Convert<Option<a>, List<a>> //~ ERROR E0409 distinct type variables |
| 125 | { |
| 126 | fn convert :: _ |
| 127 | { |
| 128 | [] |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl Convert< //~ ERROR E0409 takes 2 type arguments, but the impl gives 1 |
| 133 | Bool |
| 134 | > |
| 135 | { |
| 136 | fn convert :: _ |
| 137 | { |
| 138 | 0 |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | pub fn wrong_arity :: x: a -> a where Convert<a> |
| 143 | //~^ ERROR E0212 takes 2 type arguments, but 1 was given |
| 144 | { |
| 145 | x |
| 146 | } |
| 147 | |
| 148 | // Orphan rule: the impl must be in the module of the trait or of one of |
| 149 | // its head types (here, neither). |
| 150 | impl Show< //~ ERROR E0405 must be in the module of the trait or of the type |
| 151 | Int |
| 152 | > |
| 153 | { |
| 154 | fn show :: _ |
| 155 | { |
| 156 | "" |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // A head may also be a tuple of distinct variables (ADR 0018 Rev. 1). |
| 161 | // A tuple has no module, so such an impl lives with its trait. |
| 162 | impl Convert<(a, b), Int> |
| 163 | { |
| 164 | fn convert :: _ |
| 165 | { |
| 166 | 2 |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl Convert< |
| 171 | //~^ ERROR E0404 `Convert` is implemented more than once for `(_, _), Int` |
| 172 | (c, d) |
| 173 | , Int |
| 174 | > |
| 175 | { |
| 176 | fn convert :: _ |
| 177 | { |
| 178 | 0 |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | impl Convert<(a, a), Bool> //~ ERROR E0409 distinct type variables |
| 183 | { |
| 184 | fn convert :: _ |
| 185 | { |
| 186 | True |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | impl Convert<(a, Int), Bool> //~ ERROR E0409 distinct type variables |
| 191 | { |
| 192 | fn convert :: _ |
| 193 | { |
| 194 | True |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | impl Show< |
| 199 | //~^ ERROR E0405 impl of `Show` for `(_, _, _, _, _, _, _, _, _, _, _, _, _)` must be in the module of the trait |
| 200 | (a, b, c, d, e, f, g, h, i, j, k, l, m) |
| 201 | > |
| 202 | { |
| 203 | fn show :: _ |
| 204 | { |
| 205 | "" |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | pub fn pairs_convert :: p: (Bool, String) -> Int |
| 210 | { |
| 211 | convert p |
| 212 | } |