code: private Optional<RightProvenance> createProvenanceElements(IQTree rightTree,
                                                                 ImmutableSubstitution<? extends ImmutableTerm> selectedSubstitution,
                                                                 ImmutableSet<Variable> leftVariables,
                                                                 VariableGenerator variableGenerator) {
          if (selectedSubstitution.getImmutableMap().entrySet().stream()
                  .filter(e -> !leftVariables.contains(e.getKey()))
                  .map(Map.Entry::getValue)
                  .anyMatch(value -> value.getVariableStream()
                          .allMatch(leftVariables::contains)
                          || value.isGround())) {
  
              VariableNullability rightVariableNullability = rightTree.getVariableNullability();
  
              Optional<Variable> nonNullableRightVariable = rightTree.getVariables().stream()
                      .filter(v -> !leftVariables.contains(v))
                      .filter(v -> !rightVariableNullability.isPossiblyNullable(v))
                      .findFirst();
  
              if (nonNullableRightVariable.isPresent()) {
                  return Optional.of(new RightProvenance(nonNullableRightVariable.get()));
              }
              /*
               * Otherwise, creates a fresh variable and its construction node
               */
              else {
                  Variable provenanceVariable = variableGenerator.generateNewVariable();
  
                  ImmutableSet<Variable> newRightProjectedVariables =
                          Stream.concat(
                                      Stream.of(provenanceVariable),
                                      rightTree.getVariables().stream())
                                  .collect(ImmutableCollectors.toSet());
  
                  ConstructionNode newRightConstructionNode = iqFactory.createConstructionNode(
                          newRightProjectedVariables,
                          substitutionFactory.getSubstitution(provenanceVariable,
                                  termFactory.getProvenanceSpecialConstant()));
  
                  return Optional.of(new RightProvenance(provenanceVariable, newRightConstructionNode));
              }
          }
          else {
              return Optional.empty();
          }
      }
docs: When at least one value does not depend on a right-specific variable
  (i.e. is a ground term or only depends on left variables)