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.

157 lignes · 3436 octetsBrut · Historique
1// Associated types (ADR 0047): a trait names a type each impl chooses,
2// written `Elem<c>`, computed once the impl is known.
3
4import std/io
5import std/list
6import std/process.{type Subject, type proc}
7
8pub trait Container<c>
9{
10 type Elem
11 fn to_list :: c: c -> List<Elem<c>>
12 fn first :: c: c -> Option<Elem<c>>
13}
14
15impl Container<List<a>>
16{
17 type Elem = a
18 fn to_list :: c
19 {
20 c
21 }
22 fn first :: c
23 {
24 match c { [x, ..] -> Some(x), [] -> None }
25 }
26}
27
28pub type Pair { Pair(Int, Int) }
29
30impl Container<Pair>
31{
32 type Elem = Int
33 fn to_list :: p
34 {
35 match p { Pair(a, b) -> [a, b] }
36 }
37 fn first :: p
38 {
39 match p { Pair(a, _) -> Some(a) }
40 }
41}
42
43// A container of containers: its elements are its parts' elements.
44pub type Nested<c> { Nested(List<c>) }
45
46impl Container<Nested<c>> where Container<c>
47{
48 type Elem = Elem<c>
49 fn to_list :: n
50 {
51 match n { Nested(parts) -> list.flat_map parts to_list }
52 }
53 fn first :: n
54 {
55 match to_list n { [x, ..] -> Some(x), [] -> None }
56 }
57}
58
59// Generic code: the element type is computed from the container's.
60fn count :: c: c -> Int where Container<c>
61{
62 list.length (to_list c)
63}
64
65fn describe :: c: c -> String where Container<c>, Show<Elem<c>>
66{
67 "{count c} items, first {first c}"
68}
69
70// Inferred: `fn(a) -> Option<Elem<a>> where Container<a>`.
71fn first_of :: c
72{
73 first c
74}
75
76// A field whose type is a projection: `Boxed(41)` is checked once the
77// expected type says which container.
78pub type Boxed<c> { Boxed(Elem<c>) }
79
80fn unbox :: b: Boxed<Pair> -> Int
81{
82 match b { Boxed(n) -> n + 1 }
83}
84
85// Inferred with an equality: `fn(a) -> Option<Int> where Container<a, Elem = Int>`.
86fn first_int :: c
87{
88 let x: Option<Int> = first c
89 x
90}
91
92// Bindings: in the body, `Elem<c>` is `Int`; each call must agree.
93fn total :: c: c -> Int where Container<c, Elem = Int>
94{
95 list.fold (to_list c) 0 fn a x { a + x }
96}
97
98// A binding to a variable, used by another constraint.
99fn show_all :: c: c -> String where Container<c, Elem = e>, Show<e>
100{
101 show (to_list c)
102}
103
104// A constructor that knows its container holds Ints: its arm does too.
105type Summable { Summable(c) -> Summable where Container<c, Elem = Int> }
106
107fn sum_any :: s: Summable -> Int
108{
109 match s { Summable(c) -> total c }
110}
111
112// Generic associated types: own parameters after the trait's.
113pub trait Mappable<c>
114{
115 type Mapped<x>
116 fn map_all :: c: c f: fn(Int) -> x -> Mapped<c, x>
117}
118
119impl Mappable<Pair>
120{
121 type Mapped<x> = (x, x)
122 fn map_all :: p f
123 {
124 match p { Pair(a, b) -> (f a, f b) }
125 }
126}
127
128pub fn main :: -> () / <io, proc>
129{
130 io.println (describe [1, 2, 3])
131 io.println (describe Pair(4, 5))
132 io.println (describe Nested([[6], [7, 8]]))
133 io.println (show (first_of Pair(9, 10)))
134 io.println (show (unbox Boxed(41)))
135 io.println (show (first_int [12]))
136 io.println (show (total [1, 2, 3] + total Pair(4, 5)))
137 io.println (show_all ["x", "y"])
138 io.println
139 (show [sum_any Summable(Pair(6, 7)), sum_any Summable(Nested([[8]]))])
140 let (a, b) = map_all Pair(2, 3) fn n { "{n * n}" }
141 io.println (a <> " " <> b)
142 // A message whose type is a projection is checked like any other.
143 let inbox: Subject<Elem<List<String>>> = process.subject ()
144 process.send inbox "sent"
145 io.println (process.receive_forever inbox)
146}
147//> 3 items, first Some(1)
148//> 2 items, first Some(4)
149//> 3 items, first Some(6)
150//> Some(9)
151//> 42
152//> Some(12)
153//> 15
154//> [x, y]
155//> [13, 8]
156//> 4 9
157//> sent