Instantiate types in all tooltips

see #18
This commit is contained in:
Arne Keller 2021-07-26 10:40:40 +02:00
parent 19ec6cbc1a
commit eff5e4340e
2 changed files with 62 additions and 29 deletions

View File

@ -1,13 +1,14 @@
package edu.kit.typicalc.model.step; package edu.kit.typicalc.model.step;
import edu.kit.typicalc.model.Constraint;
import edu.kit.typicalc.model.type.FunctionType; import edu.kit.typicalc.model.type.FunctionType;
import edu.kit.typicalc.view.content.typeinferencecontent.latexcreator.LatexCreatorConstants; import edu.kit.typicalc.model.type.Type;
import edu.kit.typicalc.view.content.typeinferencecontent.latexcreator.LatexCreatorConstraints; import edu.kit.typicalc.view.content.typeinferencecontent.latexcreator.*;
import edu.kit.typicalc.view.content.typeinferencecontent.latexcreator.LatexCreatorMode; import org.apache.commons.lang3.tuple.Pair;
import edu.kit.typicalc.view.content.typeinferencecontent.latexcreator.LatexCreatorType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class StepAnnotator implements StepVisitor { public class StepAnnotator implements StepVisitor {
private final List<String> annotations = new ArrayList<>(); private final List<String> annotations = new ArrayList<>();
@ -18,58 +19,90 @@ public class StepAnnotator implements StepVisitor {
@Override @Override
public void visit(AbsStepDefault absD) { public void visit(AbsStepDefault absD) {
var t1 = ((FunctionType) absD.getConstraint().getSecondType()).getParameter(); visitAbs(absD);
var t2 = ((FunctionType) absD.getConstraint().getSecondType()).getOutput();
annotations.add("$$\\begin{align}"
+ "&" + LatexCreatorConstants.RULE_VARIABLE + "_1 := "
+ new LatexCreatorType(t1, LatexCreatorMode.NORMAL).getLatex() + LatexCreatorConstants.LATEX_NEW_LINE
+ "\n"
+ "&" + LatexCreatorConstants.RULE_VARIABLE + "_2 := "
+ new LatexCreatorType(t2, LatexCreatorMode.NORMAL).getLatex() + LatexCreatorConstants.LATEX_NEW_LINE
+ "\n"
+ "&" + LatexCreatorConstraints.createSingleConstraint(absD.getConstraint(), LatexCreatorMode.NORMAL)
+ "\\end{align}$$");
absD.getPremise().accept(this); absD.getPremise().accept(this);
} }
@Override @Override
public void visit(AbsStepWithLet absL) { public void visit(AbsStepWithLet absL) {
annotations.add("$" visitAbs(absL);
+ LatexCreatorConstraints.createSingleConstraint(absL.getConstraint(), LatexCreatorMode.NORMAL) + "$");
absL.getPremise().accept(this); absL.getPremise().accept(this);
} }
private void visitAbs(AbsStep absStep) {
var t1 = ((FunctionType) absStep.getConstraint().getSecondType()).getParameter();
var t2 = ((FunctionType) absStep.getConstraint().getSecondType()).getOutput();
visitGeneric(t1, t2, absStep.getConstraint());
}
private void visitGeneric(Type t1, Type t2, Constraint c) {
visitGeneric2(List.of(Pair.of("_1", t1), Pair.of("_2", t2)), c);
}
private void visitGeneric2(List<Pair<String, Type>> types, Constraint c) {
visitGeneric3(
types.stream()
.map(pair -> Pair.of(pair.getLeft(),
new LatexCreatorType(pair.getRight(), LatexCreatorMode.NORMAL).getLatex()))
.collect(Collectors.toList()),
c
);
}
private void visitGeneric3(List<Pair<String, String>> types, Constraint c) {
var sb = new StringBuilder("$$\\begin{align}");
for (var pair : types) {
sb.append("&" + LatexCreatorConstants.RULE_VARIABLE)
.append(pair.getLeft())
.append(" := ")
.append(pair.getRight())
.append(LatexCreatorConstants.LATEX_NEW_LINE)
.append("\n");
}
sb.append("&").append(LatexCreatorConstraints.createSingleConstraint(c, LatexCreatorMode.NORMAL));
annotations.add(sb + "\\end{align}$$");
}
@Override @Override
public void visit(AppStepDefault appD) { public void visit(AppStepDefault appD) {
annotations.add("$" visitGeneric(
+ LatexCreatorConstraints.createSingleConstraint(appD.getConstraint(), LatexCreatorMode.NORMAL) + "$"); appD.getPremise2().getConclusion().getType(),
appD.getConclusion().getType(),
appD.getConstraint());
appD.getPremise1().accept(this); appD.getPremise1().accept(this);
appD.getPremise2().accept(this); appD.getPremise2().accept(this);
} }
@Override @Override
public void visit(ConstStepDefault constD) { public void visit(ConstStepDefault constD) {
annotations.add("$" visitGeneric2(List.of(Pair.of("_c", constD.getConclusion().getType())), constD.getConstraint());
+ LatexCreatorConstraints.createSingleConstraint(constD.getConstraint(), LatexCreatorMode.NORMAL)
+ "$");
} }
@Override @Override
public void visit(VarStepDefault varD) { public void visit(VarStepDefault varD) {
annotations.add("$" visitGeneric2(List.of(
+ LatexCreatorConstraints.createSingleConstraint(varD.getConstraint(), LatexCreatorMode.NORMAL) + "$"); Pair.of("", varD.getInstantiatedTypeAbs()),
Pair.of("'", varD.getConclusion().getType())),
varD.getConstraint());
} }
@Override @Override
public void visit(VarStepWithLet varL) { public void visit(VarStepWithLet varL) {
annotations.add("$" visitGeneric3(List.of(
+ LatexCreatorConstraints.createSingleConstraint(varL.getConstraint(), LatexCreatorMode.NORMAL) + "$"); Pair.of("'",
AssumptionGeneratorUtil.generateTypeAbstraction(varL.getTypeAbsInPremise(),
LatexCreatorMode.NORMAL)),
Pair.of("",
new LatexCreatorType(varL.getInstantiatedTypeAbs(), LatexCreatorMode.NORMAL).getLatex())),
varL.getConstraint());
} }
@Override @Override
public void visit(LetStepDefault letD) { public void visit(LetStepDefault letD) {
annotations.add("$" visitGeneric2(List.of(
+ LatexCreatorConstraints.createSingleConstraint(letD.getConstraint(), LatexCreatorMode.NORMAL) + "$"); Pair.of("_1", letD.getTypeInferer().getFirstInferenceStep().getConclusion().getType()),
Pair.of("_2", letD.getPremise().getConclusion().getType())),
letD.getConstraint());
letD.getTypeInferer().getFirstInferenceStep().accept(this); letD.getTypeInferer().getFirstInferenceStep().accept(this);
letD.getPremise().accept(this); letD.getPremise().accept(this);
} }

View File

@ -37,7 +37,7 @@ public final class AssumptionGeneratorUtil {
} }
} }
protected static String generateTypeAbstraction(TypeAbstraction abs, LatexCreatorMode mode) { public static String generateTypeAbstraction(TypeAbstraction abs, LatexCreatorMode mode) {
StringBuilder abstraction = new StringBuilder(); StringBuilder abstraction = new StringBuilder();
if (abs.hasQuantifiedVariables()) { if (abs.hasQuantifiedVariables()) {
abstraction.append(FOR_ALL); abstraction.append(FOR_ALL);