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 | // User-defined derives (ADR 0019). |
| 2 | |
| 3 | @derivable describe_constructor |
| 4 | trait Describe<a> |
| 5 | { |
| 6 | fn describe :: x: a -> String |
| 7 | } |
| 8 | |
| 9 | fn describe_constructor :: c: Constructor<String> -> String |
| 10 | { |
| 11 | c.name |
| 12 | } |
| 13 | |
| 14 | impl Describe<Int> |
| 15 | { |
| 16 | fn describe :: n |
| 17 | { |
| 18 | show n |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | @derive Describe |
| 23 | pub type Point { Point(x: Int, y: Int) } |
| 24 | |
| 25 | @derive Describe |
| 26 | pub type Labelled |
| 27 | //~^ ERROR E0410 field of type `String`, which does not implement it |
| 28 | { Labelled(String) } |
| 29 | |
| 30 | @derive Describe |
| 31 | pub type Box<a> { Box(a) } |
| 32 | |
| 33 | pub fn boxed :: -> String |
| 34 | { |
| 35 | describe Box(Point(1, 2)) |
| 36 | } |
| 37 | |
| 38 | pub fn unboxable :: b: Box<Bool> -> String |
| 39 | { |
| 40 | describe |
| 41 | //~^ ERROR E0401 `Bool` does not implement `Describe` (needed for `Describe<Box<Bool>>`) |
| 42 | b |
| 43 | } |
| 44 | |
| 45 | trait Plain<a> |
| 46 | { |
| 47 | fn plain :: x: a -> Int |
| 48 | } |
| 49 | |
| 50 | @derive Plain |
| 51 | pub type NotDerivable //~ ERROR E0408 it is not a trait marked `@derivable` |
| 52 | { NotDerivable } |
| 53 | |
| 54 | // The derive function is checked against the method's result type. |
| 55 | @derivable wrong_type |
| 56 | trait Measure< |
| 57 | //~^ ERROR E0412 derive function `wrong_type` must have type `fn(Constructor<Int>) -> Int`, but it has type `fn(Constructor<String>) -> Int` |
| 58 | a |
| 59 | > |
| 60 | { |
| 61 | fn measure :: x: a -> Int |
| 62 | } |
| 63 | |
| 64 | // A parameter no constructor stores is phantom (issue #236): the derived |
| 65 | // impl needs nothing of it. |
| 66 | @derive Eq |
| 67 | pub type UserId<tag> { UserId(Int) } |
| 68 | |
| 69 | pub type User { User } |
| 70 | |
| 71 | // `User` has no `Eq`, and `UserId<User>` needs none either. |
| 72 | pub fn phantom_eq :: a: UserId<User> b: UserId<User> -> Bool |
| 73 | { |
| 74 | a == b |
| 75 | } |
| 76 | //~ TYPE phantom_eq: fn(UserId<User>, UserId<User>) -> Bool |
| 77 | |
| 78 | // But a stored parameter still constrains. |
| 79 | @derive Eq |
| 80 | pub type Marked<tag> { Marked(id: Int, marker: tag) } |
| 81 | |
| 82 | pub fn not_phantom :: a: Marked<User> b: Marked<User> -> Bool |
| 83 | { |
| 84 | a |
| 85 | //~^ ERROR E0401 `User` does not implement `Eq` (needed for `Eq<Marked<User>>`) |
| 86 | == b |
| 87 | } |
| 88 | |
| 89 | fn wrong_type :: c: Constructor<String> -> Int |
| 90 | { |
| 91 | 0 |
| 92 | } |
| 93 | |
| 94 | @derivable missing |
| 95 | trait Missing<a> //~ ERROR E0412 no function `missing` is in scope |
| 96 | { |
| 97 | fn missing_method :: x: a -> Int |
| 98 | } |
| 99 | |
| 100 | @derivable |
| 101 | trait NoFunction<a> //~ ERROR E0412 needs a derive function |
| 102 | { |
| 103 | fn no_function :: x: a -> Int |
| 104 | } |
| 105 | |
| 106 | @derivable two_constructor |
| 107 | trait TwoMethods<a> //~ ERROR E0412 exactly one method; `TwoMethods` has 2 |
| 108 | { |
| 109 | fn first :: x: a -> Int |
| 110 | fn second :: x: a -> Int |
| 111 | } |
| 112 | |
| 113 | fn two_constructor :: c: Constructor<Int> -> Int |
| 114 | { |
| 115 | 0 |
| 116 | } |
| 117 | |
| 118 | @derivable two_constructor |
| 119 | trait Binary<a> //~ ERROR E0412 must have the form `fn same :: x: a -> R` |
| 120 | { |
| 121 | fn same :: x: a y: a -> Int |
| 122 | } |
| 123 | |
| 124 | @derivable two_constructor |
| 125 | trait Producer<a> //~ ERROR E0412 must have the form |
| 126 | { |
| 127 | fn make :: -> a |
| 128 | } |
| 129 | |
| 130 | @derivable effectful |
| 131 | trait Logged<a> //~ ERROR E0412 derive function `effectful` must have type |
| 132 | { |
| 133 | fn logged :: x: a -> Int |
| 134 | } |
| 135 | |
| 136 | effect log |
| 137 | { |
| 138 | fn log :: message: String -> () |
| 139 | } |
| 140 | |
| 141 | fn effectful :: c: Constructor<Int> -> Int / <log> |
| 142 | { |
| 143 | log c.name |
| 144 | 0 |
| 145 | } |
| 146 | |
| 147 | // Polymorphic derive functions fit any result type. |
| 148 | @derivable constructor_index |
| 149 | trait Index<a> |
| 150 | { |
| 151 | fn index :: x: a -> Int |
| 152 | } |
| 153 | |
| 154 | fn constructor_index :: c: Constructor<r> -> Int |
| 155 | { |
| 156 | c.index |
| 157 | } |