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 · 2987 octetsBrut · Historique
1// User-defined derives (ADR 0019).
2
3@derivable describe_constructor
4trait Describe<a>
5{
6 fn describe :: x: a -> String
7}
8
9fn describe_constructor :: c: Constructor<String> -> String
10{
11 c.name
12}
13
14impl Describe<Int>
15{
16 fn describe :: n
17 {
18 show n
19 }
20}
21
22@derive Describe
23pub type Point { Point(x: Int, y: Int) }
24
25@derive Describe
26pub type Labelled
27//~^ ERROR E0410 field of type `String`, which does not implement it
28{ Labelled(String) }
29
30@derive Describe
31pub type Box<a> { Box(a) }
32
33pub fn boxed :: -> String
34{
35 describe Box(Point(1, 2))
36}
37
38pub fn unboxable :: b: Box<Bool> -> String
39{
40 describe
41 //~^ ERROR E0401 `Bool` does not implement `Describe` (needed for `Describe<Box<Bool>>`)
42 b
43}
44
45trait Plain<a>
46{
47 fn plain :: x: a -> Int
48}
49
50@derive Plain
51pub type NotDerivable //~ ERROR E0408 it is not a trait marked `@derivable`
52{ NotDerivable }
53
54// The derive function is checked against the method's result type.
55@derivable wrong_type
56trait Measure<
57 //~^ ERROR E0412 derive function `wrong_type` must have type `fn(Constructor<Int>) -> Int`, but it has type `fn(Constructor<String>) -> Int`
58 a
59>
60{
61 fn measure :: x: a -> Int
62}
63
64// A parameter no constructor stores is phantom (issue #236): the derived
65// impl needs nothing of it.
66@derive Eq
67pub type UserId<tag> { UserId(Int) }
68
69pub type User { User }
70
71// `User` has no `Eq`, and `UserId<User>` needs none either.
72pub fn phantom_eq :: a: UserId<User> b: UserId<User> -> Bool
73{
74 a == b
75}
76//~ TYPE phantom_eq: fn(UserId<User>, UserId<User>) -> Bool
77
78// But a stored parameter still constrains.
79@derive Eq
80pub type Marked<tag> { Marked(id: Int, marker: tag) }
81
82pub fn not_phantom :: a: Marked<User> b: Marked<User> -> Bool
83{
84 a
85 //~^ ERROR E0401 `User` does not implement `Eq` (needed for `Eq<Marked<User>>`)
86 == b
87}
88
89fn wrong_type :: c: Constructor<String> -> Int
90{
91 0
92}
93
94@derivable missing
95trait Missing<a> //~ ERROR E0412 no function `missing` is in scope
96{
97 fn missing_method :: x: a -> Int
98}
99
100@derivable
101trait NoFunction<a> //~ ERROR E0412 needs a derive function
102{
103 fn no_function :: x: a -> Int
104}
105
106@derivable two_constructor
107trait TwoMethods<a> //~ ERROR E0412 exactly one method; `TwoMethods` has 2
108{
109 fn first :: x: a -> Int
110 fn second :: x: a -> Int
111}
112
113fn two_constructor :: c: Constructor<Int> -> Int
114{
115 0
116}
117
118@derivable two_constructor
119trait Binary<a> //~ ERROR E0412 must have the form `fn same :: x: a -> R`
120{
121 fn same :: x: a y: a -> Int
122}
123
124@derivable two_constructor
125trait Producer<a> //~ ERROR E0412 must have the form
126{
127 fn make :: -> a
128}
129
130@derivable effectful
131trait Logged<a> //~ ERROR E0412 derive function `effectful` must have type
132{
133 fn logged :: x: a -> Int
134}
135
136effect log
137{
138 fn log :: message: String -> ()
139}
140
141fn effectful :: c: Constructor<Int> -> Int / <log>
142{
143 log c.name
144 0
145}
146
147// Polymorphic derive functions fit any result type.
148@derivable constructor_index
149trait Index<a>
150{
151 fn index :: x: a -> Int
152}
153
154fn constructor_index :: c: Constructor<r> -> Int
155{
156 c.index
157}