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.

172 lignes · 3350 octetsBrut · Historique
1// Currying (ADR 0061): partial application is a value, labels apply by
2// name in any order, pipes stay x-first.
3
4import std/list
5
6fn add :: a: Int b: Int -> Int
7{
8 a + b
9}
10
11fn insert :: ~into: String ~key: String value: String -> String
12{
13 "{into}:{key}:{value}"
14}
15
16// A partial application is a function of the remaining parameters.
17pub fn add2 :: -> fn(Int) -> Int
18{
19 add 2
20}
21
22// Labels partially applied by name, out of order.
23pub fn keyed :: value: String -> fn(~into: String) -> String
24{
25 insert ~key: "k" value
26}
27
28pub fn by_label :: -> String
29{
30 let into_users = insert ~into: "users"
31 into_users ~key: "k" "v"
32}
33
34// A pipe is `f x a b` (x-first): currying through it.
35pub fn through_pipes :: xs: List<Int> -> List<Int>
36{
37 xs |> list.map (add 1) |> list.map (add 2)
38}
39
40// Too many arguments is the arity error left (E0205).
41pub fn too_many :: -> Int
42{
43 add 1 2 3 //~ ERROR E0205
44}
45
46// A label no parameter has (E0204), in a partial application.
47pub fn no_such_label :: -> String
48{
49 insert ~nope: "x" "v" //~ ERROR E0204
50}
51
52// A constructor is not a function: no partial construction.
53type User { User(name: String, age: Int) }
54
55pub fn partial_ctor :: -> User
56{
57 User("ada") //~ ERROR E0205
58}
59
60// A constructor applied like a function.
61pub fn ctor_applied :: -> User
62{
63 User "ada" //~ ERROR E0216
64}
65
66// A construction's parentheses touch the constructor: with a space, they
67// are an argument (issue #319).
68pub fn spaced_construction :: -> Option<Int>
69{
70 Some (1) //~ ERROR E0216
71}
72
73// A partial application takes its handlers as the lambda `fn n { f a n }`
74// would in its place (ADR 0062, ADR 0010): passed where the parameter's
75// type declares the effect, it receives the handler from its caller...
76effect log
77{
78 fn log :: msg: String -> ()
79}
80
81fn logs :: prefix: String n: Int -> String / <log>
82{
83 log prefix
84 prefix <> show n
85}
86
87fn run :: g: fn(Int) -> String / <log> -> String
88{
89 handle
90 {
91 g 1
92 }
93 with
94 { log _ -> () }
95}
96
97pub fn passed :: -> String
98{
99 run (logs "#")
100}
101
102// ...and stored in a `let`, it uses the handlers in scope where it is
103// written, so it cannot be used inside a `handle` it was made outside of.
104pub fn stored_outside :: -> List<String>
105{
106 let g = logs "#"
107 handle
108 {
109 list.map [1] g //~ ERROR E0703
110 }
111 with
112 { log _ -> () }
113}
114
115// A partial application holds its callee and the arguments it was given,
116// as the lambda `fn n { f a n }` would: it can be sent when they can
117// (issue #325)...
118fn keep :: f: send fn(Int) -> Int -> Int
119{
120 f 1
121}
122
123fn apply :: g: fn(Int) -> Int n: Int -> Int
124{
125 g n
126}
127
128type Offset { Offset(by: Int) }
129
130pub fn sent :: n: Int -> Int
131{
132 keep (add n)
133}
134
135pub fn sent_field :: offset: Offset -> Int
136{
137 keep (add offset.by)
138}
139
140pub fn sent_later :: n: Int -> Int
141{
142 let g = add n
143 keep g
144}
145
146pub fn sent_callee :: g: send fn(Int, Int) -> Int -> Int
147{
148 keep (g 1)
149}
150
151// ...and not when it holds what cannot be: a plain `fn`, as an argument
152// or as the callee.
153pub fn holds_a_plain_fn :: g: fn(Int) -> Int -> Int
154{
155 keep (apply g) //~ ERROR E0704
156}
157
158pub fn plain_callee :: g: fn(Int, Int) -> Int -> Int
159{
160 keep (g 1) //~ ERROR E0704
161}
162
163// A handler its callers supply is received, not captured.
164fn keep_logged :: f: send fn(Int) -> String / <log> -> String / <log>
165{
166 f 1
167}
168
169pub fn sent_with_its_handler :: -> String / <log>
170{
171 keep_logged (logs "#")
172}