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 | import std/io |
| 2 | import std/list |
| 3 | |
| 4 | // Calls through dictionaries of constrained impls, built from constant |
| 5 | // ones or from other built ones (issue #278): evidence specialisation |
| 6 | // calls their methods directly and copies them; the results must not |
| 7 | // change. |
| 8 | trait Describe<a> |
| 9 | { |
| 10 | fn describe :: x: a -> String |
| 11 | } |
| 12 | |
| 13 | pub type Box<a> { Box(a) } |
| 14 | |
| 15 | impl Describe<Int> |
| 16 | { |
| 17 | fn describe :: x: Int -> String |
| 18 | { |
| 19 | show x |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl Describe<String> |
| 24 | { |
| 25 | fn describe :: x: String -> String |
| 26 | { |
| 27 | "'" <> x <> "'" |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Describe<Box<a>> where Describe<a> |
| 32 | { |
| 33 | fn describe :: x: Box<a> -> String |
| 34 | { |
| 35 | let Box(v) = x |
| 36 | let inner = describe v |
| 37 | "box(" <> inner <> ")" |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Describe<List<a>> where Describe<a> |
| 42 | { |
| 43 | fn describe :: xs: List<a> -> String |
| 44 | { |
| 45 | match xs |
| 46 | { [] -> "[]", [x, ..rest] -> describe x <> " :: " <> describe rest } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Describe<(a, b)> where Describe<a>, Describe<b> |
| 51 | { |
| 52 | fn describe :: p: (a, b) -> String |
| 53 | { |
| 54 | "(" <> describe p.0 <> ", " <> describe p.1 <> ")" |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn twice :: x: a -> String where Describe<a> |
| 59 | { |
| 60 | describe x <> " " <> describe x |
| 61 | } |
| 62 | |
| 63 | fn all :: xs: List<a> -> List<String> where Describe<a> |
| 64 | { |
| 65 | list.map xs fn x { describe x } |
| 66 | } |
| 67 | |
| 68 | fn main :: |
| 69 | { |
| 70 | io.println (twice Box(1)) |
| 71 | //> box(1) box(1) |
| 72 | io.println (twice Box(Box("deep"))) |
| 73 | //> box(box('deep')) box(box('deep')) |
| 74 | io.println (describe [1, 2, 3]) |
| 75 | //> 1 :: 2 :: 3 :: [] |
| 76 | io.println (describe [Box(1), Box(2)]) |
| 77 | //> box(1) :: box(2) :: [] |
| 78 | io.println (twice (7, "seven")) |
| 79 | //> (7, 'seven') (7, 'seven') |
| 80 | io.println (twice (Box(1), [Box("a")])) |
| 81 | //> (box(1), box('a') :: []) (box(1), box('a') :: []) |
| 82 | list.each (all [[1], [2, 3]]) io.println |
| 83 | //> 1 :: [] |
| 84 | //> 2 :: 3 :: [] |
| 85 | } |