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 | // GADTs and existential types (ADR 0041). |
| 2 | |
| 3 | pub type Expr<a> |
| 4 | { IntLit(Int) -> Expr<Int> |
| 5 | , BoolLit(Bool) -> Expr<Bool> |
| 6 | , Add(Expr<Int>, Expr<Int>) -> Expr<Int> |
| 7 | , If(Expr<Bool>, Expr<a>, Expr<a>) -> Expr<a> |
| 8 | , Pair(Expr<b>, Expr<c>) -> Expr<(b, c)> |
| 9 | } |
| 10 | |
| 11 | pub type Showable { Showable(b) -> Showable where Show<b> } |
| 12 | |
| 13 | pub type Box { Box(b) -> Box } |
| 14 | |
| 15 | // Constructor schemes: their own variables, and their constraints. |
| 16 | pub fn int_lit :: -> fn(Int) -> Expr<Int> |
| 17 | { |
| 18 | IntLit |
| 19 | } //~ TYPE int_lit: fn() -> fn(Int) -> Expr<Int> |
| 20 | |
| 21 | pub fn eval :: e: Expr<a> -> a |
| 22 | { |
| 23 | match e |
| 24 | { IntLit(n) -> n |
| 25 | , BoolLit(b) -> b |
| 26 | , Add(x, y) -> eval x + eval y |
| 27 | , If(c, t, f) -> if eval c { eval t } else { eval f } |
| 28 | , Pair(x, y) -> (eval x, eval y) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Each arm knows its own index only. |
| 33 | pub fn wrong :: e: Expr<a> -> a |
| 34 | { |
| 35 | match e |
| 36 | { IntLit(n) -> n |
| 37 | , BoolLit(b) -> 1 //~ ERROR E0202 expected `a`, found `Int` |
| 38 | , _ -> wrong e |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // A matched `IntLit` proves `a` is `Int`. |
| 43 | pub fn cast :: e: Expr<a> x: a -> Int |
| 44 | { |
| 45 | match e { IntLit(_) -> x, _ -> 0 } |
| 46 | } |
| 47 | |
| 48 | // `Expr<Int>` has no `BoolLit` or `Pair`: no arm needed. |
| 49 | pub fn ints :: e: Expr<Int> -> Int |
| 50 | { |
| 51 | match e |
| 52 | { IntLit(n) -> n |
| 53 | , Add(x, y) -> ints x + ints y |
| 54 | , If(c, t, f) -> if eval c { ints t } else { ints f } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | pub fn missing :: e: Expr<Int> -> Int |
| 59 | { |
| 60 | match e //~ ERROR E0301 `Add(_, _)` is not covered |
| 61 | { IntLit(n) -> n, If(c, t, f) -> 0 } |
| 62 | } |
| 63 | |
| 64 | // Under `IntLit` in the first column, the second is an `Expr<Int>` too. |
| 65 | pub fn both :: x: Expr<a> y: Expr<a> -> Int |
| 66 | { |
| 67 | match (x, y) |
| 68 | { (IntLit(n), IntLit(m)) -> n + m |
| 69 | , (IntLit(_), Add(_, _)) -> 1 |
| 70 | , (IntLit(_), If(_, _, _)) -> 2 |
| 71 | , (BoolLit(_), _) -> 3 |
| 72 | , (Add(_, _), _) -> 4 |
| 73 | , (If(_, _, _), _) -> 5 |
| 74 | , (Pair(_, _), _) -> 6 |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn impossible :: e: Expr<Int> -> Int |
| 79 | { |
| 80 | match e |
| 81 | { BoolLit //~ WARN W0301 |
| 82 | //~^ ERROR E0202 `BoolLit` cannot match a value of type `Expr<Int>` |
| 83 | (b) -> 0 |
| 84 | , _ -> 1 |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn describe :: s: Showable -> String |
| 89 | { |
| 90 | match s { Showable(x) -> show x } |
| 91 | } |
| 92 | |
| 93 | pub fn leak :: s: Showable -> Int |
| 94 | { |
| 95 | let r = match s |
| 96 | { Showable(x) -> x //~ ERROR E0705 would escape its `match` arm |
| 97 | } |
| 98 | 0 |
| 99 | } |
| 100 | |
| 101 | pub fn no_show :: b: Box -> String |
| 102 | { |
| 103 | match b |
| 104 | { Box(x) -> show x //~ ERROR E0402 `b` is not known to implement `Show` |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn unpack :: s: Showable -> Int |
| 109 | { |
| 110 | let Showable(_) = s //~ ERROR E0215 take it apart in a `match` arm |
| 111 | 0 |
| 112 | } |
| 113 | |
| 114 | pub fn alternative :: e: Expr<a> -> Int |
| 115 | { |
| 116 | match e |
| 117 | { IntLit(_) | BoolLit(_) -> 0 //~ ERROR E0215 take it apart in a `match` arm |
| 118 | //~^ ERROR E0215 take it apart in a `match` arm |
| 119 | , _ -> 1 |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub fn not_showable :: -> Showable |
| 124 | { |
| 125 | Showable(fn x { x }) //~ ERROR E0401 does not implement `Show` |
| 126 | } |
| 127 | |
| 128 | pub type Wrong<a> |
| 129 | { Bad(Int) -> Option<Int> //~ ERROR E0214 must be `Wrong` applied to 1 type |
| 130 | } |
| 131 | |
| 132 | pub type Loose |
| 133 | { Loose(Int) -> Loose where Show<z> //~ ERROR E0211 `z` |
| 134 | } |
| 135 | |
| 136 | @derive Show |
| 137 | pub type Derived //~ ERROR E0408 refines or hides types |
| 138 | { Derived(b) -> Derived where Show<b> } |
| 139 | |
| 140 | pub type Rec { Rec(count: Int) -> Rec } |
| 141 | |
| 142 | pub fn read :: r: Rec -> Int |
| 143 | { |
| 144 | r.count //~ ERROR E0215 use `match` to read `count` |
| 145 | } |
| 146 | |
| 147 | // Equalities are between types: no refined constructor for a type with a |
| 148 | // higher-kinded parameter. |
| 149 | pub type Hk<f> |
| 150 | { Wrap //~ ERROR E0214 must be types, or rows passed through unchanged |
| 151 | (f<Int>) |
| 152 | -> Hk<f> |
| 153 | } |
| 154 | |
| 155 | // A row parameter passed through unchanged is fine (ADR 0041, Revision 2): |
| 156 | // the state `s` is existential, the row is carried. |
| 157 | pub type FineRow<a, e> { MkFine(s, step: fn(s) -> a / <e>) -> FineRow<a, e> } |
| 158 | |
| 159 | // A match without arms on an index no constructor fits needs none, and |
| 160 | // one some constructor fits is not exhaustive (issue #218). |
| 161 | pub type OnlyInt<a> { OnlyInt(Int) -> OnlyInt<Int> } |
| 162 | |
| 163 | pub fn no_constructor_fits :: o: OnlyInt<Bool> -> Int |
| 164 | { |
| 165 | match o {} |
| 166 | } |
| 167 | |
| 168 | pub fn ints_need_arms :: e: Expr<Int> -> Int |
| 169 | { |
| 170 | match e {} //~ ERROR E0301 is not covered |
| 171 | } |
| 172 | |
| 173 | // One refinement, read by inference, trait resolution and exhaustiveness |
| 174 | // alike (issue #224): in the `IntLit` and `Add` arms `a` is `Int`, so |
| 175 | // `x + 1` needs `Num<Int>`, which holds there and only there; and the |
| 176 | // arms must still cover every constructor that can build an `Expr<a>`. |
| 177 | pub fn plus_one :: e: Expr<a> x: a -> a |
| 178 | { |
| 179 | match e |
| 180 | { IntLit(_) -> x + 1 |
| 181 | , BoolLit(_) -> x |
| 182 | , Add(_, _) -> x + 1 |
| 183 | , If(_, _, _) -> x |
| 184 | , Pair(_, _) -> x |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | pub fn plus_one_partial :: e: Expr<a> x: a -> a |
| 189 | { |
| 190 | match e //~ ERROR E0301 is not covered |
| 191 | { IntLit(_) -> x + 1, Add(_, _) -> x + 1, If(_, _, _) -> x, Pair(_, _) -> x } |
| 192 | } |
| 193 | |
| 194 | // An index no finite type satisfies: `x` would be `List<x>`. Every check |
| 195 | // refuses it, and none loops (issue #224: exhaustiveness overflowed its |
| 196 | // stack, even on a parameter of such a type). |
| 197 | pub type Cyclic<a, b, c> { Cycle(Int) -> Cyclic<a, List<a>, a> } |
| 198 | |
| 199 | pub fn takes_a_cyclic :: t: Cyclic<x, x, x> -> Int |
| 200 | { |
| 201 | 0 |
| 202 | } |
| 203 | |
| 204 | pub fn matches_a_cyclic :: t: Cyclic<x, x, x> -> Int |
| 205 | { |
| 206 | match t |
| 207 | { Cycle //~ WARN W0301 |
| 208 | //~^ ERROR E0202 `Cycle` cannot match a value of type `Cyclic<x, x, x>` |
| 209 | (n) -> n |
| 210 | } |
| 211 | } |