Repository navigation
Expand file tree
/
Copy pathProofRecording.cs
More file actions
198 lines (174 loc) · 8.36 KB
/
Copy pathProofRecording.cs
File metadata and controls
198 lines (174 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using AngouriMath.Core;
namespace AngouriMath
{
/// <summary>
/// One step in the decision of a quantified statement: the statement that was decided, the
/// rule that decided it, the verdict, and how deep it sits under the statement asked --
/// the base case of an induction is one below the induction, the identity that settles the
/// step one below that. The rule is named as the reference names it and as a checker
/// would: <see cref="Lemma"/> is the Lean 4 tactic or lemma the step corresponds to, which
/// is what an export to a proof checker needs and the design constraint of
/// <a href="https://github.com/asc-community/AngouriMath/issues/746">#746</a>'s proof engine.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1409">#1409</a>
/// </summary>
public sealed record ProofStep(Entity Statement, string Rule, string Lemma, Entity Verdict, int Depth)
{
/// <inheritdoc/>
public override string ToString()
=> $"{new string(' ', 2 * Depth)}{Statement} is {Verdict}: {Rule} [{Lemma}]";
}
/// <summary>
/// A scope that collects the steps by which the quantified statements decided inside it were
/// decided, the way <see cref="Core.Transformations.RewriteRecording"/> collects rewrites: the proof of
/// <c>forall n in ZZ+ : sum(k, k, 1, n) = n (n + 1)/2</c> is the closed form read through
/// its case, the proof of <c>forall n in ZZ+ : sum(1/(k (k + 1)), k, 1, n) = n/(n + 1)</c>
/// is a base case and a step, each a step here. Nothing is collected outside a scope, and
/// the ordinary path does not pay for one nobody opened. A decision already made is not
/// made again: an entity whose <see cref="Entity.Evaled"/> is cached, or a statement parsed
/// through the parser's cache and evaluated before, records nothing, so a proof is asked of
/// a fresh entity (<see cref="MathS.FromString(string, bool)"/> with the cache off).
/// </summary>
/// <example>
/// <code>
/// using var proof = ProofRecording.Start();
/// var verdict = "forall n in ZZ+ : sum(1/(k (k + 1)), k, 1, n) = n/(n + 1)".ToEntity().Evaled;
/// foreach (var step in proof.Steps)
/// Console.WriteLine(step);
/// </code>
/// </example>
public sealed class ProofRecording : IDisposable
{
[ConcurrentField]
private static readonly AsyncLocal<ProofRecording?> current = new();
private readonly ProofRecording? enclosing;
private readonly List<ProofStep> steps = new();
private readonly object gate = new();
private volatile bool closed;
[ThreadStatic] private static int depth;
private ProofRecording(ProofRecording? enclosing) => this.enclosing = enclosing;
/// <summary>Opens a scope; dispose it to close. Scopes nest, and each collects its own steps.</summary>
public static ProofRecording Start()
{
var recording = new ProofRecording(current.Value);
current.Value = recording;
return recording;
}
/// <summary>The steps recorded so far, in the order they were decided: a statement's sub-steps come before it.</summary>
public IReadOnlyList<ProofStep> Steps
{
get { lock (gate) return steps.ToArray(); }
}
/// <summary>
/// The steps written as the reference writes a proof: the statement asked last, its
/// sub-steps indented under the order they were needed in.
/// </summary>
public string Written()
{
var builder = new StringBuilder();
foreach (var step in Steps)
builder.AppendLine(step.ToString());
return builder.ToString();
}
/// <inheritdoc/>
public void Dispose()
{
if (closed)
return;
closed = true;
if (ReferenceEquals(current.Value, this))
current.Value = enclosing;
}
internal static ProofRecording? Current => current.Value;
/// <summary>Whether any scope is open on this flow, read once per decision.</summary>
internal static bool Recording => current.Value is { closed: false };
internal static void Enter() => depth++;
internal static void Leave() => depth--;
internal static void Add(Entity statement, string rule, string lemma, Entity verdict)
=> Add(statement, rule, lemma, verdict, depth - 1);
/// <summary>A step one below the decision in progress: a base case evaluated inside an induction.</summary>
internal static void AddBelow(Entity statement, string rule, string lemma, Entity verdict)
=> Add(statement, rule, lemma, verdict, depth);
private static void Add(Entity statement, string rule, string lemma, Entity verdict, int at)
{
var recording = current.Value;
if (recording is null || recording.closed)
return;
lock (recording.gate)
recording.steps.Add(new ProofStep(statement, rule, lemma, verdict, at));
}
/// <summary>
/// Where the steps stand now, so that an attempt which comes to nothing can be taken
/// back with <see cref="Rollback"/>: what a route tried and abandoned is not part of
/// the proof of what it found.
/// </summary>
internal static int Mark()
{
var recording = current.Value;
if (recording is null || recording.closed)
return 0;
lock (recording.gate)
return recording.steps.Count;
}
/// <summary>
/// Writes the steps recorded since <paramref name="mark"/> with <paramref name="from"/>
/// read as <paramref name="to"/>: a quantifier decides under a name of its own while
/// facts are in scope (<see cref="Functions.Boolean.QuantifierFacts"/>), and its steps
/// are written under the name it was given.
/// </summary>
internal static void Rename(int mark, Entity.Variable from, Entity.Variable to)
{
var recording = current.Value;
if (recording is null || recording.closed)
return;
lock (recording.gate)
for (var i = mark; i < recording.steps.Count; i++)
{
var step = recording.steps[i];
recording.steps[i] = step with
{
Statement = Functions.Boolean.QuantifierFacts.Renamed(step.Statement, from, to),
Rule = step.Rule.Replace(from.Name, to.Name),
Verdict = Functions.Boolean.QuantifierFacts.Renamed(step.Verdict, from, to),
};
}
}
/// <summary>
/// Writes the step the decision in progress recorded last, since <paramref name="mark"/>,
/// as a step about <paramref name="statement"/> by <paramref name="rule"/>, keeping its
/// verdict and depth. A quantifier that decided its body with facts in scope records the
/// statement as it was written, rather than the body those facts made of it.
/// </summary>
internal static void Restate(int mark, Entity statement, string rule, string lemma)
{
var recording = current.Value;
if (recording is null || recording.closed)
return;
lock (recording.gate)
{
var last = recording.steps.Count - 1;
if (last >= mark && recording.steps[last].Depth == depth)
recording.steps[last] = recording.steps[last] with { Statement = statement, Rule = rule, Lemma = lemma };
}
}
internal static void Rollback(int mark)
{
var recording = current.Value;
if (recording is null || recording.closed)
return;
lock (recording.gate)
if (recording.steps.Count > mark)
recording.steps.RemoveRange(mark, recording.steps.Count - mark);
}
}
}