tobogganSe connecter

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.

59 lignes · 1091 octetsBrut · Historique
1// Generic associated types (ADR 0047): an associated type with parameters
2// of its own, and bindings that quantify them.
3
4import std/io
5import std/list
6
7pub 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
14impl 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
24pub type Pair { Pair(Int, Int) }
25
26impl 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.
37fn 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
42fn 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
50pub 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]