| | | 1 | | using Microsoft.Z3; |
| | | 2 | | using NanoCLang.Entities; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Text; |
| | | 9 | | using Type = NanoCLang.Entities.Type; |
| | | 10 | | |
| | | 11 | | namespace NanoCLang.Environemnts { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a class for local environments. |
| | | 14 | | /// A local environment maps names in a specific scope to their respective types. |
| | | 15 | | /// Additionally local environments can contain guard expressions for code flow analysis. |
| | | 16 | | /// </summary> |
| | | 17 | | [DebuggerDisplay("{" + nameof(ToString) + "(),nq}")] |
| | | 18 | | public class LocalEnvironment : Dictionary<string, Type> { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates a new instance of an empty local environment. |
| | | 21 | | /// </summary> |
| | 168 | 22 | | public LocalEnvironment() : base() { |
| | 84 | 23 | | guards = new Stack<PureExpression>(); |
| | 84 | 24 | | structureLocations = new Dictionary<string, string>(); |
| | 84 | 25 | | } |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates a new instance of a local environment that contains all elements copied from the input environment < |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="gamma">Environment to copy from.</param> |
| | 342 | 30 | | public LocalEnvironment(LocalEnvironment gamma) : base(gamma) { |
| | 171 | 31 | | guards = new Stack<PureExpression>(gamma.guards.Reverse()); |
| | 171 | 32 | | structureLocations = new Dictionary<string, string>(gamma.structureLocations); |
| | 171 | 33 | | } |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a new instance of a local environment from a list of type assignments. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="types">Type assignments to copy from.</param> |
| | 64 | 38 | | public LocalEnvironment(IEnumerable<KeyValuePair<string, Type>> types) : base(types) { |
| | 32 | 39 | | guards = new Stack<PureExpression>(); |
| | 32 | 40 | | structureLocations = new Dictionary<string, string>(); |
| | 32 | 41 | | } |
| | | 42 | | /// <summary> |
| | | 43 | | /// Adds links to the local environment. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="links">Structure links to add.</param> |
| | 32 | 46 | | public void AddStructLocs(IEnumerable<KeyValuePair<string, string>> links) { |
| | 142 | 47 | | foreach (var (key, val) in links) |
| | 23 | 48 | | structureLocations.Add(key, val); |
| | 32 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Guard expressions in the current environment. |
| | | 53 | | /// </summary> |
| | | 54 | | private readonly Stack<PureExpression> guards; |
| | | 55 | | /// <summary> |
| | | 56 | | /// Attempts to get a struct location binding from the environment. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="loc">Location that is linked to a struct.</param> |
| | | 59 | | /// <param name="structName">Name of the struct the <paramref name="loc"/> is linked to.</param> |
| | | 60 | | /// <returns><see langword="true"/> iff the location link was found.</returns> |
| | 11 | 61 | | public bool TryGetStructLoc(string loc, [NotNullWhen(true)] out string structName) => structureLocations.TryGetV |
| | | 62 | | private readonly Dictionary<string, string> structureLocations; |
| | | 63 | | /// <summary> |
| | | 64 | | /// Add a guard expression to the environment. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="guard">Guard expression that restricts the environment.</param> |
| | 27 | 67 | | public void PushGuard(PureExpression guard) => guards.Push(guard); |
| | | 68 | | /// <summary> |
| | | 69 | | /// Removes and returns the topmost guard expression of the environment. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <returns>The guard expression that was removed from the top of the environment.</returns> |
| | | 72 | | /// <exception cref="InvalidOperationException">No guard expression is on the environment.</exception> |
| | 31 | 73 | | public PureExpression PopGuard() => guards.Pop(); |
| | | 74 | | /// <inheritdoc/> |
| | 4 | 75 | | public override string ToString() { |
| | 5 | 76 | | var sb = new StringBuilder(string.Join(',', this.Select(i => $"{i.Key}:{i.Value}"))); |
| | 7 | 77 | | if (guards.Count > 0) { |
| | 24 | 78 | | foreach (var guard in guards) sb.Append('|').Append(guard.ToString()); |
| | 3 | 79 | | } |
| | 4 | 80 | | return sb.ToString(); |
| | 4 | 81 | | } |
| | | 82 | | /// <summary> |
| | | 83 | | /// Evaluates a function with a guarded localenvironment. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <typeparam name="T">Function output to evaluate</typeparam> |
| | | 86 | | /// <param name="guard">Guard expression.</param> |
| | | 87 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 88 | | /// <returns>Evaluated function result.</returns> |
| | 24 | 89 | | public T Guarded<T>(PureExpression guard, Func<LocalEnvironment, T> p) { |
| | 24 | 90 | | PushGuard(guard); |
| | 24 | 91 | | try { |
| | 24 | 92 | | return p(this); |
| | 24 | 93 | | } finally { |
| | 24 | 94 | | if (guard != PopGuard()) throw new InvalidProgramException("Guard stack was changed out of scope!"); |
| | 24 | 95 | | } |
| | 24 | 96 | | } |
| | | 97 | | /// <summary> |
| | | 98 | | /// Evaluates a function with additional bindings on the environment. |
| | | 99 | | /// This function allows for evaluating judgements with additional bindings on an environment without the need t |
| | | 100 | | /// </summary> |
| | | 101 | | /// <typeparam name="T">Function output to evaluate.</typeparam> |
| | | 102 | | /// <param name="name">Name of the binding.</param> |
| | | 103 | | /// <param name="type">Type that the name is bound to.</param> |
| | | 104 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 105 | | /// <returns>Evaluated function result.</returns> |
| | 275 | 106 | | public T With<T>(string name, Type type, Func<LocalEnvironment, T> p) { |
| | | 107 | | // Get the value that was overridden by the name (can be null) |
| | 275 | 108 | | var overridden = TryGetValue(name, out var t) ? t : null; |
| | 275 | 109 | | this[name] = type; |
| | 275 | 110 | | try { |
| | 275 | 111 | | return p(this); |
| | 275 | 112 | | } finally { |
| | 275 | 113 | | if (overridden is Type) this[name] = overridden; |
| | 275 | 114 | | else Remove(name); |
| | 275 | 115 | | } |
| | 273 | 116 | | } |
| | | 117 | | /// <summary> |
| | | 118 | | /// Evaluates a function with additional bindings on the environment. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <typeparam name="T">Function output to evaluate.</typeparam> |
| | | 121 | | /// <param name="bindings">Additional bindings on the envionment.</param> |
| | | 122 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 123 | | /// <returns>Evaluated function result.</returns> |
| | 6 | 124 | | public T With<T>(IEnumerable<KeyValuePair<string, Type>> bindings, Func<LocalEnvironment, T> p) { |
| | 18 | 125 | | var below = new Dictionary<string, Type?>(bindings.Select(i => i.Key).Select(i => new KeyValuePair<string, T |
| | 6 | 126 | | try { |
| | 30 | 127 | | foreach (var (name, type) in bindings) |
| | 6 | 128 | | this[name] = type; |
| | 6 | 129 | | return p(this); |
| | 6 | 130 | | } finally { |
| | 30 | 131 | | foreach (var (name, type) in below) |
| | 6 | 132 | | if (type is Type) this[name] = type; |
| | 6 | 133 | | else Remove(name); |
| | 6 | 134 | | } |
| | 6 | 135 | | } |
| | | 136 | | /// <summary> |
| | | 137 | | /// Evaluates a function with additional bindings on the environment. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <typeparam name="T">Function output to evaluate.</typeparam> |
| | | 140 | | /// <param name="bindings">Additional bindings on the envionment.</param> |
| | | 141 | | /// <param name="p">Function that evaluates the body.</param> |
| | | 142 | | /// <returns>Evaluated function result.</returns> |
| | 0 | 143 | | public T WithStructLoc<T>(IEnumerable<KeyValuePair<string, string>> bindings, Func<LocalEnvironment, T> p) { |
| | 0 | 144 | | var below = new Dictionary<string, string?>(bindings.Select(i => i.Key).Select(i => new KeyValuePair<string, |
| | 0 | 145 | | try { |
| | 0 | 146 | | foreach (var (location, @struct) in bindings) |
| | 0 | 147 | | structureLocations[location] = @struct; |
| | 0 | 148 | | return p(this); |
| | 0 | 149 | | } finally { |
| | 0 | 150 | | foreach (var (location, @struct) in below) |
| | 0 | 151 | | if (@struct is string) structureLocations[location] = @struct; |
| | 0 | 152 | | else structureLocations.Remove(location); |
| | 0 | 153 | | } |
| | 0 | 154 | | } |
| | | 155 | | /// <summary> |
| | | 156 | | /// Executes an action with additional bindings on the environment. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="name">Name of the binding.</param> |
| | | 159 | | /// <param name="type">Type that the name is bound to.</param> |
| | | 160 | | /// <param name="p">Action to execute with the additional binding.</param> |
| | | 161 | | public void With(string name, Type type, Action<LocalEnvironment> p) => |
| | 595 | 162 | | _ = With(name, type, gamma => { p(this); return true; }); |
| | | 163 | | /// <summary> |
| | | 164 | | /// Converts the expression to an boolean expression that the SMT solver can handle. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="smt">SMT solver context.</param> |
| | | 167 | | /// <returns>Boolean expression that makes up the environment.</returns> |
| | | 168 | | public BoolExpr ToBoolean(NanoCSMT smt) |
| | 238 | 169 | | => smt.Context.MkAnd(guards.Select(i => i.ToBoolean(this, smt)) |
| | 138 | 170 | | .Concat(EmbedEUFA(smt).ToList())); |
| | 138 | 171 | | private IEnumerable<BoolExpr> EmbedEUFA(NanoCSMT smt) { |
| | 1911 | 172 | | foreach (var entry in this) { |
| | 666 | 173 | | if (!(entry.Value is RefinedType r)) continue; |
| | 332 | 174 | | var refinement = r.Refinement.Replace(new Substitutor() { Variables = new Dictionary<string, PureExpress |
| | 332 | 175 | | yield return refinement.ToBoolean(this, smt); |
| | 332 | 176 | | } |
| | 138 | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |