Ejemplo n.º 1
0
        public void highlightTemporarily(PrettyPrintFormat format, Color color)
        {
            var tmp = format.getPrintRule(this).Clone();

            tmp.color = color;
            format.addTemporaryRule(id + "", tmp);
        }
Ejemplo n.º 2
0
 public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
     foreach (Literal l in Literals)
     {
         content.Append(l + "\n");
     }
 }
Ejemplo n.º 3
0
 private bool printChildren(PrettyPrintFormat format, PrintRule rule)
 {
     if (Args.Length == 0)
     {
         return(false);
     }
     return(!format.rewritingEnabled || rule.printChildren);
 }
Ejemplo n.º 4
0
 private static bool linebreaksNecessary(InfoPanelContent content, PrettyPrintFormat format, bool isMultiline, int startLength)
 {
     if (format.maxWidth == 0)
     {
         return(false);
     }
     return(isMultiline || (content.Length - startLength > format.maxWidth));
 }
Ejemplo n.º 5
0
        public void PrettyPrint(InfoPanelContent content, PrettyPrintFormat format, int eqNumber = -1)
        {
            var numberingString = eqNumber == -1 ? "" : $"({eqNumber}) ";

            content.switchToDefaultFormat();
            content.Append(numberingString);
            source.PrettyPrint(content, format, numberingString.Length);
            EqualityExplanationPrinter.singleton.visit(this, Tuple.Create(content, format, false, numberingString.Length));
        }
Ejemplo n.º 6
0
 public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
     if (Conflict != null)
     {
         Conflict.InfoPanelText(content, format);
     }
     else
     {
         content.Append("No conflict");
     }
 }
Ejemplo n.º 7
0
        public void PrettyPrint(InfoPanelContent content, PrettyPrintFormat format, int indent = 0)
        {
            var indentColors = Enumerable.Empty <Color>();

            if (indent >= 2)
            {
                indentColors = indentColors.Concat(Enumerable.Repeat(PrintConstants.defaultTextColor, 1));
                indentColors = indentColors.Concat(Enumerable.Repeat(Color.Transparent, indent - 2));
            }
            indentColors = indentColors.Concat(Enumerable.Repeat(PrintConstants.defaultTextColor, format.CurrentEqualityExplanationPrintingDepth));

            PrettyPrint(content, new Stack <Color>(indentColors), format);
        }
Ejemplo n.º 8
0
 public void printName(InfoPanelContent content, PrettyPrintFormat format)
 {
     content.Append(Name);
     if (format.showType)
     {
         content.Append(GenericType);
     }
     if (iterationOffset > 0)
     {
         content.Append("_-" + iterationOffset);
     }
     if (format.showTermId)
     {
         content.Append("[" + (id >= 0 ? id.ToString() : "g" + -id) + (isPrime ? "'" : "") + "]");
     }
 }
Ejemplo n.º 9
0
        private bool needsParenthesis(PrettyPrintFormat format, PrintRule rule, PrintRule parentRule)
        {
            if (TheorySpecificMeaning != null)
            {
                return(false);
            }
            switch (rule.parentheses)
            {
            case PrintRule.ParenthesesSetting.Always:
                return(true);

            case PrintRule.ParenthesesSetting.Never:
                return(false);

            case PrintRule.ParenthesesSetting.Precedence:
                if (format.history.Count == 0)
                {
                    return(false);
                }
                if (parentRule.precedence < rule.precedence)
                {
                    return(false);
                }
                if (!string.IsNullOrWhiteSpace(parentRule.prefix(false)) &&
                    !string.IsNullOrWhiteSpace(parentRule.suffix(false)))
                {
                    return(false);
                }
                if (!string.IsNullOrWhiteSpace(parentRule.prefix(false)) &&
                    !string.IsNullOrWhiteSpace(parentRule.infix(false)) &&
                    format.childIndex == 0)
                {
                    return(false);
                }
                if (!string.IsNullOrWhiteSpace(parentRule.infix(false)) &&
                    !string.IsNullOrWhiteSpace(parentRule.suffix(false)) &&
                    format.childIndex == format.history.Last().Item1.Args.Length - 1)
                {
                    return(false);
                }
                return(format.history.Last().Item1.Name != Name || !rule.associative);

            default:
                throw new InvalidEnumArgumentException();
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Prints a section explaining the equality substitutions necessary to obtain a term matching the trigger.
        /// </summary>
        public void PrintEqualitySubstitution(InfoPanelContent content, PrettyPrintFormat format)
        {
            content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
            content.Append("\nSubstituting equalities yields:\n\n");
            content.switchToDefaultFormat();

            foreach (var blamedTerm in BlamedEffectiveTerms)
            {
                blamedTerm.highlightTemporarily(format, PrintConstants.blameColor);
            }

            foreach (var boundTerm in BoundEffectiveTerms)
            {
                boundTerm.highlightTemporarily(format, PrintConstants.bindColor);
            }

            foreach (var pair in EffectiveBlameTerms.Zip(fullPattern.Args, Tuple.Create))
            {
                var effectiveTerm       = pair.Item1;
                var usedPattern         = pair.Item2;
                var rootTerm            = equalities.FirstOrDefault(eq => eq.Key == usedPattern).Value?.FirstOrDefault(eqSource => TopLevelTerms.Contains(eqSource.Item2))?.Item2 ?? effectiveTerm;
                var topLevelTermNumber  = format.termNumbers.First(kv => kv.Key.isSubterm(rootTerm.id));
                var usedEqualityNumbers = equalities.Keys.Where(k => usedPattern.isSubterm(k)).Select(k => bindings[k])
                                          .Select(b => {
#if !DEBUG
                    try
                    {
#endif
                    return(format.equalityNumbers.First(kv => Term.semanticTermComparer.Equals(kv.Key.target, b.Item2)).Value);

#if !DEBUG
                }
                                                  catch (Exception)
                {
                    return(0);
                }
#endif
                }).Distinct();
                content.Append($"Substituting ({String.Join("), (", usedEqualityNumbers)}) in ({topLevelTermNumber.Value}):\n");
                effectiveTerm.PrettyPrint(content, format);
                content.Append("\n\n");
                content.switchToDefaultFormat();
            }
        }
Ejemplo n.º 11
0
        public override string ToString()
        {
            string t;
            bool   isBin = Term.Args.Length == 2;

            if (Negated && Term.Name == "or")
            {
                Negated = false;
                Term    = new Term("And", LogProcessor.NegateAll(Term.Args));
            }

            if (isBin)
            {
                if (Term.Name.Any(char.IsLetterOrDigit))
                {
                    isBin = false;
                }
            }

            if (Term == null)
            {
                t = "(nil)";
            }
            else if (isBin)
            {
                var content0 = new InfoPanelContent();
                var content1 = new InfoPanelContent();
                Term.Args[0].PrettyPrint(content0, PrettyPrintFormat.DefaultPrettyPrintFormat());
                Term.Args[1].PrettyPrint(content1, PrettyPrintFormat.DefaultPrettyPrintFormat());
                content0.finalize();
                content1.finalize();
                t = $"{content0}  {Term.Name}  {content1}";
            }
            else
            {
                var content = new InfoPanelContent();
                Term.PrettyPrint(content, PrettyPrintFormat.DefaultPrettyPrintFormat());
                content.finalize();
                t = content.ToString();
            }
            return(string.Format("{0}p{1}  {3}{2}", Negated ? "~" : "", Id, t, Implied == null ? "" : "[+" + Implied.Length + "] "));
        }
Ejemplo n.º 12
0
        public void tempHighlightBlameBindTerms(PrettyPrintFormat format)
        {
            if (!bindingInfo.IsPatternMatch())
            {
                foreach (var binding in bindingInfo.bindings)
                {
                    var patternTerm = binding.Key;
                    var term        = binding.Value;
                    var color       = PrintConstants.bindColor;

                    patternTerm.highlightTemporarily(format, color, bindingInfo.patternMatchContext[patternTerm.id]);
                    term.Item2.highlightTemporarily(format, color, term.Item1);
                }
                foreach (var blameTerm in bindingInfo.explicitlyBlamedTerms)
                {
                    blameTerm.highlightTemporarily(format, PrintConstants.blameColor);
                }
                return;
            }

            // highlight replaced, equal terms as well
            foreach (var eqTerm in bindingInfo.equalities.SelectMany(kv => kv.Value))
            {
                eqTerm.Item2.highlightTemporarily(format, PrintConstants.equalityColor, eqTerm.Item1);
            }

            bindingInfo.fullPattern?.highlightTemporarily(format, PrintConstants.patternMatchColor);
            foreach (var binding in bindingInfo.bindings)
            {
                var patternTerm = binding.Key;
                var term        = binding.Value;
                var color       = PrintConstants.blameColor;
                if (patternTerm.id == -1)
                {
                    color = PrintConstants.bindColor;
                }

                patternTerm.highlightTemporarily(format, color, bindingInfo.patternMatchContext[patternTerm.id]);
                term.Item2.highlightTemporarily(format, color, term.Item1);
            }
        }
Ejemplo n.º 13
0
        public void highlightTemporarily(PrettyPrintFormat format, Color color, IEnumerable <ConstraintType> pathConstraints)
        {
            var baseRule = format.getPrintRule(this);
            IEnumerable <PrintRule> newRules;

            if (pathConstraints.Any())
            {
                newRules = pathConstraints.Select(constraint =>
                {
                    var tmp   = baseRule.Clone();
                    tmp.color = color;
                    tmp.historyConstraints = constraint;
                    return(tmp);
                });
            }
            else
            {
                var tmp = baseRule.Clone();
                tmp.color = color;
                tmp.historyConstraints = new ConstraintType();
                newRules = Enumerable.Repeat(tmp, 1);
            }

            if (id == -1)
            {
                foreach (var tmp in newRules)
                {
                    format.addTemporaryRule(Name, tmp);
                }
            }
            else
            {
                foreach (var tmp in newRules)
                {
                    format.addTemporaryRule(id + "", tmp);
                }
            }
        }
Ejemplo n.º 14
0
 public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
     SummaryInfo(content);
     content.Append('\n');
     PrettyPrint(content, new Stack <Color>(), format);
 }
Ejemplo n.º 15
0
 public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
     SummaryInfo(content);
     BodyTerm.PrettyPrint(content, format);
 }
Ejemplo n.º 16
0
 public void highlightTemporarily(PrettyPrintFormat format, Color color, ConstraintType pathConstraint)
 {
     highlightTemporarily(format, color, Enumerable.Repeat(pathConstraint, 1));
 }
Ejemplo n.º 17
0
 public virtual void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
 }
Ejemplo n.º 18
0
        public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
        {
            SummaryInfo(content);
            content.Append("Highlighted terms are ");
            if (bindingInfo.IsPatternMatch())
            {
                content.switchFormat(PrintConstants.DefaultFont, PrintConstants.patternMatchColor);
                content.Append("matched");
                content.switchToDefaultFormat();
                content.Append(" or ");
                content.switchFormat(PrintConstants.DefaultFont, PrintConstants.equalityColor);
                content.Append(PrintConstants.LargeTextMode ? "matched using\nequality" : "matched using equality");
                content.switchToDefaultFormat();
                content.Append(" or ");
            }
            content.switchFormat(PrintConstants.DefaultFont, PrintConstants.blameColor);
            content.Append("blamed");
            content.switchToDefaultFormat();
            content.Append(" or ");
            content.switchFormat(PrintConstants.DefaultFont, PrintConstants.bindColor);
            content.Append("bound");
            content.switchToDefaultFormat();
            content.Append(".\n\n");

            tempHighlightBlameBindTerms(format);

            if (!bindingInfo.IsPatternMatch())
            {
                content.switchFormat(PrintConstants.BoldFont, PrintConstants.instantiationTitleColor);
                if (InstantiationMethod == "theory-solving")
                {
                    content.Append($"Instantiated by the {Quant.Namespace} theory solver.\n\n");
                }
                else
                {
                    content.Append($"Instantiated using {InstantiationMethod}.\n\n");
                }
                content.switchToDefaultFormat();

                if (bindingInfo.explicitlyBlamedTerms.Any())
                {
                    content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
                    content.Append("Blamed Terms:\n");
                    content.switchToDefaultFormat();
                }

                var termNumbering = 1;

                foreach (var t in bindingInfo.explicitlyBlamedTerms)
                {
                    if (!format.termNumbers.TryGetValue(t, out var termNumber))
                    {
                        termNumber = termNumbering;
                        ++termNumbering;
                        format.termNumbers[t] = termNumber;
                    }
                    var numberingString = $"({termNumber}) ";
                    content.Append($"\n{numberingString}");
                    t.PrettyPrint(content, format, numberingString.Length);
                    content.switchToDefaultFormat();
                    content.Append("\n\n");
                }
            }

            if (bindingInfo.IsPatternMatch())
            {
                content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
                content.Append("Blamed Terms:\n\n");
                content.switchToDefaultFormat();

                var termNumbering = 1;

                var blameTerms         = bindingInfo.getDistinctBlameTerms();
                var distinctBlameTerms = blameTerms.Where(req => bindingInfo.TopLevelTerms.Contains(req) ||
                                                          (!bindingInfo.equalities.SelectMany(eq => eq.Value).Any(t => t.Item2.id == req.id) &&
                                                           !bindingInfo.equalities.Keys.Any(k => bindingInfo.bindings[k].Item2 == req)));

                foreach (var t in distinctBlameTerms)
                {
                    if (!format.termNumbers.TryGetValue(t, out var termNumber))
                    {
                        termNumber = termNumbering;
                        ++termNumbering;
                        format.termNumbers[t] = termNumber;
                    }
                    var numberingString = $"({termNumber}) ";
                    content.Append($"\n{numberingString}");
                    t.PrettyPrint(content, format, numberingString.Length);
                    content.switchToDefaultFormat();
                    content.Append("\n\n");
                }

                if (bindingInfo.equalities.Count > 0)
                {
                    var numberOfTopLevelTerms = bindingInfo.getDistinctBlameTerms().Count;

                    content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
                    content.Append("\nRelevant equalities:\n\n");
                    content.switchToDefaultFormat();

                    format.printContextSensitive = false;
                    foreach (var equality in bindingInfo.equalities)
                    {
                        var effectiveTerm = bindingInfo.bindings[equality.Key].Item2;
                        foreach (var term in equality.Value.Select(t => t.Item2).Distinct(Term.semanticTermComparer))
                        {
                            EqualityExplanation explanation;
#if !DEBUG
                            try
                            {
#endif
                            explanation = bindingInfo.EqualityExplanations.First(ee => ee.source.id == term.id && ee.target.id == effectiveTerm.id);
#if !DEBUG
                        }
                        catch (Exception)
                        {
                            explanation = new TransitiveEqualityExplanation(term, effectiveTerm, new EqualityExplanation[0]);
                        }
#endif
                            if (!format.equalityNumbers.TryGetValue(explanation, out var termNumber))
                            {
                                termNumber = termNumbering;
                                ++termNumbering;
                                format.equalityNumbers[explanation] = termNumber;
                            }

                            if (format.ShowEqualityExplanations)
                            {
                                explanation.PrettyPrint(content, format, termNumber);
                            }
                            else
                            {
                                var numberingString = $"({termNumber}) ";
                                content.switchToDefaultFormat();
                                content.Append(numberingString);
                                var indentString = $"¦{String.Join("", Enumerable.Repeat(" ", numberingString.Length - 1))}";
                                term.PrettyPrint(content, format, numberingString.Length);
                                content.switchToDefaultFormat();
                                content.Append($"\n{indentString}= (explanation omitted)\n{indentString}");
                                effectiveTerm.PrettyPrint(content, format, numberingString.Length);
                            }
                            content.Append("\n\n");
                        }
                    }
                    format.printContextSensitive = true;

                    bindingInfo.PrintEqualitySubstitution(content, format);
                }
            }

            if (Bindings.Any())
            {
                content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
                content.Append("Binding information:");
                content.switchToDefaultFormat();

                foreach (var bindings in bindingInfo.getBindingsToFreeVars())
                {
                    content.Append("\n\n");
                    content.Append(bindings.Key.PrettyName).Append(" was bound to:\n");
                    bindings.Value.PrettyPrint(content, format);
                    content.switchToDefaultFormat();
                }
            }

            if (Quant.BodyTerm != null)
            {
                content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
                content.Append("\n\n\nThe quantifier body:\n\n");
                content.switchToDefaultFormat();
                Quant.BodyTerm.PrettyPrint(content, format);
                content.Append("\n\n");
            }
            format.restoreAllOriginalRules();

            content.switchToDefaultFormat();
            content.switchFormat(PrintConstants.SubtitleFont, PrintConstants.sectionTitleColor);
            content.Append("The resulting term:\n\n");
            content.switchToDefaultFormat();
            concreteBody.PrettyPrint(content, format);

            format.restoreAllOriginalRules();
        }
Ejemplo n.º 19
0
        private bool PrettyPrint(InfoPanelContent content, Stack <Color> indentFormats, PrettyPrintFormat format)
        {
            var printRule        = format.getPrintRule(this);
            var parentRule       = format.GetParentPrintRule();
            var isMultiline      = false;
            var breakIndices     = new List <int>();
            var startLength      = content.Length;
            var needsParenthesis = this.needsParenthesis(format, printRule, parentRule);

            content.switchFormat(printRule.font ?? PrintConstants.DefaultFont, printRule.color);

            // check for cutoff
            if (format.MaxTermPrintingDepth == 1)
            {
                if (ContainsGeneralization())
                {
                    content.switchFormat(printRule.font ?? PrintConstants.DefaultFont, PrintConstants.generalizationColor);
                    content.Append(">...<");
                }
                else
                {
                    content.Append("...");
                }
                return(false);
            }

            if (printRule.indent)
            {
                indentFormats.Push(printRule.color);
            }
            if (needsParenthesis)
            {
                content.Append('(');
            }
            addPrefix(printRule, content, breakIndices);

            if (printChildren(format, printRule))
            {
                for (var i = 0; i < Args.Length; i++)
                {
                    var t = Args[i];

                    // Note: DO NOT CHANGE ORDER (-> short circuit)
                    isMultiline = t.PrettyPrint(content, indentFormats, format.NextTermPrintingDepth(this, i)) ||
                                  isMultiline;

                    if (i < Args.Length - 1)
                    {
                        addInfix(printRule, content, breakIndices);
                    }
                }
            }

            addSuffix(printRule, content, breakIndices);
            if (needsParenthesis)
            {
                content.Append(')');
            }

            // are there any lines to break?
            var lineBreaks = linebreaksNecessary(content, format, isMultiline && (breakIndices.Count > 0), startLength);

            if (lineBreaks)
            {
                addLinebreaks(printRule, content, indentFormats, breakIndices);
            }
            else if (printRule.indent)
            {
                // just remove indent if necessary
                indentFormats.Pop();
            }

            return(lineBreaks);
        }
Ejemplo n.º 20
0
 public override void InfoPanelText(InfoPanelContent content, PrettyPrintFormat format)
 {
     Fwd.InfoPanelText(content, format);
 }