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 | // Kinds and higher-kinded type parameters (ADR 0018). |
| 2 | |
| 3 | trait Functor<f> |
| 4 | { |
| 5 | fn map :: x: f<a> g: fn(a) -> b / <e> -> f<b> / <e> |
| 6 | } |
| 7 | |
| 8 | impl Functor<List> |
| 9 | { |
| 10 | fn map :: xs g |
| 11 | { |
| 12 | lmap xs g |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl Functor<Option> |
| 17 | { |
| 18 | fn map :: o g |
| 19 | { |
| 20 | match o { Some(x) -> Some(g x), None -> None } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | fn lmap :: xs g |
| 25 | { |
| 26 | match xs { [] -> [], [x, ..rest] -> [g x, ..lmap rest g] } |
| 27 | } |
| 28 | |
| 29 | // Inferred schemes abstract over type constructors. |
| 30 | fn twice_map :: x g |
| 31 | { |
| 32 | map (map x g) g |
| 33 | } |
| 34 | //~ TYPE twice_map: fn(a<b>, fn(b) -> b / <e>) -> a<b> / <e> where Functor<a> |
| 35 | |
| 36 | fn incr_all :: x |
| 37 | { |
| 38 | map x fn n { n + 1 } |
| 39 | } |
| 40 | //~ TYPE incr_all: fn(a<Int>) -> a<Int> where Functor<a> |
| 41 | |
| 42 | fn on_list :: |
| 43 | { |
| 44 | incr_all [1, 2] |
| 45 | } |
| 46 | //~ TYPE on_list: fn() -> List<Int> |
| 47 | |
| 48 | pub fn signature :: x: f<Int> -> f<String> where Functor<f> |
| 49 | { |
| 50 | map x show |
| 51 | } |
| 52 | |
| 53 | pub type Twice<f, a> { Twice(f<a>, f<a>) } |
| 54 | //~ TYPE Twice: fn(f<a>, f<a>) -> Twice<f, a> |
| 55 | |
| 56 | // The kind of a trait's argument must match its parameter's. |
| 57 | impl Functor< |
| 58 | Int |
| 59 | //~^ ERROR E0213 `Int` with 0 type arguments has kind `Type`, but trait `Functor` expects kind `Type -> Type` here |
| 60 | > |
| 61 | { |
| 62 | fn map :: x _ |
| 63 | { |
| 64 | x |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl Functor< |
| 69 | Result |
| 70 | //~^ ERROR E0213 `Result` with 0 type arguments has kind `Type -> Type -> Type`, but trait `Functor` expects kind `Type -> Type` here |
| 71 | > |
| 72 | { |
| 73 | fn map :: x _ |
| 74 | { |
| 75 | x |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn wrong_constraint :: x: f<Int> -> Int where Show<f> |
| 80 | //~^ ERROR E0213 type variable `f` has kind `Type -> Type`, but it is used here with kind `Type` |
| 81 | { |
| 82 | 0 |
| 83 | } |
| 84 | |
| 85 | pub fn applied_and_not :: x: t<Int> y: t -> Int |
| 86 | //~^ ERROR E0213 type variable `t` has kind `Type -> Type`, but it is used here with kind `Type` |
| 87 | { |
| 88 | 0 |
| 89 | } |
| 90 | |
| 91 | pub fn not_applied :: x: List -> Int |
| 92 | //~^ ERROR E0201 `List` takes 1 type argument, but 0 were given |
| 93 | { |
| 94 | 0 |
| 95 | } |
| 96 | |
| 97 | pub fn too_many :: x: Option<Int, Int> -> Int |
| 98 | //~^ ERROR E0201 `Option` takes 1 type argument, but 2 were given |
| 99 | { |
| 100 | 0 |
| 101 | } |
| 102 | |
| 103 | // A variable is applied to types only: no `Type -> Type` argument. |
| 104 | pub fn higher_order :: x: t<List> -> Int |
| 105 | //~^ ERROR E0201 `List` takes 1 type argument, but 0 were given |
| 106 | { |
| 107 | 0 |
| 108 | } |
| 109 | |
| 110 | pub type Bad<f> |
| 111 | { Bad(f<Int>, f) |
| 112 | //~^ ERROR E0213 type variable `f` has kind `Type -> Type`, but it is used here with kind `Type` |
| 113 | } |
| 114 | |
| 115 | pub fn row_as_type :: x: e -> () / <e> |
| 116 | //~^ ERROR E0213 type variable `e` has kind `Type`, but it is used here with kind `Row` |
| 117 | { |
| 118 | () |
| 119 | } |
| 120 | |
| 121 | pub fn mismatch :: x: Option<Int> -> List<Int> |
| 122 | { |
| 123 | map x fn n { n } //~ ERROR E0202 expected `List<Int>`, found `Option<Int>` |
| 124 | } |
| 125 | |
| 126 | // `p<a, c>` takes types only, so it cannot match `Twice<List, Int>`, |
| 127 | // whose first parameter is a type constructor. |
| 128 | trait Bifunctor<p> |
| 129 | { |
| 130 | fn first :: x: p<a, c> g: fn(a) -> b -> p<b, c> |
| 131 | } |
| 132 | |
| 133 | pub fn not_bifunctor :: t: Twice<List, Int> -> Int |
| 134 | { |
| 135 | first t fn x { x } //~ ERROR E0202 found `Twice<List, Int>` |
| 136 | //~^ ERROR E0403 cannot tell which type should implement `Bifunctor` |
| 137 | 0 |
| 138 | } |
| 139 | |
| 140 | // A derived impl requires the trait only of parameters that are types. |
| 141 | @derive Show |
| 142 | pub type Wrapped<f, a> //~ ERROR E0410 field of type `f<a>` |
| 143 | { Wrapped(f<a>) } |
| 144 | |
| 145 | // `f<a>` never matches a function or tuple type. |
| 146 | pub fn not_a_constructor :: x: (Int, Int) -> (Int, Int) |
| 147 | { |
| 148 | map x fn n { n } //~ ERROR E0202 found `(Int, Int)` |
| 149 | //~^ ERROR E0403 cannot tell which type should implement `Functor` |
| 150 | //~^^ ERROR E0202 expected `(Int, Int)` |
| 151 | } |
| 152 | |
| 153 | // A tuple is a type, not a type constructor. |
| 154 | impl Functor< |
| 155 | (a, b) |
| 156 | //~^ ERROR E0213 a tuple has kind `Type`, but trait `Functor` expects kind `Type -> Type` |
| 157 | > |
| 158 | { |
| 159 | fn map :: x _ |
| 160 | { |
| 161 | x |
| 162 | } |
| 163 | } |