summaryrefslogtreecommitdiff
path: root/Source/Provers/Z3/Prover.cs
blob: 208f934b6f604ad90f905e059c2a84b3eba2e788 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
//using util;
using Microsoft.Boogie;
using Microsoft.Boogie.Simplify;
using Microsoft.Basetypes;

// Simplified interface to an external prover like Simplify or the z3 process, taken from Bird.
namespace Microsoft.Boogie.Z3
{
  internal class Z3ProverProcess : ProverProcess {
    [Peer] private Z3InstanceOptions opts;
    [Peer] private readonly Inspector/*?*/ inspector;
    private readonly bool expectingModel = false;

    private string cmdLineArgs = "";

    [ContractInvariantMethod]
    void ObjectInvariant() 
    {
      Contract.Invariant(cmdLineArgs!=null);
      Contract.Invariant(opts != null);
      Contract.Invariant(cce.NonNullElements(parameterSettings));
    }


    class OptionValue {
      public readonly string Option;
      public readonly string Value;
      [ContractInvariantMethod]
void ObjectInvariant() 
{
    Contract.Invariant(Option!=null);
        Contract.Invariant(Value != null);
}

      public OptionValue(string option, string value) {Contract.Requires(option != null);Contract.Requires(value != null); Option = option; Value = value; }
    }
    static void AddOption(List<OptionValue> parms, string option, string value)
      //modifies parms.*; TODO:
    {
    Contract.Requires(option != null);
    Contract.Requires(value != null);
      Contract.Requires(cce.NonNullElements(parms));
      OptionValue ov = new OptionValue(option, value);
      cce.Owner.AssignSame(ov, cce.Owner.ElementProxy(parms));
      parms.Add(ov);
    }

    private List<OptionValue/*!>!*/> parameterSettings;

    //[NotDelayed]
    [Captured]
    public Z3ProverProcess(Z3InstanceOptions opts, Inspector inspector)
      : base(ComputeProcessStartInfo(opts), opts.ExeName)
    { // throws ProverException
      Contract.Requires(opts != null);
    Contract.Requires(inspector == null || cce.Owner.Same(opts, inspector));
    string z3args = OptionChar() + "si";
    List<OptionValue/*!*/> parms = new List<OptionValue/*!*/>();

    if (opts.V2) {
      AddOption(parms, "MODEL_PARTIAL", "true");
      AddOption(parms, "MODEL_VALUE_COMPLETION", "false");
      AddOption(parms, "MODEL_HIDE_UNUSED_PARTITIONS", "false");
      AddOption(parms, "MODEL_V1", "true");
      AddOption(parms, "ASYNC_COMMANDS", "false");
    } else {
      AddOption(parms, "PARTIAL_MODELS", "true");
      AddOption(parms, "MODEL_VALUE_COMPLETION", "false");
      AddOption(parms, "HIDE_UNUSED_PARTITIONS", "false");
    }

      if (opts.V2) {
        // Phase selection means to always try the negative literal polarity first, seems to be good for Boogie.
        // The restart parameters change the restart behavior to match Z3 v1, which also seems to be good.
        AddOption(parms, "PHASE_SELECTION", "0");
        AddOption(parms, "RESTART_STRATEGY", "0");
        AddOption(parms, "RESTART_FACTOR", "|1.5|");

        // This is used by VCC, but could be also useful for others, if sk_hack(foo(x)) is included as trigger,
        // the foo(x0) will be activated for e-matching when x is skolemized to x0.
        AddOption(parms, "NNF_SK_HACK", "true");

        // More or less like MAM=0.
        AddOption(parms, "QI_EAGER_THRESHOLD", "100");
        // Complex proof attempts in VCC (and likely elsewhere) require matching depth of 20 or more.
        
        // the following will make the :weight option more usable
        AddOption(parms, "QI_COST", "|\"(+ weight generation)\"|");

        // Make the integer model more diverse by default, speeds up some benchmarks a lot.
        AddOption(parms, "ARITH_RANDOM_INITIAL_VALUE", "true");

        // The left-to-right structural case-splitting strategy.
        AddOption(parms, "SORT_AND_OR", "false");
        AddOption(parms, "CASE_SPLIT", "3");

        // In addition delay adding unit conflicts.
        AddOption(parms, "DELAY_UNITS", "true");
        AddOption(parms, "DELAY_UNITS_THRESHOLD", "16");

        if (opts.Inspector != null) {
          AddOption(parms, "PROGRESS_SAMPLING_FREQ", "100");
        }
      } else {
        z3args += " " + OptionChar() + "mam:" + CommandLineOptions.Clo.Z3mam;
      }

      if (CommandLineOptions.Clo.z3AtFlag) {
        z3args += " " + OptionChar() + "@ ";
      }
      if (0 <= CommandLineOptions.Clo.ProverCCLimit) {
        z3args += " " + OptionChar() + "cex:" + CommandLineOptions.Clo.ProverCCLimit;
      }
      if (0 <= opts.Timeout) {
        z3args += " " + OptionChar() + "t:" + opts.Timeout;
      }
      if (opts.Typed) {
        AddOption(parms, "TYPE_CHECK", "true");
        if (opts.BitVectors == CommandLineOptions.BvHandling.Z3Native) {
          if (opts.V2) {
            AddOption(parms, "BV_REFLECT", "true");
          } else {
            AddOption(parms, "REFLECT_BV_OPS", "true");
          }
        }
      }
      if (CommandLineOptions.Clo.PrintErrorModel >= 1 ||
          CommandLineOptions.Clo.EnhancedErrorMessages == 1 ||
          CommandLineOptions.Clo.ContractInfer ||
          CommandLineOptions.Clo.LazyInlining > 0 ||
          CommandLineOptions.Clo.StratifiedInlining > 0) {
        z3args += " " + OptionChar() + "m";
        expectingModel = true;
      }
      if (CommandLineOptions.Clo.LazyInlining == 2) {
        z3args += " " + OptionChar() + "nw";
        AddOption(parms, "MACRO_EXPANSION", "true");
      }

      // Z3 version 1.3 does not support SETPARAMETER in the input, so we tack on the OPTION=value pairs to z3args
      if (opts.V1) {
        foreach (OptionValue opt in parms) {
          z3args += string.Format(" \"{0}={1}\"", opt.Option, opt.Value);
        }
      }

      foreach (string opt in CommandLineOptions.Clo.Z3Options) {
        Contract.Assert(opt != null);
        int eq = opt.IndexOf("=");
        // we add them both to command line and the input file:
        //    - allow for overriding default options
        //    - some options (like TRACE) work only from command line
        // Also options with spaces do not work with (SETPARAMETER ...)
        if (eq > 0 && opt.IndexOf(" ") < 0 && 'A' <= opt[0] && opt[0] <= 'Z') {
          AddOption(parms, opt.Substring(0, eq), opt.Substring(eq + 1));
        }
        z3args += " \"" + opt + "\"";
      }


      cmdLineArgs = z3args;
      parameterSettings = parms;
      
    //  ProcessStartInfo psi = new ProcessStartInfo(opts.ExeName, z3args);
    //  Contract.Assert(psi!=null);
    //  psi.CreateNoWindow = true;
    //  psi.UseShellExecute = false;
    //  psi.RedirectStandardInput = true;
    //  psi.RedirectStandardOutput = true;
    //  psi.RedirectStandardError = true;
      
      //base(psi, opts.ExeName);

      cce.Owner.AssignSame(this, opts);
      this.opts = opts;
      this.inspector = inspector;
    }

    private static ProcessStartInfo ComputeProcessStartInfo(Z3InstanceOptions opts) {
      string z3args = OptionChar() + "si";
      List<OptionValue/*!*/> parms = new List<OptionValue/*!*/>();

      if (opts.V2) {
        AddOption(parms, "MODEL_PARTIAL", "true");
        AddOption(parms, "MODEL_VALUE_COMPLETION", "false");
        AddOption(parms, "MODEL_HIDE_UNUSED_PARTITIONS", "false");
        AddOption(parms, "MODEL_V1", "true");
        AddOption(parms, "ASYNC_COMMANDS", "false");
      } else {
        AddOption(parms, "PARTIAL_MODELS", "true");
        AddOption(parms, "MODEL_VALUE_COMPLETION", "false");
        AddOption(parms, "HIDE_UNUSED_PARTITIONS", "false");
      }

      if (opts.V2) {
        // Phase selection means to always try the negative literal polarity first, seems to be good for Boogie.
        // The restart parameters change the restart behavior to match Z3 v1, which also seems to be good.
        AddOption(parms, "PHASE_SELECTION", "0");
        AddOption(parms, "RESTART_STRATEGY", "0");
        AddOption(parms, "RESTART_FACTOR", "|1.5|");

        // This is used by VCC, but could be also useful for others, if sk_hack(foo(x)) is included as trigger,
        // the foo(x0) will be activated for e-matching when x is skolemized to x0.
        AddOption(parms, "NNF_SK_HACK", "true");

        // More or less like MAM=0.
        AddOption(parms, "QI_EAGER_THRESHOLD", "100");
        // Complex proof attempts in VCC (and likely elsewhere) require matching depth of 20 or more.

        // the following will make the :weight option more usable
        AddOption(parms, "QI_COST", "|\"(+ weight generation)\"|");

        // Make the integer model more diverse by default, speeds up some benchmarks a lot.
        AddOption(parms, "ARITH_RANDOM_INITIAL_VALUE", "true");

        // The left-to-right structural case-splitting strategy.
        AddOption(parms, "SORT_AND_OR", "false");
        AddOption(parms, "CASE_SPLIT", "3");

        // In addition delay adding unit conflicts.
        AddOption(parms, "DELAY_UNITS", "true");
        AddOption(parms, "DELAY_UNITS_THRESHOLD", "16");

        if (opts.Inspector != null) {
          AddOption(parms, "PROGRESS_SAMPLING_FREQ", "100");
        }
      } else {
        z3args += " " + OptionChar() + "mam:" + CommandLineOptions.Clo.Z3mam;
      }

      if (CommandLineOptions.Clo.z3AtFlag) {
        z3args += " " + OptionChar() + "@ ";
      }
      if (0 <= CommandLineOptions.Clo.ProverCCLimit) {
        z3args += " " + OptionChar() + "cex:" + CommandLineOptions.Clo.ProverCCLimit;
      }
      if (0 <= opts.Timeout) {
        z3args += " " + OptionChar() + "t:" + opts.Timeout;
      }
      if (opts.Typed) {
        AddOption(parms, "TYPE_CHECK", "true");
        if (opts.BitVectors == CommandLineOptions.BvHandling.Z3Native) {
          if (opts.V2) {
            AddOption(parms, "BV_REFLECT", "true");
          } else {
            AddOption(parms, "REFLECT_BV_OPS", "true");
          }
        }
      }
      if (CommandLineOptions.Clo.PrintErrorModel >= 1 ||
          CommandLineOptions.Clo.EnhancedErrorMessages == 1 ||
          CommandLineOptions.Clo.ContractInfer ||
          CommandLineOptions.Clo.LazyInlining > 0 ||
          CommandLineOptions.Clo.StratifiedInlining > 0) {
        z3args += " " + OptionChar() + "m";
        //expectingModel = true;
      }
      if (CommandLineOptions.Clo.LazyInlining == 2) {
        z3args += " " + OptionChar() + "nw";
        AddOption(parms, "MACRO_EXPANSION", "true");
      }

      // Z3 version 1.3 does not support SETPARAMETER in the input, so we tack on the OPTION=value pairs to z3args
      if (opts.V1) {
        foreach (OptionValue opt in parms) {
          z3args += string.Format(" \"{0}={1}\"", opt.Option, opt.Value);
        }
      }

      foreach (string opt in CommandLineOptions.Clo.Z3Options) {
        Contract.Assert(opt != null);
        int eq = opt.IndexOf("=");
        // we add them both to command line and the input file:
        //    - allow for overriding default options
        //    - some options (like TRACE) work only from command line
        // Also options with spaces do not work with (SETPARAMETER ...)
        if (eq > 0 && opt.IndexOf(" ") < 0 && 'A' <= opt[0] && opt[0] <= 'Z') {
          AddOption(parms, opt.Substring(0, eq), opt.Substring(eq + 1));
        }
        z3args += " \"" + opt + "\"";
      }


      //cmdLineArgs = z3args;
      //parameterSettings = parms;

      ProcessStartInfo psi = new ProcessStartInfo(opts.ExeName, z3args);
      Contract.Assert(psi != null);
      psi.CreateNoWindow = true;
      psi.UseShellExecute = false;
      psi.RedirectStandardInput = true;
      psi.RedirectStandardOutput = true;
      psi.RedirectStandardError = true;

      return psi;
    }
    
    public override string OptionComments()
    {
      Contract.Ensures(Contract.Result<string>() != null);

      StringBuilder sb = new StringBuilder();
      sb.AppendFormat("Z3 command line: {0} {1}\nUser supplied Z3 options:",
                      opts.ExeName, cmdLineArgs);
      Contract.Assume( cce.IsPeerConsistent(CommandLineOptions.Clo));
      foreach (string opt in CommandLineOptions.Clo.Z3Options) {
        Contract.Assert(opt!=null);
        sb.Append(" ").Append(opt);
      }
      sb.AppendFormat("\nProver options: {0}\n", opts.ToString());
      return sb.ToString();
    }

    //[Pure(false)]
    public override IEnumerable<string/*!>!*/> ParameterSettings {
      get {
        Contract.Ensures(cce.NonNullElements( Contract.Result<IEnumerable<string>>()));

        if (opts.V2) {
          foreach (OptionValue opt in parameterSettings) {
            yield return "(SETPARAMETER " + opt.Option + " " + opt.Value + ")";
          }
        }
      }
    }

    // z3 uses different magic characters for options on linux/unix and on windows
    private static string OptionChar() {
      Contract.Ensures(Contract.Result<string>() != null);

      Contract.Assume( Environment.OSVersion != null);
      switch (Environment.OSVersion.Platform) {
        case PlatformID.Unix:
          return "-";
        default:
          return "/";
      }
    }

    protected override void DoBeginCheck(string descriptiveName, string formula)
    {
      Contract.Requires(descriptiveName != null);
      Contract.Requires(formula != null);
      ToWriteLine(formula);
      ToWriteLine(String.Format(";   END OF FORMULA {0} - {1}", NumFormulasChecked.ToString(), descriptiveName));
      ToFlush();
    }

    protected int TimedFromReadChar()
    {
      if (opts.Timeout > 0)
        return FromReadChar((opts.Timeout + 1) * 1000);
      else
        return FromReadChar();
    }

    private void Trace(string msg)
    { 
      Contract.Requires(msg != null);
      Console.WriteLine("Z3: " + msg);
    }

    public override ProverOutcome CheckOutcome(Microsoft.Boogie.ProverInterface.ErrorHandler handler)
    {
    Contract.Requires(handler != null);
    Contract.EnsuresOnThrow<UnexpectedProverOutputException>(true);
      //ProverOutcome outcome;
      bool isInvalid = false;

      if (this.simplify == null) {
        return ProverOutcome.Inconclusive;
      }


      while (true) {
        int ch = TimedFromReadChar();
        if (ch == -1 && this.readTimedOut) {
          handler.OnResourceExceeded("timeout (forced)");
          return ProverOutcome.TimeOut;
        }

        string line = new string((char)ch, 1) + FromReadLine();

        if (line.StartsWith("STATS ")) {
          if (inspector != null) {
            inspector.StatsLine(line);
          }
          continue;
        }

        if (opts.Verbosity > 2) {
          Trace("INPUT: " + line);
        }

		if (line.StartsWith("WARNING: Out of allocated virtual memory.")) {
		  handler.OnResourceExceeded("memory");
		  return ProverOutcome.OutOfMemory;
		}


        if (line.StartsWith("WARNING: ")) {
          string w = line.Substring(9);
          handler.OnProverWarning(w);
          continue;
        }

        if (line.ToUpper().StartsWith("ERROR")) {
          Console.WriteLine("Z3 returns the following error:");
          Console.WriteLine("    " + line);
          return ProverOutcome.Inconclusive;
        }

        int beg = 0;
        while (beg < line.Length && '0' <= line[beg] && line[beg] <= '9')
        {
          cce.LoopInvariant(beg <= line.Length);
          beg++;
        }

        if (beg > 0 && line.Substring(beg).StartsWith(": ")) {
          string status = line.Substring(beg + 2);

          if (status.StartsWith("Valid")) {
            return ProverOutcome.Valid;
          }

          if (status.StartsWith("Timeout")) {
            handler.OnResourceExceeded("timeout");
            return ProverOutcome.TimeOut;
          }

          if (status.StartsWith("Inconclusive")) {
            return ProverOutcome.Inconclusive;
          }

          if (status.StartsWith("Memout")) {
            handler.OnResourceExceeded("memory");
            return ProverOutcome.OutOfMemory;
          }

          if (status.StartsWith("Invalid")) {
            isInvalid = true;
            continue;
          }
        }

        if (isInvalid && line == ".") {
          return ProverOutcome.NotValid;
        }

        if (isInvalid && line.StartsWith("labels: (")) {
          List<string/*!*/>/*!*/ l = ParseLabels(line);
          Contract.Assert(cce.NonNullElements(l));
          Z3ErrorModel errModel = null;
          if (expectingModel) {
            if (opts.Verbosity > 2) {
              Trace("waiting for model");
            }
            line = FromReadLine();
            if (line.StartsWith("partitions:")) {
              line = ParseModel(out errModel);
              if (opts.Verbosity > 2) {
                Trace("model parsed, final line " + line);
              }
              // Z3 always ends the model with END_OF_MODEL, not with labels: or .
              Contract.Assume( line == "END_OF_MODEL");
            } else {
              throw new UnexpectedProverOutputException(string.Format("evil input from z3 (expecting partitions): '{0}'", line));
            }
          }
          handler.OnModel(l, errModel);
          continue;
        }

        throw new UnexpectedProverOutputException(string.Format("evil input from z3: '{0}'", line));
      }
    }
  
    /* ----------------------------------------------------------------------------
    BNF Grammar to parse Z3 output, including the model generated when using the /m /si switch:

    Output  ::= VC*
    VC      ::= number ": " "Valid." | number ": " "Inconclusive" | VCI
    VCI     ::= number ": " "Invalid"
                ("labels: " "(" ID* ")"
                [MODEL] "END_OF_MODEL")+
                "."
    MODEL   ::= MBOOL MFUNC
    MBOOL   ::= "boolean assignment:"
                "partitions:"
                MAPPING*
    MAPPING ::= ZID ["{" ID+"}"] ["->" "(" (number | false | true | BITVECTOR) ")"]
    BITVECTOR ::= ulong ":bv" int
    MFUNC   ::= "function interpretations:"
                F*
    F       ::= Id "->" "{"
                MAPLET*
                "else" "->" ZID
                "}"
    MAPLET  ::= ZID* "->" ZID

    -----------------------------------------------------------------------------*/
    private string ParseModel(out Z3ErrorModel errModel)
      //TODO: modifies this.*;
      //throws UnexpectedProverOutputException;
    {
    Contract.Ensures(Contract.Result<string>() == "." || Contract.Result<string>().StartsWith("labels: (") || Contract.Result<string>() == "END_OF_MODEL");
    Contract.Ensures(Contract.Result<string>() != null);

      //Format in the grammar:
      // ZID ["{" ID+"}"] ["->" "(" (number | false | true) ")"]
      // storing the model:
      // map each ID (a string) to the corresping ZID (an integer) in a dictionary:
      Dictionary<string/*!*/, int> identifierToPartition = new Dictionary<string/*!*/, int>();
      // map each ZID to the set (in list form) of IDs belonging to it (possibly empty):
      List<List<string/*!*/>> partitionToIdentifiers = new List<List<string/*!*/>>();
      // map each ZID to the number or boolean given, if any:
      List<Object> partitionToValue = new List<Object>();
      // map each value (number or boolean) to the ZID, reverse map of partitionToValue
      Dictionary<Object, int> valueToPartition = new Dictionary<Object, int>();
      cce.Owner.AssignSame(cce.Owner.ElementProxy(partitionToValue), cce.Owner.ElementProxy(valueToPartition));

      int ch;

      // read the MAPPING
      for (int zID = 0; true; zID++)
      {
        ch = FromReadChar();
        if (ch == 'f') {
          break;
        }
        ParseModelMapping(zID, identifierToPartition, partitionToIdentifiers, partitionToValue, valueToPartition);
      }// end MAPPING

      // add the fake partition for the 'else -> #undefined' clause
      List<string/*!*/> emptyList = new List<string/*!*/>();
      cce.Owner.AssignSame(emptyList, cce.Owner.ElementProxy(partitionToIdentifiers));
      partitionToIdentifiers.Add(emptyList);
      partitionToValue.Add(null);

      // continue in ParseModelFunctions, which breaks up this long method and enables its verification
      return ParseModelFunctions(ch, out errModel, identifierToPartition, partitionToIdentifiers, partitionToValue, valueToPartition);
    }

    private void ParseModelMapping(int zID,
        Dictionary<string/*!*/, int>/*!*/ identifierToPartition,
        List<List<string/*!*/>>/*!*/ partitionToIdentifiers,
        List<Object>/*!*/ partitionToValue,
        Dictionary<Object, int>/*!*/ valueToPartition)
        //TODO: modifies this.*, identifierToPartition.*, partitionToIdentifiers.*, partitionToValue.*, valueToPartition.*;
    {
    Contract.Requires(partitionToValue != null);
      Contract.Requires(valueToPartition != null);
      Contract.Requires(cce.NonNullElements(identifierToPartition));
      Contract.Requires(cce.NonNullElements(partitionToIdentifiers));
    Contract.Requires(cce.Owner.Same(cce.Owner.ElementProxy(partitionToValue), cce.Owner.ElementProxy(valueToPartition)));
      string s = FromReadLine();
      {
        // sanity check
        int pos = s.IndexOf(' ');
        string n = s;
        int k;
        if (pos >= 0) {
          n = s.Substring(0,pos);
        }
        if (! (int.TryParse(n, out k) && zID == k)) {
          System.Console.WriteLine("mismatch: {0}!={1} '{2}'", zID, k, s);
          Contract.Assume( false);
        }
      }
      
      int j = ParseModelZidAndIdentifiers(zID, s, identifierToPartition, partitionToIdentifiers);
      
      j = s.IndexOf(" -> ", j);
      if (0 <= j) {
        j += 4;
      }
      Contract.Assume( j == -1 || j < s.Length);  // if ' -> ' is present, then more should remain of the line
      if (j == -1) {
        // no "-> " found, end of this line, store that there is no value:
        partitionToValue.Add(null);
        int idForNull;
        if (identifierToPartition.TryGetValue("nullObject", out idForNull) && idForNull == zID) {
          Contract.Assume( !valueToPartition.ContainsKey("nullObject"));  // a RHS value should occur only once in the Z3 output
          valueToPartition.Add("nullObject", zID);
          // In this case partitionToValue (as the reverse of valueToPartition) should include
          // a map from zID -> "nullObject", but doing that breaks printing out the model as 
          // it is printed out by Z3. Also, that information is not required, valueToPartition
          // works well enough by itself.
        }
        
      } else if (s[j] == 't'/*rue*/) {
        partitionToValue.Add(true);
        object boxedTrue = true;
        Contract.Assume( !valueToPartition.ContainsKey(boxedTrue));  // a RHS value should occur only once in the Z3 output
        valueToPartition.Add(boxedTrue, zID);
      } else if (s[j] == 'f'/*alse*/) {
        object boxedFalse = false;
        cce.Owner.AssignSame(boxedFalse, cce.Owner.ElementProxy(partitionToValue));
        partitionToValue.Add(boxedFalse);
        Contract.Assume( !valueToPartition.ContainsKey(boxedFalse));  // a RHS value should occur only once in the Z3 output
        valueToPartition.Add(boxedFalse, zID);
      } else if (s[j] == 'v') {
        // -> val!..., i.e. no value
        partitionToValue.Add(null);
      } else if (s[j] == '{') {
        // array
        List<List<int>/*!*/> arrayModel = new List<List<int>/*!*/>();
        Contract.Assert(Contract.ForAll(arrayModel,a=>cce.NonNullElements(a)));
        string array = s.Substring(j+1);
        int index1, index2;
        string from, to;
        List<int> tuple = new List<int>();
        while (0 <= array.IndexOf(';')) {
          index1 = array.IndexOf('*') + 1;
          index2 = array.IndexOf(' ');
          from = array.Substring(index1, index2-index1);
          tuple.Add(int.Parse(from));
          array = array.Substring(index2);
          index1 = array.IndexOf('*') + 1;
          index2 = array.IndexOf(';');
          to = array.Substring(index1, index2-index1);
          array = array.Substring(index2+2);
          tuple.Add(int.Parse(to));
          arrayModel.Add(tuple);
          tuple = new List<int>();
        }
       Contract.Assert(array.StartsWith("else ->"));
        index1 = array.IndexOf('*') + 1;
        index2 = array.IndexOf('}');
        to = array.Substring(index1, index2-index1);
        tuple.Add(int.Parse(to));
        arrayModel.Add(tuple);
        partitionToValue.Add(arrayModel);
      } else {
        string numberOrBv = s.Substring(j);
        // make number an int, then store it:
        BigNum bvVal;
        int bvSize;
        string number, type;

        int l = numberOrBv.IndexOf(':', 0);
        if (0 <= l) {
          number = numberOrBv.Substring(0, l);
          type = numberOrBv.Substring(l + 1);
        } else {
          l = numberOrBv.IndexOf('[', 0);
          if (0 <= l) {
            number = numberOrBv.Substring(2, l-2);
            int closingBracePosition = numberOrBv.IndexOf(']', l);
            if (l < closingBracePosition)
              type = "bv" + numberOrBv.Substring(l + 1, closingBracePosition - l - 1);
            else type = "int";
          } else {
            number = numberOrBv;
            type = "int";
          }
        }

        if (type == "int") {
          object boxedN = BigNum.FromString(number);
          Contract.Assume( cce.Owner.None(boxedN));
          cce.Owner.AssignSame(boxedN, cce.Owner.ElementProxy(partitionToValue));
          partitionToValue.Add(boxedN);
          Contract.Assume( !valueToPartition.ContainsKey(boxedN));  // a RHS value should occur only once in the Z3 output
          valueToPartition.Add(boxedN, zID);
        } else if (type.StartsWith("bv") && BigNum.TryParse(number, out bvVal) && int.TryParse(type.Substring(2), out bvSize)) {
          BvConst bitV = new BvConst(bvVal, bvSize);
          cce.Owner.AssignSame(bitV, cce.Owner.ElementProxy(partitionToValue));
          partitionToValue.Add(bitV);
          Contract.Assume( !valueToPartition.ContainsKey(bitV));  // a RHS value should occur only once in the Z3 output
          valueToPartition.Add(bitV, zID);
        } else {
          System.Console.WriteLine("cannot parse type: '{0}':'{1}'", number, type);
          Contract.Assume( false);
        }

      }
    }

    private static int ParseModelZidAndIdentifiers(int zID, string s,
        Dictionary<string/*!*/, int>/*!*/ identifierToPartition,
        List<List<string/*!*/>>/*!*/ partitionToIdentifiers)
      //TODO: modifies identifierToPartition.*, partitionToIdentifiers.*;
      //TODO: Find all modifies statements
    {
      Contract.Requires(identifierToPartition!=null&&cce.NonNullElements(identifierToPartition.Keys));
      Contract.Requires(partitionToIdentifiers!=null&& Contract.ForAll(partitionToIdentifiers,  identifier=>cce.NonNullElements(identifier)));
    Contract.Requires(s != null);
    Contract.Ensures(0 <= Contract.Result<int>() && Contract.Result<int>() <= s.Length);
      
      List<string/*!*/> identifiers = new List<string/*!*/>();
      int arrowIndex = s.IndexOf('>');
     Contract.Assert(0 < arrowIndex);
      int j = s.IndexOf('{', 0) + 1;  // skip the '{', if present, and set j to 0 otherwise
      if (1 <= j && j < arrowIndex) {
        // There is a list of ID's.
        Contract.Assume( j < s.Length);  // there should be more characters; the ending '}', for one
        //ID*
        while (true) 
        {Contract.Invariant(cce.IsPeerConsistent( identifiers) && cce.IsPeerConsistent(identifierToPartition) && cce.IsPeerConsistent(partitionToIdentifiers));
          Contract.Invariant(0 <= j && j < s.Length);
          int k = s.IndexOfAny(new char[]{' ', '}'}, j);
          Contract.Assume( j <= k);
          string id = s.Substring(j, k-j);
          j = k+1;
          Contract.Assume( !identifierToPartition.ContainsKey(id));  // an ID is listed only once in this list, and an ID can only belong to one ZID equivalence class
          identifierToPartition.Add(id, zID);
          identifiers.Add(id);
          if (s[k] == '}') {
            // end of reading ID*
            break;
          }
          Contract.Assume( j < s.Length);  // there should be more characters; the ending '}', for one
        }//end ID*
      } else {
        j = 0;
      }
      cce.Owner.AssignSame(identifiers, cce.Owner.ElementProxy(partitionToIdentifiers));
      partitionToIdentifiers.Add(identifiers);
      return j;
    }
                  
    private string ParseModelFunctions(int ch, out Z3ErrorModel errModel,
      Dictionary<string/*!*/, int>/*!*/ identifierToPartition,
      List<List<string/*!*/>>/*!*/ partitionToIdentifiers,
      List<Object>/*!*/ partitionToValue,
      Dictionary<Object, int>/*!*/ valueToPartition
    ) 
      //TODO:  modifies this.*;
      
    {
    Contract.Requires(identifierToPartition!=null&&cce.NonNullElements(identifierToPartition.Keys));
      Contract.Requires(partitionToIdentifiers!=null&& Contract.ForAll(partitionToIdentifiers,  identifier=>cce.NonNullElements(identifier)));
    Contract.Requires(partitionToValue!=null);
    Contract.Requires(valueToPartition!=null);
    Contract.Ensures(Contract.Result<string>() != null);
    Contract.Ensures(Contract.Result<string>() == "." || Contract.Result<string>().StartsWith("labels: (") || Contract.Result<string>() == "END_OF_MODEL");
    Contract.EnsuresOnThrow<UnexpectedProverOutputException>(true);
      // read the function F
      Expect(ch, "function interpretations:");
      FromReadLine();
      
      // mapping of function names to function definitions
      Dictionary<string/*!*/, List<List<int>>/*!*/> definedFunctions = new Dictionary<string/*!*/, List<List<int>>/*!*/>();
      // function definition given as list of 'pointwise definitions' in the form of the arguments and result
      // the last element here will always be a list with just one entry which corresponds to the else case
      List<List<int>> functionDefinition = new List<List<int>>();
      // list of arguments, followed by the result, the last element of this list is always the result
      List<int> argumentsAndResult = new List<int>();
      
      // read F
      while (true) 
      {
        functionDefinition = new List<List<int>>();
        string s = FromReadLine();
        // end of F, "END_OF_MODEL" ends model, '.' ends whole VC, 'l' starts a new set of labels and model
        //           whenever there is a model this will end with "END_OF_MODEL", the other cases can only
        //           happen when there is no model printed!
        if (s == "." || s.StartsWith("labels: (") || s == "END_OF_MODEL") {
          errModel = new Z3ErrorModel(identifierToPartition, partitionToIdentifiers, partitionToValue, valueToPartition, definedFunctions);
          return s;
        }
        int j = s.IndexOf(' ', 0);
        Contract.Assume( 0 <= j);
        string id = s.Substring(0, j);
        // id is stored into definedFunctions once the function definition for it has 
        // been completely parsed. 
        
        if (s.IndexOf("-> {") < 0) 
        {
          // This function was a macro and we are not parsing its definition currently.
          // Just move on to the next function.
          while (true) 
          {
            s = FromReadLine();
            if (0 <= s.IndexOf("{" + id + "}"))
              break;
          }
          continue;
        }
        
        // just ignore the "-> {" by dropping string s
        string zIDstring;
        
        // MAPLET
        while (true) 
        {
          argumentsAndResult = new List<int>();
          // remove the 2 spaces that are here
          FromReadChar();
          FromReadChar();
          s = FromReadLine();
          if (s.StartsWith("else ->")) break;
          j = 0;
          
          //ZID*
          while(true) 
           {Contract.Invariant( 0 <= j && j <= s.Length);
          
            j = s.IndexOfAny(new Char[]{'*', '-'}, j);
            // true because this always ends with a "->":
            Contract.Assume( 0 <= j);
            
            // reading -> means end of ZID*
            if (s[j] == '-'/*>*/) break;
            
            // start reading the ZID* with the number, not the *
            j = j + 1;
            // by input string format:
            Contract.Assume( j < s.Length);
            int k = s.IndexOf(' ', j);
            // by input string format:
            Contract.Assume( j <= k);
            zIDstring = s.Substring(j, k-j);
            // add the arguments
            argumentsAndResult.Add(int.Parse(zIDstring));
            j = k;
          }// end ZID*
          
          // j is the beginning of "-> *", we want the first character after this
          j = j + 4;
          // by input string format:
          Contract.Assume( j <= s.Length);
          zIDstring = s.Substring(j);
          // add the result
          argumentsAndResult.Add(int.Parse(zIDstring));
          // add the input line as another 'pointwise defined' element to the functions definition
          functionDefinition.Add(argumentsAndResult);
        }// end MAPLET

        // this is the 'else -> #unspecified' case
        // by input string format:
        Contract.Assume( s.IndexOf("#unspec") >= 0);
        // this stores the else line as another argumentsAndResult list
        argumentsAndResult = new List<int>();
        argumentsAndResult.Add(partitionToIdentifiers.Count - 1); // use the fake partition we have created before
        // which is then added to the function definition, which is now complete
        cce.Owner.AssignSame(argumentsAndResult, cce.Owner.ElementProxy(functionDefinition));
        functionDefinition.Add(argumentsAndResult);

    /*
        // this is the 'else -> *' case, that string is already in s
        j = s.IndexOf('*', 0) + 1;
        // by input string format:
        assume 0 < j && j < s.Length;
        zIDstring = s.Substring(j);
        // this stores the else line as another argumentsAndResult list
        argumentsAndResult = new List<int>();
        argumentsAndResult.Add(int.Parse(zIDstring));
        // which is then added to the function definition, which is now complete
        functionDefinition.Add(argumentsAndResult); */

        // and therefore added to the map of defined functions, together with the name 'id'
        // which had been extracted before
        Contract.Assume( !definedFunctions.ContainsKey(id));  // each function name in the model is listed only once
        definedFunctions.Add(id, functionDefinition);
        
        // read the line with "}"
        ch = FromReadChar();
        Expect(ch, "}");
        FromReadLine();
      }// end F
    }

  }


  public class Z3ErrorModel : ErrorModel 
  {
    public Z3ErrorModel(Dictionary<string/*!*/, int>/*!*/ identifierToPartition,
      List<List<string/*!*/>>/*!*/ partitionToIdentifiers,
    List<Object>/*!*/ partitionToValue,
    Dictionary<object, int>/*!*/ valueToPartition,
    Dictionary<string/*!*/, List<List<int>>/*!*/>/*!*/ definedFunctions)
      : base(identifierToPartition, partitionToIdentifiers, partitionToValue, valueToPartition, definedFunctions)
    {
      Contract.Requires(identifierToPartition!=null&&cce.NonNullElements(identifierToPartition.Keys));
      Contract.Requires(partitionToIdentifiers!=null&&Contract.ForAll(partitionToIdentifiers,x=>cce.NonNullElements(x)));
      Contract.Requires(partitionToValue != null);
      Contract.Requires(valueToPartition!=null);
      Contract.Requires(definedFunctions!=null&&cce.NonNullElements(definedFunctions.Keys)&&cce.NonNullElements(definedFunctions.Values));

      this.partitionNames = new string/*?*/[partitionToIdentifiers.Count];
      this.prevPartitionNames = new string/*?*/[partitionToIdentifiers.Count];
    }

    private string/*?*/[]/*!*/ partitionNames;
    private string/*?*/[]/*!*/ prevPartitionNames;

    [ContractInvariantMethod]
void ObjectInvariant() 
{
    Contract.Invariant(partitionNames!=null);
      Contract.Invariant(prevPartitionNames!=null);
}


    private void SetNames()
    {
      int len = partitionToIdentifiers.Count;
      for (int i = 0; i < 3; ++i) { // let the names stabilize a bit
        prevPartitionNames = partitionNames;
        partitionNames = new string[len];
        for (int pos = 0; pos < len; ++pos)
          GetPartitionName(pos);
      }
    }

    private int NameBadness(string name)
    {
      Contract.Requires(name!=null);
      int badness = name.Length;
      if (name.StartsWith("call") && name.IndexOf("formal@") > 0)
        badness += 1000;
      if (name.IndexOf ("(") > 0)
        badness += 500;
      return badness;
    }

    private string GetPartitionName(int pos)
    {
      Contract.Ensures(Contract.Result<string>() != null);

      string name = partitionNames[pos];
      if (name != null) {
        return name;
      }

      object tmp = partitionToValue[pos];
      if (tmp != null) {
        partitionNames[pos] = tmp is BvConst ?  ((BvConst)tmp).ToReadableString() : tmp.ToString();
      } else {
        List<string/*!*/>/*!*/ possible_names = new List<string/*!*/>();
        List<string/*!*/> pti = partitionToIdentifiers[pos];
        Contract.Assert(cce.NonNullElements(pti));

        // make sure we're not called recursively
        string prevName = prevPartitionNames[pos];
        if (prevName == null) prevName = "*" + pos;
        partitionNames[pos] = prevName;

        if (pti != null && pti.Count > 0) {
          // add identifiers
          foreach (string n in pti)
          {
            Contract.Assert(n!=null);
            possible_names.Add(n);}
        }

        // Then also look for functions,
        // and then collect possible functional definitions
        foreach (KeyValuePair<string/*!*/, List<List<int>>/*!*/> kv in definedFunctions) {
          Contract.Assert(kv.Key!=null);
          Contract.Assert(kv.Value!=null);
          foreach (List<int> parms in kv.Value) {
            if (parms.Count > 1 && parms[parms.Count - 1] == pos) {
              string s = kv.Key + "(";
              for (int i = 0; i < parms.Count - 1; ++i) {
                if (i != 0) s += ", ";
                s += GetPartitionName(parms[i]);
              }
              s += ")";
              possible_names.Add(s);
            }
          }
        }

        // choose the shortest possible name
        if (possible_names.Count > 0) {
          string best = possible_names[0];
          foreach (string s in possible_names){
            Contract.Assert(s!=null);
            if (NameBadness(s) < NameBadness(best)) best = s;}
          if (best.Length < 120)
            partitionNames[pos] = best;
        }
      }

      return cce.NonNull(partitionNames[pos]);
    }
    
    private void PrintReadableModel (TextWriter writer) {
      Contract.Requires(writer!=null);
      writer.WriteLine("Z3 error model: ");
      SetNames();
      writer.WriteLine("partitions:");
     Contract.Assert(partitionToIdentifiers.Count == partitionToValue.Count);
      for (int i = 0; i < partitionToIdentifiers.Count; i++){
        writer.Write("{0,5}: {1}  ", "*"+i, GetPartitionName(i));
        List<string/*!*/> pti = partitionToIdentifiers[i];
        Contract.Assert(cce.NonNullElements(pti));
        if (pti != null && (pti.Count > 1 || (pti.Count == 1 && partitionToValue[i] != null))) {
          writer.Write("{");
          for (int k = 0; k < pti.Count - 1; k ++) {
            writer.Write(pti[k] + " ");
          }
          //extra work to make sure no " " is at the end of the list of identifiers
          if (pti.Count != 0) {
            writer.Write(pti[pti.Count - 1]);
          }
          writer.Write("}");
        }
        writer.WriteLine();
      }

      writer.WriteLine();
      writer.WriteLine("function interpretations:");
      List<string> funNames = new List<string>(definedFunctions.Keys);
      funNames.Sort();
      foreach (string name in funNames) {
        Contract.Assert(name!=null);
        if (definedFunctions[name].Count == 1) continue; // skip functions with only the else-> clause
        foreach (List<int> parms in definedFunctions[name]) {
          Contract.Assert(parms!=null);
          string s = name + "(";
          if (parms.Count == 1) {
            continue;
            // s += "*";
          } else {
            for (int i = 0; i < parms.Count - 1; ++i) {
              if (i != 0) s += ", ";
              s += GetPartitionName(parms[i]);
            }
          }
          s += ")";
          string res = GetPartitionName(parms[parms.Count - 1]);
          if (res == s)
            res = "*" + parms[parms.Count - 1] + " (SELF)";
          writer.WriteLine("{0} = {1}", s, res);
        }
        writer.WriteLine();
      }
      writer.WriteLine("The end.");
      writer.WriteLine();
    }

    public override void Print (TextWriter writer) {
      Contract.Requires(writer!=null);
      if (CommandLineOptions.Clo.PrintErrorModel == 4) {
        PrintReadableModel(writer);
        return;
      }

      writer.WriteLine("Z3 error model: ");

      writer.WriteLine("partitions:");
     Contract.Assert(partitionToIdentifiers.Count == partitionToValue.Count);
      for (int i = 0; i < partitionToIdentifiers.Count; i++){
        writer.Write("*"+i);
        List<string> pti = partitionToIdentifiers[i];
        if (pti != null && pti.Count != 0) {
          writer.Write(" {");
          for (int k = 0; k < pti.Count - 1; k ++) {
            writer.Write(pti[k] + " ");
          }
          //extra work to make sure no " " is at the end of the list of identifiers
          if (pti.Count != 0) {
            writer.Write(pti[pti.Count - 1]);
          }
          writer.Write("}");
        }
        // temp object needed for non-null checking
        object tempPTVI = partitionToValue[i];
        if (tempPTVI != null) {
          if (tempPTVI.ToString() == "True") {
            writer.Write(" -> " + "true" + "");
          } else if (tempPTVI.ToString() == "False") {
            writer.Write(" -> " + "false" + "");
          } else if (tempPTVI is BigNum) {
            writer.Write(" -> " + tempPTVI + ":int");
          } else if (tempPTVI is List<List<int>>) {
            List<List<int>> array = tempPTVI as List<List<int>>;
            Contract.Assume( array != null);
            writer.Write(" -> {");
            foreach(List<int> l in array) {
              if (l.Count == 2) {
                writer.Write("*"+l[0] + " -> " + "*"+l[1] + "; ");
              } else {
               Contract.Assert((l.Count == 1));
                writer.Write("else -> *"+l[0]+"}");
              } 
            }
          } else {
            writer.Write(" -> " + tempPTVI + "");
          }
        } else {
          writer.Write(" ");
        }
        writer.WriteLine();
      }

      writer.WriteLine("function interpretations:");
      foreach (KeyValuePair<string, List<List<int>>> kvp in definedFunctions) {
        Contract.Assert(kvp.Key!=null);
        writer.WriteLine(kvp.Key + " -> {");
        List<List<int>> kvpValue = kvp.Value;
        if (kvpValue != null) {
          foreach(List<int> l in kvpValue) {
            writer.Write("  ");
            if (l != null) {
              for (int i = 0; i < l.Count - 1; i++) {
                writer.Write("*"+l[i] + " ");
              }
              if (l.Count == 1) {
                writer.WriteLine("else -> #unspecified");
              } else {
                writer.WriteLine("-> "+ "*" + l[l.Count - 1]);
              }
            }
          }
        }
        writer.WriteLine("}");
      }
      writer.WriteLine("END_OF_MODEL");
      writer.WriteLine(".");
      
      if (CommandLineOptions.Clo.PrintErrorModel >= 2) {
        writer.WriteLine("identifierToPartition:");
        foreach (KeyValuePair<string, int> kvp in identifierToPartition) {
          Contract.Assert(kvp.Key!=null);
          writer.WriteLine(kvp.Key + " : " + "*" + kvp.Value);
        }
        
        writer.WriteLine("valueToPartition:");
        foreach (KeyValuePair<object, int> kvp in valueToPartition) {
          writer.WriteLine(kvp.Key + " : " + "*" + kvp.Value);
        }
        writer.WriteLine("End of model.");
      }
    }
  }


}