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.

212 lignes · 3670 octetsBrut · Historique
1// Traits with several parameters (ADR 0018).
2
3trait Convert<a, b>
4{
5 fn convert :: x: a -> b
6}
7
8impl Convert<Int, String>
9{
10 fn convert :: n
11 {
12 show n
13 }
14}
15
16impl Convert<List<a>, List<b>> where Convert<a, b>
17{
18 fn convert :: xs
19 {
20 map xs convert
21 }
22}
23
24fn map :: xs f
25{
26 match xs { [] -> [], [x, ..rest] -> [f x, ..map rest f] }
27}
28
29// Constraints on generalized variables become `where` clauses.
30fn convert_each :: xs
31{
32 map xs convert
33}
34//~ TYPE convert_each: fn(List<a>) -> List<b> where Convert<a, b>
35
36fn to_strings :: xs: List<Int> -> List<String>
37{
38 convert xs
39}
40
41// Every argument must be known: there are no functional dependencies.
42fn unknown_result :: -> String
43{
44 show //~ ERROR E0403 cannot tell which type should implement `Show`
45 (convert 1)
46 //~^ ERROR E0403 cannot tell which impl of `Convert` to use here: `Convert<Int, _>` has unknown types
47}
48
49fn known_result :: -> String
50{
51 let s: String = convert 1
52 s
53}
54
55pub fn no_impl :: -> String
56{
57 convert True //~ ERROR E0401 no impl of `Convert<Bool, String>` exists
58}
59
60pub fn nested_no_impl :: -> List<String>
61{
62 convert //~ ERROR E0401 no impl of `Convert<Bool, String>` exists
63 [True]
64}
65
66pub fn missing_given :: x: a -> String where Show<a>
67{
68 convert x //~ ERROR E0402 `Convert<a, String>` is not known to hold
69}
70
71pub fn given :: x: a -> b where Convert<a, b>
72{
73 convert x
74}
75
76// A variable only in the `where` clause can never be determined.
77pub fn through :: x: a -> c where Convert<a, b>, Convert<b, c>
78//~^ ERROR E0403 type variable `b` appears only in the `where` clause of `through`
79{
80 panic "unused"
81}
82
83trait Empty<c, e>
84{
85 fn empty :: -> c
86 //~^ ERROR E0403 method `empty` does not mention the trait's type parameter `e`
87}
88
89trait Twice<a, a> //~ ERROR E0411 must be distinct
90{
91 fn twice :: x: a -> a
92}
93
94// Two impls overlap exactly when their head types are equal.
95impl Convert<Option<a>, Bool>
96{
97 fn convert :: _
98 {
99 True
100 }
101}
102
103impl Convert<
104 //~^ ERROR E0404 `Convert` is implemented more than once for `Option, Bool`
105 Option<b>
106, Bool
107>
108{
109 fn convert :: _
110 {
111 False
112 }
113}
114
115// Every argument is a named type applied to distinct variables.
116impl Convert<a, Int> //~ ERROR E0409 distinct type variables
117{
118 fn convert :: _
119 {
120 0
121 }
122}
123
124impl Convert<Option<a>, List<a>> //~ ERROR E0409 distinct type variables
125{
126 fn convert :: _
127 {
128 []
129 }
130}
131
132impl Convert< //~ ERROR E0409 takes 2 type arguments, but the impl gives 1
133 Bool
134>
135{
136 fn convert :: _
137 {
138 0
139 }
140}
141
142pub fn wrong_arity :: x: a -> a where Convert<a>
143//~^ ERROR E0212 takes 2 type arguments, but 1 was given
144{
145 x
146}
147
148// Orphan rule: the impl must be in the module of the trait or of one of
149// its head types (here, neither).
150impl Show< //~ ERROR E0405 must be in the module of the trait or of the type
151 Int
152>
153{
154 fn show :: _
155 {
156 ""
157 }
158}
159
160// A head may also be a tuple of distinct variables (ADR 0018 Rev. 1).
161// A tuple has no module, so such an impl lives with its trait.
162impl Convert<(a, b), Int>
163{
164 fn convert :: _
165 {
166 2
167 }
168}
169
170impl Convert<
171 //~^ ERROR E0404 `Convert` is implemented more than once for `(_, _), Int`
172 (c, d)
173, Int
174>
175{
176 fn convert :: _
177 {
178 0
179 }
180}
181
182impl Convert<(a, a), Bool> //~ ERROR E0409 distinct type variables
183{
184 fn convert :: _
185 {
186 True
187 }
188}
189
190impl Convert<(a, Int), Bool> //~ ERROR E0409 distinct type variables
191{
192 fn convert :: _
193 {
194 True
195 }
196}
197
198impl Show<
199 //~^ ERROR E0405 impl of `Show` for `(_, _, _, _, _, _, _, _, _, _, _, _, _)` must be in the module of the trait
200 (a, b, c, d, e, f, g, h, i, j, k, l, m)
201>
202{
203 fn show :: _
204 {
205 ""
206 }
207}
208
209pub fn pairs_convert :: p: (Bool, String) -> Int
210{
211 convert p
212}