@Override
 public void enterFacilityDecl(ResolveParser.FacilityDeclContext ctx) {
   VCAssertiveBlockBuilder block =
       new VCAssertiveBlockBuilder(g, moduleScope, "Facility_Inst=" + ctx.name.getText(), ctx);
   block.assume(g.getTrueExp());
   assertiveBlocks.push(block);
 }
 @Override
 public void exitCallStmt(ResolveParser.CallStmtContext ctx) {
   VCRuleBackedStat s = null;
   PApply callExp = (PApply) tr.exprASTs.get(ctx.progParamExp());
   OperationSymbol op = getOperation(moduleScope, callExp);
   if (inSimpleForm(op.getEnsures(), op.getParameters())) {
     // TODO: Use log instead!
     // gen.getCompiler().info("APPLYING EXPLICIT (SIMPLE) CALL RULE");
     s = new VCCall(ctx, assertiveBlocks.peek(), EXPLICIT_CALL_APPLICATION, callExp);
   } else {
     // TODO: Use log instead!
     // gen.getCompiler().info("APPLYING GENERAL CALL RULE");
     s = new VCCall(ctx, assertiveBlocks.peek(), GENERAL_CALL_APPLICATION, callExp);
   }
   stats.put(ctx, s);
 }
  @Override
  public void enterProcedureDecl(ResolveParser.ProcedureDeclContext ctx) {
    Scope s = symtab.getScope(ctx);
    try {
      List<ProgParameterSymbol> paramSyms = s.getSymbolsOfType(ProgParameterSymbol.class);

      currentProcOpSym =
          s.queryForOne(
              new OperationQuery(
                  null, ctx.name, Utils.apply(paramSyms, ProgParameterSymbol::getDeclaredType)));

      // This is the requires for the operation with some substutions made (see corrFnExp rule in
      // HH-diss)
      PExp corrFnExpRequires =
          perParameterCorrFnExpSubstitute(paramSyms, currentProcOpSym.getRequires());
      List<PExp> opParamAntecedents = new ArrayList<>();
      Utils.apply(paramSyms, opParamAntecedents, this::extractAssumptionsFromParameter);
      Set<PExp> l = getModuleLevelAssertionsOfType(ClauseType.REQUIRES);
      VCAssertiveBlockBuilder block =
          new VCAssertiveBlockBuilder(g, s, "Correct_Op_Hypo=" + ctx.name.getText(), ctx)
              .facilitySpecializations(facilitySpecFormalActualMappings)
              .assume(getModuleLevelAssertionsOfType(ClauseType.REQUIRES))
              // TODO: constraints should be added on demand via NOTICE:...
              // .assume(getModuleLevelAssertionsOfType(ClauseType.CONSTRAINT))
              .assume(opParamAntecedents) // we assume correspondence for reprs here automatically
              .assume(corrFnExpRequires)
              .remember();
      assertiveBlocks.push(block);
    } catch (SymbolTableException e) {
      throw new RuntimeException(e); // this shouldn't happen now
    }
  }
  @Override
  public void exitOperationProcedureDecl(ResolveParser.OperationProcedureDeclContext ctx) {
    Scope s = symtab.getScope(ctx);
    VCAssertiveBlockBuilder block = assertiveBlocks.pop();
    List<ProgParameterSymbol> paramSyms = s.getSymbolsOfType(ProgParameterSymbol.class);

    PExp corrFnExpEnsures =
        perParameterCorrFnExpSubstitute(
            paramSyms,
            tr.getMathExpASTFor(
                g, ctx.ensuresClause())); // postcondition[params 1..i <-- corr_fn_exp]
    corrFnExpEnsures =
        corrFnExpEnsures.withVCInfo(ctx.getStart(), "Ensures clause of " + ctx.name.getText());
    Token loc = ctx.ensuresClause() != null ? ctx.ensuresClause().getStart() : ctx.getStart();

    List<PExp> paramConsequents = new ArrayList<>();
    Utils.apply(paramSyms, paramConsequents, this::extractConsequentsFromParameter);

    // add verification statements to the assertive context/block
    block.stats(Utils.collect(VCRuleBackedStat.class, ctx.stmt(), stats));

    // add any additional confirms from the parameters, etc
    for (ProgParameterSymbol p : paramSyms) {
      confirmParameterConsequentsForBlock(block, p); // modfies 'block' with additional confims!
    }
    // TODO: Tomorrow look at the verification statements in the assertive context for
    // int_do_nothing, then
    block.finalConfirm(corrFnExpEnsures);
    outputFile.addAssertiveBlock(block.build());
  }
 @Override
 public void exitAssignStmt(ResolveParser.AssignStmtContext ctx) {
   VCRuleBackedStat s =
       new VCRuleBackedStat(
           ctx,
           assertiveBlocks.peek(),
           FUNCTION_ASSIGN_APPLICATION,
           tr.exprASTs.get(ctx.left),
           tr.exprASTs.get(ctx.right));
   stats.put(ctx, s);
 }
 private List<PExp> reduceArgs(List<ParserRuleContext> args) {
   List<PExp> result = new ArrayList<>();
   for (ParserRuleContext arg : args) {
     PExp argAsPExp = tr.getMathExpASTFor(g, arg);
     if (argAsPExp instanceof PApply) { // i.e., we're dealing with a function application
       PExp e = applyCallRuleToExp(arg, assertiveBlocks.peek(), (PApply) argAsPExp);
       result.add(e);
     } else {
       result.add(argAsPExp);
     }
   }
   return result;
 }
  @Override
  public void enterTypeImplInit(ResolveParser.TypeImplInitContext ctx) {
    Scope s = symtab.getScope(ctx.getParent());
    PExp convention = currentTypeReprSym.getConvention();
    PExp correspondence = currentTypeReprSym.getCorrespondence();
    PExp typeInitEnsures = g.getTrueExp();
    List<ModuleParameterSymbol> moduleParamSyms = getAllModuleParameterSyms();

    VCAssertiveBlockBuilder block =
        new VCAssertiveBlockBuilder(g, s, "T_Init_Hypo=" + currentTypeReprSym.getName(), ctx)
            .assume(getModuleLevelAssertionsOfType(ClauseType.REQUIRES))
            .assume(
                getAssertionsFromModuleFormalParameters(
                    moduleParamSyms, this::extractAssumptionsFromParameter));

    assertiveBlocks.push(block);
  }
 @Override
 public void exitTypeImplInit(ResolveParser.TypeImplInitContext ctx) {
   PExp typeInitEnsures = g.getTrueExp();
   PExp convention = currentTypeReprSym.getConvention();
   PExp correspondence = currentTypeReprSym.getCorrespondence();
   if (currentTypeReprSym.getDefinition() != null) {
     typeInitEnsures =
         currentTypeReprSym.getDefinition().getProgramType().getInitializationEnsures();
   }
   VCAssertiveBlockBuilder block = assertiveBlocks.pop();
   PExp newInitEnsures =
       typeInitEnsures.substitute(
           currentTypeReprSym.exemplarAsPSymbol(),
           currentTypeReprSym.conceptualExemplarAsPSymbol());
   // block.stats(Utils.collect(VCRuleBackedStat.class, ctx.stmt(), stats));
   // block.confirm(convention);  //order here is important
   block.assume(correspondence);
   throw new UnsupportedOperationException("re-institute the final confirm for this dan");
   // block.finalConfirm(newInitEnsures, "Initialization-ensures clause of " +
   // currentTypeReprSym.getName());
   // outputFile.addAssertiveBlock(block.build());
 }
  @Override
  public void exitTypeRepresentationDecl(ResolveParser.TypeRepresentationDeclContext ctx) {
    PExp constraint = g.getTrueExp();
    PExp correspondence = g.getTrueExp();
    if (currentTypeReprSym == null) return;
    correspondence = currentTypeReprSym.getCorrespondence();

    if (currentTypeReprSym.getDefinition() != null) {
      constraint = currentTypeReprSym.getDefinition().getProgramType().getConstraint();
    }
    VCAssertiveBlockBuilder block = assertiveBlocks.pop();
    PExp newConstraint =
        constraint.substitute(
            currentTypeReprSym.exemplarAsPSymbol(),
            currentTypeReprSym.conceptualExemplarAsPSymbol());
    newConstraint =
        newConstraint.withVCInfo(ctx.getStart(), "Constraint for type: " + ctx.name.getText());
    block.assume(correspondence.splitIntoConjuncts());
    // throw new UnsupportedOperationException("re-institute the final confirm for this dan");
    block.finalConfirm(newConstraint);
    outputFile.addAssertiveBlock(block.build());
  }
  @Override
  public void enterTypeRepresentationDecl(ResolveParser.TypeRepresentationDeclContext ctx) {
    Scope s = symtab.getScope(ctx);
    currentTypeReprSym = null;
    try {
      currentTypeReprSym =
          moduleScope
              .queryForOne(new UnqualifiedNameQuery(ctx.name.getText()))
              .toProgReprTypeSymbol();
    } catch (SymbolTableException e) {
    }
    List<PExp> opParamAntecedents =
        getAssertionsFromModuleFormalParameters(
            getAllModuleParameterSyms(), this::extractAssumptionsFromParameter);

    VCAssertiveBlockBuilder block =
        new VCAssertiveBlockBuilder(g, s, "Well_Def_Corr_Hyp=" + ctx.name.getText(), ctx)
            .assume(opParamAntecedents)
            .assume(getModuleLevelAssertionsOfType(ClauseType.REQUIRES))
            .assume(currentTypeReprSym.getConvention());
    assertiveBlocks.push(block);
  }
  @Override
  public void exitProcedureDecl(ResolveParser.ProcedureDeclContext ctx) {
    Scope scope = symtab.getScope(ctx);
    List<ProgParameterSymbol> paramSyms = scope.getSymbolsOfType(ProgParameterSymbol.class);
    VCAssertiveBlockBuilder block = assertiveBlocks.pop();
    List<ProgParameterSymbol> formalParameters = new ArrayList<>();
    try {
      formalParameters =
          scope.query(new SymbolTypeQuery<ProgParameterSymbol>(ProgParameterSymbol.class));
    } catch (NoSuchModuleException | UnexpectedSymbolException e) {
      e.printStackTrace();
    }

    List<PExp> corrFnExps =
        paramSyms
            .stream()
            .filter(p -> p.getDeclaredType() instanceof PTRepresentation)
            .map(p -> (PTRepresentation) p.getDeclaredType())
            .map(p -> p.getReprTypeSymbol().getCorrespondence())
            .collect(Collectors.toList());
    PExp corrFnExpEnsures =
        perParameterCorrFnExpSubstitute(paramSyms, currentProcOpSym.getEnsures())
            .withVCInfo(ctx.getStart(), "Ensures clause of " + ctx.name.getText());
    // postcondition[params 1..i <-- corr_fn_exp]

    List<PExp> paramConsequents = new ArrayList<>();
    Utils.apply(formalParameters, paramConsequents, this::extractConsequentsFromParameter);

    block
        .stats(Utils.collect(VCRuleBackedStat.class, ctx.stmt(), stats))
        .assume(corrFnExps)
        .confirm(ctx, g.formConjuncts(paramConsequents))
        .finalConfirm(corrFnExpEnsures);

    outputFile.addAssertiveBlock(block.build());
    currentProcOpSym = null;
  }
  // TODO: Would be really cool if we could hover over a given and get information about where it
  // came from
  // "constraint for type Integer", etc.
  @Override
  public void enterOperationProcedureDecl(ResolveParser.OperationProcedureDeclContext ctx) {
    Scope s = symtab.getScope(ctx);
    List<ProgParameterSymbol> paramSyms = s.getSymbolsOfType(ProgParameterSymbol.class);

    // precondition[params 1..i <-- conc.X]
    PExp corrFnExpRequires =
        perParameterCorrFnExpSubstitute(paramSyms, tr.getMathExpASTFor(g, ctx.requiresClause()));

    VCAssertiveBlockBuilder block =
        new VCAssertiveBlockBuilder(g, s, "Proc_Decl_rule=" + ctx.name.getText(), ctx)
            .facilitySpecializations(facilitySpecFormalActualMappings)
            .assume(
                getAssertionsFromModuleFormalParameters(
                    getAllModuleParameterSyms(), this::extractAssumptionsFromParameter))
            .assume(
                getAssertionsFromFormalParameters(paramSyms, this::extractAssumptionsFromParameter))
            .assume(getModuleLevelAssertionsOfType(ClauseType.REQUIRES))
            .assume(getModuleLevelAssertionsOfType(ClauseType.CONSTRAINT))
            .assume(corrFnExpRequires)
            .remember();

    assertiveBlocks.push(block);
  }