|
11 | 11 | //--------------------------------------------------------------------------------------------- |
12 | 12 |
|
13 | 13 | using System.Security; |
| 14 | +using DafnyServer.CounterExampleGeneration; |
14 | 15 | using DafnyTestGeneration; |
15 | 16 |
|
16 | 17 | namespace Microsoft.Dafny { |
@@ -214,6 +215,12 @@ public static CommandLineArgumentsResult ProcessCommandLineArguments(string[] ar |
214 | 215 | "*** Error: Only one .dfy file can be specified for testing"); |
215 | 216 | return CommandLineArgumentsResult.PREPROCESSING_ERROR; |
216 | 217 | } |
| 218 | + |
| 219 | + if (DafnyOptions.O.ExtractCounterExample && DafnyOptions.O.ModelViewFile == null) { |
| 220 | + ExecutionEngine.printer.ErrorWriteLine(Console.Out, |
| 221 | + "*** Error: ModelView file must be specified when attempting counterexample extraction"); |
| 222 | + return CommandLineArgumentsResult.PREPROCESSING_ERROR; |
| 223 | + } |
217 | 224 | return CommandLineArgumentsResult.OK; |
218 | 225 | } |
219 | 226 |
|
@@ -290,9 +297,32 @@ static ExitValue ProcessFiles(IList<DafnyFile/*!*/>/*!*/ dafnyFiles, ReadOnlyCol |
290 | 297 | if (err == null && dafnyProgram != null && DafnyOptions.O.PrintFunctionCallGraph) { |
291 | 298 | Util.PrintFunctionCallGraph(dafnyProgram); |
292 | 299 | } |
| 300 | + if (dafnyProgram != null && DafnyOptions.O.ExtractCounterExample && exitValue == ExitValue.VERIFICATION_ERROR) { |
| 301 | + PrintCounterexample(DafnyOptions.O.ModelViewFile); |
| 302 | + } |
293 | 303 | return exitValue; |
294 | 304 | } |
295 | 305 |
|
| 306 | + /// <summary> |
| 307 | + /// Extract the counterexample corresponding to the first failing |
| 308 | + /// assertion and print it to the console |
| 309 | + /// </summary> |
| 310 | + /// <param name="modelViewFile"> Name of the file from which to read |
| 311 | + /// the counterexample </param> |
| 312 | + private static void PrintCounterexample(string modelViewFile) { |
| 313 | + var model = DafnyModel.ExtractModel(File.ReadAllText(modelViewFile)); |
| 314 | + Console.WriteLine("Counterexample for first failing assertion: "); |
| 315 | + foreach (var state in model.States.Where(state => !state.IsInitialState)) { |
| 316 | + Console.WriteLine(state.FullStateName + ":"); |
| 317 | + var vars = state.ExpandedVariableSet(-1); |
| 318 | + foreach (var variable in vars) { |
| 319 | + Console.WriteLine($"\t{variable.ShortName} : " + |
| 320 | + $"{variable.Type.InDafnyFormat()} = " + |
| 321 | + $"{variable.Value}"); |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + |
296 | 326 | private static string BoogieProgramSuffix(string printFile, string suffix) { |
297 | 327 | var baseName = Path.GetFileNameWithoutExtension(printFile); |
298 | 328 | var dirName = Path.GetDirectoryName(printFile); |
|
0 commit comments