Commit Graph

35 Commits

Author SHA1 Message Date
Slava Pestov
86c15ad5c1 RequirementMachine: Generating conformances can reason about concrete conformances now 2021-12-08 00:53:35 -05:00
Slava Pestov
47f3341b40 RequirementMachine: Delete loops that don't have rules in empty context
When a rule is replaced by a rewrite path, if the rule is in context
then every step of the replacement step is necessarily always in context.

The only way new rules in empty context can be introduced by a
replacement is if the original rewrite step being replaced had no
context.

Therefore, loops that do not contain rules in empty context will
never provide any additional information during homotopy reduction.
2021-12-08 00:53:35 -05:00
Slava Pestov
1bf6102f1e RequirementMachine: Don't bother normalizing loops 2021-12-08 00:53:35 -05:00
Slava Pestov
34cbfd23a5 RequirementMachine: Don't assert if a rewrite system has unresolved rules
This is a source-level error, not an invariant violation. Instead, plumb
a new hadError() flag, which in the future will assert if no diagnostic
was produced.
2021-12-08 00:53:34 -05:00
Slava Pestov
7427b68785 RequirementMachine: Introduce RewriteStep::ConcreteConformance and ::SuperclassConformance 2021-12-06 23:04:46 -05:00
Slava Pestov
e9b94c1bf1 RequirementMachine: Move some methods from RewriteStep to RewritePathEvaluator
This better encapsulates the evaluator state into a single struct.
2021-12-06 23:04:46 -05:00
Saleem Abdulrasool
910fbee14e gardening: make c++98-compat-extra-semi an error
This cleans up 90 instances of this warning and reduces the build spew
when building on Linux.  This helps identify actual issues when
building which can get lost in the stream of warning messages.  It also
helps restore the ability to build the compiler with gcc.
2021-11-27 11:40:17 -08:00
Slava Pestov
952dafad72 RequirementMachine: Preliminary refactoring in preparation for computing top-level generic signatures 2021-11-11 22:39:20 -05:00
Slava Pestov
796123eebd RequirementMachine: Avoid some expensive invariant checks in noassert builds 2021-11-11 22:39:20 -05:00
Slava Pestov
2dd14afc5e RequirementMachine: Minor comment fixes 2021-11-10 23:02:02 -05:00
Slava Pestov
dcefd70d11 RequirementMachine: Homotopy reduction propagates Rule::isExplicit() bit
If a rewrite loop contains two rules in empty context and one of them
is explicit, propagate the explicit bit to the other rule if the original
rule was found to be redundant.

This means that if we begin with an unresolved rule like

    [P].X.[Q] => [P].X [explicit]

And then completion added the resolved rule

    [P:X].[Q] => [P:X]

Then the new rule will become explicit once the original rule becomes
redundant.
2021-11-10 00:18:57 -05:00
Slava Pestov
8ab1c1f32e RequirementMachine: Remove one of the three homotopy reduction passes
This was a hack from before the change to minimize away the
least canonical rules first. Now, there's no reason to perform
a separate pass over rules containing unresolved name symbols.
2021-11-10 00:18:18 -05:00
Slava Pestov
ddecb54ba4 RequirementMachine: Fix replaceRuleWithPath() to handle RewriteStep::Decompose
When re-contextualizing a path containing RewriteStep::Decompose,
don't update StartOffset/EndOffset for steps that execute with
more than one term on the stack.
2021-11-01 22:51:59 -04:00
Slava Pestov
450c7c268a RequirementMachine: Split off RewriteLoop.cpp from HomotopyReduction.cpp 2021-10-27 01:28:24 -04:00
Slava Pestov
1057b56395 RequirementMachine: Rename HomotopyGenerator to RewriteLoop
Also, stop talking about 2-cells and 3-cells.
2021-10-27 01:28:24 -04:00
Slava Pestov
745acea7ce RequirementMachine: Introduce Rule::isIdentityConformanceRule() 2021-10-27 00:39:07 -04:00
Slava Pestov
a729bebbc4 RequirementMachine: Improved rule deletion heuristic
Instead of finding the best redundancy candidate from the first
homotopy generator that has one, find the best redundancy
candidate from *all* homotopy generators that have one.

This correctly minimizes the "Joe Groff monoid" without hitting
the assertion guarding against simplified rules being non-redundant.

It also brings us closer to being able to correctly minimize
the protocol from https://bugs.swift.org/browse/SR-7353.
2021-10-27 00:38:33 -04:00
Slava Pestov
b01e97f2c6 RequirementMachine: Rewrite steps are instructions for a two-stack machine
I need to simplify concrete substitutions when adding a rewrite rule, so
for example if X.Y => Z, we want to simplify

  A.[concrete: G<τ_0_0, τ_0_1> with <X.Y, X>] => A

to

  A.[concrete: G<τ_0_0, τ_0_1> with <Z, X>] => A

The requirement machine used to do this, but I took it out, because it
didn't fit with the rewrite path representation. However, I found a case
where this is needed so I need to bring it back.

Until now, a rewrite path was a composition of rewrite steps where each
step would transform a source term into a destination term.

The question then becomes, how do we represent concrete substitution
simplification with such a scheme.

One approach is to rework rewrite paths to a 'nested' representation,
where a new kind of rewrite step applies a sequence of rewrite paths to
the concrete substitution terms. Unfortunately this would complicate
memory management and require recursion when visiting the steps of a
rewrite path.

Another, simpler approach that I'm going with here is to generalize a
rewrite path to a stack machine program instead.

I'm adding two new kinds of rewrite steps which manipulate a pair of
stacks, called 'A' and 'B':

- Decompose, which takes a term ending in a superclass or concrete type
  symbol, and pushes each concrete substitution on the 'A' stack.

- A>B, which pops the top of the 'A' stack and pushes it onto the 'B'
  stack.

Since all rewrite steps are invertible, the inverse of the two new
step kinds are as follows:

- Compose, which pops a series of terms from the 'A' stack, and replaces
  the concrete substitutions in the term ending in a superclass or
  concrete type symbol underneath.

- B>A, which pops the top of the 'B' stack and pushes it onto the
  'B' stack.

Both Decompose and Compose take an operand, which is the number of
concrete substitutions to expect. This is encoded in the RuleID field
of RewriteStep.

The two existing rewrite steps ApplyRewriteRule and AdjustConcreteType
simply pop and push the term at the top of the 'A' stack.

Now, if addRule() wishes to transform

  A.[concrete: G<τ_0_0, τ_0_1> with <X.Y, X>] => A

into

  A.[concrete: G<τ_0_0, τ_0_1> with <Z, X>] => A

it can construct the rewrite path

  Decompose(2) ⊗ A>B ⊗ <<rewrite path from X.Y to Z>> ⊗ B>A ⊗ Compose(2)

This commit lays down the plumbing for these new rewrite steps, and
replaces the existing 'evaluation' walks over rewrite paths that
mutate a single MutableTerm with a new RewritePathEvaluator type, that
stores the two stacks.

The changes to addRule() are coming in a subsequent commit.
2021-10-27 00:11:16 -04:00
Slava Pestov
0571b65cb8 RequirementMachine: Move protocol linear order from ProtocolGraph to RewriteContext 2021-10-21 19:00:10 -04:00
Slava Pestov
77b8dfeeb2 RequirementMachine: Homotopy reduction prefers to delete less-canonical rules
Instead of deleting lower numbered rules, compare the LHS, and if they're
equal, compare the RHS, then use that to pick the rule to delete.
2021-10-15 15:13:22 -04:00
Slava Pestov
6441b8bd73 RequirementMachine: Extract some verification logic out of minimizeRewriteSystem() 2021-10-14 15:03:08 -04:00
Slava Pestov
3ee99d6619 RequirementMachine: NFC cleanups for generating conformances 2021-10-09 19:11:15 -04:00
Slava Pestov
371366eb4b RequirementMachine: Assert that non-redundant rules are fully resolved 2021-10-08 14:28:27 -04:00
Slava Pestov
b8177995df RequirementMachine: Sort requirements and canonicalize same-type requirements 2021-10-08 14:28:27 -04:00
Slava Pestov
566971dbc2 RequirementMachine: Add some comments and assertions around state transitions 2021-10-08 14:28:27 -04:00
Slava Pestov
2db676d41a RequirementMachine: Homotopy reduction deletes rules with unresolved name symbols first 2021-10-01 00:57:44 -04:00
Slava Pestov
86ee6c2f55 RequirementMachine: Add some comments 2021-10-01 00:56:50 -04:00
Slava Pestov
a46a7b893f RequirementMachine: Check that homotopy reduction was able to eliminate all redundant conformances 2021-10-01 00:56:50 -04:00
Slava Pestov
34592cb2fa RequirementMachine: Fix generating conformances algorithm for rules of the form [P].[P] => [P] 2021-10-01 00:56:50 -04:00
Slava Pestov
64d1931e15 RequirementMachine: Sketch out "generating conformances" algorithm 2021-09-27 19:04:16 -04:00
Slava Pestov
5f3d781ed3 RequirementMachine: Homotopy reduction tweaks
- Skip permanent rules (there's no point, we'll add them back next time)
- Skip conformance rules (these will be handled separately)
- Delete 3-cells that are entirely "in-context" (but don't quote me on
  this one, I'm not quite yet convinced it's correct, but it feels right)
2021-09-27 19:03:34 -04:00
Slava Pestov
2b068b3c48 RequirementMachine: Encapsulate 3-cells in a new HomotopyGenerator data type 2021-09-24 08:59:51 -04:00
Slava Pestov
5210e5d475 RequirementMachine: Remove some debugging code 2021-09-24 08:59:51 -04:00
Slava Pestov
67fad897ab RequirementMachine: Compute left canonical forms of paths 2021-09-24 08:59:51 -04:00
Slava Pestov
5f03c737d2 RequirementMachine: Preliminary implementation of homotopy reduction 2021-09-24 08:59:50 -04:00