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 | // Generic associated types (ADR 0047): an associated type with parameters |
| 2 | // of its own, and bindings that quantify them. |
| 3 | |
| 4 | import std/io |
| 5 | import std/list |
| 6 | |
| 7 | pub trait Mappable<c> |
| 8 | { |
| 9 | type Elem |
| 10 | type Mapped<x> |
| 11 | fn map_all :: c: c f: fn(Elem<c>) -> x -> Mapped<c, x> |
| 12 | } |
| 13 | |
| 14 | impl Mappable<List<a>> |
| 15 | { |
| 16 | type Elem = a |
| 17 | type Mapped<x> = List<x> |
| 18 | fn map_all :: c f |
| 19 | { |
| 20 | list.map c f |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub type Pair { Pair(Int, Int) } |
| 25 | |
| 26 | impl Mappable<Pair> |
| 27 | { |
| 28 | type Elem = Int |
| 29 | type Mapped<x> = (x, x) |
| 30 | fn map_all :: p f |
| 31 | { |
| 32 | match p { Pair(a, b) -> (f a, f b) } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Whatever `c` is, mapping it gives a list: the body may rely on it. |
| 37 | fn lengths :: c: c -> Int where Mappable<c, Elem = String, Mapped<y> = List<y>> |
| 38 | { |
| 39 | list.length (map_all c fn s { s }) |
| 40 | } |
| 41 | |
| 42 | fn doubled :: |
| 43 | c: c |
| 44 | -> List<Int> |
| 45 | where Mappable<c, Elem = Int, Mapped<y> = List<y>> |
| 46 | { |
| 47 | map_all c fn n { n * 2 } |
| 48 | } |
| 49 | |
| 50 | pub fn main :: -> () / <io> |
| 51 | { |
| 52 | io.println (show (lengths ["a", "bb", "ccc"])) |
| 53 | io.println (show (doubled [1, 2])) |
| 54 | let (a, b) = map_all Pair(3, 4) fn n { n + 1 } |
| 55 | io.println (show [a, b]) |
| 56 | } |
| 57 | //> 3 |
| 58 | //> [2, 4] |
| 59 | //> [4, 5] |