|
| 1 | +using Semgus.Model.Smt.Terms; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +using static Semgus.Model.Smt.SmtCommonIdentifiers; |
| 11 | + |
| 12 | +namespace Semgus.Model.Smt.Theories |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// The theory of arrays with extensions |
| 16 | + /// </summary> |
| 17 | + internal class SmtArraysExTheory : ISmtTheory |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// A singleton theory instance |
| 21 | + /// </summary> |
| 22 | + public static SmtArraysExTheory Instance { get; } = new(); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Underlying array sort |
| 26 | + /// </summary> |
| 27 | + internal sealed class ArraySort : SmtSort |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// Cache of instantiated sorts. We need this since sorts are compared by reference |
| 31 | + /// </summary> |
| 32 | + private static readonly IDictionary<(SmtSortIdentifier, SmtSortIdentifier), ArraySort> _sortCache |
| 33 | + = new Dictionary<(SmtSortIdentifier, SmtSortIdentifier), ArraySort>(); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The sort used for indexing the array |
| 37 | + /// </summary> |
| 38 | + public SmtSort IndexSort { get; private set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The sort used for the array element values |
| 42 | + /// </summary> |
| 43 | + public SmtSort ValueSort { get; private set; } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Constructs a new array sort with the given parameters |
| 47 | + /// </summary> |
| 48 | + /// <param name="size">Size of bit vectors in this sort</param> |
| 49 | + private ArraySort(SmtSortIdentifier indexSort, SmtSortIdentifier valueSort) : |
| 50 | + base(new(new SmtIdentifier(ArraySortPrimaryId.Symbol), indexSort, valueSort)) |
| 51 | + { |
| 52 | + IndexSort = new UnresolvedParameterSort(indexSort); |
| 53 | + ValueSort = new UnresolvedParameterSort(valueSort); |
| 54 | + IsParametric = true; |
| 55 | + Arity = 2; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets the array sort for the given index and value sorts |
| 60 | + /// </summary> |
| 61 | + /// <param name="index">The index sort to use</param> |
| 62 | + /// <param name="value">The value sort to use</param> |
| 63 | + /// <returns>The array sort for the given index and value sorts</returns> |
| 64 | + public static ArraySort GetSort(SmtSortIdentifier index, SmtSortIdentifier value) |
| 65 | + { |
| 66 | + if (_sortCache.TryGetValue((index, value), out ArraySort? sort)) |
| 67 | + { |
| 68 | + return sort; |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + sort = new ArraySort(index, value); |
| 73 | + _sortCache.Add((index, value), sort); |
| 74 | + return sort; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Updates this sort with the resolved parameteric sorts |
| 80 | + /// </summary> |
| 81 | + /// <param name="resolved">Resolved parameters. Must have arity 2</param> |
| 82 | + public override void UpdateForResolvedParameters(IList<SmtSort> resolved) |
| 83 | + { |
| 84 | + if (resolved.Count != 2) |
| 85 | + { |
| 86 | + throw new InvalidOperationException("Got list of resolved sorts not of length 2!"); |
| 87 | + } |
| 88 | + |
| 89 | + IndexSort = resolved[0]; |
| 90 | + ValueSort = resolved[1]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// This theory's name |
| 96 | + /// </summary> |
| 97 | + public SmtIdentifier Name { get; } = ArraysExTheoryId; |
| 98 | + |
| 99 | + #region Deprecated |
| 100 | + public IReadOnlyDictionary<SmtIdentifier, IApplicable> Functions { get; } |
| 101 | + #endregion |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// The primary (i.e., non-indexed) sort symbols (e.g., "Array") |
| 105 | + /// </summary> |
| 106 | + public IReadOnlySet<SmtIdentifier> PrimarySortSymbols { get; } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// The primary (i.e., non-indexed) function symbols |
| 110 | + /// </summary> |
| 111 | + public IReadOnlySet<SmtIdentifier> PrimaryFunctionSymbols { get; } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Constructs an instance of the theory of arrays |
| 115 | + /// </summary> |
| 116 | + /// <param name="core">Reference to the core theory</param> |
| 117 | + private SmtArraysExTheory() |
| 118 | + { |
| 119 | + SmtSourceBuilder sb = new(this); |
| 120 | + sb.AddOnTheFlyFn("select"); |
| 121 | + sb.AddOnTheFlyFn("store"); |
| 122 | + |
| 123 | + Functions = sb.Functions; |
| 124 | + PrimaryFunctionSymbols = sb.PrimaryFunctionSymbols; |
| 125 | + PrimarySortSymbols = new HashSet<SmtIdentifier>() { ArraySortPrimaryId }; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Looks up a sort symbol in this theory |
| 130 | + /// </summary> |
| 131 | + /// <param name="sortId">The sort ID</param> |
| 132 | + /// <param name="resolvedSort">The resolved sort</param> |
| 133 | + /// <returns>True if successfully gotten</returns> |
| 134 | + public bool TryGetSort(SmtSortIdentifier sortId, [NotNullWhen(true)] out SmtSort? resolvedSort) |
| 135 | + { |
| 136 | + if (sortId.Arity == 2 && sortId.Name == ArraySortPrimaryId) |
| 137 | + { |
| 138 | + resolvedSort = ArraySort.GetSort(sortId.Parameters[0], sortId.Parameters[1]); |
| 139 | + return true; |
| 140 | + } |
| 141 | + resolvedSort = default; |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Looks up a function in this theory |
| 147 | + /// </summary> |
| 148 | + /// <param name="functionId">The function ID to look up</param> |
| 149 | + /// <param name="resolvedFunction">The resolved function</param> |
| 150 | + /// <returns>True if successfully gotten</returns> |
| 151 | + public bool TryGetFunction(SmtIdentifier functionId, [NotNullWhen(true)] out IApplicable? resolvedFunction) |
| 152 | + { |
| 153 | + return Functions.TryGetValue(functionId, out resolvedFunction); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments