Skip to content

Commit a2d8b3d

Browse files
authored
Merge pull request #160 from nikomatsakis/upcast-arc-and-more
Usability improvements
2 parents 4387277 + 047e156 commit a2d8b3d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2284
-1035
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pretty_assertions = "1.3.0"
1818
expect-test = "1.4.0"
1919
formality-macros = { version = "0.1.0", path = "crates/formality-macros" }
2020
formality-core = { version = "0.1.0", path = "crates/formality-core" }
21+
tracing = "0.1.40"
2122

2223

2324
[dependencies]

book/src/SUMMARY.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
- [Intro](./intro.md)
44
- [`formality_core`: the Formality system](./formality_core.md)
5-
- [Defining your lang](./formality_core/lang.md)
6-
- [Defining terms with the `term` macro](./formality_core/terms.md)
7-
- [Parsing](./formality_core/parse.md)
8-
- [Customizing debug](./formality_core/debug.md)
9-
- [Variables](./formality_core/variables.md)
10-
- [Collections](./formality_core/collections.md)
11-
- [Judgment functions and inference rules](./formality_core/judgment_fn.md)
12-
- [FAQ and troubleshooting](./formality_core/faq.md)
5+
- [Defining your lang](./formality_core/lang.md)
6+
- [Defining terms with the `term` macro](./formality_core/terms.md)
7+
- [Parsing](./formality_core/parse.md)
8+
- [Customizing debug](./formality_core/debug.md)
9+
- [Constructors](./formality_core/constructors.md)
10+
- [Variables](./formality_core/variables.md)
11+
- [Collections](./formality_core/collections.md)
12+
- [Judgment functions and inference rules](./formality_core/judgment_fn.md)
13+
- [FAQ and troubleshooting](./formality_core/faq.md)

book/src/formality_core/parse.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct MyEnum {
2323

2424
When parsing an enum there will be multiple possibilities. We will attempt to parse them all. If more than one succeeds, the parser will attempt to resolve the ambiguity by looking for the **longest match**. However, we don't just consider the number of characters, we look for a **reduction prefix**:
2525

26-
* When parsing, we track the list of things we had to parse. If there are two variants at the same precedence level, but one of them had to parse strictly more things than the other and in the same way, we'll prefer the longer one. So for example if one variant parsed a `Ty` and the other parsed a `Ty Ty`, we'd take the `Ty Ty`.
27-
* When considering whether a reduction is "significant", we take casts into account. See `ActiveVariant::mark_as_cast_variant` for a more detailed explanation and set of examples.
26+
- When parsing, we track the list of things we had to parse. If there are two variants at the same precedence level, but one of them had to parse strictly more things than the other and in the same way, we'll prefer the longer one. So for example if one variant parsed a `Ty` and the other parsed a `Ty Ty`, we'd take the `Ty Ty`.
27+
- When considering whether a reduction is "significant", we take casts into account. See `ActiveVariant::mark_as_cast_variant` for a more detailed explanation and set of examples.
2828

2929
### Precedence and left-recursive grammars
3030

@@ -36,42 +36,41 @@ We support left-recursive grammars like this one from the `parse-torture-tests`:
3636

3737
We also support ambiguous grammars. For example, you can code up arithmetic expressions like this:
3838

39-
4039
```rust
4140
{{#include ../../../tests/parser-torture-tests/left_associative.rs:Expr}}
4241
```
4342

4443
When specifying the `#[precedence]` of a variant, the default is left-associativity, which can be written more explicitly as `#[precedence(L, left)]`. If you prefer, you can specify right-associativity (`#[precedence(L, right)]`) or non-associativity `#[precedence(L, none)]`. This affects how things of the same level are parsed:
4544

46-
* `1 + 1 + 1` when left-associative is `(1 + 1) + 1`
47-
* `1 + 1 + 1` when right-associative is `1 + (1 + 1)`
48-
* `1 + 1 + 1` when none-associative is an error.
45+
- `1 + 1 + 1` when left-associative is `(1 + 1) + 1`
46+
- `1 + 1 + 1` when right-associative is `1 + (1 + 1)`
47+
- `1 + 1 + 1` when none-associative is an error.
4948

5049
### Symbols
5150

52-
A grammar consists of a series of *symbols*. Each symbol matches some text in the input string. Symbols come in two varieties:
53-
54-
* Most things are *terminals* or *tokens*: this means they just match themselves:
55-
* For example, the `*` in `#[grammar($v0 * $v1)]` is a terminal, and it means to parse a `*` from the input.
56-
* Delimeters are accepted but must be matched, e.g., `( /* tokens */ )` or `[ /* tokens */ ]`.
57-
* Things beginning with `$` are *nonterminals* -- they parse the contents of a field. The grammar for a field is generally determined from its type.
58-
* If fields have names, then `$field` should name the field.
59-
* For position fields (e.g., the T and U in `Mul(Expr, Expr)`), use `$v0`, `$v1`, etc.
60-
* Exception: `$$` is treated as the terminal `'$'`.
61-
* Nonterminals have various modes:
62-
* `$field` -- just parse the field's type
63-
* `$*field` -- the field must be a `Vec<T>` -- parse any number of `T` instances. Something like `[ $*field ]` would parse `[f1 f2 f3]`, assuming `f1`, `f2`, and `f3` are valid values for `field`.
64-
* `$,field` -- similar to the above, but uses a comma separated list (with optional trailing comma). So `[ $,field ]` will parse something like `[f1, f2, f3]`.
65-
* `$?field` -- will parse `field` and use `Default::default()` value if not present.
66-
* `$<field>` -- parse `<E1, E2, E3>`, where `field: Vec<E>`
67-
* `$<?field>` -- parse `<E1, E2, E3>`, where `field: Vec<E>`, but accept empty string as empty vector
68-
* `$(field)` -- parse `(E1, E2, E3)`, where `field: Vec<E>`
69-
* `$(?field)` -- parse `(E1, E2, E3)`, where `field: Vec<E>`, but accept empty string as empty vector
70-
* `$[field]` -- parse `[E1, E2, E3]`, where `field: Vec<E>`
71-
* `$[?field]` -- parse `[E1, E2, E3]`, where `field: Vec<E>`, but accept empty string as empty vector
72-
* `${field}` -- parse `{E1, E2, E3}`, where `field: Vec<E>`
73-
* `${?field}` -- parse `{E1, E2, E3}`, where `field: Vec<E>`, but accept empty string as empty vector
74-
* `$:guard <nonterminal>` -- parses `<nonterminal>` but only if the keyword `guard` is present. For example, `$:where $,where_clauses` would parse `where WhereClause1, WhereClause2, WhereClause3` but would also accept nothing (in which case, you would get an empty vector).
51+
A grammar consists of a series of _symbols_. Each symbol matches some text in the input string. Symbols come in two varieties:
52+
53+
- Most things are _terminals_ or _tokens_: this means they just match themselves:
54+
- For example, the `*` in `#[grammar($v0 * $v1)]` is a terminal, and it means to parse a `*` from the input.
55+
- Delimeters are accepted but must be matched, e.g., `( /* tokens */ )` or `[ /* tokens */ ]`.
56+
- Things beginning with `$` are _nonterminals_ -- they parse the contents of a field. The grammar for a field is generally determined from its type.
57+
- If fields have names, then `$field` should name the field.
58+
- For position fields (e.g., the T and U in `Mul(Expr, Expr)`), use `$v0`, `$v1`, etc.
59+
- Exception: `$$` is treated as the terminal `'$'`.
60+
- Nonterminals have various modes:
61+
- `$field` -- just parse the field's type
62+
- `$*field` -- the field must be a collection of `T` (e.g., `Vec<T>`, `Set<T>`) -- parse any number of `T` instances. Something like `[ $*field ]` would parse `[f1 f2 f3]`, assuming `f1`, `f2`, and `f3` are valid values for `field`.
63+
- `$,field` -- similar to the above, but uses a comma separated list (with optional trailing comma). So `[ $,field ]` will parse something like `[f1, f2, f3]`.
64+
- `$?field` -- will parse `field` and use `Default::default()` value if not present.
65+
- `$<field>` -- parse `<E1, E2, E3>`, where `field` is a collection of `E`
66+
- `$<?field>` -- parse `<E1, E2, E3>`, where `field` is a collection of `E`, but accept empty string as empty vector
67+
- `$(field)` -- parse `(E1, E2, E3)`, where `field` is a collection of `E`
68+
- `$(?field)` -- parse `(E1, E2, E3)`, where `field` is a collection of `E`, but accept empty string as empty vector
69+
- `$[field]` -- parse `[E1, E2, E3]`, where `field` is a collection of `E`
70+
- `$[?field]` -- parse `[E1, E2, E3]`, where `field` is a collection of `E`, but accept empty string as empty vector
71+
- `${field}` -- parse `{E1, E2, E3}`, where `field` is a collection of `E`
72+
- `${?field}` -- parse `{E1, E2, E3}`, where `field` is a collection of `E`, but accept empty string as empty vector
73+
- `$:guard <nonterminal>` -- parses `<nonterminal>` but only if the keyword `guard` is present. For example, `$:where $,where_clauses` would parse `where WhereClause1, WhereClause2, WhereClause3` but would also accept nothing (in which case, you would get an empty vector).
7574

7675
### Greediness
7776

@@ -81,8 +80,8 @@ Parsing is generally greedy. So `$*x` and `$,x`, for example, consume as many en
8180

8281
If no grammar is supplied, the default grammar is determined as follows:
8382

84-
* If a `#[cast]` or `#[variable]` annotation is present, then the default grammar is just `$v0`.
85-
* Otherwise, the default grammar is the name of the type (for structs) or variant (for enums), followed by `()`, with the values for the fields in order. So `Mul(Expr, Expr)` would have a default grammar `mul($v0, $v1)`.
83+
- If a `#[cast]` or `#[variable]` annotation is present, then the default grammar is just `$v0`.
84+
- Otherwise, the default grammar is the name of the type (for structs) or variant (for enums), followed by `()`, with the values for the fields in order. So `Mul(Expr, Expr)` would have a default grammar `mul($v0, $v1)`.
8685

8786
### Customizing the parse
8887

@@ -96,7 +95,6 @@ You must then supply an impl of `Parse` yourself. Because `Parse` is a trait ali
9695

9796
In the Rust code, the impl for `RigidTy` looks as follows:
9897

99-
10098
```rust
10199
{{#include ../../../crates/formality-types/src/grammar/ty/parse_impls.rs:RigidTy_impl}}
102-
```
100+
```

crates/formality-check/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl Check<'_> {
9999
assert!(env.encloses((&assumptions, &goal)));
100100

101101
let cs = formality_prove::prove(self.decls, env, &assumptions, &goal);
102+
let cs = cs.into_set()?;
102103
if cs.iter().any(|c| c.unconditionally_true()) {
103104
return Ok(());
104105
}
@@ -145,10 +146,10 @@ impl Check<'_> {
145146
&existential_goal,
146147
);
147148

148-
if cs.is_empty() {
149+
if !cs.is_proven() {
149150
return Ok(());
150151
}
151152

152-
bail!("failed to disprove\n {goal:?}\ngiven\n {assumptions:?}\ngot\n{cs:#?}")
153+
bail!("failed to disprove\n {goal:?}\ngiven\n {assumptions:?}\ngot\n{cs:?}")
153154
}
154155
}

crates/formality-core/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ formality-macros = { version = "0.1.0", path = "../formality-macros" }
2121
anyhow = "1.0.75"
2222
contracts = "0.6.3"
2323
final_fn = "0.1.0"
24+
itertools = "0.12.0"
25+
either = "1.9.0"
26+
expect-test = "1.4.1"
27+
regex = "1.10.2"
2428

2529
[dev-dependencies]
2630
expect-test = "1.4.1"

crates/formality-core/src/binder.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,24 @@ where
261261
T: std::fmt::Debug,
262262
{
263263
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
264-
if !self.kinds.is_empty() {
265-
write!(f, "{}", L::BINDING_OPEN)?;
266-
for (kind, i) in self.kinds.iter().zip(0..) {
267-
if i > 0 {
268-
write!(f, ", ")?;
264+
if f.alternate() {
265+
f.debug_struct("Binder")
266+
.field("kinds", &self.kinds)
267+
.field("term", &self.term)
268+
.finish()
269+
} else {
270+
if !self.kinds.is_empty() {
271+
write!(f, "{}", L::BINDING_OPEN)?;
272+
for (kind, i) in self.kinds.iter().zip(0..) {
273+
if i > 0 {
274+
write!(f, ", ")?;
275+
}
276+
write!(f, "{:?}", kind)?;
269277
}
270-
write!(f, "{:?}", kind)?;
278+
write!(f, "{} ", L::BINDING_CLOSE)?;
271279
}
272-
write!(f, "{} ", L::BINDING_CLOSE)?;
280+
write!(f, "{:?}", &self.term)?;
281+
Ok(())
273282
}
274-
write!(f, "{:?}", &self.term)?;
275-
Ok(())
276283
}
277284
}

0 commit comments

Comments
 (0)