Skip to content

Commit b4dec4d

Browse files
committed
Refactor: resolving ranks requires SmtContext
1 parent 06a20a6 commit b4dec4d

9 files changed

Lines changed: 21 additions & 16 deletions

File tree

ParserLibrary/Commands/GrammarBlockHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static (SemgusGrammar, IReadOnlyList<SemgusTermType>, SemgusTermType, Smt
192192
semDatum.InputVariables.Where(v => v.Name == identifier).First());
193193
}
194194
else if (ctx.TryGetFunctionDeclaration(identifier, out var constant)
195-
&& constant.TryResolveRank(out var rank, default))
195+
&& constant.TryResolveRank(ctx, out var rank, default))
196196
{
197197
constructor = new(GensymUtils.Gensym("_SyProd", identifier.Symbol));
198198
newProds.Add(new(ttDatum.NonTerminal, constructor, Enumerable.Empty<SemgusGrammar.NonTerminal>()));

ParserLibrary/Reader/Converters/TermConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public override bool TryConvertImpl(Type tFrom, Type tTo, object from, [NotNullW
7676
}
7777
else if (_contextProvider.Context.TryGetFunctionDeclaration(qid.Id, out var defn))
7878
{
79-
if (defn.TryResolveRank(out var rank, GetSortOrDie(qid.Sort) /* No arguments */))
79+
if (defn.TryResolveRank(_contextProvider.Context, out var rank, GetSortOrDie(qid.Sort) /* No arguments */))
8080
{
8181
to = new SmtFunctionApplication(defn, rank, new List<SmtTerm>());
8282
}
@@ -362,7 +362,7 @@ public override bool TryConvertImpl(Type tFrom, Type tTo, object from, [NotNullW
362362
return true;
363363
}
364364

365-
if (!orf!.TryResolveRank(out var rank, boolsort, Enumerable.Repeat(boolsort, convTerms.Count).ToArray()))
365+
if (!orf!.TryResolveRank(_contextProvider.Context, out var rank, boolsort, Enumerable.Repeat(boolsort, convTerms.Count).ToArray()))
366366
{
367367
throw new InvalidOperationException("Too many terms to match pattern.");
368368
}
@@ -434,7 +434,7 @@ public override bool TryConvertImpl(Type tFrom, Type tTo, object from, [NotNullW
434434
return true;
435435
}
436436

437-
if (defn.TryResolveRank(out var rank, GetSortOrDie(af.Id.Sort), argSorts))
437+
if (defn.TryResolveRank(_contextProvider.Context, out var rank, GetSortOrDie(af.Id.Sort), argSorts))
438438
{
439439
to = new SmtFunctionApplication(defn, rank, args);
440440
}

ParserTests/BitVectors/ExtractTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public void CanResolve(long i, long j, long m)
9090
var retSort = SmtBitVectorsTheory.BitVectorsSort.GetSort(n);
9191

9292
// First, try without specifying the return sort
93-
AssertTrue(function.TryResolveRank(out var rank, default, argSort));
93+
AssertTrue(function.TryResolveRank(ctx, out var rank, default, argSort));
9494
Assert.Single(rank.ArgumentSorts);
9595
Assert.Equal(argSort, rank.ArgumentSorts[0]);
9696
Assert.Equal(retSort, rank.ReturnSort);
9797

9898
// Then, with specifying the return sort
99-
AssertTrue(function.TryResolveRank(out rank, retSort, argSort));
99+
AssertTrue(function.TryResolveRank(ctx, out rank, retSort, argSort));
100100
Assert.Single(rank.ArgumentSorts);
101101
Assert.Equal(argSort, rank.ArgumentSorts[0]);
102102
Assert.Equal(retSort, rank.ReturnSort);
@@ -119,10 +119,10 @@ public void CannotResolve(long i, long j, long m, long? np = default)
119119
var retSort = SmtBitVectorsTheory.BitVectorsSort.GetSort(n);
120120

121121
// First, try without specifying the return sort
122-
AssertFalse(function.TryResolveRank(out var _, default, argSort));
122+
AssertFalse(function.TryResolveRank(ctx, out var _, default, argSort));
123123

124124
// Then, with specifying the return sort
125-
AssertFalse(function.TryResolveRank(out _, retSort, argSort));
125+
AssertFalse(function.TryResolveRank(ctx, out _, retSort, argSort));
126126
}
127127
}
128128
}

ParserTests/Issues/Issue43.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void DoesNotThrowParseExceptionForBool()
7474
{
7575
SmtContext ctx = new();
7676
Assert.True(ctx.TryGetFunctionDeclaration(new("true"), out var truefn));
77-
Assert.True(truefn!.TryResolveRank(out var rank, default));
77+
Assert.True(truefn!.TryResolveRank(ctx, out var rank, default));
7878

7979
SemgusToken testToken = new SymbolToken("test", default);
8080
SmtTerm testTerm = new SmtFunctionApplication(truefn, rank!, new List<SmtTerm>());

Semgus-Lib/Model/Smt/IApplicable.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public interface IApplicable
2525
/// <summary>
2626
/// Tries to get an appropriate rank for the given parameter and return sorts
2727
/// </summary>
28+
/// <param name="ctx">The SMT context</param>
2829
/// <param name="rank">The resolved rank</param>
2930
/// <param name="returnSort">The return sort, if known</param>
3031
/// <param name="argumentSorts">The argument sorts</param>
3132
/// <returns>True if successfully resolved the rank</returns>
32-
public bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts);
33+
public bool TryResolveRank(SmtContext ctx, [NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts);
3334

3435
/// <summary>
3536
/// Gets an informative string about available ranks

Semgus-Lib/Model/Smt/SmtFunction.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,19 @@ public void AddRankTemplate(SmtFunctionRank rank)
9696
/// <summary>
9797
/// Attempts to resolve a concrete rank for the given function signature
9898
/// </summary>
99+
/// <param name="ctx">The SMT context</param>
99100
/// <param name="rank">The resolved rank</param>
100101
/// <param name="returnSort">Return sort of function call, if known</param>
101102
/// <param name="argumentSorts">Function call argument sorts</param>
102103
/// <returns>True if successfully resolved a concrete rank</returns>
103-
public bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
104+
public bool TryResolveRank(SmtContext ctx, [NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
104105
{
105106
var sameArity = _rankTemplates
106107
.Where(r => r.Arity == argumentSorts.Length);
107108

108109
foreach (var template in sameArity)
109110
{
110-
if (TryResolveRank(out rank, template, returnSort, argumentSorts))
111+
if (TryResolveRank(ctx, out rank, template, returnSort, argumentSorts))
111112
{
112113
return true; // We just pick the first one...maybe we need to check all templates and report ambiguities. TODO.
113114
}
@@ -119,12 +120,13 @@ public bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtSor
119120
/// <summary>
120121
/// Attempts to resolve a concrete rank for the given function signature, from a single rank template
121122
/// </summary>
123+
/// <param name="ctx">The SMT context</param>
122124
/// <param name="rank">The resolved rank</param>
123125
/// <param name="template">The rank template to try</param>
124126
/// <param name="returnSort">Return sort of function call, if known</param>
125127
/// <param name="argumentSorts">Function call argument sorts</param>
126128
/// <returns>True if successfully resolved a concrete rank</returns>
127-
private bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtFunctionRank template, SmtSort? returnSort, params SmtSort[] argumentSorts)
129+
private bool TryResolveRank(SmtContext ctx, [NotNullWhen(true)] out SmtFunctionRank? rank, SmtFunctionRank template, SmtSort? returnSort, params SmtSort[] argumentSorts)
128130
{
129131
Dictionary<SmtSort, SmtSort> resolvedParameters = new();
130132

Semgus-Lib/Model/Smt/SmtMacro.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,12 @@ private bool CheckSortMatch(SmtSort a, SmtSort b, IDictionary<SmtSort, SmtSort>
362362
/// <summary>
363363
/// Tries to resolve a rank for this macro
364364
/// </summary>
365+
/// <param name="ctx">The SMT context</param>
365366
/// <param name="rank">The resolved rank</param>
366367
/// <param name="returnSort">The return sort of the expression, if known</param>
367368
/// <param name="argumentSorts">Sorts of arguments</param>
368369
/// <returns>True if successfully resolved</returns>
369-
public bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
370+
public bool TryResolveRank(SmtContext ctx, [NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
370371
{
371372
var paramQueue = new Queue<MacroParameter>(_lambdaList);
372373
MacroParameter? formal = default;

Semgus-Lib/Model/Smt/Sorts/SmtDatatypeConstructor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ public bool IsArityPossible(int arity)
6969
/// <summary>
7070
/// Tries to get an appropriate rank for the given parameter and return sorts
7171
/// </summary>
72+
/// <param name="ctx">The SMT context</param>
7273
/// <param name="rank">The resolved rank</param>
7374
/// <param name="returnSort">The return sort, if known</param>
7475
/// <param name="argumentSorts">The argument sorts</param>
7576
/// <returns>True if successfully resolved the rank</returns>
76-
public bool TryResolveRank([NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
77+
public bool TryResolveRank(SmtContext ctx, [NotNullWhen(true)] out SmtFunctionRank? rank, SmtSort? returnSort, params SmtSort[] argumentSorts)
7778
{
7879
// TODO: handle parameterized datatypes
7980
if (returnSort is not null && returnSort != Parent)

Semgus-Lib/Model/Smt/Terms/SmtTermBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static SmtTerm Apply(SmtContext ctx, Action<object, string> onError, SmtI
7474
throw new InvalidOperationException();
7575
}
7676

77-
if (!function.TryResolveRank(out SmtFunctionRank? rank, default, args.Select(a => a.Sort).ToArray()))
77+
if (!function.TryResolveRank(ctx, out SmtFunctionRank? rank, default, args.Select(a => a.Sort).ToArray()))
7878
{
7979
onError(id, $"Unable to resolve function rank: {id}, with arg sorts {args.Select(a => a.Sort).ToArray()}");
8080
throw new InvalidOperationException();

0 commit comments

Comments
 (0)