Skip to content

Commit 7ea92b7

Browse files
bors[bot]Marwes
andauthored
Merge #886
886: feat: Compile block expressions as monadic sequences r=Marwes a=Marwes Rather than removing block expressions entirely, this changes them to behave as sequence (`seq`) expressions. ```f# io.println "Hello" io.println "World" // Is now equivalent to seq io.println "Hello" io.println "World" ``` Co-authored-by: Markus Westerlind <[email protected]>
2 parents 112263e + 9751073 commit 7ea92b7

39 files changed

+292
-254
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benches/function_call.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ fn gluon_rust_boundary_overhead(b: &mut Bencher) {
7171
if n #Int== 0 then
7272
()
7373
else
74-
f n
75-
f n
76-
f n
77-
f n
78-
f n
79-
f n
80-
f n
81-
f n
82-
f n
83-
f n
74+
let _ = f n
75+
let _ = f n
76+
let _ = f n
77+
let _ = f n
78+
let _ = f n
79+
let _ = f n
80+
let _ = f n
81+
let _ = f n
82+
let _ = f n
83+
let _ = f n
8484
for (n #Int- 10) f
8585
for
8686
"#;

book/src/metadata.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type Tree a = | Tip a | Branch (Tree a) (Tree a)
8383
8484
let tree = Branch (Tip 1) (Branch (Tip 2) (Tip 3))
8585
86-
show tree
87-
tree == Tip 1
86+
let tree_str = show tree // "Branch (Tip 1) (Branch (Tip 2) (Tip 3))"
87+
tree == Tip 1 // False
8888
```
8989

9090
### #[doc(hidden)]

book/src/syntax-and-semantics.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If, on the other hand, you are familiar with functional languages you will be ri
88

99
### Identifiers and Literals
1010

11-
The simplest syntactical elements in Gluon are identifiers and literals and none of them should be especially surprising if you are experienced in programming.
11+
The simplest syntactical elements in Gluon are identifiers and literals and none of them should be especially surprising if you are experienced in programming.
1212

1313
Identifiers are a sequence of alphanumeric characters including underscore ("\_") which are required to start with either a letter or an underscore. Literals come in four different forms - integer, float, string and character literals.
1414

@@ -37,7 +37,7 @@ r###" "## "###
3737

3838
### Comments
3939

40-
Comments should be immediately familiar if you are accustomed to C-like languages.
40+
Comments should be immediately familiar if you are accustomed to C-like languages.
4141

4242
`//` starts a line comment which is ended by a newline
4343

@@ -206,7 +206,9 @@ Here, we write out a pattern for each of the variant's constructors and the valu
206206
```f#,rust
207207
match { x = 1.0, pi = 3.14 } with
208208
| { x = y, pi } -> y + pi
209+
```
209210

211+
```f#,rust
210212
// Patterns can be nested as well
211213
match { x = Some (Some 123) } with
212214
| { x = Some None } -> 0
@@ -270,6 +272,9 @@ While we have seen that functions can be defined in let expressions it is often
270272
```f#,rust
271273
// \(<identifier)* -> <expr>
272274
\x y -> x + y - 10
275+
```
276+
277+
```f#,rust
273278
// Equivalent to
274279
let f x y = x + y - 10 in f
275280
```
@@ -329,9 +334,9 @@ do { y } = Some { y = "" }
329334
Some y
330335
```
331336

332-
### Seq expressions
337+
### Sequence expressions
333338

334-
`seq` expressions work just like `do` expressions, only they do not have a binding.
339+
Sequence expressions work just like `do` expressions, only they do not have a binding.
335340

336341
```f#,rust
337342
let io @ { ? } = import! std.io
@@ -340,6 +345,17 @@ seq io.print " "
340345
io.println "world!"
341346
```
342347

348+
The `seq` keyword can also be omitted.
349+
350+
```f#,rust
351+
let io @ { ? } = import! std.io
352+
io.print "Hello"
353+
io.print " "
354+
io.println "world!"
355+
```
356+
357+
(In the future one of these ways are likely to be deprecated with only one way remaining, the formatter will be able to update the code in any case).
358+
343359
### Indentation
344360

345361
If you have been following along this far, you may be think that the syntax so far is pretty limiting. In particular, you wouldn't be wrong in thinking that the `let` and `type` syntax are clunky due to their need to be closed by the `in` keyword. Luckily, Gluon offers a more convenient way of writing bindings by relying on indentation.
@@ -386,7 +402,7 @@ module.id module.pi
386402

387403
## Typesystem
388404

389-
In gluon, identifiers starting with an uppercase letter is a type whereas identifiers starting with a lowercase letter are type variables.
405+
In gluon, identifiers starting with an uppercase letter is a type whereas identifiers starting with a lowercase letter are type variables.
390406

391407
### Function types
392408

check/tests/effect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_check! {
2323
test_check_err! {
2424
not_variant_effect,
2525
r#"
26-
convert_effect! ()
26+
let _ = convert_effect! ()
2727
convert_effect! 1
2828
"#,
2929
TypeError::Message(_),

check/tests/forall.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ let show : Show a -> Show (List a) = \d ->
967967
}
968968
let list@{ } = { show }
969969
970-
list.show string_show
970+
let _ = list.show string_show
971971
list.show int_show
972972
"#;
973973
let result = support::typecheck(text);
@@ -997,7 +997,7 @@ let semigroup : Semigroup (List a) =
997997
998998
let { append } = semigroup
999999
1000-
append (Cons 1 Nil) Nil
1000+
let _ = append (Cons 1 Nil) Nil
10011001
append (Cons "" Nil) Nil
10021002
"#;
10031003
let result = support::typecheck(text);

check/tests/implicits.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ let f ?eq l r: [a -> a -> Bool] -> a -> a -> Bool = eq l r
135135
let eq_int l r : Int -> Int -> Bool = True
136136
#[implicit]
137137
let eq_string l r : String -> String -> Bool = True
138-
f 1 2
139-
f "" ""
138+
let _ = f 1 2
139+
let _ = f "" ""
140140
()
141141
"#;
142142
let result = support::typecheck(text);
@@ -194,8 +194,8 @@ let eq_string @ { ? } =
194194
#[implicit]
195195
let eq l r : String -> String -> Bool = True
196196
{ eq }
197-
f 1 2
198-
f "" ""
197+
let _ = f 1 2
198+
let _ = f "" ""
199199
()
200200
"#;
201201
let result = support::typecheck(text);
@@ -348,7 +348,7 @@ type Test a = | Test a
348348
let f ?x y : [Test a] -> () -> Test a = x
349349
let g ?x y : [Test a] -> a -> Test a = f ()
350350
let i = Test 1
351-
g 2
351+
let _ = g 2
352352
()
353353
"#;
354354
let (expr, result) = support::typecheck_expr(text);
@@ -522,7 +522,6 @@ let applicative : Applicative Test = {
522522
}
523523
524524
\_ -> wrap 123
525-
()
526525
"#;
527526
let (_expr, result) = support::typecheck_expr(text);
528527
assert_err!(result, TypeError::UnableToResolveImplicit(..));
@@ -712,7 +711,7 @@ let put value : s -> State s () = any ()
712711
713712
let get : State s s = any ()
714713
715-
(put 1 *> get)
714+
let _ = (put 1 *> get)
716715
(put "hello" *> get)
717716
"#;
718717
let result = support::typecheck(text);

check/tests/row_polymorphism.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ fn equality_of_records_with_differing_fields() {
9191
let text = r#"
9292
let eq x y : a -> a -> () = ()
9393
let f v1 v2 =
94-
v1.x
95-
v2.y
94+
let _ = v1.x
95+
let _ = v2.y
9696
eq v1 v2
9797
()
9898
"#;

codegen/tests/derive_getable.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ fn enum_tuple_variants() {
5959
let { tuple_enum_to_str } = import! functions
6060
let { assert } = import! std.test
6161
62-
assert (tuple_enum_to_str Variant == "Variant")
63-
assert (tuple_enum_to_str OtherVariant == "OtherVariant")
64-
assert (tuple_enum_to_str (One 1) == "One(1)")
62+
let _ = assert (tuple_enum_to_str Variant == "Variant")
63+
let _ = assert (tuple_enum_to_str OtherVariant == "OtherVariant")
64+
let _ = assert (tuple_enum_to_str (One 1) == "One(1)")
6565
assert (tuple_enum_to_str (LotsOfTupleThings 42 "Text" 0.0) == "LotsOfTupleThings(42, \"Text\", 0.0)")
6666
"#;
6767

@@ -103,7 +103,7 @@ fn enum_struct_variants() {
103103
let { struct_enum_to_str } = import! functions
104104
let { assert } = import! std.test
105105
106-
assert (struct_enum_to_str (OneField { field = 1337 }) == "OneField { field: 1337 }")
106+
let _ = assert (struct_enum_to_str (OneField { field = 1337 }) == "OneField { field: 1337 }")
107107
assert (struct_enum_to_str (TwoFields { name = "Pi", val = 3.14 }) == "TwoFields { name: \"Pi\", val: 3.14 }")
108108
"#;
109109

@@ -158,11 +158,11 @@ fn enum_generic_variants() {
158158
let { assert } = import! std.test
159159
160160
let l: Either Int Float = Left 42
161-
assert (left l == Some 42)
161+
let _ = assert (left l == Some 42)
162162
let r: Either Int Float = Right 0.0
163-
assert (left r == None)
163+
let _ = assert (left r == None)
164164
165-
assert (extract_str (Left "left") == "left")
165+
let _ = assert (extract_str (Left "left") == "left")
166166
assert (extract_str (Right "right") == "right")
167167
"#;
168168

@@ -193,7 +193,7 @@ fn derive_generates_same_type_as_gluon_define() {
193193
194194
type Enum = | TestVariant | TestVariant2 Int
195195
196-
test TestVariant
196+
let _ = test TestVariant
197197
test (TestVariant2 123)
198198
"#;
199199

codegen/tests/derive_pushable.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ fn normal_struct() {
6262
let { new_struct } = import! functions
6363
let { assert } = import! std.test
6464
let { index, len } = import! std.array
65-
65+
6666
let { string, number, vec } = new_struct ()
6767
68-
assert (string == "hello")
69-
assert (number == 1)
70-
assert (len vec == 3)
71-
assert (index vec 0 == 1.0)
72-
assert (index vec 1 == 2.0)
68+
let _ = assert (string == "hello")
69+
let _ = assert (number == 1)
70+
let _ = assert (len vec == 3)
71+
let _ = assert (index vec 0 == 1.0)
72+
let _ = assert (index vec 1 == 2.0)
7373
assert (index vec 2 == 3.0)
7474
"#;
7575

@@ -121,12 +121,12 @@ fn generic_struct() {
121121
122122
let { generic, other } = new_generic_struct "hi rust"
123123
124-
assert (generic == "hi rust")
125-
assert (other == 2012)
124+
let _ = assert (generic == "hi rust")
125+
let _ = assert (other == 2012)
126126
127127
let { generic, other } = new_generic_struct 3.14
128128
129-
assert (generic == 3.14)
129+
let _ = assert (generic == 3.14)
130130
assert (other == 2012)
131131
"#;
132132

@@ -177,7 +177,7 @@ fn lifetime_struct() {
177177
178178
let { string, other } = new_lifetime_struct ()
179179
180-
assert (string == "I'm borrowed")
180+
let _ = assert (string == "I'm borrowed")
181181
assert (other == 6.6)
182182
"#;
183183

@@ -231,18 +231,18 @@ fn normal_enum() {
231231
match enum with
232232
| Nothing -> 0
233233
| Tuple x y ->
234-
assert (x == 1920)
235-
assert (y == 1080)
234+
let _ = assert (x == 1920)
235+
let _ = assert (y == 1080)
236236
1
237237
| Struct { key, value } ->
238-
assert (key == "under the doormat")
239-
assert (value == "lots of gold")
238+
let _ = assert (key == "under the doormat")
239+
let _ = assert (value == "lots of gold")
240240
2
241-
241+
242242
assert (tag == actual_tag)
243-
244-
assert_enum (new_enum 0) 0
245-
assert_enum (new_enum 1) 1
243+
244+
let _ = assert_enum (new_enum 0) 0
245+
let _ = assert_enum (new_enum 1) 1
246246
assert_enum (new_enum 2) 2
247247
"#;
248248

0 commit comments

Comments
 (0)